Mercurial > hg > soundsoftware-site
comparison test/integration/api_test/users_test.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 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. |
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, :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 |
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_response :success | |
37 assert_tag :tag => 'user', | 38 assert_tag :tag => 'user', |
38 :child => {:tag => 'id', :content => '2'} | 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 | |
39 end | 51 end |
40 end | 52 end |
41 | 53 |
42 context ".json" do | 54 context ".json" do |
43 should "return requested user" do | 55 should "return requested user" do |
44 get '/users/2.json' | 56 get '/users/2.json' |
45 | 57 |
58 assert_response :success | |
46 json = ActiveSupport::JSON.decode(response.body) | 59 json = ActiveSupport::JSON.decode(response.body) |
47 assert_kind_of Hash, json | 60 assert_kind_of Hash, json |
48 assert_kind_of Hash, json['user'] | 61 assert_kind_of Hash, json['user'] |
49 assert_equal 2, json['user']['id'] | 62 assert_equal 2, json['user']['id'] |
50 end | 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 | |
51 end | 79 end |
52 end | 80 end |
53 | 81 |
54 context "GET /users/current" do | 82 context "GET /users/current" do |
55 context ".xml" do | 83 context ".xml" do |
58 | 86 |
59 assert_response 401 | 87 assert_response 401 |
60 end | 88 end |
61 | 89 |
62 should "return current user" do | 90 should "return current user" do |
63 get '/users/current.xml', {}, :authorization => credentials('jsmith') | 91 get '/users/current.xml', {}, credentials('jsmith') |
64 | 92 |
65 assert_tag :tag => 'user', | 93 assert_tag :tag => 'user', |
66 :child => {:tag => 'id', :content => '2'} | 94 :child => {:tag => 'id', :content => '2'} |
67 end | 95 end |
68 end | 96 end |
69 end | 97 end |
70 | 98 |
71 context "POST /users" do | 99 context "POST /users" do |
72 context "with valid parameters" do | 100 context "with valid parameters" do |
73 setup do | 101 setup do |
74 @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}} | 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 } | |
75 end | 109 end |
76 | 110 |
77 context ".xml" do | 111 context ".xml" do |
78 should_allow_api_authentication(:post, | 112 should_allow_api_authentication(:post, |
79 '/users.xml', | 113 '/users.xml', |
80 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}}, | 114 {:user => { |
115 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
116 :mail => 'foo@example.net', :password => 'secret123' | |
117 }}, | |
81 {:success_code => :created}) | 118 {:success_code => :created}) |
82 | 119 |
83 should "create a user with the attributes" do | 120 should "create a user with the attributes" do |
84 assert_difference('User.count') do | 121 assert_difference('User.count') do |
85 post '/users.xml', @parameters, :authorization => credentials('admin') | 122 post '/users.xml', @parameters, credentials('admin') |
86 end | 123 end |
87 | 124 |
88 user = User.first(:order => 'id DESC') | 125 user = User.first(:order => 'id DESC') |
89 assert_equal 'foo', user.login | 126 assert_equal 'foo', user.login |
90 assert_equal 'Firstname', user.firstname | 127 assert_equal 'Firstname', user.firstname |
91 assert_equal 'Lastname', user.lastname | 128 assert_equal 'Lastname', user.lastname |
92 assert_equal 'foo@example.net', user.mail | 129 assert_equal 'foo@example.net', user.mail |
93 assert_equal 'only_assigned', user.mail_notification | 130 assert_equal 'only_assigned', user.mail_notification |
94 assert !user.admin? | 131 assert !user.admin? |
95 assert user.check_password?('secret') | 132 assert user.check_password?('secret123') |
96 | 133 |
97 assert_response :created | 134 assert_response :created |
98 assert_equal 'application/xml', @response.content_type | 135 assert_equal 'application/xml', @response.content_type |
99 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} | 136 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} |
100 end | 137 end |
101 end | 138 end |
102 | 139 |
103 context ".json" do | 140 context ".json" do |
104 should_allow_api_authentication(:post, | 141 should_allow_api_authentication(:post, |
105 '/users.json', | 142 '/users.json', |
106 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}}, | 143 {:user => { |
144 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', | |
145 :mail => 'foo@example.net' | |
146 }}, | |
107 {:success_code => :created}) | 147 {:success_code => :created}) |
108 | 148 |
109 should "create a user with the attributes" do | 149 should "create a user with the attributes" do |
110 assert_difference('User.count') do | 150 assert_difference('User.count') do |
111 post '/users.json', @parameters, :authorization => credentials('admin') | 151 post '/users.json', @parameters, credentials('admin') |
112 end | 152 end |
113 | 153 |
114 user = User.first(:order => 'id DESC') | 154 user = User.first(:order => 'id DESC') |
115 assert_equal 'foo', user.login | 155 assert_equal 'foo', user.login |
116 assert_equal 'Firstname', user.firstname | 156 assert_equal 'Firstname', user.firstname |
134 end | 174 end |
135 | 175 |
136 context ".xml" do | 176 context ".xml" do |
137 should "return errors" do | 177 should "return errors" do |
138 assert_no_difference('User.count') do | 178 assert_no_difference('User.count') do |
139 post '/users.xml', @parameters, :authorization => credentials('admin') | 179 post '/users.xml', @parameters, credentials('admin') |
140 end | 180 end |
141 | 181 |
142 assert_response :unprocessable_entity | 182 assert_response :unprocessable_entity |
143 assert_equal 'application/xml', @response.content_type | 183 assert_equal 'application/xml', @response.content_type |
144 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} | 184 assert_tag 'errors', :child => { |
185 :tag => 'error', | |
186 :content => "First name can't be blank" | |
187 } | |
145 end | 188 end |
146 end | 189 end |
147 | 190 |
148 context ".json" do | 191 context ".json" do |
149 should "return errors" do | 192 should "return errors" do |
150 assert_no_difference('User.count') do | 193 assert_no_difference('User.count') do |
151 post '/users.json', @parameters, :authorization => credentials('admin') | 194 post '/users.json', @parameters, credentials('admin') |
152 end | 195 end |
153 | 196 |
154 assert_response :unprocessable_entity | 197 assert_response :unprocessable_entity |
155 assert_equal 'application/json', @response.content_type | 198 assert_equal 'application/json', @response.content_type |
156 json = ActiveSupport::JSON.decode(response.body) | 199 json = ActiveSupport::JSON.decode(response.body) |
163 end | 206 end |
164 | 207 |
165 context "PUT /users/2" do | 208 context "PUT /users/2" do |
166 context "with valid parameters" do | 209 context "with valid parameters" do |
167 setup do | 210 setup do |
168 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}} | 211 @parameters = { |
212 :user => { | |
213 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
214 :mail => 'jsmith@somenet.foo' | |
215 } | |
216 } | |
169 end | 217 end |
170 | 218 |
171 context ".xml" do | 219 context ".xml" do |
172 should_allow_api_authentication(:put, | 220 should_allow_api_authentication(:put, |
173 '/users/2.xml', | 221 '/users/2.xml', |
174 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, | 222 {:user => { |
223 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
224 :mail => 'jsmith@somenet.foo' | |
225 }}, | |
175 {:success_code => :ok}) | 226 {:success_code => :ok}) |
176 | 227 |
177 should "update user with the attributes" do | 228 should "update user with the attributes" do |
178 assert_no_difference('User.count') do | 229 assert_no_difference('User.count') do |
179 put '/users/2.xml', @parameters, :authorization => credentials('admin') | 230 put '/users/2.xml', @parameters, credentials('admin') |
180 end | 231 end |
181 | 232 |
182 user = User.find(2) | 233 user = User.find(2) |
183 assert_equal 'jsmith', user.login | 234 assert_equal 'jsmith', user.login |
184 assert_equal 'John', user.firstname | 235 assert_equal 'John', user.firstname |
185 assert_equal 'Renamed', user.lastname | 236 assert_equal 'Renamed', user.lastname |
186 assert_equal 'jsmith@somenet.foo', user.mail | 237 assert_equal 'jsmith@somenet.foo', user.mail |
187 assert !user.admin? | 238 assert !user.admin? |
188 | 239 |
189 assert_response :ok | 240 assert_response :ok |
241 assert_equal '', @response.body | |
190 end | 242 end |
191 end | 243 end |
192 | 244 |
193 context ".json" do | 245 context ".json" do |
194 should_allow_api_authentication(:put, | 246 should_allow_api_authentication(:put, |
195 '/users/2.json', | 247 '/users/2.json', |
196 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}, | 248 {:user => { |
249 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', | |
250 :mail => 'jsmith@somenet.foo' | |
251 }}, | |
197 {:success_code => :ok}) | 252 {:success_code => :ok}) |
198 | 253 |
199 should "update user with the attributes" do | 254 should "update user with the attributes" do |
200 assert_no_difference('User.count') do | 255 assert_no_difference('User.count') do |
201 put '/users/2.json', @parameters, :authorization => credentials('admin') | 256 put '/users/2.json', @parameters, credentials('admin') |
202 end | 257 end |
203 | 258 |
204 user = User.find(2) | 259 user = User.find(2) |
205 assert_equal 'jsmith', user.login | 260 assert_equal 'jsmith', user.login |
206 assert_equal 'John', user.firstname | 261 assert_equal 'John', user.firstname |
207 assert_equal 'Renamed', user.lastname | 262 assert_equal 'Renamed', user.lastname |
208 assert_equal 'jsmith@somenet.foo', user.mail | 263 assert_equal 'jsmith@somenet.foo', user.mail |
209 assert !user.admin? | 264 assert !user.admin? |
210 | 265 |
211 assert_response :ok | 266 assert_response :ok |
267 assert_equal '', @response.body | |
212 end | 268 end |
213 end | 269 end |
214 end | 270 end |
215 | 271 |
216 context "with invalid parameters" do | 272 context "with invalid parameters" do |
217 setup do | 273 setup do |
218 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}} | 274 @parameters = { |
275 :user => { | |
276 :login => 'jsmith', :firstname => '', :lastname => 'Lastname', | |
277 :mail => 'foo' | |
278 } | |
279 } | |
219 end | 280 end |
220 | 281 |
221 context ".xml" do | 282 context ".xml" do |
222 should "return errors" do | 283 should "return errors" do |
223 assert_no_difference('User.count') do | 284 assert_no_difference('User.count') do |
224 put '/users/2.xml', @parameters, :authorization => credentials('admin') | 285 put '/users/2.xml', @parameters, credentials('admin') |
225 end | 286 end |
226 | 287 |
227 assert_response :unprocessable_entity | 288 assert_response :unprocessable_entity |
228 assert_equal 'application/xml', @response.content_type | 289 assert_equal 'application/xml', @response.content_type |
229 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"} | 290 assert_tag 'errors', :child => { |
291 :tag => 'error', | |
292 :content => "First name can't be blank" | |
293 } | |
230 end | 294 end |
231 end | 295 end |
232 | 296 |
233 context ".json" do | 297 context ".json" do |
234 should "return errors" do | 298 should "return errors" do |
235 assert_no_difference('User.count') do | 299 assert_no_difference('User.count') do |
236 put '/users/2.json', @parameters, :authorization => credentials('admin') | 300 put '/users/2.json', @parameters, credentials('admin') |
237 end | 301 end |
238 | 302 |
239 assert_response :unprocessable_entity | 303 assert_response :unprocessable_entity |
240 assert_equal 'application/json', @response.content_type | 304 assert_equal 'application/json', @response.content_type |
241 json = ActiveSupport::JSON.decode(response.body) | 305 json = ActiveSupport::JSON.decode(response.body) |
254 {}, | 318 {}, |
255 {:success_code => :ok}) | 319 {:success_code => :ok}) |
256 | 320 |
257 should "delete user" do | 321 should "delete user" do |
258 assert_difference('User.count', -1) do | 322 assert_difference('User.count', -1) do |
259 delete '/users/2.xml', {}, :authorization => credentials('admin') | 323 delete '/users/2.xml', {}, credentials('admin') |
260 end | 324 end |
261 | 325 |
262 assert_response :ok | 326 assert_response :ok |
327 assert_equal '', @response.body | |
263 end | 328 end |
264 end | 329 end |
265 | 330 |
266 context ".json" do | 331 context ".json" do |
267 should_allow_api_authentication(:delete, | 332 should_allow_api_authentication(:delete, |
269 {}, | 334 {}, |
270 {:success_code => :ok}) | 335 {:success_code => :ok}) |
271 | 336 |
272 should "delete user" do | 337 should "delete user" do |
273 assert_difference('User.count', -1) do | 338 assert_difference('User.count', -1) do |
274 delete '/users/2.json', {}, :authorization => credentials('admin') | 339 delete '/users/2.json', {}, credentials('admin') |
275 end | 340 end |
276 | 341 |
277 assert_response :ok | 342 assert_response :ok |
278 end | 343 assert_equal '', @response.body |
279 end | 344 end |
280 end | 345 end |
281 | |
282 def credentials(user, password=nil) | |
283 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) | |
284 end | 346 end |
285 end | 347 end |