This registration service may be able to be abstracted/
reused 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/features/support/env.rb | 8 +++-
src/spec/controllers/users_controller_spec.rb | 51 +++++++++++++++++++----
src/spec/fixtures/privileges.yml | 39 +++++++++++++++++
src/spec/fixtures/roles.yml | 47 +++++++++++++++++++++
src/spec/models/registration_service_spec.rb | 51 +++++++++++++++++++++++
9 files changed, 224 insertions(+), 10 deletions(-)
create mode 100644 src/app/services/registration_service.rb
create mode 100644 src/spec/fixtures/privileges.yml
create mode 100644 src/spec/fixtures/roles.yml
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/features/support/env.rb b/src/features/support/env.rb
index 917fb2d..341854f 100644
--- a/src/features/support/env.rb
+++ b/src/features/support/env.rb
@@ -4,7 +4,7 @@
# instead of editing this one. Cucumber will automatically load all
features/**/*.rb
# files.
-ENV["RAILS_ENV"] ||= "cucumber"
+ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/formatter/unicode' # Remove this line if you don't want
Cucumber Unicode support
@@ -47,6 +47,12 @@ ActionController::Base.allow_rescue = false
# block that will explicitly put your database in a known state.
Cucumber::Rails::World.use_transactional_fixtures = true
+#Seed the DB with fixture data
+Fixtures.reset_cache
+fixtures_folder = File.join(RAILS_ROOT, 'spec', 'fixtures')
+fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f,
'.yml') }
+Fixtures.create_fixtures(fixtures_folder, fixtures)
+
# How to clean your database when transactions are turned off. See
# http://github.com/bmabey/database_cleaner for more info.
require 'database_cleaner'
diff --git a/src/spec/controllers/users_controller_spec.rb
b/src/spec/controllers/users_controller_spec.rb
index 1c18ba7..5f010cc 100644
--- a/src/spec/controllers/users_controller_spec.rb
+++ b/src/spec/controllers/users_controller_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe UsersController do
-
+ fixtures :all
before(:each) do
@tuser = Factory :tuser
activate_authlogic
@@ -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/fixtures/privileges.yml b/src/spec/fixtures/privileges.yml
new file mode 100644
index 0000000..d1f44ab
--- /dev/null
+++ b/src/spec/fixtures/privileges.yml
@@ -0,0 +1,39 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
+set_perms:
+ name: set_perms
+view_perms:
+ name: view_perms
+instance_modify:
+ name: instance_modify
+instance_control:
+ name: instance_control
+instance_view:
+ name: instance_view
+stats_view:
+ name: stats_view
+account_modify:
+ name: account_modify
+account_view:
+ name: account_view
+pool_modify:
+ name: pool_modify
+pool_view:
+ name: pool_view
+quota_modify:
+ name: quota_modify
+quota_view:
+ name: quota_view
+provider_modify:
+ name: provider_modify
+provider_view:
+ name: provider_view
+user_modify:
+ name: user_modify
+user_view:
+ name: user_view
diff --git a/src/spec/fixtures/roles.yml b/src/spec/fixtures/roles.yml
new file mode 100644
index 0000000..2ac7e5f
--- /dev/null
+++ b/src/spec/fixtures/roles.yml
@@ -0,0 +1,47 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
+instance_controller:
+ name: Instance Controller
+ scope: PortalPool
+ privileges: instance_control, instance_view, pool_view
+instance_controller_with_monitoring:
+ name: Instance Controller With Monitoring
+ scope: PortalPool
+ privileges: instance_control, instance_view, pool_view, stats_view
+instance_creator_and_user:
+ name: Instance Creator and User
+ scope: PortalPool
+ privileges: instance_control, instance_view, pool_view, stats_view,
instance_modify, quota_view, set_perms, view_perms
+self_service_pool_user:
+ name: Self-service Pool User
+ scope: PortalPool
+ privileges: instance_control, instance_view, pool_view, stats_view,
instance_modify, quota_view, set_perms, view_perms, account_modify
+pool_creator:
+ name: Pool Creator
+ scope: Provider
+ privileges: provider_view, pool_modify, pool_view, quota_view
+pool_administrator:
+ name: Pool Administrator
+ scope: Provider
+ privileges: provider_view, pool_modify, pool_view, quota_modify, quota_view,
account_modify, account_view, stats_view, set_perms, view_perms
+account_administrator:
+ name: Account Administrator
+ scope: CloudAccount
+ privileges: account_modify, account_view, stats_view, set_perms, view_perms
+account_user:
+ name: Account User
+ scope: CloudAccount
+ privileges: account_view
+provider_administrator:
+ name: Provider Administrator
+ scope: BasePortalObject
+ privileges: provider_modify, provider_view, stats_view
+site_administrator:
+ name: Site Administrator
+ scope: BasePortalObject
+ privileges: provider_modify, provider_view, account_modify, account_view,
user_modify, user_view, set_perms, view_perms, pool_modify, pool_view,
quota_modify, quota_view, stats_view, instance_modify, instance_control,
instance_view
diff --git a/src/spec/models/registration_service_spec.rb
b/src/spec/models/registration_service_spec.rb
new file mode 100644
index 0000000..aa25a1e
--- /dev/null
+++ b/src/spec/models/registration_service_spec.rb
@@ -0,0 +1,51 @@
+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)
+ roles = Role.find(:all)
+ Rails::logger.info("Current Roles: ")
+ Rails::logger.info(roles.each {|role| role.name})
+ 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