##// END OF EJS Templates
Use generated groups....
Jean-Philippe Lang -
r14328:c63da2c0a5b5
parent child
Show More
@@ -1,209 +1,216
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
21 21 fixtures :users, :groups_users, :email_addresses
22 22
23 23 test "GET /groups.xml should require authentication" do
24 24 get '/groups.xml'
25 25 assert_response 401
26 26 end
27 27
28 28 test "GET /groups.xml should return givable groups" do
29 29 get '/groups.xml', {}, credentials('admin')
30 30 assert_response :success
31 31 assert_equal 'application/xml', response.content_type
32 32
33 33 assert_select 'groups' do
34 34 assert_select 'group', Group.givable.count
35 35 assert_select 'group' do
36 36 assert_select 'name', :text => 'A Team'
37 37 assert_select 'id', :text => '10'
38 38 end
39 39 end
40 40 end
41 41
42 42 test "GET /groups.xml?builtin=1 should return all groups" do
43 43 get '/groups.xml?builtin=1', {}, credentials('admin')
44 44 assert_response :success
45 45 assert_equal 'application/xml', response.content_type
46 46
47 47 assert_select 'groups' do
48 48 assert_select 'group', Group.givable.count + 2
49 49 assert_select 'group' do
50 50 assert_select 'builtin', :text => 'non_member'
51 51 assert_select 'id', :text => '12'
52 52 end
53 53 assert_select 'group' do
54 54 assert_select 'builtin', :text => 'anonymous'
55 55 assert_select 'id', :text => '13'
56 56 end
57 57 end
58 58 end
59 59
60 60 test "GET /groups.json should require authentication" do
61 61 get '/groups.json'
62 62 assert_response 401
63 63 end
64 64
65 65 test "GET /groups.json should return groups" do
66 66 get '/groups.json', {}, credentials('admin')
67 67 assert_response :success
68 68 assert_equal 'application/json', response.content_type
69 69
70 70 json = MultiJson.load(response.body)
71 71 groups = json['groups']
72 72 assert_kind_of Array, groups
73 73 group = groups.detect {|g| g['name'] == 'A Team'}
74 74 assert_not_nil group
75 75 assert_equal({'id' => 10, 'name' => 'A Team'}, group)
76 76 end
77 77
78 78 test "GET /groups/:id.xml should return the group" do
79 79 get '/groups/10.xml', {}, credentials('admin')
80 80 assert_response :success
81 81 assert_equal 'application/xml', response.content_type
82 82
83 83 assert_select 'group' do
84 84 assert_select 'name', :text => 'A Team'
85 85 assert_select 'id', :text => '10'
86 86 end
87 87 end
88 88
89 89 test "GET /groups/:id.xml should return the builtin group" do
90 90 get '/groups/12.xml', {}, credentials('admin')
91 91 assert_response :success
92 92 assert_equal 'application/xml', response.content_type
93 93
94 94 assert_select 'group' do
95 95 assert_select 'builtin', :text => 'non_member'
96 96 assert_select 'id', :text => '12'
97 97 end
98 98 end
99 99
100 100 test "GET /groups/:id.xml should include users if requested" do
101 101 get '/groups/10.xml?include=users', {}, credentials('admin')
102 102 assert_response :success
103 103 assert_equal 'application/xml', response.content_type
104 104
105 105 assert_select 'group' do
106 106 assert_select 'users' do
107 107 assert_select 'user', Group.find(10).users.count
108 108 assert_select 'user[id="8"]'
109 109 end
110 110 end
111 111 end
112 112
113 113 test "GET /groups/:id.xml include memberships if requested" do
114 114 get '/groups/10.xml?include=memberships', {}, credentials('admin')
115 115 assert_response :success
116 116 assert_equal 'application/xml', response.content_type
117 117
118 118 assert_select 'group' do
119 119 assert_select 'memberships'
120 120 end
121 121 end
122 122
123 123 test "POST /groups.xml with valid parameters should create the group" do
124 124 assert_difference('Group.count') do
125 125 post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
126 126 assert_response :created
127 127 assert_equal 'application/xml', response.content_type
128 128 end
129 129
130 130 group = Group.order('id DESC').first
131 131 assert_equal 'Test', group.name
132 132 assert_equal [2, 3], group.users.map(&:id).sort
133 133
134 134 assert_select 'group' do
135 135 assert_select 'name', :text => 'Test'
136 136 end
137 137 end
138 138
139 139 test "POST /groups.xml with invalid parameters should return errors" do
140 140 assert_no_difference('Group.count') do
141 141 post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
142 142 end
143 143 assert_response :unprocessable_entity
144 144 assert_equal 'application/xml', response.content_type
145 145
146 146 assert_select 'errors' do
147 147 assert_select 'error', :text => /Name cannot be blank/
148 148 end
149 149 end
150 150
151 151 test "PUT /groups/:id.xml with valid parameters should update the group" do
152 put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
152 group = Group.generate!
153 put "/groups/#{group.id}.xml", {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
153 154 assert_response :ok
154 155 assert_equal '', @response.body
155 156
156 group = Group.find(10)
157 assert_equal 'New name', group.name
157 assert_equal 'New name', group.reload.name
158 158 assert_equal [2, 3], group.users.map(&:id).sort
159 159 end
160 160
161 161 test "PUT /groups/:id.xml with invalid parameters should return errors" do
162 put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
162 group = Group.generate!
163 put "/groups/#{group.id}.xml", {:group => {:name => ''}}, credentials('admin')
163 164 assert_response :unprocessable_entity
164 165 assert_equal 'application/xml', response.content_type
165 166
166 167 assert_select 'errors' do
167 168 assert_select 'error', :text => /Name cannot be blank/
168 169 end
169 170 end
170 171
171 172 test "DELETE /groups/:id.xml should delete the group" do
173 group = Group.generate!
172 174 assert_difference 'Group.count', -1 do
173 delete '/groups/10.xml', {}, credentials('admin')
175 delete "/groups/#{group.id}.xml", {}, credentials('admin')
174 176 assert_response :ok
175 177 assert_equal '', @response.body
176 178 end
177 179 end
178 180
179 181 test "POST /groups/:id/users.xml should add user to the group" do
180 assert_difference 'Group.find(10).users.count' do
181 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
182 group = Group.generate!
183 assert_difference 'group.reload.users.count' do
184 post "/groups/#{group.id}/users.xml", {:user_id => 5}, credentials('admin')
182 185 assert_response :ok
183 186 assert_equal '', @response.body
184 187 end
185 assert_include User.find(5), Group.find(10).users
188 assert_include User.find(5), group.reload.users
186 189 end
187 190
188 191 test "POST /groups/:id/users.xml should not add the user if already added" do
189 Group.find(10).users << User.find(5)
192 group = Group.generate!
193 group.users << User.find(5)
190 194
191 assert_no_difference 'Group.find(10).users.count' do
192 post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
195 assert_no_difference 'group.reload.users.count' do
196 post "/groups/#{group.id}/users.xml", {:user_id => 5}, credentials('admin')
193 197 assert_response :unprocessable_entity
194 198 end
195 199
196 200 assert_select 'errors' do
197 201 assert_select 'error', :text => /User is invalid/
198 202 end
199 203 end
200 204
201 205 test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
202 assert_difference 'Group.find(10).users.count', -1 do
203 delete '/groups/10/users/8.xml', {}, credentials('admin')
206 group = Group.generate!
207 group.users << User.find(8)
208
209 assert_difference 'group.reload.users.count', -1 do
210 delete "/groups/#{group.id}/users/8.xml", {}, credentials('admin')
204 211 assert_response :ok
205 212 assert_equal '', @response.body
206 213 end
207 assert_not_include User.find(8), Group.find(10).users
214 assert_not_include User.find(8), group.reload.users
208 215 end
209 216 end
General Comments 0
You need to be logged in to leave comments. Login now