annotate .svn/pristine/58/583f59658b5b4c108e320b78c4ff88cab8b9c15d.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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