annotate .svn/pristine/47/475e48920f645a29c9410b5f2003c06d6f0a84d4.svn-base @ 1384:b51b5ae3734c luisf

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