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