Replaced system_agent_limit Setting MiniTest with RSpec.

This commit is contained in:
Thorsten Eckel 2018-06-22 19:42:28 +02:00
parent baa7b97d07
commit 48ff43aaed
3 changed files with 162 additions and 110 deletions

View file

@ -6,4 +6,32 @@ RSpec.describe Role do
let(:new_group_access_instance) { build(:role) } let(:new_group_access_instance) { build(:role) }
include_examples 'HasGroups' include_examples 'HasGroups'
context '#validate_agent_limit_by_attributes' do
context 'agent creation limit surpassing prevention' do
def current_agent_count
User.with_permissions('ticket.agent').count
end
it 'prevents re-activation of Role with agent permission' do
Setting.set('system_agent_limit', current_agent_count)
inactive_agent_role = create(:role,
active: false,
permissions: Permission.where(name: 'ticket.agent'))
create(:user, roles: [inactive_agent_role])
initial_agent_count = current_agent_count
expect do
inactive_agent_role.update!(active: true)
end.to raise_error(Exceptions::UnprocessableEntity)
expect(current_agent_count).to eq(initial_agent_count)
end
end
end
end end

View file

@ -530,4 +530,138 @@ RSpec.describe User do
end end
end end
end end
context 'agent limit' do
def current_agent_count
User.with_permissions('ticket.agent').count
end
let(:agent_role) { Role.lookup(name: 'Agent') }
let(:admin_role) { Role.lookup(name: 'Admin') }
context '#validate_agent_limit_by_role' do
context 'agent creation limit not reached' do
it 'grants agent creation' do
Setting.set('system_agent_limit', current_agent_count + 1)
expect do
create(:agent_user)
end.to change {
current_agent_count
}.by(1)
end
it 'grants role change' do
Setting.set('system_agent_limit', current_agent_count + 1)
future_agent = create(:customer_user)
expect do
future_agent.roles = [agent_role]
end.to change {
current_agent_count
}.by(1)
end
context 'role updates' do
it 'grants update by instances' do
Setting.set('system_agent_limit', current_agent_count + 1)
agent = create(:agent_user)
expect do
agent.roles = [
admin_role,
agent_role
]
agent.save!
end.not_to raise_error
end
it 'grants update by id (Integer)' do
Setting.set('system_agent_limit', current_agent_count + 1)
agent = create(:agent_user)
expect do
agent.role_ids = [
admin_role.id,
agent_role.id
]
agent.save!
end.not_to raise_error
end
it 'grants update by id (String)' do
Setting.set('system_agent_limit', current_agent_count + 1)
agent = create(:agent_user)
expect do
agent.role_ids = [
admin_role.id.to_s,
agent_role.id.to_s
]
agent.save!
end.not_to raise_error
end
end
end
context 'agent creation limit surpassing prevention' do
it 'creation of new agents' do
Setting.set('system_agent_limit', current_agent_count + 2)
create_list(:agent_user, 2)
initial_agent_count = current_agent_count
expect do
create(:agent_user)
end.to raise_error(Exceptions::UnprocessableEntity)
expect(current_agent_count).to eq(initial_agent_count)
end
it 'prevents role change' do
Setting.set('system_agent_limit', current_agent_count)
future_agent = create(:customer_user)
initial_agent_count = current_agent_count
expect do
future_agent.roles = [agent_role]
end.to raise_error(Exceptions::UnprocessableEntity)
expect(current_agent_count).to eq(initial_agent_count)
end
end
end
context '#validate_agent_limit_by_attributes' do
context 'agent creation limit surpassing prevention' do
it 'prevents re-activation of agents' do
Setting.set('system_agent_limit', current_agent_count)
inactive_agent = create(:agent_user, active: false)
initial_agent_count = current_agent_count
expect do
inactive_agent.update!(active: true)
end.to raise_error(Exceptions::UnprocessableEntity)
expect(current_agent_count).to eq(initial_agent_count)
end
end
end
end
end end

View file

@ -1,110 +0,0 @@
require 'test_helper'
class UserValidateAgentLimit < ActiveSupport::TestCase
test 'user_validate_agent_limit' do
UserInfo.current_user_id = 1
agent_max = User.with_permissions('ticket.agent').count + 2
Setting.set('system_agent_limit', agent_max)
role_agent = Role.lookup(name: 'Agent')
role_customer = Role.lookup(name: 'Customer')
user1 = User.create!(
firstname: 'Firstname',
lastname: 'Lastname',
email: 'some-agentlimit-user@example.com',
login: 'some-agentlimit-user@example.com',
roles: [role_agent],
active: true,
)
user2 = User.create!(
firstname: 'Firstname1',
lastname: 'Lastname1',
email: 'some-agentlimit-user-1@example.com',
login: 'some-agentlimit-user-1@example.com',
roles: [role_agent],
active: true,
)
assert_raises(Exceptions::UnprocessableEntity) do
user3 = User.create!(
firstname: 'Firstname2',
lastname: 'Lastname2',
email: 'some-agentlimit-user-2@example.com',
login: 'some-agentlimit-user-2@example.com',
roles: [role_agent],
active: true,
)
end
user3 = User.create!(
firstname: 'Firstname2',
lastname: 'Lastname2',
email: 'some-agentlimit-user-2@example.com',
login: 'some-agentlimit-user-2@example.com',
roles: [role_customer],
active: true,
)
assert_raises(Exceptions::UnprocessableEntity) do
user3.roles = [role_agent]
end
assert_equal(User.with_permissions('ticket.agent').count, agent_max)
Setting.set('system_agent_limit', agent_max + 1)
user3.reload
user3.roles = [role_agent]
user3.save!
user3.active = false
user3.save!
Setting.set('system_agent_limit', agent_max)
# try to activate inactive agent again
assert_raises(Exceptions::UnprocessableEntity) do
user3 = User.find(user3.id)
user3.active = true
user3.save!
end
assert_equal(User.with_permissions('ticket.agent').count, agent_max)
# try to activate inactive role again
role_agent_limit = Role.create!(
name: 'agent-limit-test-invalid-role',
note: 'agent-limit-test-invalid-role Role.',
permissions: Permission.where(name: 'ticket.agent'),
active: false,
)
user3.roles = [role_agent_limit]
user3.active = true
user3.save!
assert_raises(Exceptions::UnprocessableEntity) do
role_agent_limit.active = true
role_agent_limit.save!
end
assert_equal(User.with_permissions('ticket.agent').count, agent_max)
# set roles of agent again
role_admin = Role.lookup(name: 'Admin')
user2.roles = [role_agent, role_admin]
user2.save!
user2.role_ids = [role_admin.id, role_agent_limit.id]
user2.save!
user2.role_ids = [role_admin.id.to_s, role_agent_limit.id.to_s]
user2.save!
user1.destroy!
user2.destroy!
user3.destroy!
role_agent_limit.destroy!
Setting.set('system_agent_limit', nil)
end
end