2019-03-16 15:03:47 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2010-12-03 11:28:44 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2010-12-03 11:28:44 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-31 23:15:39 +00:00
|
|
|
#
|
2010-12-03 11:28:44 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-08-31 23:15:39 +00:00
|
|
|
#
|
2010-12-03 11:28:44 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
2023-01-01 07:13:39 +00:00
|
|
|
require_relative '../../test_helper'
|
2013-01-22 18:26:04 +00:00
|
|
|
|
|
|
|
|
class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
|
2024-03-21 21:12:26 +00:00
|
|
|
fixtures :users, :groups_users, :email_addresses, :members, :member_roles, :roles, :projects
|
2010-12-03 11:28:44 +00:00
|
|
|
|
2014-12-03 20:21:03 +00:00
|
|
|
test "GET /users.xml should return users" do
|
2020-12-23 02:58:49 +00:00
|
|
|
users = User.active.order('login')
|
|
|
|
|
users.last.update(twofa_scheme: 'totp')
|
2022-01-21 00:31:11 +00:00
|
|
|
Redmine::Configuration.with 'avatar_server_url' => 'https://gravatar.com' do
|
|
|
|
|
with_settings :gravatar_enabled => '1', :gravatar_default => 'mm' do
|
|
|
|
|
get '/users.xml', :headers => credentials('admin')
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-12-03 20:21:03 +00:00
|
|
|
|
|
|
|
|
assert_response :success
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/xml', response.media_type
|
2014-12-03 20:21:03 +00:00
|
|
|
assert_select 'users' do
|
2020-07-28 15:11:39 +00:00
|
|
|
assert_select 'user', :count => users.size do |nodeset|
|
|
|
|
|
nodeset.zip(users) do |user_element, user|
|
|
|
|
|
assert_select user_element, 'id', :text => user.id.to_s
|
|
|
|
|
assert_select user_element, 'updated_on', :text => user.updated_on.iso8601
|
2020-12-23 02:58:49 +00:00
|
|
|
assert_select user_element, 'twofa_scheme', :text => user.twofa_scheme.to_s
|
2020-07-28 15:11:39 +00:00
|
|
|
|
|
|
|
|
# No one has changed password.
|
|
|
|
|
assert_select user_element, 'passwd_changed_on', :text => ''
|
2024-05-03 07:31:17 +00:00
|
|
|
assert_select user_element, 'avatar_url', :text => %r|\Ahttps://gravatar.com/avatar/\h{64}\?default=mm|
|
2020-12-23 02:58:49 +00:00
|
|
|
|
|
|
|
|
if user == users.last
|
|
|
|
|
assert_select user_element, 'twofa_scheme', :text => 'totp'
|
|
|
|
|
else
|
|
|
|
|
assert_select user_element, 'twofa_scheme', :text => ''
|
|
|
|
|
end
|
2020-07-28 15:11:39 +00:00
|
|
|
end
|
|
|
|
|
end
|
2014-12-03 20:21:03 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users.json should return users" do
|
2020-12-23 02:58:49 +00:00
|
|
|
users = User.active.order('login')
|
|
|
|
|
users.last.update(twofa_scheme: 'totp')
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users.json', :headers => credentials('admin')
|
2014-12-03 20:21:03 +00:00
|
|
|
|
|
|
|
|
assert_response :success
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/json', response.media_type
|
2014-12-03 20:21:03 +00:00
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
2020-07-28 15:11:39 +00:00
|
|
|
|
|
|
|
|
users = User.active.order('login')
|
|
|
|
|
assert_equal users.size, json['users'].size
|
|
|
|
|
|
|
|
|
|
json['users'].zip(users) do |user_json, user|
|
|
|
|
|
assert_equal user.id, user_json['id']
|
|
|
|
|
assert_equal user.updated_on.iso8601, user_json['updated_on']
|
2024-01-25 16:40:13 +00:00
|
|
|
assert_equal user.status, user_json['status']
|
2020-07-28 15:11:39 +00:00
|
|
|
|
|
|
|
|
# No one has changed password.
|
|
|
|
|
assert_nil user_json['passwd_changed_on']
|
2020-12-23 02:58:49 +00:00
|
|
|
|
|
|
|
|
if user == users.last
|
|
|
|
|
assert_equal 'totp', user_json['twofa_scheme']
|
|
|
|
|
else
|
|
|
|
|
assert_nil user_json['twofa_scheme']
|
|
|
|
|
end
|
2020-07-28 15:11:39 +00:00
|
|
|
end
|
2014-12-03 20:21:03 +00:00
|
|
|
end
|
|
|
|
|
|
2023-10-15 01:42:07 +00:00
|
|
|
test "GET /users.json with legacy filter params" do
|
2023-10-16 14:17:35 +00:00
|
|
|
get '/users.json', headers: credentials('admin'), params: { status: 3 }
|
2023-10-15 01:42:07 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
users = User.where(status: 3)
|
|
|
|
|
assert_equal users.size, json['users'].size
|
|
|
|
|
|
2024-01-25 04:58:00 +00:00
|
|
|
get '/users.json', headers: credentials('admin'), params: { status: '*' }
|
|
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
users = User.logged
|
|
|
|
|
assert_equal users.size, json['users'].size
|
|
|
|
|
|
2023-10-16 14:17:35 +00:00
|
|
|
get '/users.json', headers: credentials('admin'), params: { name: 'jsmith' }
|
2023-10-15 01:42:07 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
assert_equal 1, json['users'].size
|
|
|
|
|
assert_equal 2, json['users'][0]['id']
|
|
|
|
|
|
2023-10-16 14:17:35 +00:00
|
|
|
get '/users.json', headers: credentials('admin'), params: { group_id: '10' }
|
2023-10-15 01:42:07 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
assert_equal 1, json['users'].size
|
|
|
|
|
assert_equal 8, json['users'][0]['id']
|
|
|
|
|
|
|
|
|
|
# there should be an implicit filter for status = 1
|
|
|
|
|
User.where(id: [2, 8]).update_all status: 3
|
|
|
|
|
|
2023-10-16 14:17:35 +00:00
|
|
|
get '/users.json', headers: credentials('admin'), params: { name: 'jsmith' }
|
2023-10-15 01:42:07 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
assert_equal 0, json['users'].size
|
|
|
|
|
|
2023-10-16 14:17:35 +00:00
|
|
|
get '/users.json', headers: credentials('admin'), params: { group_id: '10' }
|
2023-10-15 01:42:07 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
assert_equal 0, json['users'].size
|
|
|
|
|
end
|
|
|
|
|
|
2024-01-31 03:24:44 +00:00
|
|
|
test "GET /users.json with include=auth_source" do
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
user.update(:auth_source_id => 1)
|
|
|
|
|
get '/users.json?include=auth_source', :headers => credentials('admin')
|
|
|
|
|
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
|
|
|
|
|
2024-02-01 11:45:48 +00:00
|
|
|
json['users'].each do |user_json|
|
2024-01-31 03:24:44 +00:00
|
|
|
if user_json['id'] == user.id
|
|
|
|
|
assert_kind_of Hash, user_json['auth_source']
|
|
|
|
|
assert_equal user.auth_source.id, user_json['auth_source']['id']
|
|
|
|
|
assert_equal user.auth_source.name, user_json['auth_source']['name']
|
|
|
|
|
else
|
|
|
|
|
assert_nil user_json['auth_source']
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-01-25 04:58:00 +00:00
|
|
|
test "GET /users.json with short filters" do
|
|
|
|
|
get '/users.json', headers: credentials('admin'), params: { status: "1|3" }
|
|
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert json.key?('users')
|
2024-01-27 01:47:10 +00:00
|
|
|
users = User.where(status: [1, 3])
|
2024-01-25 04:58:00 +00:00
|
|
|
assert_equal users.size, json['users'].size
|
|
|
|
|
end
|
|
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "GET /users/:id.xml should return the user" do
|
2022-01-21 00:31:11 +00:00
|
|
|
Redmine::Configuration.with 'avatar_server_url' => 'https://gravatar.com' do
|
|
|
|
|
with_settings :gravatar_enabled => '1', :gravatar_default => 'robohash' do
|
|
|
|
|
get '/users/2.xml'
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-05-18 08:57:27 +00:00
|
|
|
|
|
|
|
|
assert_response :success
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user id', :text => '2'
|
2020-08-02 05:26:26 +00:00
|
|
|
assert_select 'user updated_on', :text => Time.zone.parse('2006-07-19T20:42:15Z').iso8601
|
2020-07-28 15:11:39 +00:00
|
|
|
assert_select 'user passwd_changed_on', :text => ''
|
2024-05-03 07:31:17 +00:00
|
|
|
assert_select 'user avatar_url', :text => %r|\Ahttps://gravatar.com/avatar/\h{64}\?default=robohash|
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
|
|
|
|
|
2022-05-27 22:14:59 +00:00
|
|
|
test "GET /users/:id.xml should not return avatar_url when not set email address" do
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
user.email_addresses.delete_all
|
|
|
|
|
assert_equal 'jsmith', user.login
|
|
|
|
|
assert_nil user.mail
|
|
|
|
|
|
|
|
|
|
Redmine::Configuration.with 'avatar_server_url' => 'https://gravatar.com' do
|
|
|
|
|
with_settings :gravatar_enabled => '1', :gravatar_default => 'robohash' do
|
|
|
|
|
get '/users/2.xml'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
|
assert_select 'user id', :text => '2'
|
|
|
|
|
assert_select 'user login', :text => 'jsmith'
|
|
|
|
|
assert_select 'user avatar_url', :count => 0
|
|
|
|
|
end
|
|
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "GET /users/:id.json should return the user" do
|
|
|
|
|
get '/users/2.json'
|
2010-12-03 11:28:44 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert_kind_of Hash, json
|
|
|
|
|
assert_kind_of Hash, json['user']
|
|
|
|
|
assert_equal 2, json['user']['id']
|
2020-08-02 05:26:26 +00:00
|
|
|
assert_equal Time.zone.parse('2006-07-19T20:42:15Z').iso8601, json['user']['updated_on']
|
2020-07-28 15:11:39 +00:00
|
|
|
assert_nil json['user']['passwd_changed_on']
|
2020-12-23 02:58:49 +00:00
|
|
|
assert_nil json['user']['twofa_scheme']
|
2024-01-25 16:13:08 +00:00
|
|
|
assert_nil json['user']['auth_source']
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "GET /users/:id.xml with include=memberships should include memberships" do
|
|
|
|
|
get '/users/2.xml?include=memberships'
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_response :success
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user memberships', 1
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "GET /users/:id.json with include=memberships should include memberships" do
|
|
|
|
|
get '/users/2.json?include=memberships'
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert_kind_of Array, json['user']['memberships']
|
|
|
|
|
assert_equal [{
|
|
|
|
|
"id"=>1,
|
|
|
|
|
"project"=>{"name"=>"eCookbook", "id"=>1},
|
|
|
|
|
"roles"=>[{"name"=>"Manager", "id"=>1}]
|
|
|
|
|
}], json['user']['memberships']
|
|
|
|
|
end
|
|
|
|
|
|
2024-01-25 16:13:08 +00:00
|
|
|
test "GET /users/:id.json with include=auth_source should include auth_source for administrators" do
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
user.update(:auth_source_id => 1)
|
|
|
|
|
get '/users/2.json?include=auth_source', :headers => credentials('admin')
|
|
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
|
|
|
|
|
assert_equal user.auth_source.id, json['user']['auth_source']['id']
|
|
|
|
|
assert_equal user.auth_source.name, json['user']['auth_source']['name']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/:id.json without include=auth_source should not include auth_source" do
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
user.update(:auth_source_id => 1)
|
|
|
|
|
get '/users/2.json', :headers => credentials('admin')
|
|
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
|
assert_nil json['user']['auth_source']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/:id.json should not include auth_source for standard user" do
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
user.update(:auth_source_id => 1)
|
|
|
|
|
get '/users/2.json?include=auth_source', :headers => credentials('jsmith')
|
|
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
|
|
|
|
|
assert_equal user.id, json['user']['id']
|
|
|
|
|
assert_nil json['user']['auth_source']
|
|
|
|
|
end
|
|
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "GET /users/current.xml should require authentication" do
|
|
|
|
|
get '/users/current.xml'
|
|
|
|
|
|
2024-05-18 05:56:55 +00:00
|
|
|
assert_response :unauthorized
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/current.xml should return current user" do
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users/current.xml', :headers => credentials('jsmith')
|
2013-05-18 08:57:27 +00:00
|
|
|
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user id', :text => '2'
|
2010-12-20 17:45:09 +00:00
|
|
|
end
|
2010-12-03 11:28:44 +00:00
|
|
|
|
2018-12-12 00:10:09 +00:00
|
|
|
test "GET /users/:id should return login for visible user" do
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users/3.xml', :headers => credentials('jsmith')
|
2013-02-18 17:24:54 +00:00
|
|
|
assert_response :success
|
2018-12-12 00:10:09 +00:00
|
|
|
assert_select 'user login', :text => 'dlopper'
|
2013-02-18 17:24:54 +00:00
|
|
|
end
|
|
|
|
|
|
2013-03-01 16:32:30 +00:00
|
|
|
test "GET /users/:id should not return api_key for other user" do
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users/3.xml', :headers => credentials('jsmith')
|
2013-03-01 16:32:30 +00:00
|
|
|
assert_response :success
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user api_key', 0
|
2013-03-01 16:32:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/:id should return api_key for current user" do
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users/2.xml', :headers => credentials('jsmith')
|
2013-03-01 16:32:30 +00:00
|
|
|
assert_response :success
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user api_key', :text => User.find(2).api_key
|
2013-03-01 16:32:30 +00:00
|
|
|
end
|
|
|
|
|
|
2013-05-05 08:19:17 +00:00
|
|
|
test "GET /users/:id should not return status for standard user" do
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users/3.xml', :headers => credentials('jsmith')
|
2013-05-05 08:19:17 +00:00
|
|
|
assert_response :success
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user status', 0
|
2013-05-05 08:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/:id should return status for administrators" do
|
2017-06-01 18:17:27 +00:00
|
|
|
get '/users/2.xml', :headers => credentials('admin')
|
2013-05-05 08:19:17 +00:00
|
|
|
assert_response :success
|
2020-12-31 00:44:39 +00:00
|
|
|
assert_select 'user status', :text => User.find(2).status.to_s
|
2013-05-05 08:19:17 +00:00
|
|
|
end
|
|
|
|
|
|
2018-09-20 14:54:38 +00:00
|
|
|
test "GET /users/:id should return admin status for current user" do
|
|
|
|
|
get '/users/2.xml', :headers => credentials('jsmith')
|
|
|
|
|
assert_response :success
|
|
|
|
|
assert_select 'user admin', :text => 'false'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/:id should not return admin status for other user" do
|
|
|
|
|
get '/users/3.xml', :headers => credentials('jsmith')
|
|
|
|
|
assert_response :success
|
|
|
|
|
assert_select 'user admin', 0
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-23 03:47:45 +00:00
|
|
|
test "GET /users/:id should not return twofa_scheme for standard user" do
|
2022-02-25 07:57:37 +00:00
|
|
|
# User and password authentication is disabled when twofa is enabled
|
|
|
|
|
# Use token authentication
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
token = Token.create!(:user => user, :action => 'api')
|
|
|
|
|
user.update(twofa_scheme: 'totp')
|
|
|
|
|
|
|
|
|
|
get '/users/3.xml', :headers => credentials(token.value, 'X')
|
2020-12-23 03:47:45 +00:00
|
|
|
assert_response :success
|
|
|
|
|
assert_select 'twofa_scheme', 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "GET /users/:id should return twofa_scheme for administrators" do
|
|
|
|
|
User.find(2).update(twofa_scheme: 'totp')
|
|
|
|
|
get '/users/2.xml', :headers => credentials('admin')
|
|
|
|
|
assert_response :success
|
|
|
|
|
assert_select 'twofa_scheme', :text => 'totp'
|
|
|
|
|
end
|
|
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "POST /users.xml with valid parameters should create the user" do
|
|
|
|
|
assert_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
post(
|
|
|
|
|
'/users.xml',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
|
|
|
|
|
:mail => 'foo@example.net', :password => 'secret123',
|
|
|
|
|
:mail_notification => 'only_assigned'
|
|
|
|
|
}
|
2013-05-18 08:57:27 +00:00
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
2014-01-10 11:24:59 +00:00
|
|
|
user = User.order('id DESC').first
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_equal 'foo', user.login
|
|
|
|
|
assert_equal 'Firstname', user.firstname
|
|
|
|
|
assert_equal 'Lastname', user.lastname
|
|
|
|
|
assert_equal 'foo@example.net', user.mail
|
|
|
|
|
assert_equal 'only_assigned', user.mail_notification
|
|
|
|
|
assert !user.admin?
|
|
|
|
|
assert user.check_password?('secret123')
|
|
|
|
|
|
|
|
|
|
assert_response :created
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/xml', @response.media_type
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'user id', :text => user.id.to_s
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
2018-05-06 08:31:23 +00:00
|
|
|
test "POST /users.xml with generate_password should generate password" do
|
|
|
|
|
assert_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
post(
|
|
|
|
|
'/users.xml',
|
2018-05-06 08:31:23 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
|
|
|
|
|
:mail => 'foo@example.net', :generate_password => 'true'
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2018-05-06 08:31:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
user = User.order('id DESC').first
|
|
|
|
|
assert user.hashed_password.present?
|
|
|
|
|
end
|
|
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "POST /users.json with valid parameters should create the user" do
|
|
|
|
|
assert_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
post(
|
|
|
|
|
'/users.json',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
|
|
|
|
|
:mail => 'foo@example.net', :password => 'secret123',
|
|
|
|
|
:mail_notification => 'only_assigned'
|
|
|
|
|
}
|
2013-05-18 08:57:27 +00:00
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
2014-01-10 11:24:59 +00:00
|
|
|
user = User.order('id DESC').first
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_equal 'foo', user.login
|
|
|
|
|
assert_equal 'Firstname', user.firstname
|
|
|
|
|
assert_equal 'Lastname', user.lastname
|
|
|
|
|
assert_equal 'foo@example.net', user.mail
|
|
|
|
|
assert !user.admin?
|
|
|
|
|
|
|
|
|
|
assert_response :created
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/json', @response.media_type
|
2013-05-18 08:57:27 +00:00
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert_kind_of Hash, json
|
|
|
|
|
assert_kind_of Hash, json['user']
|
|
|
|
|
assert_equal user.id, json['user']['id']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "POST /users.xml with with invalid parameters should return errors" do
|
|
|
|
|
assert_no_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
post(
|
|
|
|
|
'/users.xml',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user =>{
|
|
|
|
|
:login => 'foo', :lastname => 'Lastname', :mail => 'foo'
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
assert_response :unprocessable_entity
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/xml', @response.media_type
|
2014-12-21 12:39:48 +00:00
|
|
|
assert_select 'errors error', :text => "First name cannot be blank"
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "POST /users.json with with invalid parameters should return errors" do
|
|
|
|
|
assert_no_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
post(
|
|
|
|
|
'/users.json',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'foo', :lastname => 'Lastname', :mail => 'foo'
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_response :unprocessable_entity
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/json', @response.media_type
|
2013-05-18 08:57:27 +00:00
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert_kind_of Hash, json
|
|
|
|
|
assert json.has_key?('errors')
|
|
|
|
|
assert_kind_of Array, json['errors']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "PUT /users/:id.xml with valid parameters should update the user" do
|
|
|
|
|
assert_no_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
put(
|
|
|
|
|
'/users/2.xml',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
|
|
|
|
|
:mail => 'jsmith@somenet.foo'
|
|
|
|
|
}
|
2013-05-18 08:57:27 +00:00
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
2013-05-18 08:57:27 +00:00
|
|
|
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
assert_equal 'jsmith', user.login
|
|
|
|
|
assert_equal 'John', user.firstname
|
|
|
|
|
assert_equal 'Renamed', user.lastname
|
|
|
|
|
assert_equal 'jsmith@somenet.foo', user.mail
|
|
|
|
|
assert !user.admin?
|
|
|
|
|
|
2019-02-05 08:33:29 +00:00
|
|
|
assert_response :no_content
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_equal '', @response.body
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
|
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "PUT /users/:id.json with valid parameters should update the user" do
|
|
|
|
|
assert_no_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
put(
|
|
|
|
|
'/users/2.json',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
|
|
|
|
|
:mail => 'jsmith@somenet.foo'
|
|
|
|
|
}
|
2013-05-18 08:57:27 +00:00
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
user = User.find(2)
|
|
|
|
|
assert_equal 'jsmith', user.login
|
|
|
|
|
assert_equal 'John', user.firstname
|
|
|
|
|
assert_equal 'Renamed', user.lastname
|
|
|
|
|
assert_equal 'jsmith@somenet.foo', user.mail
|
|
|
|
|
assert !user.admin?
|
|
|
|
|
|
2019-02-05 08:33:29 +00:00
|
|
|
assert_response :no_content
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_equal '', @response.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "PUT /users/:id.xml with invalid parameters" do
|
|
|
|
|
assert_no_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
put(
|
|
|
|
|
'/users/2.xml',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'jsmith', :firstname => '', :lastname => 'Lastname',
|
|
|
|
|
:mail => 'foo'
|
|
|
|
|
}
|
2013-05-18 08:57:27 +00:00
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
2013-05-18 08:57:27 +00:00
|
|
|
|
|
|
|
|
assert_response :unprocessable_entity
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/xml', @response.media_type
|
2014-12-21 12:39:48 +00:00
|
|
|
assert_select 'errors error', :text => "First name cannot be blank"
|
2011-01-16 15:23:11 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "PUT /users/:id.json with invalid parameters" do
|
|
|
|
|
assert_no_difference('User.count') do
|
2019-11-08 12:34:40 +00:00
|
|
|
put(
|
|
|
|
|
'/users/2.json',
|
2017-06-01 18:17:27 +00:00
|
|
|
:params => {
|
|
|
|
|
:user => {
|
|
|
|
|
:login => 'jsmith', :firstname => '', :lastname => 'Lastname',
|
|
|
|
|
:mail => 'foo'
|
|
|
|
|
}
|
2013-05-18 08:57:27 +00:00
|
|
|
},
|
2019-11-08 12:34:40 +00:00
|
|
|
:headers => credentials('admin'))
|
2011-01-16 15:23:11 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_response :unprocessable_entity
|
2020-01-27 03:54:25 +00:00
|
|
|
assert_equal 'application/json', @response.media_type
|
2013-05-18 08:57:27 +00:00
|
|
|
json = ActiveSupport::JSON.decode(response.body)
|
|
|
|
|
assert_kind_of Hash, json
|
|
|
|
|
assert json.has_key?('errors')
|
|
|
|
|
assert_kind_of Array, json['errors']
|
|
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2013-05-18 08:57:27 +00:00
|
|
|
test "DELETE /users/:id.xml should delete the user" do
|
|
|
|
|
assert_difference('User.count', -1) do
|
2017-06-01 18:17:27 +00:00
|
|
|
delete '/users/2.xml', :headers => credentials('admin')
|
2013-05-18 08:57:27 +00:00
|
|
|
end
|
2011-08-31 23:15:39 +00:00
|
|
|
|
2019-02-05 08:33:29 +00:00
|
|
|
assert_response :no_content
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_equal '', @response.body
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
test "DELETE /users/:id.json should delete the user" do
|
|
|
|
|
assert_difference('User.count', -1) do
|
2017-06-01 18:17:27 +00:00
|
|
|
delete '/users/2.json', :headers => credentials('admin')
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
2013-05-18 08:57:27 +00:00
|
|
|
|
2019-02-05 08:33:29 +00:00
|
|
|
assert_response :no_content
|
2013-05-18 08:57:27 +00:00
|
|
|
assert_equal '', @response.body
|
2010-12-03 11:28:44 +00:00
|
|
|
end
|
|
|
|
|
end
|