annotate test/integration/api_test/users_test.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children
rev   line source
Chris@119 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@119 3 #
Chris@119 4 # This program is free software; you can redistribute it and/or
Chris@119 5 # modify it under the terms of the GNU General Public License
Chris@119 6 # as published by the Free Software Foundation; either version 2
Chris@119 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@119 9 # This program is distributed in the hope that it will be useful,
Chris@119 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@119 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@119 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@119 14 # You should have received a copy of the GNU General Public License
Chris@119 15 # along with this program; if not, write to the Free Software
Chris@119 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@119 17
Chris@119 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@1295 19
Chris@1295 20 class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
Chris@1115 21 fixtures :users, :members, :member_roles, :roles, :projects
Chris@119 22
Chris@119 23 def setup
Chris@119 24 Setting.rest_api_enabled = '1'
Chris@119 25 end
Chris@119 26
Chris@119 27 context "GET /users" do
Chris@119 28 should_allow_api_authentication(:get, "/users.xml")
Chris@119 29 should_allow_api_authentication(:get, "/users.json")
Chris@119 30 end
Chris@119 31
Chris@119 32 context "GET /users/2" do
Chris@119 33 context ".xml" do
Chris@119 34 should "return requested user" do
Chris@119 35 get '/users/2.xml'
Chris@909 36
Chris@1115 37 assert_response :success
Chris@119 38 assert_tag :tag => 'user',
Chris@119 39 :child => {:tag => 'id', :content => '2'}
Chris@119 40 end
Chris@1115 41
Chris@1115 42 context "with include=memberships" do
Chris@1115 43 should "include memberships" do
Chris@1115 44 get '/users/2.xml?include=memberships'
Chris@1115 45
Chris@1115 46 assert_response :success
Chris@1115 47 assert_tag :tag => 'memberships',
Chris@1115 48 :parent => {:tag => 'user'},
Chris@1115 49 :children => {:count => 1}
Chris@1115 50 end
Chris@1115 51 end
Chris@119 52 end
Chris@119 53
Chris@119 54 context ".json" do
Chris@119 55 should "return requested user" do
Chris@119 56 get '/users/2.json'
Chris@909 57
Chris@1115 58 assert_response :success
Chris@119 59 json = ActiveSupport::JSON.decode(response.body)
Chris@119 60 assert_kind_of Hash, json
Chris@119 61 assert_kind_of Hash, json['user']
Chris@119 62 assert_equal 2, json['user']['id']
Chris@119 63 end
Chris@1115 64
Chris@1115 65 context "with include=memberships" do
Chris@1115 66 should "include memberships" do
Chris@1115 67 get '/users/2.json?include=memberships'
Chris@1115 68
Chris@1115 69 assert_response :success
Chris@1115 70 json = ActiveSupport::JSON.decode(response.body)
Chris@1115 71 assert_kind_of Array, json['user']['memberships']
Chris@1115 72 assert_equal [{
Chris@1115 73 "id"=>1,
Chris@1115 74 "project"=>{"name"=>"eCookbook", "id"=>1},
Chris@1115 75 "roles"=>[{"name"=>"Manager", "id"=>1}]
Chris@1115 76 }], json['user']['memberships']
Chris@1115 77 end
Chris@1115 78 end
Chris@119 79 end
Chris@119 80 end
Chris@909 81
Chris@119 82 context "GET /users/current" do
Chris@119 83 context ".xml" do
Chris@119 84 should "require authentication" do
Chris@119 85 get '/users/current.xml'
Chris@909 86
Chris@119 87 assert_response 401
Chris@119 88 end
Chris@909 89
Chris@119 90 should "return current user" do
Chris@1115 91 get '/users/current.xml', {}, credentials('jsmith')
Chris@909 92
Chris@119 93 assert_tag :tag => 'user',
Chris@119 94 :child => {:tag => 'id', :content => '2'}
Chris@119 95 end
Chris@119 96 end
Chris@119 97 end
Chris@119 98
Chris@1295 99 test "GET /users/:id should not return login for other user" do
Chris@1295 100 get '/users/3.xml', {}, credentials('jsmith')
Chris@1295 101 assert_response :success
Chris@1295 102 assert_no_tag 'user', :child => {:tag => 'login'}
Chris@1295 103 end
Chris@1295 104
Chris@1295 105 test "GET /users/:id should return login for current user" do
Chris@1295 106 get '/users/2.xml', {}, credentials('jsmith')
Chris@1295 107 assert_response :success
Chris@1295 108 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
Chris@1295 109 end
Chris@1295 110
Chris@1295 111 test "GET /users/:id should not return api_key for other user" do
Chris@1295 112 get '/users/3.xml', {}, credentials('jsmith')
Chris@1295 113 assert_response :success
Chris@1295 114 assert_no_tag 'user', :child => {:tag => 'api_key'}
Chris@1295 115 end
Chris@1295 116
Chris@1295 117 test "GET /users/:id should return api_key for current user" do
Chris@1295 118 get '/users/2.xml', {}, credentials('jsmith')
Chris@1295 119 assert_response :success
Chris@1295 120 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
Chris@1295 121 end
Chris@1295 122
Chris@119 123 context "POST /users" do
Chris@119 124 context "with valid parameters" do
Chris@119 125 setup do
Chris@1115 126 @parameters = {
Chris@1115 127 :user => {
Chris@1115 128 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1115 129 :mail => 'foo@example.net', :password => 'secret123',
Chris@1115 130 :mail_notification => 'only_assigned'
Chris@1115 131 }
Chris@1115 132 }
Chris@119 133 end
Chris@909 134
Chris@119 135 context ".xml" do
Chris@119 136 should_allow_api_authentication(:post,
Chris@119 137 '/users.xml',
Chris@1115 138 {:user => {
Chris@1115 139 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1115 140 :mail => 'foo@example.net', :password => 'secret123'
Chris@1115 141 }},
Chris@119 142 {:success_code => :created})
Chris@909 143
Chris@119 144 should "create a user with the attributes" do
Chris@119 145 assert_difference('User.count') do
Chris@1115 146 post '/users.xml', @parameters, credentials('admin')
Chris@119 147 end
Chris@909 148
Chris@119 149 user = User.first(:order => 'id DESC')
Chris@119 150 assert_equal 'foo', user.login
Chris@119 151 assert_equal 'Firstname', user.firstname
Chris@119 152 assert_equal 'Lastname', user.lastname
Chris@119 153 assert_equal 'foo@example.net', user.mail
Chris@119 154 assert_equal 'only_assigned', user.mail_notification
Chris@119 155 assert !user.admin?
Chris@1115 156 assert user.check_password?('secret123')
Chris@909 157
Chris@119 158 assert_response :created
Chris@119 159 assert_equal 'application/xml', @response.content_type
Chris@119 160 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
Chris@119 161 end
Chris@119 162 end
Chris@909 163
Chris@119 164 context ".json" do
Chris@119 165 should_allow_api_authentication(:post,
Chris@119 166 '/users.json',
Chris@1115 167 {:user => {
Chris@1115 168 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1115 169 :mail => 'foo@example.net'
Chris@1115 170 }},
Chris@119 171 {:success_code => :created})
Chris@909 172
Chris@119 173 should "create a user with the attributes" do
Chris@119 174 assert_difference('User.count') do
Chris@1115 175 post '/users.json', @parameters, credentials('admin')
Chris@119 176 end
Chris@909 177
Chris@119 178 user = User.first(:order => 'id DESC')
Chris@119 179 assert_equal 'foo', user.login
Chris@119 180 assert_equal 'Firstname', user.firstname
Chris@119 181 assert_equal 'Lastname', user.lastname
Chris@119 182 assert_equal 'foo@example.net', user.mail
Chris@119 183 assert !user.admin?
Chris@909 184
Chris@119 185 assert_response :created
Chris@119 186 assert_equal 'application/json', @response.content_type
Chris@119 187 json = ActiveSupport::JSON.decode(response.body)
Chris@119 188 assert_kind_of Hash, json
Chris@119 189 assert_kind_of Hash, json['user']
Chris@119 190 assert_equal user.id, json['user']['id']
Chris@119 191 end
Chris@119 192 end
Chris@119 193 end
Chris@909 194
Chris@119 195 context "with invalid parameters" do
Chris@119 196 setup do
Chris@119 197 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
Chris@119 198 end
Chris@909 199
Chris@119 200 context ".xml" do
Chris@119 201 should "return errors" do
Chris@119 202 assert_no_difference('User.count') do
Chris@1115 203 post '/users.xml', @parameters, credentials('admin')
Chris@119 204 end
Chris@909 205
Chris@119 206 assert_response :unprocessable_entity
Chris@119 207 assert_equal 'application/xml', @response.content_type
Chris@1115 208 assert_tag 'errors', :child => {
Chris@1115 209 :tag => 'error',
Chris@1115 210 :content => "First name can't be blank"
Chris@1115 211 }
Chris@119 212 end
Chris@119 213 end
Chris@909 214
Chris@119 215 context ".json" do
Chris@119 216 should "return errors" do
Chris@119 217 assert_no_difference('User.count') do
Chris@1115 218 post '/users.json', @parameters, credentials('admin')
Chris@119 219 end
Chris@909 220
Chris@119 221 assert_response :unprocessable_entity
Chris@119 222 assert_equal 'application/json', @response.content_type
Chris@119 223 json = ActiveSupport::JSON.decode(response.body)
Chris@119 224 assert_kind_of Hash, json
Chris@119 225 assert json.has_key?('errors')
Chris@119 226 assert_kind_of Array, json['errors']
Chris@119 227 end
Chris@119 228 end
Chris@119 229 end
Chris@119 230 end
Chris@119 231
Chris@119 232 context "PUT /users/2" do
Chris@119 233 context "with valid parameters" do
Chris@119 234 setup do
Chris@1115 235 @parameters = {
Chris@1115 236 :user => {
Chris@1115 237 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1115 238 :mail => 'jsmith@somenet.foo'
Chris@1115 239 }
Chris@1115 240 }
Chris@119 241 end
Chris@909 242
Chris@119 243 context ".xml" do
Chris@119 244 should_allow_api_authentication(:put,
Chris@119 245 '/users/2.xml',
Chris@1115 246 {:user => {
Chris@1115 247 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1115 248 :mail => 'jsmith@somenet.foo'
Chris@1115 249 }},
Chris@119 250 {:success_code => :ok})
Chris@909 251
Chris@119 252 should "update user with the attributes" do
Chris@119 253 assert_no_difference('User.count') do
Chris@1115 254 put '/users/2.xml', @parameters, credentials('admin')
Chris@119 255 end
Chris@909 256
Chris@119 257 user = User.find(2)
Chris@119 258 assert_equal 'jsmith', user.login
Chris@119 259 assert_equal 'John', user.firstname
Chris@119 260 assert_equal 'Renamed', user.lastname
Chris@119 261 assert_equal 'jsmith@somenet.foo', user.mail
Chris@119 262 assert !user.admin?
Chris@909 263
Chris@119 264 assert_response :ok
Chris@1115 265 assert_equal '', @response.body
Chris@119 266 end
Chris@119 267 end
Chris@909 268
Chris@119 269 context ".json" do
Chris@119 270 should_allow_api_authentication(:put,
Chris@119 271 '/users/2.json',
Chris@1115 272 {:user => {
Chris@1115 273 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1115 274 :mail => 'jsmith@somenet.foo'
Chris@1115 275 }},
Chris@119 276 {:success_code => :ok})
Chris@909 277
Chris@119 278 should "update user with the attributes" do
Chris@119 279 assert_no_difference('User.count') do
Chris@1115 280 put '/users/2.json', @parameters, credentials('admin')
Chris@119 281 end
Chris@909 282
Chris@119 283 user = User.find(2)
Chris@119 284 assert_equal 'jsmith', user.login
Chris@119 285 assert_equal 'John', user.firstname
Chris@119 286 assert_equal 'Renamed', user.lastname
Chris@119 287 assert_equal 'jsmith@somenet.foo', user.mail
Chris@119 288 assert !user.admin?
Chris@909 289
Chris@119 290 assert_response :ok
Chris@1115 291 assert_equal '', @response.body
Chris@119 292 end
Chris@119 293 end
Chris@119 294 end
Chris@909 295
Chris@119 296 context "with invalid parameters" do
Chris@119 297 setup do
Chris@1115 298 @parameters = {
Chris@1115 299 :user => {
Chris@1115 300 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
Chris@1115 301 :mail => 'foo'
Chris@1115 302 }
Chris@1115 303 }
Chris@119 304 end
Chris@909 305
Chris@119 306 context ".xml" do
Chris@119 307 should "return errors" do
Chris@119 308 assert_no_difference('User.count') do
Chris@1115 309 put '/users/2.xml', @parameters, credentials('admin')
Chris@119 310 end
Chris@909 311
Chris@119 312 assert_response :unprocessable_entity
Chris@119 313 assert_equal 'application/xml', @response.content_type
Chris@1115 314 assert_tag 'errors', :child => {
Chris@1115 315 :tag => 'error',
Chris@1115 316 :content => "First name can't be blank"
Chris@1115 317 }
Chris@119 318 end
Chris@119 319 end
Chris@909 320
Chris@119 321 context ".json" do
Chris@119 322 should "return errors" do
Chris@119 323 assert_no_difference('User.count') do
Chris@1115 324 put '/users/2.json', @parameters, credentials('admin')
Chris@119 325 end
Chris@909 326
Chris@119 327 assert_response :unprocessable_entity
Chris@119 328 assert_equal 'application/json', @response.content_type
Chris@119 329 json = ActiveSupport::JSON.decode(response.body)
Chris@119 330 assert_kind_of Hash, json
Chris@119 331 assert json.has_key?('errors')
Chris@119 332 assert_kind_of Array, json['errors']
Chris@119 333 end
Chris@119 334 end
Chris@119 335 end
Chris@128 336 end
Chris@909 337
Chris@128 338 context "DELETE /users/2" do
Chris@128 339 context ".xml" do
Chris@128 340 should_allow_api_authentication(:delete,
Chris@128 341 '/users/2.xml',
Chris@128 342 {},
Chris@128 343 {:success_code => :ok})
Chris@909 344
Chris@128 345 should "delete user" do
Chris@128 346 assert_difference('User.count', -1) do
Chris@1115 347 delete '/users/2.xml', {}, credentials('admin')
Chris@128 348 end
Chris@909 349
Chris@128 350 assert_response :ok
Chris@1115 351 assert_equal '', @response.body
Chris@128 352 end
Chris@128 353 end
Chris@909 354
Chris@128 355 context ".json" do
Chris@128 356 should_allow_api_authentication(:delete,
Chris@128 357 '/users/2.xml',
Chris@128 358 {},
Chris@128 359 {:success_code => :ok})
Chris@909 360
Chris@128 361 should "delete user" do
Chris@128 362 assert_difference('User.count', -1) do
Chris@1115 363 delete '/users/2.json', {}, credentials('admin')
Chris@119 364 end
Chris@909 365
Chris@128 366 assert_response :ok
Chris@1115 367 assert_equal '', @response.body
Chris@119 368 end
Chris@119 369 end
Chris@119 370 end
Chris@119 371 end