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,7 +50,8 @@ class ApplicationController < ActionController::Base
50 end 50 end
51 51
52 def load_profile 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 end 55 end
55 56
56 def user 57 def user
app/controllers/profile_admin/enterprise_editor_controller.rb
1 class EnterpriseEditorController < ProfileAdminController 1 class EnterpriseEditorController < ProfileAdminController
2 - needs_profile  
3 - protect 'edit_profile', :profile, :exept => :destroy 2 + protect 'edit_profile', :profile, :user, :except => :destroy
4 protect 'destroy_profile', :profile, :only => :destroy 3 protect 'destroy_profile', :profile, :only => :destroy
5 4
6 before_filter :check_enterprise 5 before_filter :check_enterprise
@@ -28,7 +27,7 @@ class EnterpriseEditorController &lt; ProfileAdminController @@ -28,7 +27,7 @@ class EnterpriseEditorController &lt; ProfileAdminController
28 # Elimitates the enterprise of the system 27 # Elimitates the enterprise of the system
29 def destroy 28 def destroy
30 #raise "bli" 29 #raise "bli"
31 - if @enterprise.destroy! 30 + if @enterprise.destroy
32 flash[:notice] = _('Enterprise sucessfully erased from the system') 31 flash[:notice] = _('Enterprise sucessfully erased from the system')
33 redirect_to :controller => 'profile_editor', :action => 'index', :profile => current_user.login 32 redirect_to :controller => 'profile_editor', :action => 'index', :profile => current_user.login
34 else 33 else
@@ -36,24 +35,8 @@ class EnterpriseEditorController &lt; ProfileAdminController @@ -36,24 +35,8 @@ class EnterpriseEditorController &lt; ProfileAdminController
36 end 35 end
37 end 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 protected 38 protected
50 39
51 - def permission  
52 - 'bli'  
53 - end  
54 - def permission=(perm)  
55 - @p = perm  
56 - end  
57 def check_enterprise 40 def check_enterprise
58 if profile.is_a?(Enterprise) 41 if profile.is_a?(Enterprise)
59 @enterprise = profile 42 @enterprise = profile
app/controllers/profile_admin/profile_members_controller.rb
1 class ProfileMembersController < ProfileAdminController 1 class ProfileMembersController < ProfileAdminController
2 -  
3 protect 'manage_memberships', :profile 2 protect 'manage_memberships', :profile
4 3
5 def index 4 def index
6 - @members = profile.people.uniq 5 + @members = profile.members
7 end 6 end
8 7
9 def change_roles 8 def change_roles
@@ -12,8 +11,8 @@ class ProfileMembersController &lt; ProfileAdminController @@ -12,8 +11,8 @@ class ProfileMembersController &lt; ProfileAdminController
12 end 11 end
13 12
14 def update_roles 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 if @person.define_roles(@roles, profile) 16 if @person.define_roles(@roles, profile)
18 flash[:notice] = _('Roles successfuly updated') 17 flash[:notice] = _('Roles successfuly updated')
19 else 18 else
@@ -25,7 +24,7 @@ class ProfileMembersController &lt; ProfileAdminController @@ -25,7 +24,7 @@ class ProfileMembersController &lt; ProfileAdminController
25 def change_role 24 def change_role
26 @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) } 25 @roles = Role.find(:all).select{ |r| r.has_kind?(:profile) }
27 @member = Person.find(params[:id]) 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 end 28 end
30 29
31 def add_role 30 def add_role
app/controllers/profile_admin_controller.rb
@@ -13,8 +13,8 @@ class ProfileAdminController &lt; ApplicationController @@ -13,8 +13,8 @@ class ProfileAdminController &lt; ApplicationController
13 # a subclass of Person) 13 # a subclass of Person)
14 def self.requires_profile_class(some_class) 14 def self.requires_profile_class(some_class)
15 before_filter do |controller| 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 controller.render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'access_denied.rhtml'), :layout => true, :status => 403 18 controller.render :file => File.join(RAILS_ROOT, 'app', 'views', 'shared', 'access_denied.rhtml'), :layout => true, :status => 403
19 end 19 end
20 end 20 end
app/models/profile.rb
@@ -54,7 +54,6 @@ class Profile &lt; ActiveRecord::Base @@ -54,7 +54,6 @@ class Profile &lt; ActiveRecord::Base
54 belongs_to :environment 54 belongs_to :environment
55 55
56 has_many :role_assignments, :as => :resource 56 has_many :role_assignments, :as => :resource
57 - has_many :people, :through => :role_assignments  
58 57
59 58
60 # Sets the identifier for this profile. Raises an exception when called on a 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,7 +4,8 @@
4 4
5 <%= _('Roles: ') %> <br> 5 <%= _('Roles: ') %> <br>
6 <% @roles.each do |r| %> 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 <% end %> 9 <% end %>
9 <%= hidden_field_tag 'person', @member.id %> 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,8 +12,8 @@ class EnterpriseEditorControllerTest &lt; Test::Unit::TestCase
12 end 12 end
13 13
14 should 'not see index if do not logged in' do 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 assert_response :success 18 assert_response :success
19 assert_template 'access_denied.rhtml' 19 assert_template 'access_denied.rhtml'
@@ -33,17 +33,56 @@ class EnterpriseEditorControllerTest &lt; Test::Unit::TestCase @@ -33,17 +33,56 @@ class EnterpriseEditorControllerTest &lt; Test::Unit::TestCase
33 end 33 end
34 34
35 should 'see index if have permission' do 35 should 'see index if have permission' do
36 - user = create_user('test_user').person  
37 ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enterprise') 36 ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enterprise')
  37 + user = create_user('test_user').person
38 role = Role.create!(:name => 'test_role', :permissions => ['edit_profile']) 38 role = Role.create!(:name => 'test_role', :permissions => ['edit_profile'])
39 assert user.add_role(role, ent) 39 assert user.add_role(role, ent)
40 assert user.has_permission?('edit_profile', ent) 40 assert user.has_permission?('edit_profile', ent)
41 login_as :test_user 41 login_as :test_user
42 42
  43 + assert_equal ent, Profile.find_by_identifier('test_enterprise')
  44 +
43 get 'index', :profile => 'test_enterprise' 45 get 'index', :profile => 'test_enterprise'
44 46
45 assert_response :success 47 assert_response :success
46 - assert @controller.send(:profile) 48 + assert_equal ent, @controller.send(:profile)
  49 + assert_equal user, @controller.send(:user)
47 assert_template 'index' 50 assert_template 'index'
48 end 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 end 88 end
test/functional/profile_admin_controller_test.rb
@@ -6,7 +6,6 @@ class ProfileAdminController; def rescue_action(e) raise e end; end @@ -6,7 +6,6 @@ class ProfileAdminController; def rescue_action(e) raise e end; end
6 6
7 class OnlyForPersonTestController < ProfileAdminController 7 class OnlyForPersonTestController < ProfileAdminController
8 requires_profile_class Person 8 requires_profile_class Person
9 - design :holder => :profile  
10 def index 9 def index
11 render :text => '<div>something</div>' 10 render :text => '<div>something</div>'
12 end 11 end
@@ -23,19 +22,17 @@ class ProfileAdminControllerTest &lt; Test::Unit::TestCase @@ -23,19 +22,17 @@ class ProfileAdminControllerTest &lt; Test::Unit::TestCase
23 22
24 def test_should_allow_person 23 def test_should_allow_person
25 @controller = OnlyForPersonTestController.new 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 assert_response :success 28 assert_response :success
31 end 29 end
32 30
33 def test_should_not_allow_bare_profile 31 def test_should_not_allow_bare_profile
34 @controller = OnlyForPersonTestController.new 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 assert_response 403 # forbidden 36 assert_response 403 # forbidden
40 end 37 end
41 end 38 end
test/functional/profile_members_controller_test.rb
@@ -11,8 +11,60 @@ class ProfileMembersControllerTest &lt; Test::Unit::TestCase @@ -11,8 +11,60 @@ class ProfileMembersControllerTest &lt; Test::Unit::TestCase
11 @response = ActionController::TestResponse.new 11 @response = ActionController::TestResponse.new
12 end 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 end 69 end
18 end 70 end
test/test_helper.rb
@@ -78,6 +78,19 @@ class Test::Unit::TestCase @@ -78,6 +78,19 @@ class Test::Unit::TestCase
78 :password_confirmation => name.underscore) 78 :password_confirmation => name.underscore)
79 end 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 private 94 private
82 95
83 def uses_host(name) 96 def uses_host(name)