comparison test/integration/api_test/users_test.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents cbce1fd3b1b7
children 433d4f72a19b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2010 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__)
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
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 = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}}
75 end 75 end
76 76
77 context ".xml" do 77 context ".xml" do
78 should_allow_api_authentication(:post, 78 should_allow_api_authentication(:post,
79 '/users.xml', 79 '/users.xml',
80 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}}, 80 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}},
81 {:success_code => :created}) 81 {:success_code => :created})
82 82
83 should "create a user with the attributes" do 83 should "create a user with the attributes" do
84 assert_difference('User.count') do 84 assert_difference('User.count') do
85 post '/users.xml', @parameters, :authorization => credentials('admin') 85 post '/users.xml', @parameters, :authorization => credentials('admin')
86 end 86 end
87 87
88 user = User.first(:order => 'id DESC') 88 user = User.first(:order => 'id DESC')
89 assert_equal 'foo', user.login 89 assert_equal 'foo', user.login
90 assert_equal 'Firstname', user.firstname 90 assert_equal 'Firstname', user.firstname
91 assert_equal 'Lastname', user.lastname 91 assert_equal 'Lastname', user.lastname
92 assert_equal 'foo@example.net', user.mail 92 assert_equal 'foo@example.net', user.mail
93 assert_equal 'only_assigned', user.mail_notification 93 assert_equal 'only_assigned', user.mail_notification
94 assert !user.admin? 94 assert !user.admin?
95 assert user.check_password?('secret') 95 assert user.check_password?('secret')
96 96
97 assert_response :created 97 assert_response :created
98 assert_equal 'application/xml', @response.content_type 98 assert_equal 'application/xml', @response.content_type
99 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} 99 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
100 end 100 end
101 end 101 end
102 102
103 context ".json" do 103 context ".json" do
104 should_allow_api_authentication(:post, 104 should_allow_api_authentication(:post,
105 '/users.json', 105 '/users.json',
106 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}}, 106 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}},
107 {:success_code => :created}) 107 {:success_code => :created})
108 108
109 should "create a user with the attributes" do 109 should "create a user with the attributes" do
110 assert_difference('User.count') do 110 assert_difference('User.count') do
111 post '/users.json', @parameters, :authorization => credentials('admin') 111 post '/users.json', @parameters, :authorization => credentials('admin')
112 end 112 end
113 113
114 user = User.first(:order => 'id DESC') 114 user = User.first(:order => 'id DESC')
115 assert_equal 'foo', user.login 115 assert_equal 'foo', user.login
116 assert_equal 'Firstname', user.firstname 116 assert_equal 'Firstname', user.firstname
117 assert_equal 'Lastname', user.lastname 117 assert_equal 'Lastname', user.lastname
118 assert_equal 'foo@example.net', user.mail 118 assert_equal 'foo@example.net', user.mail
119 assert !user.admin? 119 assert !user.admin?
120 120
121 assert_response :created 121 assert_response :created
122 assert_equal 'application/json', @response.content_type 122 assert_equal 'application/json', @response.content_type
123 json = ActiveSupport::JSON.decode(response.body) 123 json = ActiveSupport::JSON.decode(response.body)
124 assert_kind_of Hash, json 124 assert_kind_of Hash, json
125 assert_kind_of Hash, json['user'] 125 assert_kind_of Hash, json['user']
126 assert_equal user.id, json['user']['id'] 126 assert_equal user.id, json['user']['id']
127 end 127 end
128 end 128 end
129 end 129 end
130 130
131 context "with invalid parameters" do 131 context "with invalid parameters" do
132 setup do 132 setup do
133 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} 133 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
134 end 134 end
135 135
136 context ".xml" do 136 context ".xml" do
137 should "return errors" do 137 should "return errors" do
138 assert_no_difference('User.count') do 138 assert_no_difference('User.count') do
139 post '/users.xml', @parameters, :authorization => credentials('admin') 139 post '/users.xml', @parameters, :authorization => credentials('admin')
140 end 140 end
141 141
142 assert_response :unprocessable_entity 142 assert_response :unprocessable_entity
143 assert_equal 'application/xml', @response.content_type 143 assert_equal 'application/xml', @response.content_type
144 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} 144 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"}
145 end 145 end
146 end 146 end
147 147
148 context ".json" do 148 context ".json" do
149 should "return errors" do 149 should "return errors" do
150 assert_no_difference('User.count') do 150 assert_no_difference('User.count') do
151 post '/users.json', @parameters, :authorization => credentials('admin') 151 post '/users.json', @parameters, :authorization => credentials('admin')
152 end 152 end
153 153
154 assert_response :unprocessable_entity 154 assert_response :unprocessable_entity
155 assert_equal 'application/json', @response.content_type 155 assert_equal 'application/json', @response.content_type
156 json = ActiveSupport::JSON.decode(response.body) 156 json = ActiveSupport::JSON.decode(response.body)
157 assert_kind_of Hash, json 157 assert_kind_of Hash, json
158 assert json.has_key?('errors') 158 assert json.has_key?('errors')
165 context "PUT /users/2" do 165 context "PUT /users/2" do
166 context "with valid parameters" do 166 context "with valid parameters" do
167 setup do 167 setup do
168 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}} 168 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}
169 end 169 end
170 170
171 context ".xml" do 171 context ".xml" do
172 should_allow_api_authentication(:put, 172 should_allow_api_authentication(:put,
173 '/users/2.xml', 173 '/users/2.xml',
174 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, 174 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
175 {:success_code => :ok}) 175 {:success_code => :ok})
176 176
177 should "update user with the attributes" do 177 should "update user with the attributes" do
178 assert_no_difference('User.count') do 178 assert_no_difference('User.count') do
179 put '/users/2.xml', @parameters, :authorization => credentials('admin') 179 put '/users/2.xml', @parameters, :authorization => credentials('admin')
180 end 180 end
181 181
182 user = User.find(2) 182 user = User.find(2)
183 assert_equal 'jsmith', user.login 183 assert_equal 'jsmith', user.login
184 assert_equal 'John', user.firstname 184 assert_equal 'John', user.firstname
185 assert_equal 'Renamed', user.lastname 185 assert_equal 'Renamed', user.lastname
186 assert_equal 'jsmith@somenet.foo', user.mail 186 assert_equal 'jsmith@somenet.foo', user.mail
187 assert !user.admin? 187 assert !user.admin?
188 188
189 assert_response :ok 189 assert_response :ok
190 end 190 end
191 end 191 end
192 192
193 context ".json" do 193 context ".json" do
194 should_allow_api_authentication(:put, 194 should_allow_api_authentication(:put,
195 '/users/2.json', 195 '/users/2.json',
196 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, 196 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
197 {:success_code => :ok}) 197 {:success_code => :ok})
198 198
199 should "update user with the attributes" do 199 should "update user with the attributes" do
200 assert_no_difference('User.count') do 200 assert_no_difference('User.count') do
201 put '/users/2.json', @parameters, :authorization => credentials('admin') 201 put '/users/2.json', @parameters, :authorization => credentials('admin')
202 end 202 end
203 203
204 user = User.find(2) 204 user = User.find(2)
205 assert_equal 'jsmith', user.login 205 assert_equal 'jsmith', user.login
206 assert_equal 'John', user.firstname 206 assert_equal 'John', user.firstname
207 assert_equal 'Renamed', user.lastname 207 assert_equal 'Renamed', user.lastname
208 assert_equal 'jsmith@somenet.foo', user.mail 208 assert_equal 'jsmith@somenet.foo', user.mail
209 assert !user.admin? 209 assert !user.admin?
210 210
211 assert_response :ok 211 assert_response :ok
212 end 212 end
213 end 213 end
214 end 214 end
215 215
216 context "with invalid parameters" do 216 context "with invalid parameters" do
217 setup do 217 setup do
218 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}} 218 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}}
219 end 219 end
220 220
221 context ".xml" do 221 context ".xml" do
222 should "return errors" do 222 should "return errors" do
223 assert_no_difference('User.count') do 223 assert_no_difference('User.count') do
224 put '/users/2.xml', @parameters, :authorization => credentials('admin') 224 put '/users/2.xml', @parameters, :authorization => credentials('admin')
225 end 225 end
226 226
227 assert_response :unprocessable_entity 227 assert_response :unprocessable_entity
228 assert_equal 'application/xml', @response.content_type 228 assert_equal 'application/xml', @response.content_type
229 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} 229 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"}
230 end 230 end
231 end 231 end
232 232
233 context ".json" do 233 context ".json" do
234 should "return errors" do 234 should "return errors" do
235 assert_no_difference('User.count') do 235 assert_no_difference('User.count') do
236 put '/users/2.json', @parameters, :authorization => credentials('admin') 236 put '/users/2.json', @parameters, :authorization => credentials('admin')
237 end 237 end
238 238
239 assert_response :unprocessable_entity 239 assert_response :unprocessable_entity
240 assert_equal 'application/json', @response.content_type 240 assert_equal 'application/json', @response.content_type
241 json = ActiveSupport::JSON.decode(response.body) 241 json = ActiveSupport::JSON.decode(response.body)
242 assert_kind_of Hash, json 242 assert_kind_of Hash, json
243 assert json.has_key?('errors') 243 assert json.has_key?('errors')
244 assert_kind_of Array, json['errors'] 244 assert_kind_of Array, json['errors']
245 end 245 end
246 end 246 end
247 end 247 end
248 end 248 end
249 249
250 context "DELETE /users/2" do 250 context "DELETE /users/2" do
251 context ".xml" do 251 context ".xml" do
252 should_allow_api_authentication(:delete, 252 should_allow_api_authentication(:delete,
253 '/users/2.xml', 253 '/users/2.xml',
254 {}, 254 {},
255 {:success_code => :ok}) 255 {:success_code => :ok})
256 256
257 should "delete user" do 257 should "delete user" do
258 assert_difference('User.count', -1) do 258 assert_difference('User.count', -1) do
259 delete '/users/2.xml', {}, :authorization => credentials('admin') 259 delete '/users/2.xml', {}, :authorization => credentials('admin')
260 end 260 end
261 261
262 assert_response :ok 262 assert_response :ok
263 end 263 end
264 end 264 end
265 265
266 context ".json" do 266 context ".json" do
267 should_allow_api_authentication(:delete, 267 should_allow_api_authentication(:delete,
268 '/users/2.xml', 268 '/users/2.xml',
269 {}, 269 {},
270 {:success_code => :ok}) 270 {:success_code => :ok})
271 271
272 should "delete user" do 272 should "delete user" do
273 assert_difference('User.count', -1) do 273 assert_difference('User.count', -1) do
274 delete '/users/2.json', {}, :authorization => credentials('admin') 274 delete '/users/2.json', {}, :authorization => credentials('admin')
275 end 275 end
276 276
277 assert_response :ok 277 assert_response :ok
278 end 278 end
279 end 279 end
280 end 280 end
281 281
282 def credentials(user, password=nil) 282 def credentials(user, password=nil)
283 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) 283 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
284 end 284 end
285 end 285 end