annotate test/integration/api_test/users_test.rb @ 1613:90bed4e10cc8 deploy

Download file link
author Chris Cannam
date Wed, 30 Aug 2017 17:24:37 +0100
parents dffacf8a6908
children
rev   line source
Chris@119 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1464 19
Chris@1464 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@1464 27 should_allow_api_authentication(:get, "/users.xml")
Chris@1464 28 should_allow_api_authentication(:get, "/users.json")
Chris@1464 29 should_allow_api_authentication(:post,
Chris@1464 30 '/users.xml',
Chris@1464 31 {:user => {
Chris@1464 32 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1464 33 :mail => 'foo@example.net', :password => 'secret123'
Chris@1464 34 }},
Chris@1464 35 {:success_code => :created})
Chris@1464 36 should_allow_api_authentication(:post,
Chris@1464 37 '/users.json',
Chris@1464 38 {:user => {
Chris@1464 39 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1464 40 :mail => 'foo@example.net'
Chris@1464 41 }},
Chris@1464 42 {:success_code => :created})
Chris@1464 43 should_allow_api_authentication(:put,
Chris@1464 44 '/users/2.xml',
Chris@1464 45 {:user => {
Chris@1464 46 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1464 47 :mail => 'jsmith@somenet.foo'
Chris@1464 48 }},
Chris@1464 49 {:success_code => :ok})
Chris@1464 50 should_allow_api_authentication(:put,
Chris@1464 51 '/users/2.json',
Chris@1464 52 {:user => {
Chris@1464 53 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1464 54 :mail => 'jsmith@somenet.foo'
Chris@1464 55 }},
Chris@1464 56 {:success_code => :ok})
Chris@1464 57 should_allow_api_authentication(:delete,
Chris@1464 58 '/users/2.xml',
Chris@1464 59 {},
Chris@1464 60 {:success_code => :ok})
Chris@1464 61 should_allow_api_authentication(:delete,
Chris@1464 62 '/users/2.xml',
Chris@1464 63 {},
Chris@1464 64 {:success_code => :ok})
Chris@1464 65
Chris@1464 66 test "GET /users/:id.xml should return the user" do
Chris@1464 67 get '/users/2.xml'
Chris@1464 68
Chris@1464 69 assert_response :success
Chris@1464 70 assert_tag :tag => 'user',
Chris@1464 71 :child => {:tag => 'id', :content => '2'}
Chris@119 72 end
Chris@119 73
Chris@1464 74 test "GET /users/:id.json should return the user" do
Chris@1464 75 get '/users/2.json'
Chris@909 76
Chris@1464 77 assert_response :success
Chris@1464 78 json = ActiveSupport::JSON.decode(response.body)
Chris@1464 79 assert_kind_of Hash, json
Chris@1464 80 assert_kind_of Hash, json['user']
Chris@1464 81 assert_equal 2, json['user']['id']
Chris@1464 82 end
Chris@1115 83
Chris@1464 84 test "GET /users/:id.xml with include=memberships should include memberships" do
Chris@1464 85 get '/users/2.xml?include=memberships'
Chris@1464 86
Chris@1464 87 assert_response :success
Chris@1464 88 assert_tag :tag => 'memberships',
Chris@1464 89 :parent => {:tag => 'user'},
Chris@1464 90 :children => {:count => 1}
Chris@1464 91 end
Chris@1464 92
Chris@1464 93 test "GET /users/:id.json with include=memberships should include memberships" do
Chris@1464 94 get '/users/2.json?include=memberships'
Chris@1464 95
Chris@1464 96 assert_response :success
Chris@1464 97 json = ActiveSupport::JSON.decode(response.body)
Chris@1464 98 assert_kind_of Array, json['user']['memberships']
Chris@1464 99 assert_equal [{
Chris@1464 100 "id"=>1,
Chris@1464 101 "project"=>{"name"=>"eCookbook", "id"=>1},
Chris@1464 102 "roles"=>[{"name"=>"Manager", "id"=>1}]
Chris@1464 103 }], json['user']['memberships']
Chris@1464 104 end
Chris@1464 105
Chris@1464 106 test "GET /users/current.xml should require authentication" do
Chris@1464 107 get '/users/current.xml'
Chris@1464 108
Chris@1464 109 assert_response 401
Chris@1464 110 end
Chris@1464 111
Chris@1464 112 test "GET /users/current.xml should return current user" do
Chris@1464 113 get '/users/current.xml', {}, credentials('jsmith')
Chris@1464 114
Chris@1464 115 assert_tag :tag => 'user',
Chris@1464 116 :child => {:tag => 'id', :content => '2'}
Chris@1464 117 end
Chris@1464 118
Chris@1464 119 test "GET /users/:id should not return login for other user" do
Chris@1464 120 get '/users/3.xml', {}, credentials('jsmith')
Chris@1464 121 assert_response :success
Chris@1464 122 assert_no_tag 'user', :child => {:tag => 'login'}
Chris@1464 123 end
Chris@1464 124
Chris@1464 125 test "GET /users/:id should return login for current user" do
Chris@1464 126 get '/users/2.xml', {}, credentials('jsmith')
Chris@1464 127 assert_response :success
Chris@1464 128 assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'}
Chris@1464 129 end
Chris@1464 130
Chris@1464 131 test "GET /users/:id should not return api_key for other user" do
Chris@1464 132 get '/users/3.xml', {}, credentials('jsmith')
Chris@1464 133 assert_response :success
Chris@1464 134 assert_no_tag 'user', :child => {:tag => 'api_key'}
Chris@1464 135 end
Chris@1464 136
Chris@1464 137 test "GET /users/:id should return api_key for current user" do
Chris@1464 138 get '/users/2.xml', {}, credentials('jsmith')
Chris@1464 139 assert_response :success
Chris@1464 140 assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key}
Chris@1464 141 end
Chris@1464 142
Chris@1464 143 test "GET /users/:id should not return status for standard user" do
Chris@1464 144 get '/users/3.xml', {}, credentials('jsmith')
Chris@1464 145 assert_response :success
Chris@1464 146 assert_no_tag 'user', :child => {:tag => 'status'}
Chris@1464 147 end
Chris@1464 148
Chris@1464 149 test "GET /users/:id should return status for administrators" do
Chris@1464 150 get '/users/2.xml', {}, credentials('admin')
Chris@1464 151 assert_response :success
Chris@1464 152 assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s}
Chris@1464 153 end
Chris@1464 154
Chris@1464 155 test "POST /users.xml with valid parameters should create the user" do
Chris@1464 156 assert_difference('User.count') do
Chris@1464 157 post '/users.xml', {
Chris@1464 158 :user => {
Chris@1464 159 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1464 160 :mail => 'foo@example.net', :password => 'secret123',
Chris@1464 161 :mail_notification => 'only_assigned'}
Chris@1464 162 },
Chris@1464 163 credentials('admin')
Chris@119 164 end
Chris@119 165
Chris@1517 166 user = User.order('id DESC').first
Chris@1464 167 assert_equal 'foo', user.login
Chris@1464 168 assert_equal 'Firstname', user.firstname
Chris@1464 169 assert_equal 'Lastname', user.lastname
Chris@1464 170 assert_equal 'foo@example.net', user.mail
Chris@1464 171 assert_equal 'only_assigned', user.mail_notification
Chris@1464 172 assert !user.admin?
Chris@1464 173 assert user.check_password?('secret123')
Chris@909 174
Chris@1464 175 assert_response :created
Chris@1464 176 assert_equal 'application/xml', @response.content_type
Chris@1464 177 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
Chris@119 178 end
Chris@909 179
Chris@1464 180 test "POST /users.json with valid parameters should create the user" do
Chris@1464 181 assert_difference('User.count') do
Chris@1464 182 post '/users.json', {
Chris@1464 183 :user => {
Chris@1464 184 :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
Chris@1464 185 :mail => 'foo@example.net', :password => 'secret123',
Chris@1464 186 :mail_notification => 'only_assigned'}
Chris@1464 187 },
Chris@1464 188 credentials('admin')
Chris@1464 189 end
Chris@909 190
Chris@1517 191 user = User.order('id DESC').first
Chris@1464 192 assert_equal 'foo', user.login
Chris@1464 193 assert_equal 'Firstname', user.firstname
Chris@1464 194 assert_equal 'Lastname', user.lastname
Chris@1464 195 assert_equal 'foo@example.net', user.mail
Chris@1464 196 assert !user.admin?
Chris@909 197
Chris@1464 198 assert_response :created
Chris@1464 199 assert_equal 'application/json', @response.content_type
Chris@1464 200 json = ActiveSupport::JSON.decode(response.body)
Chris@1464 201 assert_kind_of Hash, json
Chris@1464 202 assert_kind_of Hash, json['user']
Chris@1464 203 assert_equal user.id, json['user']['id']
Chris@119 204 end
Chris@119 205
Chris@1464 206 test "POST /users.xml with with invalid parameters should return errors" do
Chris@1464 207 assert_no_difference('User.count') do
Chris@1464 208 post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
Chris@119 209 end
Chris@909 210
Chris@1464 211 assert_response :unprocessable_entity
Chris@1464 212 assert_equal 'application/xml', @response.content_type
Chris@1464 213 assert_tag 'errors', :child => {
Chris@1464 214 :tag => 'error',
Chris@1464 215 :content => "First name can't be blank"
Chris@1464 216 }
Chris@119 217 end
Chris@119 218
Chris@1464 219 test "POST /users.json with with invalid parameters should return errors" do
Chris@1464 220 assert_no_difference('User.count') do
Chris@1464 221 post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
Chris@119 222 end
Chris@909 223
Chris@1464 224 assert_response :unprocessable_entity
Chris@1464 225 assert_equal 'application/json', @response.content_type
Chris@1464 226 json = ActiveSupport::JSON.decode(response.body)
Chris@1464 227 assert_kind_of Hash, json
Chris@1464 228 assert json.has_key?('errors')
Chris@1464 229 assert_kind_of Array, json['errors']
Chris@128 230 end
Chris@909 231
Chris@1464 232 test "PUT /users/:id.xml with valid parameters should update the user" do
Chris@1464 233 assert_no_difference('User.count') do
Chris@1464 234 put '/users/2.xml', {
Chris@1464 235 :user => {
Chris@1464 236 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1464 237 :mail => 'jsmith@somenet.foo'}
Chris@1464 238 },
Chris@1464 239 credentials('admin')
Chris@128 240 end
Chris@909 241
Chris@1464 242 user = User.find(2)
Chris@1464 243 assert_equal 'jsmith', user.login
Chris@1464 244 assert_equal 'John', user.firstname
Chris@1464 245 assert_equal 'Renamed', user.lastname
Chris@1464 246 assert_equal 'jsmith@somenet.foo', user.mail
Chris@1464 247 assert !user.admin?
Chris@909 248
Chris@1464 249 assert_response :ok
Chris@1464 250 assert_equal '', @response.body
Chris@1464 251 end
Chris@909 252
Chris@1464 253 test "PUT /users/:id.json with valid parameters should update the user" do
Chris@1464 254 assert_no_difference('User.count') do
Chris@1464 255 put '/users/2.json', {
Chris@1464 256 :user => {
Chris@1464 257 :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
Chris@1464 258 :mail => 'jsmith@somenet.foo'}
Chris@1464 259 },
Chris@1464 260 credentials('admin')
Chris@119 261 end
Chris@1464 262
Chris@1464 263 user = User.find(2)
Chris@1464 264 assert_equal 'jsmith', user.login
Chris@1464 265 assert_equal 'John', user.firstname
Chris@1464 266 assert_equal 'Renamed', user.lastname
Chris@1464 267 assert_equal 'jsmith@somenet.foo', user.mail
Chris@1464 268 assert !user.admin?
Chris@1464 269
Chris@1464 270 assert_response :ok
Chris@1464 271 assert_equal '', @response.body
Chris@1464 272 end
Chris@1464 273
Chris@1464 274 test "PUT /users/:id.xml with invalid parameters" do
Chris@1464 275 assert_no_difference('User.count') do
Chris@1464 276 put '/users/2.xml', {
Chris@1464 277 :user => {
Chris@1464 278 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
Chris@1464 279 :mail => 'foo'}
Chris@1464 280 },
Chris@1464 281 credentials('admin')
Chris@1464 282 end
Chris@1464 283
Chris@1464 284 assert_response :unprocessable_entity
Chris@1464 285 assert_equal 'application/xml', @response.content_type
Chris@1464 286 assert_tag 'errors', :child => {
Chris@1464 287 :tag => 'error',
Chris@1464 288 :content => "First name can't be blank"
Chris@1464 289 }
Chris@1464 290 end
Chris@1464 291
Chris@1464 292 test "PUT /users/:id.json with invalid parameters" do
Chris@1464 293 assert_no_difference('User.count') do
Chris@1464 294 put '/users/2.json', {
Chris@1464 295 :user => {
Chris@1464 296 :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
Chris@1464 297 :mail => 'foo'}
Chris@1464 298 },
Chris@1464 299 credentials('admin')
Chris@1464 300 end
Chris@1464 301
Chris@1464 302 assert_response :unprocessable_entity
Chris@1464 303 assert_equal 'application/json', @response.content_type
Chris@1464 304 json = ActiveSupport::JSON.decode(response.body)
Chris@1464 305 assert_kind_of Hash, json
Chris@1464 306 assert json.has_key?('errors')
Chris@1464 307 assert_kind_of Array, json['errors']
Chris@1464 308 end
Chris@1464 309
Chris@1464 310 test "DELETE /users/:id.xml should delete the user" do
Chris@1464 311 assert_difference('User.count', -1) do
Chris@1464 312 delete '/users/2.xml', {}, credentials('admin')
Chris@1464 313 end
Chris@1464 314
Chris@1464 315 assert_response :ok
Chris@1464 316 assert_equal '', @response.body
Chris@1464 317 end
Chris@1464 318
Chris@1464 319 test "DELETE /users/:id.json should delete the user" do
Chris@1464 320 assert_difference('User.count', -1) do
Chris@1464 321 delete '/users/2.json', {}, credentials('admin')
Chris@1464 322 end
Chris@1464 323
Chris@1464 324 assert_response :ok
Chris@1464 325 assert_equal '', @response.body
Chris@119 326 end
Chris@119 327 end