annotate .svn/pristine/c9/c915cdc5a63346a4f09f2d9cd1c727112cb89f1c.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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