2020-03-13 17:10:38 +01:00
|
|
|
# frozen_string_literal: true
|
2016-04-13 10:43:25 +02:00
|
|
|
|
2023-01-06 17:18:44 +01:00
|
|
|
require 'test_helper'
|
|
|
|
|
2020-03-13 17:10:38 +01:00
|
|
|
class AdminsTest < ActionDispatch::IntegrationTest
|
2016-04-13 10:43:25 +02:00
|
|
|
# Called before every test method runs. Can be used
|
|
|
|
# to set up fixture information.
|
|
|
|
def setup
|
2016-11-23 16:30:19 +01:00
|
|
|
@admin = User.find_by(username: 'admin')
|
2016-04-13 10:43:25 +02:00
|
|
|
login_as(@admin, scope: :user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'create an admin' do
|
|
|
|
post '/api/admins',
|
2020-03-13 17:10:38 +01:00
|
|
|
params: {
|
2018-12-27 14:53:11 +01:00
|
|
|
admin: {
|
|
|
|
username: 'glepower',
|
|
|
|
email: 'gerard.lepower@admins.net',
|
|
|
|
profile_attributes: {
|
|
|
|
first_name: 'Gérard',
|
|
|
|
last_name: 'Lepower',
|
2019-06-04 13:33:00 +02:00
|
|
|
phone: '0547124852'
|
2019-05-29 14:28:14 +02:00
|
|
|
},
|
|
|
|
invoicing_profile_attributes: {
|
2018-12-27 14:53:11 +01:00
|
|
|
address_attributes: {
|
|
|
|
address: '6 Avenue Henri de Bournazel, 19000 Tulle'
|
|
|
|
}
|
2019-06-04 13:33:00 +02:00
|
|
|
},
|
|
|
|
statistic_profile_attributes: {
|
|
|
|
gender: true,
|
|
|
|
birthday: '1999-09-19'
|
2016-04-13 10:43:25 +02:00
|
|
|
}
|
2018-12-27 14:53:11 +01:00
|
|
|
}
|
2016-04-13 10:43:25 +02:00
|
|
|
}.to_json,
|
2020-03-13 17:10:38 +01:00
|
|
|
headers: default_headers
|
2016-04-13 10:43:25 +02:00
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2016-04-13 10:43:25 +02:00
|
|
|
|
|
|
|
# Check the correct admin was created
|
|
|
|
admin = json_response(response.body)
|
|
|
|
user = User.where(id: admin[:admin][:id]).first
|
|
|
|
assert_not_nil user, 'admin was not created in database'
|
|
|
|
|
|
|
|
# Check he's got the admin role
|
|
|
|
assert user.has_role?(:admin), 'admin does not have the admin role'
|
|
|
|
end
|
2018-12-27 14:53:11 +01:00
|
|
|
|
|
|
|
test 'list all admins' do
|
|
|
|
get '/api/admins'
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2018-12-27 14:53:11 +01:00
|
|
|
|
|
|
|
# Check the list items are ok
|
|
|
|
admins = json_response(response.body)
|
|
|
|
assert_equal 1, admins.count, 'not all admins retrieved'
|
|
|
|
assert_equal @admin.id, admins[:admins][0][:id], 'admin id matches'
|
|
|
|
assert_equal @admin.profile.user_avatar.id,
|
|
|
|
admins[:admins][0][:profile_attributes][:user_avatar][:id],
|
|
|
|
'admin avatar does not match'
|
|
|
|
end
|
2023-01-06 17:18:44 +01:00
|
|
|
|
|
|
|
test 'admin cannot delete himself' do
|
|
|
|
delete "/api/admins/#{@admin.id}"
|
|
|
|
|
|
|
|
assert_response :unauthorized
|
|
|
|
end
|
2018-12-27 14:53:11 +01:00
|
|
|
end
|