comparison .svn/pristine/c9/c915cdc5a63346a4f09f2d9cd1c727112cb89f1c.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require File.expand_path('../../../test_helper', __FILE__)
19
20 class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
21 fixtures :users, :members, :member_roles, :roles, :projects
22
23 def setup
24 Setting.rest_api_enabled = '1'
25 end
26
27 context "GET /users" do
28 should_allow_api_authentication(:get, "/users.xml")
29 should_allow_api_authentication(:get, "/users.json")
30 end
31
32 context "GET /users/2" do
33 context ".xml" do
34 should "return requested user" do
35 get '/users/2.xml'
36
37 assert_response :success
38 assert_tag :tag => 'user',
39 :child => {:tag => 'id', :content => '2'}
40 end
41
42 context "with include=memberships" do
43 should "include memberships" do
44 get '/users/2.xml?include=memberships'
45
46 assert_response :success
47 assert_tag :tag => 'memberships',
48 :parent => {:tag => 'user'},
49 :children => {:count => 1}
50 end
51 end
52 end
53
54 context ".json" do
55 should "return requested user" do
56 get '/users/2.json'
57
58 assert_response :success
59 json = ActiveSupport::JSON.decode(response.body)
60 assert_kind_of Hash, json
61 assert_kind_of Hash, json['user']
62 assert_equal 2, json['user']['id']
63 end
64
65 context "with include=memberships" do
66 should "include memberships" do
67 get '/users/2.json?include=memberships'
68
69 assert_response :success
70 json = ActiveSupport::JSON.decode(response.body)
71 assert_kind_of Array, json['user']['memberships']
72 assert_equal [{
73 "id"=>1,
74 "project"=>{"name"=>"eCookbook", "id"=>1},
75 "roles"=>[{"name"=>"Manager", "id"=>1}]
76 }], json['user']['memberships']
77 end
78 end
79 end
80 end
81
82 context "GET /users/current" do
83 context ".xml" do
84 should "require authentication" do
85 get '/users/current.xml'
86
87 assert_response 401
88 end
89
90 should "return current user" do
91 get '/users/current.xml', {}, credentials('jsmith')
92
93 assert_tag :tag => 'user',
94 :child => {:tag => 'id', :content => '2'}
95 end
96 end
97 end
98
99 test "GET /users/:id should not return login for other user" do
100 get '/users/3.xml', {}, credentials('jsmith')
101 assert_response :success
102 assert_no_tag 'user', :child => {:tag => 'login'}
103 end
104
105 test "GET /users/:id should return login for current user" do
106 get '/users/2.xml', {}, credentials('jsmith')
107 assert_response :success
108 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
109 end
110
111 test "GET /users/:id should not return api_key for other user" do
112 get '/users/3.xml', {}, credentials('jsmith')
113 assert_response :success
114 assert_no_tag 'user', :child => {:tag => 'api_key'}
115 end
116
117 test "GET /users/:id should return api_key for current user" do
118 get '/users/2.xml', {}, credentials('jsmith')
119 assert_response :success
120 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
121 end
122
123 context "POST /users" do
124 context "with valid parameters" do
125 setup do
126 @parameters = {
127 :user => {
128 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
129 :mail => 'foo@example.net', :password => 'secret123',
130 :mail_notification => 'only_assigned'
131 }
132 }
133 end
134
135 context ".xml" do
136 should_allow_api_authentication(:post,
137 '/users.xml',
138 {:user => {
139 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
140 :mail => 'foo@example.net', :password => 'secret123'
141 }},
142 {:success_code => :created})
143
144 should "create a user with the attributes" do
145 assert_difference('User.count') do
146 post '/users.xml', @parameters, credentials('admin')
147 end
148
149 user = User.first(:order => 'id DESC')
150 assert_equal 'foo', user.login
151 assert_equal 'Firstname', user.firstname
152 assert_equal 'Lastname', user.lastname
153 assert_equal 'foo@example.net', user.mail
154 assert_equal 'only_assigned', user.mail_notification
155 assert !user.admin?
156 assert user.check_password?('secret123')
157
158 assert_response :created
159 assert_equal 'application/xml', @response.content_type
160 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
161 end
162 end
163
164 context ".json" do
165 should_allow_api_authentication(:post,
166 '/users.json',
167 {:user => {
168 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
169 :mail => 'foo@example.net'
170 }},
171 {:success_code => :created})
172
173 should "create a user with the attributes" do
174 assert_difference('User.count') do
175 post '/users.json', @parameters, credentials('admin')
176 end
177
178 user = User.first(:order => 'id DESC')
179 assert_equal 'foo', user.login
180 assert_equal 'Firstname', user.firstname
181 assert_equal 'Lastname', user.lastname
182 assert_equal 'foo@example.net', user.mail
183 assert !user.admin?
184
185 assert_response :created
186 assert_equal 'application/json', @response.content_type
187 json = ActiveSupport::JSON.decode(response.body)
188 assert_kind_of Hash, json
189 assert_kind_of Hash, json['user']
190 assert_equal user.id, json['user']['id']
191 end
192 end
193 end
194
195 context "with invalid parameters" do
196 setup do
197 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
198 end
199
200 context ".xml" do
201 should "return errors" do
202 assert_no_difference('User.count') do
203 post '/users.xml', @parameters, credentials('admin')
204 end
205
206 assert_response :unprocessable_entity
207 assert_equal 'application/xml', @response.content_type
208 assert_tag 'errors', :child => {
209 :tag => 'error',
210 :content => "First name can't be blank"
211 }
212 end
213 end
214
215 context ".json" do
216 should "return errors" do
217 assert_no_difference('User.count') do
218 post '/users.json', @parameters, credentials('admin')
219 end
220
221 assert_response :unprocessable_entity
222 assert_equal 'application/json', @response.content_type
223 json = ActiveSupport::JSON.decode(response.body)
224 assert_kind_of Hash, json
225 assert json.has_key?('errors')
226 assert_kind_of Array, json['errors']
227 end
228 end
229 end
230 end
231
232 context "PUT /users/2" do
233 context "with valid parameters" do
234 setup do
235 @parameters = {
236 :user => {
237 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
238 :mail => 'jsmith@somenet.foo'
239 }
240 }
241 end
242
243 context ".xml" do
244 should_allow_api_authentication(:put,
245 '/users/2.xml',
246 {:user => {
247 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
248 :mail => 'jsmith@somenet.foo'
249 }},
250 {:success_code => :ok})
251
252 should "update user with the attributes" do
253 assert_no_difference('User.count') do
254 put '/users/2.xml', @parameters, credentials('admin')
255 end
256
257 user = User.find(2)
258 assert_equal 'jsmith', user.login
259 assert_equal 'John', user.firstname
260 assert_equal 'Renamed', user.lastname
261 assert_equal 'jsmith@somenet.foo', user.mail
262 assert !user.admin?
263
264 assert_response :ok
265 assert_equal '', @response.body
266 end
267 end
268
269 context ".json" do
270 should_allow_api_authentication(:put,
271 '/users/2.json',
272 {:user => {
273 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
274 :mail => 'jsmith@somenet.foo'
275 }},
276 {:success_code => :ok})
277
278 should "update user with the attributes" do
279 assert_no_difference('User.count') do
280 put '/users/2.json', @parameters, credentials('admin')
281 end
282
283 user = User.find(2)
284 assert_equal 'jsmith', user.login
285 assert_equal 'John', user.firstname
286 assert_equal 'Renamed', user.lastname
287 assert_equal 'jsmith@somenet.foo', user.mail
288 assert !user.admin?
289
290 assert_response :ok
291 assert_equal '', @response.body
292 end
293 end
294 end
295
296 context "with invalid parameters" do
297 setup do
298 @parameters = {
299 :user => {
300 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
301 :mail => 'foo'
302 }
303 }
304 end
305
306 context ".xml" do
307 should "return errors" do
308 assert_no_difference('User.count') do
309 put '/users/2.xml', @parameters, credentials('admin')
310 end
311
312 assert_response :unprocessable_entity
313 assert_equal 'application/xml', @response.content_type
314 assert_tag 'errors', :child => {
315 :tag => 'error',
316 :content => "First name can't be blank"
317 }
318 end
319 end
320
321 context ".json" do
322 should "return errors" do
323 assert_no_difference('User.count') do
324 put '/users/2.json', @parameters, credentials('admin')
325 end
326
327 assert_response :unprocessable_entity
328 assert_equal 'application/json', @response.content_type
329 json = ActiveSupport::JSON.decode(response.body)
330 assert_kind_of Hash, json
331 assert json.has_key?('errors')
332 assert_kind_of Array, json['errors']
333 end
334 end
335 end
336 end
337
338 context "DELETE /users/2" do
339 context ".xml" do
340 should_allow_api_authentication(:delete,
341 '/users/2.xml',
342 {},
343 {:success_code => :ok})
344
345 should "delete user" do
346 assert_difference('User.count', -1) do
347 delete '/users/2.xml', {}, credentials('admin')
348 end
349
350 assert_response :ok
351 assert_equal '', @response.body
352 end
353 end
354
355 context ".json" do
356 should_allow_api_authentication(:delete,
357 '/users/2.xml',
358 {},
359 {:success_code => :ok})
360
361 should "delete user" do
362 assert_difference('User.count', -1) do
363 delete '/users/2.json', {}, credentials('admin')
364 end
365
366 assert_response :ok
367 assert_equal '', @response.body
368 end
369 end
370 end
371 end