annotate .svn/pristine/45/457635320d7cf0fe410b58dcbd4445d01bddd1f8.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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