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