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