annotate .svn/pristine/47/475e48920f645a29c9410b5f2003c06d6f0a84d4.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-2012 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 require 'pp'
Chris@1295 20 class ApiTest::UsersTest < ActionController::IntegrationTest
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 context "POST /users" do
Chris@1295 100 context "with valid parameters" do
Chris@1295 101 setup do
Chris@1295 102 @parameters = {
Chris@1295 103 :user => {
Chris@1295 104 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1295 105 :mail => 'foo@example.net', :password => 'secret123',
Chris@1295 106 :mail_notification => 'only_assigned'
Chris@1295 107 }
Chris@1295 108 }
Chris@1295 109 end
Chris@1295 110
Chris@1295 111 context ".xml" do
Chris@1295 112 should_allow_api_authentication(:post,
Chris@1295 113 '/users.xml',
Chris@1295 114 {:user => {
Chris@1295 115 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1295 116 :mail => 'foo@example.net', :password => 'secret123'
Chris@1295 117 }},
Chris@1295 118 {:success_code => :created})
Chris@1295 119
Chris@1295 120 should "create a user with the attributes" do
Chris@1295 121 assert_difference('User.count') do
Chris@1295 122 post '/users.xml', @parameters, credentials('admin')
Chris@1295 123 end
Chris@1295 124
Chris@1295 125 user = User.first(:order => 'id DESC')
Chris@1295 126 assert_equal 'foo', user.login
Chris@1295 127 assert_equal 'Firstname', user.firstname
Chris@1295 128 assert_equal 'Lastname', user.lastname
Chris@1295 129 assert_equal 'foo@example.net', user.mail
Chris@1295 130 assert_equal 'only_assigned', user.mail_notification
Chris@1295 131 assert !user.admin?
Chris@1295 132 assert user.check_password?('secret123')
Chris@1295 133
Chris@1295 134 assert_response :created
Chris@1295 135 assert_equal 'application/xml', @response.content_type
Chris@1295 136 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
Chris@1295 137 end
Chris@1295 138 end
Chris@1295 139
Chris@1295 140 context ".json" do
Chris@1295 141 should_allow_api_authentication(:post,
Chris@1295 142 '/users.json',
Chris@1295 143 {:user => {
Chris@1295 144 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1295 145 :mail => 'foo@example.net'
Chris@1295 146 }},
Chris@1295 147 {:success_code => :created})
Chris@1295 148
Chris@1295 149 should "create a user with the attributes" do
Chris@1295 150 assert_difference('User.count') do
Chris@1295 151 post '/users.json', @parameters, credentials('admin')
Chris@1295 152 end
Chris@1295 153
Chris@1295 154 user = User.first(:order => 'id DESC')
Chris@1295 155 assert_equal 'foo', user.login
Chris@1295 156 assert_equal 'Firstname', user.firstname
Chris@1295 157 assert_equal 'Lastname', user.lastname
Chris@1295 158 assert_equal 'foo@example.net', user.mail
Chris@1295 159 assert !user.admin?
Chris@1295 160
Chris@1295 161 assert_response :created
Chris@1295 162 assert_equal 'application/json', @response.content_type
Chris@1295 163 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 164 assert_kind_of Hash, json
Chris@1295 165 assert_kind_of Hash, json['user']
Chris@1295 166 assert_equal user.id, json['user']['id']
Chris@1295 167 end
Chris@1295 168 end
Chris@1295 169 end
Chris@1295 170
Chris@1295 171 context "with invalid parameters" do
Chris@1295 172 setup do
Chris@1295 173 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
Chris@1295 174 end
Chris@1295 175
Chris@1295 176 context ".xml" do
Chris@1295 177 should "return errors" do
Chris@1295 178 assert_no_difference('User.count') do
Chris@1295 179 post '/users.xml', @parameters, credentials('admin')
Chris@1295 180 end
Chris@1295 181
Chris@1295 182 assert_response :unprocessable_entity
Chris@1295 183 assert_equal 'application/xml', @response.content_type
Chris@1295 184 assert_tag 'errors', :child => {
Chris@1295 185 :tag => 'error',
Chris@1295 186 :content => "First name can't be blank"
Chris@1295 187 }
Chris@1295 188 end
Chris@1295 189 end
Chris@1295 190
Chris@1295 191 context ".json" do
Chris@1295 192 should "return errors" do
Chris@1295 193 assert_no_difference('User.count') do
Chris@1295 194 post '/users.json', @parameters, credentials('admin')
Chris@1295 195 end
Chris@1295 196
Chris@1295 197 assert_response :unprocessable_entity
Chris@1295 198 assert_equal 'application/json', @response.content_type
Chris@1295 199 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 200 assert_kind_of Hash, json
Chris@1295 201 assert json.has_key?('errors')
Chris@1295 202 assert_kind_of Array, json['errors']
Chris@1295 203 end
Chris@1295 204 end
Chris@1295 205 end
Chris@1295 206 end
Chris@1295 207
Chris@1295 208 context "PUT /users/2" do
Chris@1295 209 context "with valid parameters" do
Chris@1295 210 setup do
Chris@1295 211 @parameters = {
Chris@1295 212 :user => {
Chris@1295 213 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1295 214 :mail => 'jsmith@somenet.foo'
Chris@1295 215 }
Chris@1295 216 }
Chris@1295 217 end
Chris@1295 218
Chris@1295 219 context ".xml" do
Chris@1295 220 should_allow_api_authentication(:put,
Chris@1295 221 '/users/2.xml',
Chris@1295 222 {:user => {
Chris@1295 223 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1295 224 :mail => 'jsmith@somenet.foo'
Chris@1295 225 }},
Chris@1295 226 {:success_code => :ok})
Chris@1295 227
Chris@1295 228 should "update user with the attributes" do
Chris@1295 229 assert_no_difference('User.count') do
Chris@1295 230 put '/users/2.xml', @parameters, credentials('admin')
Chris@1295 231 end
Chris@1295 232
Chris@1295 233 user = User.find(2)
Chris@1295 234 assert_equal 'jsmith', user.login
Chris@1295 235 assert_equal 'John', user.firstname
Chris@1295 236 assert_equal 'Renamed', user.lastname
Chris@1295 237 assert_equal 'jsmith@somenet.foo', user.mail
Chris@1295 238 assert !user.admin?
Chris@1295 239
Chris@1295 240 assert_response :ok
Chris@1295 241 assert_equal '', @response.body
Chris@1295 242 end
Chris@1295 243 end
Chris@1295 244
Chris@1295 245 context ".json" do
Chris@1295 246 should_allow_api_authentication(:put,
Chris@1295 247 '/users/2.json',
Chris@1295 248 {:user => {
Chris@1295 249 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1295 250 :mail => 'jsmith@somenet.foo'
Chris@1295 251 }},
Chris@1295 252 {:success_code => :ok})
Chris@1295 253
Chris@1295 254 should "update user with the attributes" do
Chris@1295 255 assert_no_difference('User.count') do
Chris@1295 256 put '/users/2.json', @parameters, credentials('admin')
Chris@1295 257 end
Chris@1295 258
Chris@1295 259 user = User.find(2)
Chris@1295 260 assert_equal 'jsmith', user.login
Chris@1295 261 assert_equal 'John', user.firstname
Chris@1295 262 assert_equal 'Renamed', user.lastname
Chris@1295 263 assert_equal 'jsmith@somenet.foo', user.mail
Chris@1295 264 assert !user.admin?
Chris@1295 265
Chris@1295 266 assert_response :ok
Chris@1295 267 assert_equal '', @response.body
Chris@1295 268 end
Chris@1295 269 end
Chris@1295 270 end
Chris@1295 271
Chris@1295 272 context "with invalid parameters" do
Chris@1295 273 setup do
Chris@1295 274 @parameters = {
Chris@1295 275 :user => {
Chris@1295 276 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
Chris@1295 277 :mail => 'foo'
Chris@1295 278 }
Chris@1295 279 }
Chris@1295 280 end
Chris@1295 281
Chris@1295 282 context ".xml" do
Chris@1295 283 should "return errors" do
Chris@1295 284 assert_no_difference('User.count') do
Chris@1295 285 put '/users/2.xml', @parameters, credentials('admin')
Chris@1295 286 end
Chris@1295 287
Chris@1295 288 assert_response :unprocessable_entity
Chris@1295 289 assert_equal 'application/xml', @response.content_type
Chris@1295 290 assert_tag 'errors', :child => {
Chris@1295 291 :tag => 'error',
Chris@1295 292 :content => "First name can't be blank"
Chris@1295 293 }
Chris@1295 294 end
Chris@1295 295 end
Chris@1295 296
Chris@1295 297 context ".json" do
Chris@1295 298 should "return errors" do
Chris@1295 299 assert_no_difference('User.count') do
Chris@1295 300 put '/users/2.json', @parameters, credentials('admin')
Chris@1295 301 end
Chris@1295 302
Chris@1295 303 assert_response :unprocessable_entity
Chris@1295 304 assert_equal 'application/json', @response.content_type
Chris@1295 305 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 306 assert_kind_of Hash, json
Chris@1295 307 assert json.has_key?('errors')
Chris@1295 308 assert_kind_of Array, json['errors']
Chris@1295 309 end
Chris@1295 310 end
Chris@1295 311 end
Chris@1295 312 end
Chris@1295 313
Chris@1295 314 context "DELETE /users/2" do
Chris@1295 315 context ".xml" do
Chris@1295 316 should_allow_api_authentication(:delete,
Chris@1295 317 '/users/2.xml',
Chris@1295 318 {},
Chris@1295 319 {:success_code => :ok})
Chris@1295 320
Chris@1295 321 should "delete user" do
Chris@1295 322 assert_difference('User.count', -1) do
Chris@1295 323 delete '/users/2.xml', {}, credentials('admin')
Chris@1295 324 end
Chris@1295 325
Chris@1295 326 assert_response :ok
Chris@1295 327 assert_equal '', @response.body
Chris@1295 328 end
Chris@1295 329 end
Chris@1295 330
Chris@1295 331 context ".json" do
Chris@1295 332 should_allow_api_authentication(:delete,
Chris@1295 333 '/users/2.xml',
Chris@1295 334 {},
Chris@1295 335 {:success_code => :ok})
Chris@1295 336
Chris@1295 337 should "delete user" do
Chris@1295 338 assert_difference('User.count', -1) do
Chris@1295 339 delete '/users/2.json', {}, credentials('admin')
Chris@1295 340 end
Chris@1295 341
Chris@1295 342 assert_response :ok
Chris@1295 343 assert_equal '', @response.body
Chris@1295 344 end
Chris@1295 345 end
Chris@1295 346 end
Chris@1295 347 end