Commit 588fa3fc336a9e08a9103f8ea49b8dca1e0dd56d
1 parent
2585996c
Exists in
master
and in
23 other branches
ActionItem114: added some unit tests to cover more code
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@828 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
39 additions
and
5 deletions
Show diff stats
app/controllers/profile_admin/membership_editor_controller.rb
| @@ -7,7 +7,7 @@ class MembershipEditorController < ProfileAdminController | @@ -7,7 +7,7 @@ class MembershipEditorController < ProfileAdminController | ||
| 7 | protect [:index, :new_enterprise, :create_enterprise ], 'edit_profile', :profile | 7 | protect [:index, :new_enterprise, :create_enterprise ], 'edit_profile', :profile |
| 8 | 8 | ||
| 9 | def index | 9 | def index |
| 10 | - @memberships = current_user.person.enterprises_memberships | 10 | + @memberships = current_user.person.enterprise_memberships |
| 11 | end | 11 | end |
| 12 | 12 | ||
| 13 | def new_enterprise | 13 | def new_enterprise |
app/models/person.rb
| @@ -23,7 +23,7 @@ class Person < Profile | @@ -23,7 +23,7 @@ class Person < Profile | ||
| 23 | :select => 'profiles.*').uniq | 23 | :select => 'profiles.*').uniq |
| 24 | end | 24 | end |
| 25 | 25 | ||
| 26 | - def enterprises_memberships | 26 | + def enterprise_memberships |
| 27 | memberships.select{|p|p.class == Enterprise} | 27 | memberships.select{|p|p.class == Enterprise} |
| 28 | end | 28 | end |
| 29 | 29 |
test/test_helper.rb
| @@ -71,6 +71,13 @@ class Test::Unit::TestCase | @@ -71,6 +71,13 @@ class Test::Unit::TestCase | ||
| 71 | admin_user.login | 71 | admin_user.login |
| 72 | end | 72 | end |
| 73 | 73 | ||
| 74 | + def create_user(name) | ||
| 75 | + User.create!(:login => name, | ||
| 76 | + :email => name + '@noosfero.org', | ||
| 77 | + :password => name.underscore, | ||
| 78 | + :password_confirmation => name.underscore) | ||
| 79 | + end | ||
| 80 | + | ||
| 74 | private | 81 | private |
| 75 | 82 | ||
| 76 | def uses_host(name) | 83 | def uses_host(name) |
test/unit/person_test.rb
| @@ -30,6 +30,7 @@ class PersonTest < Test::Unit::TestCase | @@ -30,6 +30,7 @@ class PersonTest < Test::Unit::TestCase | ||
| 30 | member_role = Role.create(:name => 'member') | 30 | member_role = Role.create(:name => 'member') |
| 31 | e.affiliate(p, member_role) | 31 | e.affiliate(p, member_role) |
| 32 | assert p.memberships.include?(e) | 32 | assert p.memberships.include?(e) |
| 33 | + assert p.enterprise_memberships.include?(e) | ||
| 33 | end | 34 | end |
| 34 | 35 | ||
| 35 | def test_can_have_user | 36 | def test_can_have_user |
| @@ -97,4 +98,14 @@ class PersonTest < Test::Unit::TestCase | @@ -97,4 +98,14 @@ class PersonTest < Test::Unit::TestCase | ||
| 97 | assert_nil p.email | 98 | assert_nil p.email |
| 98 | end | 99 | end |
| 99 | 100 | ||
| 101 | + should 'be an admin if have permission of environment administration' do | ||
| 102 | + role = Role.create!(:name => 'just_another_admin_role') | ||
| 103 | + env = Environment.create!(:name => 'blah') | ||
| 104 | + person = create_user('just_another_person').person | ||
| 105 | + env.affiliate(person, role) | ||
| 106 | + assert ! person.is_admin? | ||
| 107 | + role.update_attributes(:permissions => ['view_environment_admin_panel']) | ||
| 108 | + person.reload | ||
| 109 | + assert person.is_admin? | ||
| 110 | + end | ||
| 100 | end | 111 | end |
test/unit/profile_test.rb
| @@ -40,9 +40,9 @@ class ProfileTest < Test::Unit::TestCase | @@ -40,9 +40,9 @@ class ProfileTest < Test::Unit::TestCase | ||
| 40 | end | 40 | end |
| 41 | 41 | ||
| 42 | def test_cannot_rename | 42 | def test_cannot_rename |
| 43 | - p1 = profiles(:johndoe) | 43 | + assert_valid p = Profile.create(:name => 'new_profile', :identifier => 'new_profile') |
| 44 | assert_raise ArgumentError do | 44 | assert_raise ArgumentError do |
| 45 | - p1.identifier = 'bli' | 45 | + p.identifier = 'other_profile' |
| 46 | end | 46 | end |
| 47 | end | 47 | end |
| 48 | 48 | ||
| @@ -151,6 +151,23 @@ class ProfileTest < Test::Unit::TestCase | @@ -151,6 +151,23 @@ class ProfileTest < Test::Unit::TestCase | ||
| 151 | assert_equal 'testprofile@example.com', p.contact_email | 151 | assert_equal 'testprofile@example.com', p.contact_email |
| 152 | end | 152 | end |
| 153 | 153 | ||
| 154 | + should 'affiliate and provide a list of the affiliated users' do | ||
| 155 | + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting') | ||
| 156 | + person = create_user('test_user').person | ||
| 157 | + role = Role.create!(:name => 'just_another_test_role') | ||
| 158 | + assert profile.affiliate(person, role) | ||
| 159 | + assert profile.members.map(&:id).include?(person.id) | ||
| 160 | + end | ||
| 161 | + | ||
| 162 | + should 'authorize users that have permission on the environment' do | ||
| 163 | + env = Environment.create!(:name => 'test_env') | ||
| 164 | + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) | ||
| 165 | + person = create_user('test_user').person | ||
| 166 | + role = Role.create!(:name => 'just_another_test_role', :permissions => ['edit_profile']) | ||
| 167 | + assert env.affiliate(person, role) | ||
| 168 | + assert person.has_permission?('edit_profile', profile) | ||
| 169 | + end | ||
| 170 | + | ||
| 154 | private | 171 | private |
| 155 | 172 | ||
| 156 | def assert_invalid_identifier(id) | 173 | def assert_invalid_identifier(id) |
| @@ -158,5 +175,4 @@ class ProfileTest < Test::Unit::TestCase | @@ -158,5 +175,4 @@ class ProfileTest < Test::Unit::TestCase | ||
| 158 | assert !profile.valid? | 175 | assert !profile.valid? |
| 159 | assert profile.errors.invalid?(:identifier) | 176 | assert profile.errors.invalid?(:identifier) |
| 160 | end | 177 | end |
| 161 | - | ||
| 162 | end | 178 | end |