annotate test/integration/api_test/users_test.rb @ 1458:b1f4c9a2af24 bug_794

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