From: Ladislav Martincik lmartinc@redhat.com
- Fixing validations for User with maximum allowing blank - Merged 2 spec files for registration service to one file - Turned fixtures on again for now to have all permissions loaded --- src/app/models/user.rb | 1 - src/spec/controllers/users_controller_spec.rb | 6 ++- src/spec/models/registration_service_spec.rb | 64 ------------------------ src/spec/services/registration_service_spec.rb | 37 ++++++++++++++ src/spec/spec_helper.rb | 2 +- 5 files changed, 42 insertions(+), 68 deletions(-) delete mode 100644 src/spec/models/registration_service_spec.rb
diff --git a/src/app/models/user.rb b/src/app/models/user.rb index 0332095..7942ae4 100644 --- a/src/app/models/user.rb +++ b/src/app/models/user.rb @@ -31,7 +31,6 @@ class User < ActiveRecord::Base validates_length_of :first_name, :maximum => 255, :allow_blank => true validates_length_of :last_name, :maximum => 255, :allow_blank => true
- # authlogic's password confirmation doesn't fire up when we fill in the # confirmation field but leave the password field blank. We have to check # that manually: diff --git a/src/spec/controllers/users_controller_spec.rb b/src/spec/controllers/users_controller_spec.rb index eedb0b3..c1fad30 100644 --- a/src/spec/controllers/users_controller_spec.rb +++ b/src/spec/controllers/users_controller_spec.rb @@ -3,10 +3,12 @@ require 'spec_helper' describe UsersController do
before(:each) do - Factory(:base_permission_object) + unless BasePermissionObject.find_by_name(BasePermissionObject::GENERAL_PERMISSION_SCOPE) + Factory(:base_permission_object) + end @tuser = Factory :tuser @admin_permission = Factory :admin_permission - @admin_permission.role.privileges << Privilege.new(:name => 'user_modify') + @admin_permission.role.privileges << Privilege.find_by_name('user_modify') @admin = @admin_permission.user Factory.create(:default_quota_metadata) Factory.create(:default_role_metadata) diff --git a/src/spec/models/registration_service_spec.rb b/src/spec/models/registration_service_spec.rb deleted file mode 100644 index 2a97d11..0000000 --- a/src/spec/models/registration_service_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -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, pool and self-service permission" do - user = User.new({:login => 'gooduser', - :email => 'guser@example.com', - :password => 'password', - :password_confirmation => 'password'}) - r = RegistrationService.new(user) - 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 - user.errors.find_all {|attr,msg| - ["email", "password", "password_confirmation"].include?(attr).should be_true - } - end - - it "should return 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 - - it "should register a user with default pool/quota/role when default settings set" do - @user = Factory :user - @pool = Factory(:pool, :name => "default_pool") - @role = Role.find_by_name("Instance Creator and User") - @quota = Factory :quota - - MetadataObject.set("allow_self_service_logins", "true") - MetadataObject.set("self_service_default_pool", @pool) - MetadataObject.set("self_service_default_role", @role) - MetadataObject.set("self_service_default_quota", @quota) - - @registration_service = RegistrationService.new(@user) - @registration_service.save - - @pools = Pool.list_for_user(@user, Privilege::INSTANCE_VIEW) - @pools.size.should == 1 - @pools[0].name.should == "default_pool" - - @user.quota.maximum_running_instances.should == @quota.maximum_running_instances - @user.quota.maximum_total_instances.should == @quota.maximum_total_instances - end -end diff --git a/src/spec/services/registration_service_spec.rb b/src/spec/services/registration_service_spec.rb index 7b26c63..41f6764 100644 --- a/src/spec/services/registration_service_spec.rb +++ b/src/spec/services/registration_service_spec.rb @@ -8,6 +8,43 @@ describe RegistrationService do Factory.create(:default_pool_metadata) end
+ describe "with validations" do + + it "should return errors on user when user is missing required fields" do + user = User.new(:login => 'baduser') + r = RegistrationService.new(user) + r.save.should be_false + user.errors.empty?.should be_false + user.errors.find_all do |attr,msg| + ["email","password","password_confirmation"].include?(attr).should be_true + end + end + + it "should register a user with default pool/quota/role when default settings set" do + @user = Factory :user + @pool = Factory(:pool, :name => "default_pool") + privilege = Privilege.find_by_name('instance_view') + @role = Factory(:role, :privileges => [privilege]) + @quota = Factory :quota + + MetadataObject.set("allow_self_service_logins", "true") + MetadataObject.set("self_service_default_pool", @pool) + MetadataObject.set("self_service_default_role", @role) + MetadataObject.set("self_service_default_quota", @quota) + + @registration_service = RegistrationService.new(@user) + @registration_service.save + + @pools = Pool.list_for_user(@user, Privilege::INSTANCE_VIEW) + @pools.length.should == 1 + @pools[0].name.should == "default_pool" + + @user.quota.maximum_running_instances.should == @quota.maximum_running_instances + @user.quota.maximum_total_instances.should == @quota.maximum_total_instances + end + + end + describe "with quota" do
it "passed via nested attributes to user model" do diff --git a/src/spec/spec_helper.rb b/src/spec/spec_helper.rb index 12727d8..f7db6ee 100644 --- a/src/spec/spec_helper.rb +++ b/src/spec/spec_helper.rb @@ -19,7 +19,7 @@ Spec::Runner.configure do |config| # If you're not using ActiveRecord you should remove these # lines, delete config/database.yml and disable :active_record # in your config/boot.rb - config.use_transactional_fixtures = false + config.use_transactional_fixtures = true config.use_instantiated_fixtures = false config.fixture_path = RAILS_ROOT + '/spec/fixtures/'