##// END OF EJS Templates
Rails4: replace deprecated Relation#first with finder options at ApiTest::UsersTest...
Toshi MARUYAMA -
r12332:c53854358388
parent child
Show More
@@ -1,327 +1,327
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 should_allow_api_authentication(:get, "/users.xml")
28 28 should_allow_api_authentication(:get, "/users.json")
29 29 should_allow_api_authentication(:post,
30 30 '/users.xml',
31 31 {:user => {
32 32 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
33 33 :mail => 'foo@example.net', :password => 'secret123'
34 34 }},
35 35 {:success_code => :created})
36 36 should_allow_api_authentication(:post,
37 37 '/users.json',
38 38 {:user => {
39 39 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
40 40 :mail => 'foo@example.net'
41 41 }},
42 42 {:success_code => :created})
43 43 should_allow_api_authentication(:put,
44 44 '/users/2.xml',
45 45 {:user => {
46 46 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
47 47 :mail => 'jsmith@somenet.foo'
48 48 }},
49 49 {:success_code => :ok})
50 50 should_allow_api_authentication(:put,
51 51 '/users/2.json',
52 52 {:user => {
53 53 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
54 54 :mail => 'jsmith@somenet.foo'
55 55 }},
56 56 {:success_code => :ok})
57 57 should_allow_api_authentication(:delete,
58 58 '/users/2.xml',
59 59 {},
60 60 {:success_code => :ok})
61 61 should_allow_api_authentication(:delete,
62 62 '/users/2.xml',
63 63 {},
64 64 {:success_code => :ok})
65 65
66 66 test "GET /users/:id.xml should return the user" do
67 67 get '/users/2.xml'
68 68
69 69 assert_response :success
70 70 assert_tag :tag => 'user',
71 71 :child => {:tag => 'id', :content => '2'}
72 72 end
73 73
74 74 test "GET /users/:id.json should return the user" do
75 75 get '/users/2.json'
76 76
77 77 assert_response :success
78 78 json = ActiveSupport::JSON.decode(response.body)
79 79 assert_kind_of Hash, json
80 80 assert_kind_of Hash, json['user']
81 81 assert_equal 2, json['user']['id']
82 82 end
83 83
84 84 test "GET /users/:id.xml with include=memberships should include memberships" do
85 85 get '/users/2.xml?include=memberships'
86 86
87 87 assert_response :success
88 88 assert_tag :tag => 'memberships',
89 89 :parent => {:tag => 'user'},
90 90 :children => {:count => 1}
91 91 end
92 92
93 93 test "GET /users/:id.json with include=memberships should include memberships" do
94 94 get '/users/2.json?include=memberships'
95 95
96 96 assert_response :success
97 97 json = ActiveSupport::JSON.decode(response.body)
98 98 assert_kind_of Array, json['user']['memberships']
99 99 assert_equal [{
100 100 "id"=>1,
101 101 "project"=>{"name"=>"eCookbook", "id"=>1},
102 102 "roles"=>[{"name"=>"Manager", "id"=>1}]
103 103 }], json['user']['memberships']
104 104 end
105 105
106 106 test "GET /users/current.xml should require authentication" do
107 107 get '/users/current.xml'
108 108
109 109 assert_response 401
110 110 end
111 111
112 112 test "GET /users/current.xml should return current user" do
113 113 get '/users/current.xml', {}, credentials('jsmith')
114 114
115 115 assert_tag :tag => 'user',
116 116 :child => {:tag => 'id', :content => '2'}
117 117 end
118 118
119 119 test "GET /users/:id should not return login for other user" do
120 120 get '/users/3.xml', {}, credentials('jsmith')
121 121 assert_response :success
122 122 assert_no_tag 'user', :child => {:tag => 'login'}
123 123 end
124 124
125 125 test "GET /users/:id should return login for current user" do
126 126 get '/users/2.xml', {}, credentials('jsmith')
127 127 assert_response :success
128 128 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
129 129 end
130 130
131 131 test "GET /users/:id should not return api_key for other user" do
132 132 get '/users/3.xml', {}, credentials('jsmith')
133 133 assert_response :success
134 134 assert_no_tag 'user', :child => {:tag => 'api_key'}
135 135 end
136 136
137 137 test "GET /users/:id should return api_key for current user" do
138 138 get '/users/2.xml', {}, credentials('jsmith')
139 139 assert_response :success
140 140 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
141 141 end
142 142
143 143 test "GET /users/:id should not return status for standard user" do
144 144 get '/users/3.xml', {}, credentials('jsmith')
145 145 assert_response :success
146 146 assert_no_tag 'user', :child => {:tag => 'status'}
147 147 end
148 148
149 149 test "GET /users/:id should return status for administrators" do
150 150 get '/users/2.xml', {}, credentials('admin')
151 151 assert_response :success
152 152 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s}
153 153 end
154 154
155 155 test "POST /users.xml with valid parameters should create the user" do
156 156 assert_difference('User.count') do
157 157 post '/users.xml', {
158 158 :user => {
159 159 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
160 160 :mail => 'foo@example.net', :password => 'secret123',
161 161 :mail_notification => 'only_assigned'}
162 162 },
163 163 credentials('admin')
164 164 end
165 165
166 user = User.first(:order => 'id DESC')
166 user = User.order('id DESC').first
167 167 assert_equal 'foo', user.login
168 168 assert_equal 'Firstname', user.firstname
169 169 assert_equal 'Lastname', user.lastname
170 170 assert_equal 'foo@example.net', user.mail
171 171 assert_equal 'only_assigned', user.mail_notification
172 172 assert !user.admin?
173 173 assert user.check_password?('secret123')
174 174
175 175 assert_response :created
176 176 assert_equal 'application/xml', @response.content_type
177 177 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
178 178 end
179 179
180 180 test "POST /users.json with valid parameters should create the user" do
181 181 assert_difference('User.count') do
182 182 post '/users.json', {
183 183 :user => {
184 184 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
185 185 :mail => 'foo@example.net', :password => 'secret123',
186 186 :mail_notification => 'only_assigned'}
187 187 },
188 188 credentials('admin')
189 189 end
190 190
191 user = User.first(:order => 'id DESC')
191 user = User.order('id DESC').first
192 192 assert_equal 'foo', user.login
193 193 assert_equal 'Firstname', user.firstname
194 194 assert_equal 'Lastname', user.lastname
195 195 assert_equal 'foo@example.net', user.mail
196 196 assert !user.admin?
197 197
198 198 assert_response :created
199 199 assert_equal 'application/json', @response.content_type
200 200 json = ActiveSupport::JSON.decode(response.body)
201 201 assert_kind_of Hash, json
202 202 assert_kind_of Hash, json['user']
203 203 assert_equal user.id, json['user']['id']
204 204 end
205 205
206 206 test "POST /users.xml with with invalid parameters should return errors" do
207 207 assert_no_difference('User.count') do
208 208 post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
209 209 end
210 210
211 211 assert_response :unprocessable_entity
212 212 assert_equal 'application/xml', @response.content_type
213 213 assert_tag 'errors', :child => {
214 214 :tag => 'error',
215 215 :content => "First name can't be blank"
216 216 }
217 217 end
218 218
219 219 test "POST /users.json with with invalid parameters should return errors" do
220 220 assert_no_difference('User.count') do
221 221 post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
222 222 end
223 223
224 224 assert_response :unprocessable_entity
225 225 assert_equal 'application/json', @response.content_type
226 226 json = ActiveSupport::JSON.decode(response.body)
227 227 assert_kind_of Hash, json
228 228 assert json.has_key?('errors')
229 229 assert_kind_of Array, json['errors']
230 230 end
231 231
232 232 test "PUT /users/:id.xml with valid parameters should update the user" do
233 233 assert_no_difference('User.count') do
234 234 put '/users/2.xml', {
235 235 :user => {
236 236 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
237 237 :mail => 'jsmith@somenet.foo'}
238 238 },
239 239 credentials('admin')
240 240 end
241 241
242 242 user = User.find(2)
243 243 assert_equal 'jsmith', user.login
244 244 assert_equal 'John', user.firstname
245 245 assert_equal 'Renamed', user.lastname
246 246 assert_equal 'jsmith@somenet.foo', user.mail
247 247 assert !user.admin?
248 248
249 249 assert_response :ok
250 250 assert_equal '', @response.body
251 251 end
252 252
253 253 test "PUT /users/:id.json with valid parameters should update the user" do
254 254 assert_no_difference('User.count') do
255 255 put '/users/2.json', {
256 256 :user => {
257 257 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
258 258 :mail => 'jsmith@somenet.foo'}
259 259 },
260 260 credentials('admin')
261 261 end
262 262
263 263 user = User.find(2)
264 264 assert_equal 'jsmith', user.login
265 265 assert_equal 'John', user.firstname
266 266 assert_equal 'Renamed', user.lastname
267 267 assert_equal 'jsmith@somenet.foo', user.mail
268 268 assert !user.admin?
269 269
270 270 assert_response :ok
271 271 assert_equal '', @response.body
272 272 end
273 273
274 274 test "PUT /users/:id.xml with invalid parameters" do
275 275 assert_no_difference('User.count') do
276 276 put '/users/2.xml', {
277 277 :user => {
278 278 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
279 279 :mail => 'foo'}
280 280 },
281 281 credentials('admin')
282 282 end
283 283
284 284 assert_response :unprocessable_entity
285 285 assert_equal 'application/xml', @response.content_type
286 286 assert_tag 'errors', :child => {
287 287 :tag => 'error',
288 288 :content => "First name can't be blank"
289 289 }
290 290 end
291 291
292 292 test "PUT /users/:id.json with invalid parameters" do
293 293 assert_no_difference('User.count') do
294 294 put '/users/2.json', {
295 295 :user => {
296 296 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
297 297 :mail => 'foo'}
298 298 },
299 299 credentials('admin')
300 300 end
301 301
302 302 assert_response :unprocessable_entity
303 303 assert_equal 'application/json', @response.content_type
304 304 json = ActiveSupport::JSON.decode(response.body)
305 305 assert_kind_of Hash, json
306 306 assert json.has_key?('errors')
307 307 assert_kind_of Array, json['errors']
308 308 end
309 309
310 310 test "DELETE /users/:id.xml should delete the user" do
311 311 assert_difference('User.count', -1) do
312 312 delete '/users/2.xml', {}, credentials('admin')
313 313 end
314 314
315 315 assert_response :ok
316 316 assert_equal '', @response.body
317 317 end
318 318
319 319 test "DELETE /users/:id.json should delete the user" do
320 320 assert_difference('User.count', -1) do
321 321 delete '/users/2.json', {}, credentials('admin')
322 322 end
323 323
324 324 assert_response :ok
325 325 assert_equal '', @response.body
326 326 end
327 327 end
General Comments 0
You need to be logged in to leave comments. Login now