comparison .svn/pristine/47/475e48920f645a29c9410b5f2003c06d6f0a84d4.svn-base @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1296:038ba2d95de8
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 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 require 'pp'
20 class ApiTest::UsersTest < ActionController::IntegrationTest
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 context "POST /users" do
100 context "with valid parameters" do
101 setup do
102 @parameters = {
103 :user => {
104 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
105 :mail => 'foo@example.net', :password => 'secret123',
106 :mail_notification => 'only_assigned'
107 }
108 }
109 end
110
111 context ".xml" do
112 should_allow_api_authentication(:post,
113 '/users.xml',
114 {:user => {
115 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
116 :mail => 'foo@example.net', :password => 'secret123'
117 }},
118 {:success_code => :created})
119
120 should "create a user with the attributes" do
121 assert_difference('User.count') do
122 post '/users.xml', @parameters, credentials('admin')
123 end
124
125 user = User.first(:order => 'id DESC')
126 assert_equal 'foo', user.login
127 assert_equal 'Firstname', user.firstname
128 assert_equal 'Lastname', user.lastname
129 assert_equal 'foo@example.net', user.mail
130 assert_equal 'only_assigned', user.mail_notification
131 assert !user.admin?
132 assert user.check_password?('secret123')
133
134 assert_response :created
135 assert_equal 'application/xml', @response.content_type
136 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
137 end
138 end
139
140 context ".json" do
141 should_allow_api_authentication(:post,
142 '/users.json',
143 {:user => {
144 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
145 :mail => 'foo@example.net'
146 }},
147 {:success_code => :created})
148
149 should "create a user with the attributes" do
150 assert_difference('User.count') do
151 post '/users.json', @parameters, credentials('admin')
152 end
153
154 user = User.first(:order => 'id DESC')
155 assert_equal 'foo', user.login
156 assert_equal 'Firstname', user.firstname
157 assert_equal 'Lastname', user.lastname
158 assert_equal 'foo@example.net', user.mail
159 assert !user.admin?
160
161 assert_response :created
162 assert_equal 'application/json', @response.content_type
163 json = ActiveSupport::JSON.decode(response.body)
164 assert_kind_of Hash, json
165 assert_kind_of Hash, json['user']
166 assert_equal user.id, json['user']['id']
167 end
168 end
169 end
170
171 context "with invalid parameters" do
172 setup do
173 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
174 end
175
176 context ".xml" do
177 should "return errors" do
178 assert_no_difference('User.count') do
179 post '/users.xml', @parameters, credentials('admin')
180 end
181
182 assert_response :unprocessable_entity
183 assert_equal 'application/xml', @response.content_type
184 assert_tag 'errors', :child => {
185 :tag => 'error',
186 :content => "First name can't be blank"
187 }
188 end
189 end
190
191 context ".json" do
192 should "return errors" do
193 assert_no_difference('User.count') do
194 post '/users.json', @parameters, credentials('admin')
195 end
196
197 assert_response :unprocessable_entity
198 assert_equal 'application/json', @response.content_type
199 json = ActiveSupport::JSON.decode(response.body)
200 assert_kind_of Hash, json
201 assert json.has_key?('errors')
202 assert_kind_of Array, json['errors']
203 end
204 end
205 end
206 end
207
208 context "PUT /users/2" do
209 context "with valid parameters" do
210 setup do
211 @parameters = {
212 :user => {
213 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
214 :mail => 'jsmith@somenet.foo'
215 }
216 }
217 end
218
219 context ".xml" do
220 should_allow_api_authentication(:put,
221 '/users/2.xml',
222 {:user => {
223 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
224 :mail => 'jsmith@somenet.foo'
225 }},
226 {:success_code => :ok})
227
228 should "update user with the attributes" do
229 assert_no_difference('User.count') do
230 put '/users/2.xml', @parameters, credentials('admin')
231 end
232
233 user = User.find(2)
234 assert_equal 'jsmith', user.login
235 assert_equal 'John', user.firstname
236 assert_equal 'Renamed', user.lastname
237 assert_equal 'jsmith@somenet.foo', user.mail
238 assert !user.admin?
239
240 assert_response :ok
241 assert_equal '', @response.body
242 end
243 end
244
245 context ".json" do
246 should_allow_api_authentication(:put,
247 '/users/2.json',
248 {:user => {
249 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
250 :mail => 'jsmith@somenet.foo'
251 }},
252 {:success_code => :ok})
253
254 should "update user with the attributes" do
255 assert_no_difference('User.count') do
256 put '/users/2.json', @parameters, credentials('admin')
257 end
258
259 user = User.find(2)
260 assert_equal 'jsmith', user.login
261 assert_equal 'John', user.firstname
262 assert_equal 'Renamed', user.lastname
263 assert_equal 'jsmith@somenet.foo', user.mail
264 assert !user.admin?
265
266 assert_response :ok
267 assert_equal '', @response.body
268 end
269 end
270 end
271
272 context "with invalid parameters" do
273 setup do
274 @parameters = {
275 :user => {
276 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
277 :mail => 'foo'
278 }
279 }
280 end
281
282 context ".xml" do
283 should "return errors" do
284 assert_no_difference('User.count') do
285 put '/users/2.xml', @parameters, credentials('admin')
286 end
287
288 assert_response :unprocessable_entity
289 assert_equal 'application/xml', @response.content_type
290 assert_tag 'errors', :child => {
291 :tag => 'error',
292 :content => "First name can't be blank"
293 }
294 end
295 end
296
297 context ".json" do
298 should "return errors" do
299 assert_no_difference('User.count') do
300 put '/users/2.json', @parameters, credentials('admin')
301 end
302
303 assert_response :unprocessable_entity
304 assert_equal 'application/json', @response.content_type
305 json = ActiveSupport::JSON.decode(response.body)
306 assert_kind_of Hash, json
307 assert json.has_key?('errors')
308 assert_kind_of Array, json['errors']
309 end
310 end
311 end
312 end
313
314 context "DELETE /users/2" do
315 context ".xml" do
316 should_allow_api_authentication(:delete,
317 '/users/2.xml',
318 {},
319 {:success_code => :ok})
320
321 should "delete user" do
322 assert_difference('User.count', -1) do
323 delete '/users/2.xml', {}, credentials('admin')
324 end
325
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
347 end