##// END OF EJS Templates
Adds tests for Role#has_permissions?...
Jean-Philippe Lang -
r13343:a8e375945887
parent child
Show More
@@ -1,129 +1,140
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class RoleTest < ActiveSupport::TestCase
20 class RoleTest < ActiveSupport::TestCase
21 fixtures :roles, :workflows, :trackers
21 fixtures :roles, :workflows, :trackers
22
22
23 def test_sorted_scope
23 def test_sorted_scope
24 assert_equal Role.all.sort, Role.sorted.to_a
24 assert_equal Role.all.sort, Role.sorted.to_a
25 end
25 end
26
26
27 def test_givable_scope
27 def test_givable_scope
28 assert_equal Role.all.reject(&:builtin?).sort, Role.givable.to_a
28 assert_equal Role.all.reject(&:builtin?).sort, Role.givable.to_a
29 end
29 end
30
30
31 def test_builtin_scope
31 def test_builtin_scope
32 assert_equal Role.all.select(&:builtin?).sort, Role.builtin(true).to_a.sort
32 assert_equal Role.all.select(&:builtin?).sort, Role.builtin(true).to_a.sort
33 assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).to_a.sort
33 assert_equal Role.all.reject(&:builtin?).sort, Role.builtin(false).to_a.sort
34 end
34 end
35
35
36 def test_copy_from
36 def test_copy_from
37 role = Role.find(1)
37 role = Role.find(1)
38 copy = Role.new.copy_from(role)
38 copy = Role.new.copy_from(role)
39
39
40 assert_nil copy.id
40 assert_nil copy.id
41 assert_equal '', copy.name
41 assert_equal '', copy.name
42 assert_equal role.permissions, copy.permissions
42 assert_equal role.permissions, copy.permissions
43
43
44 copy.name = 'Copy'
44 copy.name = 'Copy'
45 assert copy.save
45 assert copy.save
46 end
46 end
47
47
48 def test_copy_workflows
48 def test_copy_workflows
49 source = Role.find(1)
49 source = Role.find(1)
50 assert_equal 90, source.workflow_rules.size
50 assert_equal 90, source.workflow_rules.size
51
51
52 target = Role.new(:name => 'Target')
52 target = Role.new(:name => 'Target')
53 assert target.save
53 assert target.save
54 target.workflow_rules.copy(source)
54 target.workflow_rules.copy(source)
55 target.reload
55 target.reload
56 assert_equal 90, target.workflow_rules.size
56 assert_equal 90, target.workflow_rules.size
57 end
57 end
58
58
59 def test_permissions_should_be_unserialized_with_its_coder
59 def test_permissions_should_be_unserialized_with_its_coder
60 Role::PermissionsAttributeCoder.expects(:load).once
60 Role::PermissionsAttributeCoder.expects(:load).once
61 Role.find(1).permissions
61 Role.find(1).permissions
62 end
62 end
63
63
64 def test_add_permission
64 def test_add_permission
65 role = Role.find(1)
65 role = Role.find(1)
66 size = role.permissions.size
66 size = role.permissions.size
67 role.add_permission!("apermission", "anotherpermission")
67 role.add_permission!("apermission", "anotherpermission")
68 role.reload
68 role.reload
69 assert role.permissions.include?(:anotherpermission)
69 assert role.permissions.include?(:anotherpermission)
70 assert_equal size + 2, role.permissions.size
70 assert_equal size + 2, role.permissions.size
71 end
71 end
72
72
73 def test_remove_permission
73 def test_remove_permission
74 role = Role.find(1)
74 role = Role.find(1)
75 size = role.permissions.size
75 size = role.permissions.size
76 perm = role.permissions[0..1]
76 perm = role.permissions[0..1]
77 role.remove_permission!(*perm)
77 role.remove_permission!(*perm)
78 role.reload
78 role.reload
79 assert ! role.permissions.include?(perm[0])
79 assert ! role.permissions.include?(perm[0])
80 assert_equal size - 2, role.permissions.size
80 assert_equal size - 2, role.permissions.size
81 end
81 end
82
82
83 def test_has_permission
84 role = Role.create!(:name => 'Test', :permissions => [:view_issues, :edit_issues])
85 assert_equal true, role.has_permission?(:view_issues)
86 assert_equal false, role.has_permission?(:delete_issues)
87 end
88
89 def test_has_permission_without_permissions
90 role = Role.create!(:name => 'Test')
91 assert_equal false, role.has_permission?(:delete_issues)
92 end
93
83 def test_name
94 def test_name
84 I18n.locale = 'fr'
95 I18n.locale = 'fr'
85 assert_equal 'Manager', Role.find(1).name
96 assert_equal 'Manager', Role.find(1).name
86 assert_equal 'Anonyme', Role.anonymous.name
97 assert_equal 'Anonyme', Role.anonymous.name
87 assert_equal 'Non membre', Role.non_member.name
98 assert_equal 'Non membre', Role.non_member.name
88 end
99 end
89
100
90 def test_find_all_givable
101 def test_find_all_givable
91 assert_equal Role.all.reject(&:builtin?).sort, Role.find_all_givable
102 assert_equal Role.all.reject(&:builtin?).sort, Role.find_all_givable
92 end
103 end
93
104
94 def test_anonymous_should_return_the_anonymous_role
105 def test_anonymous_should_return_the_anonymous_role
95 assert_no_difference('Role.count') do
106 assert_no_difference('Role.count') do
96 role = Role.anonymous
107 role = Role.anonymous
97 assert role.builtin?
108 assert role.builtin?
98 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
109 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
99 end
110 end
100 end
111 end
101
112
102 def test_anonymous_with_a_missing_anonymous_role_should_return_the_anonymous_role
113 def test_anonymous_with_a_missing_anonymous_role_should_return_the_anonymous_role
103 Role.where(:builtin => Role::BUILTIN_ANONYMOUS).delete_all
114 Role.where(:builtin => Role::BUILTIN_ANONYMOUS).delete_all
104
115
105 assert_difference('Role.count') do
116 assert_difference('Role.count') do
106 role = Role.anonymous
117 role = Role.anonymous
107 assert role.builtin?
118 assert role.builtin?
108 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
119 assert_equal Role::BUILTIN_ANONYMOUS, role.builtin
109 end
120 end
110 end
121 end
111
122
112 def test_non_member_should_return_the_non_member_role
123 def test_non_member_should_return_the_non_member_role
113 assert_no_difference('Role.count') do
124 assert_no_difference('Role.count') do
114 role = Role.non_member
125 role = Role.non_member
115 assert role.builtin?
126 assert role.builtin?
116 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
127 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
117 end
128 end
118 end
129 end
119
130
120 def test_non_member_with_a_missing_non_member_role_should_return_the_non_member_role
131 def test_non_member_with_a_missing_non_member_role_should_return_the_non_member_role
121 Role.where(:builtin => Role::BUILTIN_NON_MEMBER).delete_all
132 Role.where(:builtin => Role::BUILTIN_NON_MEMBER).delete_all
122
133
123 assert_difference('Role.count') do
134 assert_difference('Role.count') do
124 role = Role.non_member
135 role = Role.non_member
125 assert role.builtin?
136 assert role.builtin?
126 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
137 assert_equal Role::BUILTIN_NON_MEMBER, role.builtin
127 end
138 end
128 end
139 end
129 end
140 end
General Comments 0
You need to be logged in to leave comments. Login now