Mercurial > hg > soundsoftware-site
comparison test/integration/api_test/users_test.rb @ 1464:261b3d9a4903 redmine-2.4
Update to Redmine 2.4 branch rev 12663
author | Chris Cannam |
---|---|
date | Tue, 14 Jan 2014 14:37:42 +0000 |
parents | 433d4f72a19b |
children | e248c7af89ec |
comparison
equal
deleted
inserted
replaced
1296:038ba2d95de8 | 1464:261b3d9a4903 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 Jean-Philippe Lang | 2 # Copyright (C) 2006-2013 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. |
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 require 'pp' | 19 |
20 class ApiTest::UsersTest < ActionController::IntegrationTest | 20 class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base |
21 fixtures :users, :members, :member_roles, :roles, :projects | 21 fixtures :users, :members, :member_roles, :roles, :projects |
22 | 22 |
23 def setup | 23 def setup |
24 Setting.rest_api_enabled = '1' | 24 Setting.rest_api_enabled = '1' |
25 end | 25 end |
26 | 26 |
27 context "GET /users" do | 27 should_allow_api_authentication(:get, "/users.xml") |
28 should_allow_api_authentication(:get, "/users.xml") | 28 should_allow_api_authentication(:get, "/users.json") |
29 should_allow_api_authentication(:get, "/users.json") | 29 should_allow_api_authentication(:post, |
30 end | 30 '/users.xml', |
31 | 31 {:user => { |
32 context "GET /users/2" do | 32 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
33 context ".xml" do | 33 :mail => 'foo@example.net', :password => 'secret123' |
34 should "return requested user" do | 34 }}, |
35 get '/users/2.xml' | 35 {:success_code => :created}) |
36 | 36 should_allow_api_authentication(:post, |
37 assert_response :success | 37 '/users.json', |
38 assert_tag :tag => 'user', | 38 {:user => { |
39 :child => {:tag => 'id', :content => '2'} | 39 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
40 end | 40 :mail => 'foo@example.net' |
41 | 41 }}, |
42 context "with include=memberships" do | 42 {:success_code => :created}) |
43 should "include memberships" do | 43 should_allow_api_authentication(:put, |
44 get '/users/2.xml?include=memberships' | 44 '/users/2.xml', |
45 | 45 {:user => { |
46 assert_response :success | 46 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
47 assert_tag :tag => 'memberships', | 47 :mail => 'jsmith@somenet.foo' |
48 :parent => {:tag => 'user'}, | 48 }}, |
49 :children => {:count => 1} | 49 {:success_code => :ok}) |
50 end | 50 should_allow_api_authentication(:put, |
51 end | 51 '/users/2.json', |
52 end | 52 {:user => { |
53 | 53 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
54 context ".json" do | 54 :mail => 'jsmith@somenet.foo' |
55 should "return requested user" do | 55 }}, |
56 get '/users/2.json' | 56 {:success_code => :ok}) |
57 | 57 should_allow_api_authentication(:delete, |
58 assert_response :success | 58 '/users/2.xml', |
59 json = ActiveSupport::JSON.decode(response.body) | 59 {}, |
60 assert_kind_of Hash, json | 60 {:success_code => :ok}) |
61 assert_kind_of Hash, json['user'] | 61 should_allow_api_authentication(:delete, |
62 assert_equal 2, json['user']['id'] | 62 '/users/2.xml', |
63 end | 63 {}, |
64 | 64 {:success_code => :ok}) |
65 context "with include=memberships" do | 65 |
66 should "include memberships" do | 66 test "GET /users/:id.xml should return the user" do |
67 get '/users/2.json?include=memberships' | 67 get '/users/2.xml' |
68 | 68 |
69 assert_response :success | 69 assert_response :success |
70 json = ActiveSupport::JSON.decode(response.body) | 70 assert_tag :tag => 'user', |
71 assert_kind_of Array, json['user']['memberships'] | 71 :child => {:tag => 'id', :content => '2'} |
72 assert_equal [{ | 72 end |
73 "id"=>1, | 73 |
74 "project"=>{"name"=>"eCookbook", "id"=>1}, | 74 test "GET /users/:id.json should return the user" do |
75 "roles"=>[{"name"=>"Manager", "id"=>1}] | 75 get '/users/2.json' |
76 }], json['user']['memberships'] | 76 |
77 end | 77 assert_response :success |
78 end | 78 json = ActiveSupport::JSON.decode(response.body) |
79 end | 79 assert_kind_of Hash, json |
80 end | 80 assert_kind_of Hash, json['user'] |
81 | 81 assert_equal 2, json['user']['id'] |
82 context "GET /users/current" do | 82 end |
83 context ".xml" do | 83 |
84 should "require authentication" do | 84 test "GET /users/:id.xml with include=memberships should include memberships" do |
85 get '/users/current.xml' | 85 get '/users/2.xml?include=memberships' |
86 | 86 |
87 assert_response 401 | 87 assert_response :success |
88 end | 88 assert_tag :tag => 'memberships', |
89 | 89 :parent => {:tag => 'user'}, |
90 should "return current user" do | 90 :children => {:count => 1} |
91 get '/users/current.xml', {}, credentials('jsmith') | 91 end |
92 | 92 |
93 assert_tag :tag => 'user', | 93 test "GET /users/:id.json with include=memberships should include memberships" do |
94 :child => {:tag => 'id', :content => '2'} | 94 get '/users/2.json?include=memberships' |
95 end | 95 |
96 end | 96 assert_response :success |
97 end | 97 json = ActiveSupport::JSON.decode(response.body) |
98 | 98 assert_kind_of Array, json['user']['memberships'] |
99 context "POST /users" do | 99 assert_equal [{ |
100 context "with valid parameters" do | 100 "id"=>1, |
101 setup do | 101 "project"=>{"name"=>"eCookbook", "id"=>1}, |
102 @parameters = { | 102 "roles"=>[{"name"=>"Manager", "id"=>1}] |
103 :user => { | 103 }], json['user']['memberships'] |
104 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | 104 end |
105 :mail => 'foo@example.net', :password => 'secret123', | 105 |
106 :mail_notification => 'only_assigned' | 106 test "GET /users/current.xml should require authentication" do |
107 } | 107 get '/users/current.xml' |
108 } | 108 |
109 end | 109 assert_response 401 |
110 | 110 end |
111 context ".xml" do | 111 |
112 should_allow_api_authentication(:post, | 112 test "GET /users/current.xml should return current user" do |
113 '/users.xml', | 113 get '/users/current.xml', {}, credentials('jsmith') |
114 {:user => { | 114 |
115 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | 115 assert_tag :tag => 'user', |
116 :mail => 'foo@example.net', :password => 'secret123' | 116 :child => {:tag => 'id', :content => '2'} |
117 }}, | 117 end |
118 {:success_code => :created}) | 118 |
119 | 119 test "GET /users/:id should not return login for other user" do |
120 should "create a user with the attributes" do | 120 get '/users/3.xml', {}, credentials('jsmith') |
121 assert_difference('User.count') do | 121 assert_response :success |
122 post '/users.xml', @parameters, credentials('admin') | 122 assert_no_tag 'user', :child => {:tag => 'login'} |
123 end | 123 end |
124 | 124 |
125 user = User.first(:order => 'id DESC') | 125 test "GET /users/:id should return login for current user" do |
126 assert_equal 'foo', user.login | 126 get '/users/2.xml', {}, credentials('jsmith') |
127 assert_equal 'Firstname', user.firstname | 127 assert_response :success |
128 assert_equal 'Lastname', user.lastname | 128 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'} |
129 assert_equal 'foo@example.net', user.mail | 129 end |
130 assert_equal 'only_assigned', user.mail_notification | 130 |
131 assert !user.admin? | 131 test "GET /users/:id should not return api_key for other user" do |
132 assert user.check_password?('secret123') | 132 get '/users/3.xml', {}, credentials('jsmith') |
133 | 133 assert_response :success |
134 assert_response :created | 134 assert_no_tag 'user', :child => {:tag => 'api_key'} |
135 assert_equal 'application/xml', @response.content_type | 135 end |
136 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} | 136 |
137 end | 137 test "GET /users/:id should return api_key for current user" do |
138 end | 138 get '/users/2.xml', {}, credentials('jsmith') |
139 | 139 assert_response :success |
140 context ".json" do | 140 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key} |
141 should_allow_api_authentication(:post, | 141 end |
142 '/users.json', | 142 |
143 {:user => { | 143 test "GET /users/:id should not return status for standard user" do |
144 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | 144 get '/users/3.xml', {}, credentials('jsmith') |
145 :mail => 'foo@example.net' | 145 assert_response :success |
146 }}, | 146 assert_no_tag 'user', :child => {:tag => 'status'} |
147 {:success_code => :created}) | 147 end |
148 | 148 |
149 should "create a user with the attributes" do | 149 test "GET /users/:id should return status for administrators" do |
150 assert_difference('User.count') do | 150 get '/users/2.xml', {}, credentials('admin') |
151 post '/users.json', @parameters, credentials('admin') | 151 assert_response :success |
152 end | 152 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s} |
153 | 153 end |
154 user = User.first(:order => 'id DESC') | 154 |
155 assert_equal 'foo', user.login | 155 test "POST /users.xml with valid parameters should create the user" do |
156 assert_equal 'Firstname', user.firstname | 156 assert_difference('User.count') do |
157 assert_equal 'Lastname', user.lastname | 157 post '/users.xml', { |
158 assert_equal 'foo@example.net', user.mail | 158 :user => { |
159 assert !user.admin? | 159 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
160 | 160 :mail => 'foo@example.net', :password => 'secret123', |
161 assert_response :created | 161 :mail_notification => 'only_assigned'} |
162 assert_equal 'application/json', @response.content_type | 162 }, |
163 json = ActiveSupport::JSON.decode(response.body) | 163 credentials('admin') |
164 assert_kind_of Hash, json | 164 end |
165 assert_kind_of Hash, json['user'] | 165 |
166 assert_equal user.id, json['user']['id'] | 166 user = User.first(:order => 'id DESC') |
167 end | 167 assert_equal 'foo', user.login |
168 end | 168 assert_equal 'Firstname', user.firstname |
169 end | 169 assert_equal 'Lastname', user.lastname |
170 | 170 assert_equal 'foo@example.net', user.mail |
171 context "with invalid parameters" do | 171 assert_equal 'only_assigned', user.mail_notification |
172 setup do | 172 assert !user.admin? |
173 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} | 173 assert user.check_password?('secret123') |
174 end | 174 |
175 | 175 assert_response :created |
176 context ".xml" do | 176 assert_equal 'application/xml', @response.content_type |
177 should "return errors" do | 177 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} |
178 assert_no_difference('User.count') do | 178 end |
179 post '/users.xml', @parameters, credentials('admin') | 179 |
180 end | 180 test "POST /users.json with valid parameters should create the user" do |
181 | 181 assert_difference('User.count') do |
182 assert_response :unprocessable_entity | 182 post '/users.json', { |
183 assert_equal 'application/xml', @response.content_type | 183 :user => { |
184 assert_tag 'errors', :child => { | 184 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', |
185 :tag => 'error', | 185 :mail => 'foo@example.net', :password => 'secret123', |
186 :content => "First name can't be blank" | 186 :mail_notification => 'only_assigned'} |
187 } | 187 }, |
188 end | 188 credentials('admin') |
189 end | 189 end |
190 | 190 |
191 context ".json" do | 191 user = User.first(:order => 'id DESC') |
192 should "return errors" do | 192 assert_equal 'foo', user.login |
193 assert_no_difference('User.count') do | 193 assert_equal 'Firstname', user.firstname |
194 post '/users.json', @parameters, credentials('admin') | 194 assert_equal 'Lastname', user.lastname |
195 end | 195 assert_equal 'foo@example.net', user.mail |
196 | 196 assert !user.admin? |
197 assert_response :unprocessable_entity | 197 |
198 assert_equal 'application/json', @response.content_type | 198 assert_response :created |
199 json = ActiveSupport::JSON.decode(response.body) | 199 assert_equal 'application/json', @response.content_type |
200 assert_kind_of Hash, json | 200 json = ActiveSupport::JSON.decode(response.body) |
201 assert json.has_key?('errors') | 201 assert_kind_of Hash, json |
202 assert_kind_of Array, json['errors'] | 202 assert_kind_of Hash, json['user'] |
203 end | 203 assert_equal user.id, json['user']['id'] |
204 end | 204 end |
205 end | 205 |
206 end | 206 test "POST /users.xml with with invalid parameters should return errors" do |
207 | 207 assert_no_difference('User.count') do |
208 context "PUT /users/2" do | 208 post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin') |
209 context "with valid parameters" do | 209 end |
210 setup do | 210 |
211 @parameters = { | 211 assert_response :unprocessable_entity |
212 :user => { | 212 assert_equal 'application/xml', @response.content_type |
213 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | 213 assert_tag 'errors', :child => { |
214 :mail => 'jsmith@somenet.foo' | 214 :tag => 'error', |
215 } | 215 :content => "First name can't be blank" |
216 } | 216 } |
217 end | 217 end |
218 | 218 |
219 context ".xml" do | 219 test "POST /users.json with with invalid parameters should return errors" do |
220 should_allow_api_authentication(:put, | 220 assert_no_difference('User.count') do |
221 '/users/2.xml', | 221 post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin') |
222 {:user => { | 222 end |
223 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | 223 |
224 :mail => 'jsmith@somenet.foo' | 224 assert_response :unprocessable_entity |
225 }}, | 225 assert_equal 'application/json', @response.content_type |
226 {:success_code => :ok}) | 226 json = ActiveSupport::JSON.decode(response.body) |
227 | 227 assert_kind_of Hash, json |
228 should "update user with the attributes" do | 228 assert json.has_key?('errors') |
229 assert_no_difference('User.count') do | 229 assert_kind_of Array, json['errors'] |
230 put '/users/2.xml', @parameters, credentials('admin') | 230 end |
231 end | 231 |
232 | 232 test "PUT /users/:id.xml with valid parameters should update the user" do |
233 user = User.find(2) | 233 assert_no_difference('User.count') do |
234 assert_equal 'jsmith', user.login | 234 put '/users/2.xml', { |
235 assert_equal 'John', user.firstname | 235 :user => { |
236 assert_equal 'Renamed', user.lastname | 236 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
237 assert_equal 'jsmith@somenet.foo', user.mail | 237 :mail => 'jsmith@somenet.foo'} |
238 assert !user.admin? | 238 }, |
239 | 239 credentials('admin') |
240 assert_response :ok | 240 end |
241 assert_equal '', @response.body | 241 |
242 end | 242 user = User.find(2) |
243 end | 243 assert_equal 'jsmith', user.login |
244 | 244 assert_equal 'John', user.firstname |
245 context ".json" do | 245 assert_equal 'Renamed', user.lastname |
246 should_allow_api_authentication(:put, | 246 assert_equal 'jsmith@somenet.foo', user.mail |
247 '/users/2.json', | 247 assert !user.admin? |
248 {:user => { | 248 |
249 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | 249 assert_response :ok |
250 :mail => 'jsmith@somenet.foo' | 250 assert_equal '', @response.body |
251 }}, | 251 end |
252 {:success_code => :ok}) | 252 |
253 | 253 test "PUT /users/:id.json with valid parameters should update the user" do |
254 should "update user with the attributes" do | 254 assert_no_difference('User.count') do |
255 assert_no_difference('User.count') do | 255 put '/users/2.json', { |
256 put '/users/2.json', @parameters, credentials('admin') | 256 :user => { |
257 end | 257 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', |
258 | 258 :mail => 'jsmith@somenet.foo'} |
259 user = User.find(2) | 259 }, |
260 assert_equal 'jsmith', user.login | 260 credentials('admin') |
261 assert_equal 'John', user.firstname | 261 end |
262 assert_equal 'Renamed', user.lastname | 262 |
263 assert_equal 'jsmith@somenet.foo', user.mail | 263 user = User.find(2) |
264 assert !user.admin? | 264 assert_equal 'jsmith', user.login |
265 | 265 assert_equal 'John', user.firstname |
266 assert_response :ok | 266 assert_equal 'Renamed', user.lastname |
267 assert_equal '', @response.body | 267 assert_equal 'jsmith@somenet.foo', user.mail |
268 end | 268 assert !user.admin? |
269 end | 269 |
270 end | 270 assert_response :ok |
271 | 271 assert_equal '', @response.body |
272 context "with invalid parameters" do | 272 end |
273 setup do | 273 |
274 @parameters = { | 274 test "PUT /users/:id.xml with invalid parameters" do |
275 :user => { | 275 assert_no_difference('User.count') do |
276 :login => 'jsmith', :firstname => '', :lastname => 'Lastname', | 276 put '/users/2.xml', { |
277 :mail => 'foo' | 277 :user => { |
278 } | 278 :login => 'jsmith', :firstname => '', :lastname => 'Lastname', |
279 } | 279 :mail => 'foo'} |
280 end | 280 }, |
281 | 281 credentials('admin') |
282 context ".xml" do | 282 end |
283 should "return errors" do | 283 |
284 assert_no_difference('User.count') do | 284 assert_response :unprocessable_entity |
285 put '/users/2.xml', @parameters, credentials('admin') | 285 assert_equal 'application/xml', @response.content_type |
286 end | 286 assert_tag 'errors', :child => { |
287 | 287 :tag => 'error', |
288 assert_response :unprocessable_entity | 288 :content => "First name can't be blank" |
289 assert_equal 'application/xml', @response.content_type | 289 } |
290 assert_tag 'errors', :child => { | 290 end |
291 :tag => 'error', | 291 |
292 :content => "First name can't be blank" | 292 test "PUT /users/:id.json with invalid parameters" do |
293 } | 293 assert_no_difference('User.count') do |
294 end | 294 put '/users/2.json', { |
295 end | 295 :user => { |
296 | 296 :login => 'jsmith', :firstname => '', :lastname => 'Lastname', |
297 context ".json" do | 297 :mail => 'foo'} |
298 should "return errors" do | 298 }, |
299 assert_no_difference('User.count') do | 299 credentials('admin') |
300 put '/users/2.json', @parameters, credentials('admin') | 300 end |
301 end | 301 |
302 | 302 assert_response :unprocessable_entity |
303 assert_response :unprocessable_entity | 303 assert_equal 'application/json', @response.content_type |
304 assert_equal 'application/json', @response.content_type | 304 json = ActiveSupport::JSON.decode(response.body) |
305 json = ActiveSupport::JSON.decode(response.body) | 305 assert_kind_of Hash, json |
306 assert_kind_of Hash, json | 306 assert json.has_key?('errors') |
307 assert json.has_key?('errors') | 307 assert_kind_of Array, json['errors'] |
308 assert_kind_of Array, json['errors'] | 308 end |
309 end | 309 |
310 end | 310 test "DELETE /users/:id.xml should delete the user" do |
311 end | 311 assert_difference('User.count', -1) do |
312 end | 312 delete '/users/2.xml', {}, credentials('admin') |
313 | 313 end |
314 context "DELETE /users/2" do | 314 |
315 context ".xml" do | 315 assert_response :ok |
316 should_allow_api_authentication(:delete, | 316 assert_equal '', @response.body |
317 '/users/2.xml', | 317 end |
318 {}, | 318 |
319 {:success_code => :ok}) | 319 test "DELETE /users/:id.json should delete the user" do |
320 | 320 assert_difference('User.count', -1) do |
321 should "delete user" do | 321 delete '/users/2.json', {}, credentials('admin') |
322 assert_difference('User.count', -1) do | 322 end |
323 delete '/users/2.xml', {}, credentials('admin') | 323 |
324 end | 324 assert_response :ok |
325 | 325 assert_equal '', @response.body |
326 assert_response :ok | |
327 assert_equal '', @response.body | |
328 end | |
329 end | |
330 | |
331 context ".json" do | |
332 should_allow_api_authentication(:delete, | |
333 '/users/2.xml', | |
334 {}, | |
335 {:success_code => :ok}) | |
336 | |
337 should "delete user" do | |
338 assert_difference('User.count', -1) do | |
339 delete '/users/2.json', {}, credentials('admin') | |
340 end | |
341 | |
342 assert_response :ok | |
343 assert_equal '', @response.body | |
344 end | |
345 end | |
346 end | 326 end |
347 end | 327 end |