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