Commit 43e8b3ad1d7f92657c94d06de421469949fba152

Authored by MoisesMachado
1 parent e0003411

ActionItem114: updated tests

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@879 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -50,7 +50,8 @@ class ApplicationController < ActionController::Base
50 50 end
51 51  
52 52 def load_profile
53   - @profile = Profile.find_by_identifier(params[:profile])
  53 + @profile = Profile.find_by_identifier(params[:profile]) unless @profile
  54 + render_not_found unless @profile
54 55 end
55 56  
56 57 def user
... ...
app/controllers/profile_admin/enterprise_editor_controller.rb
1 1 class EnterpriseEditorController < ProfileAdminController
2   - needs_profile
3   - protect 'edit_profile', :profile, :exept => :destroy
  2 + protect 'edit_profile', :profile, :user, :except => :destroy
4 3 protect 'destroy_profile', :profile, :only => :destroy
5 4  
6 5 before_filter :check_enterprise
... ... @@ -28,7 +27,7 @@ class EnterpriseEditorController &lt; ProfileAdminController
28 27 # Elimitates the enterprise of the system
29 28 def destroy
30 29 #raise "bli"
31   - if @enterprise.destroy!
  30 + if @enterprise.destroy
32 31 flash[:notice] = _('Enterprise sucessfully erased from the system')
33 32 redirect_to :controller => 'profile_editor', :action => 'index', :profile => current_user.login
34 33 else
... ... @@ -36,24 +35,8 @@ class EnterpriseEditorController &lt; ProfileAdminController
36 35 end
37 36 end
38 37  
39   - # Activate a validated enterprise
40   - def activate
41   - if @enterprise.activatepermission.nil?
42   - flash[:notice] = _('Enterprise successfuly activacted')
43   - else
44   - flash[:notice] = _('Failed to activate the enterprise')
45   - end
46   - redirect_to :action => 'index'
47   - end
48   -
49 38 protected
50 39  
51   - def permission
52   - 'bli'
53   - end
54   - def permission=(perm)
55   - @p = perm
56   - end
57 40 def check_enterprise
58 41 if profile.is_a?(Enterprise)
59 42 @enterprise = profile
... ...
app/controllers/profile_admin/profile_members_controller.rb
1 1 class ProfileMembersController < ProfileAdminController
2   -
3 2 protect 'manage_memberships', :profile
4 3  
5 4 def index
6   - @members = profile.people.uniq
  5 + @members = profile.members
7 6 end
8 7  
9 8 def change_roles
... ... @@ -12,8 +11,8 @@ class ProfileMembersController &lt; ProfileAdminController
12 11 end
13 12  
14 13 def update_roles
15   - @roles = Role.find(params[:roles])
16   - @person = Person.find(params[:person])
  14 + @roles = params[:roles] ? Role.find(params[:roles]) : []
  15 + @person = Person.find(params[:person])
17 16 if @person.define_roles(@roles, profile)
18 17 flash[:notice] = _('Roles successfuly updated')
19 18 else
... ... @@ -25,7 +24,7 @@ class ProfileMembersController &lt; ProfileAdminController
25 24 def change_role
26 25 @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
27 26 @member = Person.find(params[:id])
28   - @associations = RoleAssignment.find(:all, :conditions => {:person_id => @member, :resource_id => @profile, :resource_type => @profile.class.base_class.name})
  27 + @associations = @member.find_roles(@profile)
29 28 end
30 29  
31 30 def add_role
... ...
app/controllers/profile_admin_controller.rb
... ... @@ -13,8 +13,8 @@ class ProfileAdminController &lt; ApplicationController
13 13 # a subclass of Person)
14 14 def self.requires_profile_class(some_class)
15 15 before_filter do |controller|
16   - unless controller.profile.kind_of?(some_class)
17   - controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.profile.name)
  16 + unless controller.send(:profile).kind_of?(some_class)
  17 + controller.instance_variable_set('@message', _("This action is not available for \"%s\".") % controller.send(:profile).name)
18 18 controller.render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'access_denied.rhtml'), :layout => true, :status => 403
19 19 end
20 20 end
... ...
app/models/profile.rb
... ... @@ -54,7 +54,6 @@ class Profile &lt; ActiveRecord::Base
54 54 belongs_to :environment
55 55  
56 56 has_many :role_assignments, :as => :resource
57   - has_many :people, :through => :role_assignments
58 57  
59 58  
60 59 # Sets the identifier for this profile. Raises an exception when called on a
... ...
app/views/profile_members/change_role.rhtml
... ... @@ -4,7 +4,8 @@
4 4  
5 5 <%= _('Roles: ') %> <br>
6 6 <% @roles.each do |r| %>
7   - <%= labelled_form_field(r.name, (check_box_tag "roles[]", r.id, @associations.map(&:role).include?(r))) %>
  7 + <%= check_box_tag "roles[]", r.id, @associations.map(&:role).include?(r) %>
  8 + <%= r.name.capitalize %><br/>
8 9 <% end %>
9 10 <%= hidden_field_tag 'person', @member.id %>
10 11  
... ...
test/functional/enterprise_editor_controller_test.rb
... ... @@ -12,8 +12,8 @@ class EnterpriseEditorControllerTest &lt; Test::Unit::TestCase
12 12 end
13 13  
14 14 should 'not see index if do not logged in' do
15   - ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enteprise')
16   - get 'index', :profile => 'test_enterprise'
  15 + ent1 = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1')
  16 + get 'index', :profile => 'test_enterprise1'
17 17  
18 18 assert_response :success
19 19 assert_template 'access_denied.rhtml'
... ... @@ -33,17 +33,56 @@ class EnterpriseEditorControllerTest &lt; Test::Unit::TestCase
33 33 end
34 34  
35 35 should 'see index if have permission' do
36   - user = create_user('test_user').person
37 36 ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enterprise')
  37 + user = create_user('test_user').person
38 38 role = Role.create!(:name => 'test_role', :permissions => ['edit_profile'])
39 39 assert user.add_role(role, ent)
40 40 assert user.has_permission?('edit_profile', ent)
41 41 login_as :test_user
42 42  
  43 + assert_equal ent, Profile.find_by_identifier('test_enterprise')
  44 +
43 45 get 'index', :profile => 'test_enterprise'
44 46  
45 47 assert_response :success
46   - assert @controller.send(:profile)
  48 + assert_equal ent, @controller.send(:profile)
  49 + assert_equal user, @controller.send(:user)
47 50 assert_template 'index'
48 51 end
  52 +
  53 + should 'show the edit form' do
  54 + ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enterprise')
  55 + user = create_user_with_permission('test_user', 'edit_profile', ent)
  56 + login_as :test_user
  57 +
  58 + get 'edit', :profile => 'test_enterprise'
  59 +
  60 + assert_response :success
  61 + assert_equal ent, @controller.send(:profile)
  62 + assert_template 'edit'
  63 + end
  64 +
  65 + should 'update' do
  66 + ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enterprise')
  67 + user = create_user_with_permission('test_user', 'edit_profile', ent)
  68 + login_as :test_user
  69 +
  70 + post 'update', :profile => 'test_enterprise', :enterprise => {:name => 'test_name'}
  71 +
  72 + assert_response :redirect
  73 + assert_redirected_to :action => 'index'
  74 + ent.reload
  75 + assert_equal 'test_name', ent.name
  76 + end
  77 +
  78 + should 'destroy' do
  79 + ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enterprise')
  80 + user = create_user_with_permission('test_user', 'destroy_profile', ent)
  81 + login_as :test_user
  82 +
  83 + post 'destroy', :profile => 'test_enterprise'
  84 +
  85 + assert_response :redirect
  86 + assert_redirected_to :controller => 'profile_editor', :profile => 'test_user'
  87 + end
49 88 end
... ...
test/functional/profile_admin_controller_test.rb
... ... @@ -6,7 +6,6 @@ class ProfileAdminController; def rescue_action(e) raise e end; end
6 6  
7 7 class OnlyForPersonTestController < ProfileAdminController
8 8 requires_profile_class Person
9   - design :holder => :profile
10 9 def index
11 10 render :text => '<div>something</div>'
12 11 end
... ... @@ -23,19 +22,17 @@ class ProfileAdminControllerTest &lt; Test::Unit::TestCase
23 22  
24 23 def test_should_allow_person
25 24 @controller = OnlyForPersonTestController.new
26   - person = Person.new(:name => 'Random Joe')
27   - @controller.stubs(:profile).returns(person)
  25 + person = create_user('random_joe')
28 26  
29   - get :index
  27 + get :index, :profile => 'random_joe'
30 28 assert_response :success
31 29 end
32 30  
33 31 def test_should_not_allow_bare_profile
34 32 @controller = OnlyForPersonTestController.new
35   - org = Organization.new(:name => 'Hacking Institute')
36   - @controller.stubs(:profile).returns(org)
  33 + org = Organization.create!(:identifier => 'hacking_institute', :name => 'Hacking Institute')
37 34  
38   - get :index
  35 + get :index, :profile => 'hacking_institute'
39 36 assert_response 403 # forbidden
40 37 end
41 38 end
... ...
test/functional/profile_members_controller_test.rb
... ... @@ -11,8 +11,60 @@ class ProfileMembersControllerTest &lt; Test::Unit::TestCase
11 11 @response = ActionController::TestResponse.new
12 12 end
13 13  
14   - # Replace this with your real tests.
15   - def test_truth
16   - assert true
  14 + should 'not access index if dont have permission' do
  15 + user = create_user('test_user')
  16 + Enterprise.create!(:identifier => 'test_enterprise', :name => 'test enterprise')
  17 + login_as :test_user
  18 +
  19 + get 'index', :profile => 'test_enterprise'
  20 +
  21 + assert_response :success
  22 + assert_template 'access_denied.rhtml'
  23 + end
  24 +
  25 + should 'access index' do
  26 + ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'test enterprise')
  27 + user = create_user_with_permission('test_user', 'manage_memberships', ent)
  28 + login_as :test_user
  29 +
  30 + get 'index', :profile => 'test_enterprise'
  31 +
  32 + assert_response :success
  33 + assert_template 'index'
  34 + end
  35 +
  36 + should 'show form to change role' do
  37 + ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'test enterprise')
  38 + role = Role.create!(:name => 'member_role', :permissions => ['edit_profile'])
  39 + member = create_user('test_member').person
  40 + member.add_role(role, ent)
  41 + user = create_user_with_permission('test_user', 'manage_memberships', ent)
  42 + login_as :test_user
  43 +
  44 + get 'change_role', :profile => 'test_enterprise' , :id => member
  45 +
  46 + assert_response :success
  47 + assert_equal member, assigns('member')
  48 + assert_template 'change_role'
  49 + end
  50 +
  51 + should 'update roles' do
  52 + ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'test enterprise')
  53 + role = Role.create!(:name => 'member_role', :permissions => ['edit_profile'])
  54 + orole = Role.create!(:name => 'owner_role', :permissions => ['edit_profile', 'destroy_profile'])
  55 +
  56 + member = create_user('test_member').person
  57 + member.add_role(role, ent)
  58 + user = create_user_with_permission('test_user', 'manage_memberships', ent)
  59 + login_as :test_user
  60 +
  61 + post 'update_roles', :profile => 'test_enterprise', :roles => [orole.id], :person => member
  62 +
  63 + assert_response :redirect
  64 + member.reload
  65 + assert member.find_roles(ent).map(&:role).include?(orole)
  66 + assert !member.find_roles(ent).map(&:role).include?(role)
  67 +
  68 +
17 69 end
18 70 end
... ...
test/test_helper.rb
... ... @@ -78,6 +78,19 @@ class Test::Unit::TestCase
78 78 :password_confirmation => name.underscore)
79 79 end
80 80  
  81 + def create_user_with_permission(name, permission, target)
  82 + user = create_user(name).person
  83 + i = 0
  84 + while Role.find_by_name('test_role' + i.to_s)
  85 + i+=1
  86 + end
  87 +
  88 + role = Role.create!(:name => 'test_role' + i.to_s, :permissions => [permission])
  89 + assert user.add_role(role, target)
  90 + assert user.has_permission?(permission, target)
  91 + user
  92 + end
  93 +
81 94 private
82 95  
83 96 def uses_host(name)
... ...