2017-04-18 07:38:53 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class RoleValidateAgentLimit < ActiveSupport::TestCase
|
2017-11-17 14:39:56 +00:00
|
|
|
test 'role validate agent limit' do
|
2017-04-18 07:38:53 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
agent_max = User.with_permissions('ticket.agent').count
|
2017-04-18 07:38:53 +00:00
|
|
|
UserInfo.current_user_id = 1
|
2017-10-20 11:32:52 +00:00
|
|
|
Setting.set('system_agent_limit', agent_max + 2)
|
2017-04-18 07:38:53 +00:00
|
|
|
|
|
|
|
permission_ticket_agent = Permission.where(name: 'ticket.agent')
|
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
role_agent_limit_success = Role.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'agent-limit-test-success',
|
|
|
|
note: 'agent-limit-test-success Role.',
|
2017-04-18 07:38:53 +00:00
|
|
|
permissions: [],
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2017-04-18 07:38:53 +00:00
|
|
|
)
|
2017-10-20 11:32:52 +00:00
|
|
|
role_agent_limit_fail = Role.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'agent-limit-test-fail',
|
|
|
|
note: 'agent-limit-test-fail Role.',
|
2017-04-18 07:38:53 +00:00
|
|
|
permissions: [],
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2017-04-18 07:38:53 +00:00
|
|
|
)
|
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
user1 = User.create!(
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some-agentlimit-role@example.com',
|
|
|
|
login: 'some-agentlimit-role@example.com',
|
|
|
|
roles: [role_agent_limit_success],
|
|
|
|
active: true,
|
2017-04-18 07:38:53 +00:00
|
|
|
)
|
2017-10-20 11:32:52 +00:00
|
|
|
user2 = User.create!(
|
|
|
|
firstname: 'Firstname1',
|
|
|
|
lastname: 'Lastname1',
|
|
|
|
email: 'some-agentlimit-role-1@example.com',
|
|
|
|
login: 'some-agentlimit-role-1@example.com',
|
|
|
|
roles: [role_agent_limit_success],
|
|
|
|
active: true,
|
2017-04-18 07:38:53 +00:00
|
|
|
)
|
2017-10-20 11:32:52 +00:00
|
|
|
user3 = User.create!(
|
2017-04-18 07:38:53 +00:00
|
|
|
firstname: 'Firstname2',
|
2018-09-12 04:23:48 +00:00
|
|
|
lastname: 'Lastname2',
|
|
|
|
email: 'some-agentlimit-role-2@example.com',
|
|
|
|
login: 'some-agentlimit-role-2@example.com',
|
|
|
|
roles: [role_agent_limit_fail],
|
|
|
|
active: true,
|
|
|
|
)
|
|
|
|
|
|
|
|
role_agent_limit_success.permissions = permission_ticket_agent
|
|
|
|
assert_raises(Exceptions::UnprocessableEntity) do
|
|
|
|
role_agent_limit_fail.permissions = permission_ticket_agent
|
|
|
|
end
|
|
|
|
|
|
|
|
role_agent_limit_fail.active = false
|
|
|
|
role_agent_limit_fail.save!
|
|
|
|
|
|
|
|
role_agent_limit_fail.permissions = permission_ticket_agent
|
|
|
|
|
|
|
|
assert_raises(Exceptions::UnprocessableEntity) do
|
|
|
|
role_agent_limit_fail.active = true
|
|
|
|
role_agent_limit_fail.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
user1.destroy!
|
|
|
|
user2.destroy!
|
|
|
|
user3.destroy!
|
|
|
|
role_agent_limit_success.destroy!
|
|
|
|
role_agent_limit_fail.destroy!
|
|
|
|
Setting.set('system_agent_limit', nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'role validate agent limit as string' do
|
|
|
|
|
|
|
|
agent_max = User.with_permissions('ticket.agent').count
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
Setting.set('system_agent_limit', (agent_max + 2).to_s)
|
|
|
|
|
|
|
|
permission_ticket_agent = Permission.where(name: 'ticket.agent')
|
|
|
|
|
|
|
|
role_agent_limit_success = Role.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'agent-limit-test-success',
|
|
|
|
note: 'agent-limit-test-success Role.',
|
2018-09-12 04:23:48 +00:00
|
|
|
permissions: [],
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2018-09-12 04:23:48 +00:00
|
|
|
)
|
|
|
|
role_agent_limit_fail = Role.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'agent-limit-test-fail',
|
|
|
|
note: 'agent-limit-test-fail Role.',
|
2018-09-12 04:23:48 +00:00
|
|
|
permissions: [],
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2018-09-12 04:23:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
user1 = User.create!(
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some-agentlimit-role@example.com',
|
|
|
|
login: 'some-agentlimit-role@example.com',
|
|
|
|
roles: [role_agent_limit_success],
|
|
|
|
active: true,
|
|
|
|
)
|
|
|
|
user2 = User.create!(
|
|
|
|
firstname: 'Firstname1',
|
|
|
|
lastname: 'Lastname1',
|
|
|
|
email: 'some-agentlimit-role-1@example.com',
|
|
|
|
login: 'some-agentlimit-role-1@example.com',
|
|
|
|
roles: [role_agent_limit_success],
|
|
|
|
active: true,
|
|
|
|
)
|
|
|
|
user3 = User.create!(
|
|
|
|
firstname: 'Firstname2',
|
2017-04-18 07:38:53 +00:00
|
|
|
lastname: 'Lastname2',
|
2017-10-20 11:32:52 +00:00
|
|
|
email: 'some-agentlimit-role-2@example.com',
|
|
|
|
login: 'some-agentlimit-role-2@example.com',
|
2017-04-18 07:38:53 +00:00
|
|
|
roles: [role_agent_limit_fail],
|
2017-10-20 11:32:52 +00:00
|
|
|
active: true,
|
2017-04-18 07:38:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
role_agent_limit_success.permissions = permission_ticket_agent
|
2017-10-01 12:25:52 +00:00
|
|
|
assert_raises(Exceptions::UnprocessableEntity) do
|
2017-04-18 07:38:53 +00:00
|
|
|
role_agent_limit_fail.permissions = permission_ticket_agent
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-04-18 07:38:53 +00:00
|
|
|
|
2017-10-20 11:32:52 +00:00
|
|
|
role_agent_limit_fail.active = false
|
|
|
|
role_agent_limit_fail.save!
|
|
|
|
|
|
|
|
role_agent_limit_fail.permissions = permission_ticket_agent
|
|
|
|
|
|
|
|
assert_raises(Exceptions::UnprocessableEntity) do
|
|
|
|
role_agent_limit_fail.active = true
|
|
|
|
role_agent_limit_fail.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
user1.destroy!
|
|
|
|
user2.destroy!
|
|
|
|
user3.destroy!
|
|
|
|
role_agent_limit_success.destroy!
|
|
|
|
role_agent_limit_fail.destroy!
|
2017-04-18 07:38:53 +00:00
|
|
|
Setting.set('system_agent_limit', nil)
|
|
|
|
end
|
2017-11-17 14:39:56 +00:00
|
|
|
|
|
|
|
test 'role validate agent limit - 1 user 2 ticket.agent roles' do
|
|
|
|
|
2017-12-13 11:17:57 +00:00
|
|
|
current_agent_max = User.with_permissions('ticket.agent').count + 1
|
2017-11-17 14:39:56 +00:00
|
|
|
UserInfo.current_user_id = 1
|
2017-12-13 11:17:57 +00:00
|
|
|
Setting.set('system_agent_limit', current_agent_max)
|
2017-11-17 14:39:56 +00:00
|
|
|
|
|
|
|
permission_ticket_agent = Permission.find_by(name: 'ticket.agent')
|
|
|
|
|
|
|
|
role_agent_limit1 = Role.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'agent-limit-test1',
|
|
|
|
note: 'agent-limit-test1 Role.',
|
2017-11-17 14:39:56 +00:00
|
|
|
permissions: [permission_ticket_agent],
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2017-11-17 14:39:56 +00:00
|
|
|
)
|
|
|
|
role_agent_limit2 = Role.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
name: 'agent-limit-test2',
|
|
|
|
note: 'agent-limit-test2 Role.',
|
2017-11-17 14:39:56 +00:00
|
|
|
permissions: [permission_ticket_agent],
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true,
|
2017-11-17 14:39:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
user1 = User.create!(
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some-agentlimit-role@example.com',
|
|
|
|
login: 'some-agentlimit-role@example.com',
|
|
|
|
roles: [role_agent_limit1, role_agent_limit2],
|
|
|
|
active: true,
|
|
|
|
)
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
user1.roles = Role.where(name: %w[Admin Agent])
|
2017-11-17 14:39:56 +00:00
|
|
|
|
|
|
|
user1.role_ids = [Role.find_by(name: 'Agent').id]
|
|
|
|
|
|
|
|
user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id]
|
|
|
|
|
2017-12-13 11:17:57 +00:00
|
|
|
user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id, role_agent_limit2.id]
|
|
|
|
|
2017-11-17 14:39:56 +00:00
|
|
|
assert_raises(Exceptions::UnprocessableEntity) do
|
|
|
|
user2 = User.create!(
|
|
|
|
firstname: 'Firstname2',
|
|
|
|
lastname: 'Lastname2',
|
|
|
|
email: 'some-agentlimit-role-2@example.com',
|
|
|
|
login: 'some-agentlimit-role-2@example.com',
|
|
|
|
roles: [role_agent_limit1],
|
|
|
|
active: true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-12-13 11:17:57 +00:00
|
|
|
assert_equal(current_agent_max, User.with_permissions('ticket.agent').count)
|
|
|
|
|
|
|
|
current_agent_max = User.with_permissions('ticket.agent').count + 1
|
|
|
|
Setting.set('system_agent_limit', current_agent_max)
|
|
|
|
|
|
|
|
user3 = User.create!(
|
|
|
|
firstname: 'Firstname',
|
|
|
|
lastname: 'Lastname',
|
|
|
|
email: 'some-agentlimit-role-3@example.com',
|
|
|
|
login: 'some-agentlimit-role-3@example.com',
|
|
|
|
role_ids: [Role.find_by(name: 'Agent').id],
|
|
|
|
active: true,
|
|
|
|
)
|
|
|
|
|
2017-11-17 14:39:56 +00:00
|
|
|
role_agent_limit1.destroy!
|
|
|
|
role_agent_limit2.destroy!
|
|
|
|
Setting.set('system_agent_limit', nil)
|
|
|
|
end
|
|
|
|
|
2017-04-18 07:38:53 +00:00
|
|
|
end
|