annotate test/integration/api_test/users_test.rb @ 1176:7d9db6065048 bug_352

Close obsolete branch bug_352
author Chris Cannam
date Wed, 01 Feb 2012 16:09:00 +0000
parents cbce1fd3b1b7
children cbb26bc654de
rev   line source
Chris@119 1 # Redmine - project management software
Chris@119 2 # Copyright (C) 2006-2010 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@119 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@119 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@119 21 fixtures :users
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@119 36
Chris@119 37 assert_tag :tag => 'user',
Chris@119 38 :child => {:tag => 'id', :content => '2'}
Chris@119 39 end
Chris@119 40 end
Chris@119 41
Chris@119 42 context ".json" do
Chris@119 43 should "return requested user" do
Chris@119 44 get '/users/2.json'
Chris@119 45
Chris@119 46 json = ActiveSupport::JSON.decode(response.body)
Chris@119 47 assert_kind_of Hash, json
Chris@119 48 assert_kind_of Hash, json['user']
Chris@119 49 assert_equal 2, json['user']['id']
Chris@119 50 end
Chris@119 51 end
Chris@119 52 end
Chris@119 53
Chris@119 54 context "GET /users/current" do
Chris@119 55 context ".xml" do
Chris@119 56 should "require authentication" do
Chris@119 57 get '/users/current.xml'
Chris@119 58
Chris@119 59 assert_response 401
Chris@119 60 end
Chris@119 61
Chris@119 62 should "return current user" do
Chris@119 63 get '/users/current.xml', {}, :authorization => credentials('jsmith')
Chris@119 64
Chris@119 65 assert_tag :tag => 'user',
Chris@119 66 :child => {:tag => 'id', :content => '2'}
Chris@119 67 end
Chris@119 68 end
Chris@119 69 end
Chris@119 70
Chris@119 71 context "POST /users" do
Chris@119 72 context "with valid parameters" do
Chris@119 73 setup do
Chris@119 74 @parameters = {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret', :mail_notification => 'only_assigned'}}
Chris@119 75 end
Chris@119 76
Chris@119 77 context ".xml" do
Chris@119 78 should_allow_api_authentication(:post,
Chris@119 79 '/users.xml',
Chris@119 80 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net', :password => 'secret'}},
Chris@119 81 {:success_code => :created})
Chris@119 82
Chris@119 83 should "create a user with the attributes" do
Chris@119 84 assert_difference('User.count') do
Chris@119 85 post '/users.xml', @parameters, :authorization => credentials('admin')
Chris@119 86 end
Chris@119 87
Chris@119 88 user = User.first(:order => 'id DESC')
Chris@119 89 assert_equal 'foo', user.login
Chris@119 90 assert_equal 'Firstname', user.firstname
Chris@119 91 assert_equal 'Lastname', user.lastname
Chris@119 92 assert_equal 'foo@example.net', user.mail
Chris@119 93 assert_equal 'only_assigned', user.mail_notification
Chris@119 94 assert !user.admin?
Chris@119 95 assert user.check_password?('secret')
Chris@119 96
Chris@119 97 assert_response :created
Chris@119 98 assert_equal 'application/xml', @response.content_type
Chris@119 99 assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
Chris@119 100 end
Chris@119 101 end
Chris@119 102
Chris@119 103 context ".json" do
Chris@119 104 should_allow_api_authentication(:post,
Chris@119 105 '/users.json',
Chris@119 106 {:user => {:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :mail => 'foo@example.net'}},
Chris@119 107 {:success_code => :created})
Chris@119 108
Chris@119 109 should "create a user with the attributes" do
Chris@119 110 assert_difference('User.count') do
Chris@119 111 post '/users.json', @parameters, :authorization => credentials('admin')
Chris@119 112 end
Chris@119 113
Chris@119 114 user = User.first(:order => 'id DESC')
Chris@119 115 assert_equal 'foo', user.login
Chris@119 116 assert_equal 'Firstname', user.firstname
Chris@119 117 assert_equal 'Lastname', user.lastname
Chris@119 118 assert_equal 'foo@example.net', user.mail
Chris@119 119 assert !user.admin?
Chris@119 120
Chris@119 121 assert_response :created
Chris@119 122 assert_equal 'application/json', @response.content_type
Chris@119 123 json = ActiveSupport::JSON.decode(response.body)
Chris@119 124 assert_kind_of Hash, json
Chris@119 125 assert_kind_of Hash, json['user']
Chris@119 126 assert_equal user.id, json['user']['id']
Chris@119 127 end
Chris@119 128 end
Chris@119 129 end
Chris@119 130
Chris@119 131 context "with invalid parameters" do
Chris@119 132 setup do
Chris@119 133 @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
Chris@119 134 end
Chris@119 135
Chris@119 136 context ".xml" do
Chris@119 137 should "return errors" do
Chris@119 138 assert_no_difference('User.count') do
Chris@119 139 post '/users.xml', @parameters, :authorization => credentials('admin')
Chris@119 140 end
Chris@119 141
Chris@119 142 assert_response :unprocessable_entity
Chris@119 143 assert_equal 'application/xml', @response.content_type
Chris@441 144 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"}
Chris@119 145 end
Chris@119 146 end
Chris@119 147
Chris@119 148 context ".json" do
Chris@119 149 should "return errors" do
Chris@119 150 assert_no_difference('User.count') do
Chris@119 151 post '/users.json', @parameters, :authorization => credentials('admin')
Chris@119 152 end
Chris@119 153
Chris@119 154 assert_response :unprocessable_entity
Chris@119 155 assert_equal 'application/json', @response.content_type
Chris@119 156 json = ActiveSupport::JSON.decode(response.body)
Chris@119 157 assert_kind_of Hash, json
Chris@119 158 assert json.has_key?('errors')
Chris@119 159 assert_kind_of Array, json['errors']
Chris@119 160 end
Chris@119 161 end
Chris@119 162 end
Chris@119 163 end
Chris@119 164
Chris@119 165 context "PUT /users/2" do
Chris@119 166 context "with valid parameters" do
Chris@119 167 setup do
Chris@119 168 @parameters = {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}}
Chris@119 169 end
Chris@119 170
Chris@119 171 context ".xml" do
Chris@119 172 should_allow_api_authentication(:put,
Chris@119 173 '/users/2.xml',
Chris@119 174 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
Chris@119 175 {:success_code => :ok})
Chris@119 176
Chris@119 177 should "update user with the attributes" do
Chris@119 178 assert_no_difference('User.count') do
Chris@119 179 put '/users/2.xml', @parameters, :authorization => credentials('admin')
Chris@119 180 end
Chris@119 181
Chris@119 182 user = User.find(2)
Chris@119 183 assert_equal 'jsmith', user.login
Chris@119 184 assert_equal 'John', user.firstname
Chris@119 185 assert_equal 'Renamed', user.lastname
Chris@119 186 assert_equal 'jsmith@somenet.foo', user.mail
Chris@119 187 assert !user.admin?
Chris@119 188
Chris@119 189 assert_response :ok
Chris@119 190 end
Chris@119 191 end
Chris@119 192
Chris@119 193 context ".json" do
Chris@119 194 should_allow_api_authentication(:put,
Chris@119 195 '/users/2.json',
Chris@119 196 {:user => {:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :mail => 'jsmith@somenet.foo'}},
Chris@119 197 {:success_code => :ok})
Chris@119 198
Chris@119 199 should "update user with the attributes" do
Chris@119 200 assert_no_difference('User.count') do
Chris@119 201 put '/users/2.json', @parameters, :authorization => credentials('admin')
Chris@119 202 end
Chris@119 203
Chris@119 204 user = User.find(2)
Chris@119 205 assert_equal 'jsmith', user.login
Chris@119 206 assert_equal 'John', user.firstname
Chris@119 207 assert_equal 'Renamed', user.lastname
Chris@119 208 assert_equal 'jsmith@somenet.foo', user.mail
Chris@119 209 assert !user.admin?
Chris@119 210
Chris@119 211 assert_response :ok
Chris@119 212 end
Chris@119 213 end
Chris@119 214 end
Chris@119 215
Chris@119 216 context "with invalid parameters" do
Chris@119 217 setup do
Chris@119 218 @parameters = {:user => {:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :mail => 'foo'}}
Chris@119 219 end
Chris@119 220
Chris@119 221 context ".xml" do
Chris@119 222 should "return errors" do
Chris@119 223 assert_no_difference('User.count') do
Chris@119 224 put '/users/2.xml', @parameters, :authorization => credentials('admin')
Chris@119 225 end
Chris@119 226
Chris@119 227 assert_response :unprocessable_entity
Chris@119 228 assert_equal 'application/xml', @response.content_type
Chris@441 229 assert_tag 'errors', :child => {:tag => 'error', :content => "First name can't be blank"}
Chris@119 230 end
Chris@119 231 end
Chris@119 232
Chris@119 233 context ".json" do
Chris@119 234 should "return errors" do
Chris@119 235 assert_no_difference('User.count') do
Chris@119 236 put '/users/2.json', @parameters, :authorization => credentials('admin')
Chris@119 237 end
Chris@119 238
Chris@119 239 assert_response :unprocessable_entity
Chris@119 240 assert_equal 'application/json', @response.content_type
Chris@119 241 json = ActiveSupport::JSON.decode(response.body)
Chris@119 242 assert_kind_of Hash, json
Chris@119 243 assert json.has_key?('errors')
Chris@119 244 assert_kind_of Array, json['errors']
Chris@119 245 end
Chris@119 246 end
Chris@119 247 end
Chris@128 248 end
Chris@119 249
Chris@128 250 context "DELETE /users/2" do
Chris@128 251 context ".xml" do
Chris@128 252 should_allow_api_authentication(:delete,
Chris@128 253 '/users/2.xml',
Chris@128 254 {},
Chris@128 255 {:success_code => :ok})
Chris@128 256
Chris@128 257 should "delete user" do
Chris@128 258 assert_difference('User.count', -1) do
Chris@128 259 delete '/users/2.xml', {}, :authorization => credentials('admin')
Chris@128 260 end
Chris@128 261
Chris@128 262 assert_response :ok
Chris@128 263 end
Chris@128 264 end
Chris@128 265
Chris@128 266 context ".json" do
Chris@128 267 should_allow_api_authentication(:delete,
Chris@128 268 '/users/2.xml',
Chris@128 269 {},
Chris@128 270 {:success_code => :ok})
Chris@119 271
Chris@128 272 should "delete user" do
Chris@128 273 assert_difference('User.count', -1) do
Chris@128 274 delete '/users/2.json', {}, :authorization => credentials('admin')
Chris@119 275 end
Chris@128 276
Chris@128 277 assert_response :ok
Chris@119 278 end
Chris@119 279 end
Chris@119 280 end
Chris@119 281
Chris@119 282 def credentials(user, password=nil)
Chris@119 283 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
Chris@119 284 end
Chris@119 285 end