This registration service may be able to be abstracted/
resued in future patches.
---
src/app/controllers/users_controller.rb | 3 +-
src/app/services/registration_service.rb | 29 +++++++++++++
src/features/authentication.feature | 1 +
src/features/step_definitions/authentication.rb | 5 ++
src/spec/controllers/users_controller_spec.rb | 49 +++++++++++++++++++---
src/spec/models/registration_service_spec.rb | 48 ++++++++++++++++++++++
6 files changed, 127 insertions(+), 8 deletions(-)
create mode 100644 src/app/services/registration_service.rb
create mode 100644 src/spec/models/registration_service_spec.rb
diff --git a/src/app/controllers/users_controller.rb
b/src/app/controllers/users_controller.rb
index 310edf6..6c6c021 100644
--- a/src/app/controllers/users_controller.rb
+++ b/src/app/controllers/users_controller.rb
@@ -29,7 +29,8 @@ class UsersController < ApplicationController
def create
@user = User.new(params[:user])
- if @user.save
+ @registration = RegistrationService.new(@user)
+ if @registration.save
flash[:notice] = "User registered!"
redirect_back_or_default account_url
else
diff --git a/src/app/services/registration_service.rb
b/src/app/services/registration_service.rb
new file mode 100644
index 0000000..e8d2e65
--- /dev/null
+++ b/src/app/services/registration_service.rb
@@ -0,0 +1,29 @@
+class RegistrationService
+
+ def initialize(user)
+ @user = user
+ end
+
+ def save
+ return false unless valid?
+ begin
+ User.transaction do
+ @user.save!
+ PortalPool.transaction do
+ @portal_pool = PortalPool.create!({ :name => @user.login, :owner =>
@user})
+ Permission.transaction do
+ Permission.create!({:user => @user,
+ :role => Role.find_by_name("Self-service Pool
User"),
+ :permission_object => @portal_pool})
+ end
+ end
+ end
+ rescue
+ false
+ end
+ end
+
+ def valid?
+ @user.valid?
+ end
+end
\ No newline at end of file
diff --git a/src/features/authentication.feature
b/src/features/authentication.feature
index 0dea3ac..78754b9 100644
--- a/src/features/authentication.feature
+++ b/src/features/authentication.feature
@@ -19,6 +19,7 @@ Feature: User authentication
And I press "Create Account"
Then I should be on the account page
And I should see "User registered!"
+ And I should have one private pool named "testuser"
Scenario: Log in as registered user
Given I am a registered user
diff --git a/src/features/step_definitions/authentication.rb
b/src/features/step_definitions/authentication.rb
index 92e7874..7abcb83 100644
--- a/src/features/step_definitions/authentication.rb
+++ b/src/features/step_definitions/authentication.rb
@@ -35,3 +35,8 @@ end
Then /^I should be logged out$/ do
UserSession.find.should == nil
end
+
+Then /^I should have one private pool named "([^\"]*)"$/ do |login|
+ PortalPool.find_by_name(login).should_not be_nil
+ PortalPool.find_by_name(login).permissions.size.should == 1
+end
diff --git a/src/spec/controllers/users_controller_spec.rb
b/src/spec/controllers/users_controller_spec.rb
index 1c18ba7..a74702a 100644
--- a/src/spec/controllers/users_controller_spec.rb
+++ b/src/spec/controllers/users_controller_spec.rb
@@ -15,13 +15,48 @@ describe UsersController do
response.should be_success
end
- it "should create user" do
- lambda {
- post :create, :user => { :login => "tuser2", :email =>
"[email protected]",
- :password => "testpass",
- :password_confirmation => "testpass" }
- }.should change{ User.count }
- response.should redirect_to(account_path)
+ describe "#create" do
+ before(:each) do
+
+ end
+
+ context "user enters valid input" do
+ it "should create user" do
+ lambda {
+ post :create, :user => { :login => "tuser2", :email =>
"[email protected]",
+ :password => "testpass",
+ :password_confirmation => "testpass" }
+ }.should change{ User.count }
+ p = PortalPool.find_by_name("tuser2")
+ p.should_not be_nil
+ assigns[:user].login.should == p.owner.login
+ p.name.should == "tuser2"
+ p.permissions.size.should == 1
+ p.permissions.any? {
+ |perm| perm.role.name.eql?('Self-service Pool User')
+ }.should be_true
+ response.should redirect_to(account_path)
+ end
+
+ it "fails to create pool" do
+ lambda {
+ post :create, :user => {}
+ }.should_not change{ User.count }
+ p = PortalPool.find_by_name("tuser2")
+ p.should be_nil
+ returned_user = assigns[:user]
+ returned_user.errors.empty?.should be_false
+ returned_user.should have(2).errors_on(:login)
+ returned_user.should have(2).errors_on(:email)
+ returned_user.should have(1).error_on(:password)
+ returned_user.should have(1).error_on(:password_confirmation)
+ #assigns[:user].errors.find_all {|attr,msg|
+ # ["login", "email", "password", "password_confirmation"].
+ # include?(attr).should be_true
+ #}
+ response.should render_template('new')
+ end
+ end
end
it "should show user" do
diff --git a/src/spec/models/registration_service_spec.rb
b/src/spec/models/registration_service_spec.rb
new file mode 100644
index 0000000..eeea5c8
--- /dev/null
+++ b/src/spec/models/registration_service_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe RegistrationService do
+ #fixtures :all
+ before(:each) do
+ @tuser = Factory :tuser
+ end
+
+ it "should initialize a new instance given valid attributes" do
+ RegistrationService.new(@tuser)
+ end
+
+ describe "#save" do
+
+ context "adding valid user with no errors" do
+ it "should create user, portal_pool and self-service permission" do
+ user = User.new({:login => 'gooduser',
+ :email => '[email protected]',
+ :password => 'password',
+ :password_confirmation => 'password'})
+ r = RegistrationService.new(user)
+ Rails::logger.info("ERRORS - Printing errors on user object:")
+ Rails::logger.info(
+ user.errors.each_full { |msg| "ERROR: #{msg}" }
+ )
+ r.save.should be_true
+ end
+ end
+
+ context "save fails" do
+ it "should return errors on user when user is missing required field" do
+ user = User.new(:login => 'baduser')
+ r = RegistrationService.new(user)
+ r.save.should be_false
+ user.errors.empty?.should be_false
+ #Rails::logger.info(msg)
+ user.errors.find_all {|attr,msg|
+ ["email", "password", "password_confirmation"].include?(attr).should
be_true
+ }
+ end
+
+ it "should return portal_pool errors if pool create fails" do
+ #TODO: implement this test. We should check this, but not sure of best
+ # way right now.
+ end
+ end
+ end
+end
--
1.6.6.1
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel