annotate test/integration/api_test/users_test.rb @ 904:0a8317a50fa0 redmine-1.1

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