From 588fa3fc336a9e08a9103f8ea49b8dca1e0dd56d Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Thu, 25 Oct 2007 22:44:51 +0000 Subject: [PATCH] ActionItem114: added some unit tests to cover more code --- app/controllers/profile_admin/membership_editor_controller.rb | 2 +- app/models/person.rb | 2 +- test/test_helper.rb | 7 +++++++ test/unit/person_test.rb | 11 +++++++++++ test/unit/profile_test.rb | 22 +++++++++++++++++++--- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/controllers/profile_admin/membership_editor_controller.rb b/app/controllers/profile_admin/membership_editor_controller.rb index d6b8bbe..0652007 100644 --- a/app/controllers/profile_admin/membership_editor_controller.rb +++ b/app/controllers/profile_admin/membership_editor_controller.rb @@ -7,7 +7,7 @@ class MembershipEditorController < ProfileAdminController protect [:index, :new_enterprise, :create_enterprise ], 'edit_profile', :profile def index - @memberships = current_user.person.enterprises_memberships + @memberships = current_user.person.enterprise_memberships end def new_enterprise diff --git a/app/models/person.rb b/app/models/person.rb index 5308aa5..1761e71 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -23,7 +23,7 @@ class Person < Profile :select => 'profiles.*').uniq end - def enterprises_memberships + def enterprise_memberships memberships.select{|p|p.class == Enterprise} end diff --git a/test/test_helper.rb b/test/test_helper.rb index a6a6d36..af78b04 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -71,6 +71,13 @@ class Test::Unit::TestCase admin_user.login end + def create_user(name) + User.create!(:login => name, + :email => name + '@noosfero.org', + :password => name.underscore, + :password_confirmation => name.underscore) + end + private def uses_host(name) diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 241a820..a32632d 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -30,6 +30,7 @@ class PersonTest < Test::Unit::TestCase member_role = Role.create(:name => 'member') e.affiliate(p, member_role) assert p.memberships.include?(e) + assert p.enterprise_memberships.include?(e) end def test_can_have_user @@ -97,4 +98,14 @@ class PersonTest < Test::Unit::TestCase assert_nil p.email end + should 'be an admin if have permission of environment administration' do + role = Role.create!(:name => 'just_another_admin_role') + env = Environment.create!(:name => 'blah') + person = create_user('just_another_person').person + env.affiliate(person, role) + assert ! person.is_admin? + role.update_attributes(:permissions => ['view_environment_admin_panel']) + person.reload + assert person.is_admin? + end end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index cd020f7..c5544cf 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -40,9 +40,9 @@ class ProfileTest < Test::Unit::TestCase end def test_cannot_rename - p1 = profiles(:johndoe) + assert_valid p = Profile.create(:name => 'new_profile', :identifier => 'new_profile') assert_raise ArgumentError do - p1.identifier = 'bli' + p.identifier = 'other_profile' end end @@ -151,6 +151,23 @@ class ProfileTest < Test::Unit::TestCase assert_equal 'testprofile@example.com', p.contact_email end + should 'affiliate and provide a list of the affiliated users' do + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting') + person = create_user('test_user').person + role = Role.create!(:name => 'just_another_test_role') + assert profile.affiliate(person, role) + assert profile.members.map(&:id).include?(person.id) + end + + should 'authorize users that have permission on the environment' do + env = Environment.create!(:name => 'test_env') + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + person = create_user('test_user').person + role = Role.create!(:name => 'just_another_test_role', :permissions => ['edit_profile']) + assert env.affiliate(person, role) + assert person.has_permission?('edit_profile', profile) + end + private def assert_invalid_identifier(id) @@ -158,5 +175,4 @@ class ProfileTest < Test::Unit::TestCase assert !profile.valid? assert profile.errors.invalid?(:identifier) end - end -- libgit2 0.21.2