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