2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-05 02:41:40 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 05:49:53 +02:00
|
|
|
RSpec.describe UserRole do
|
2022-07-05 02:41:40 +02:00
|
|
|
subject { described_class.create(name: 'Foo', position: 1) }
|
|
|
|
|
|
|
|
describe '#can?' do
|
|
|
|
context 'with a single flag' do
|
|
|
|
it 'returns true if any of them are present' do
|
2024-03-13 16:42:39 +01:00
|
|
|
subject.permissions = described_class::FLAGS[:manage_reports]
|
2022-07-05 02:41:40 +02:00
|
|
|
expect(subject.can?(:manage_reports)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if it is not set' do
|
|
|
|
expect(subject.can?(:manage_reports)).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple flags' do
|
|
|
|
it 'returns true if any of them are present' do
|
2024-03-13 16:42:39 +01:00
|
|
|
subject.permissions = described_class::FLAGS[:manage_users]
|
2022-07-05 02:41:40 +02:00
|
|
|
expect(subject.can?(:manage_reports, :manage_users)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if none of them are present' do
|
|
|
|
expect(subject.can?(:manage_reports, :manage_users)).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an unknown flag' do
|
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject.can?(:foo) }.to raise_error ArgumentError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#overrides?' do
|
|
|
|
it 'returns true if other role has lower position' do
|
|
|
|
expect(subject.overrides?(described_class.new(position: subject.position - 1))).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if other role is nil' do
|
|
|
|
expect(subject.overrides?(nil)).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if other role has higher position' do
|
|
|
|
expect(subject.overrides?(described_class.new(position: subject.position + 1))).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#permissions_as_keys' do
|
|
|
|
before do
|
2024-03-13 16:42:39 +01:00
|
|
|
subject.permissions = described_class::FLAGS[:invite_users] | described_class::FLAGS[:view_dashboard] | described_class::FLAGS[:manage_reports]
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an array' do
|
|
|
|
expect(subject.permissions_as_keys).to match_array %w(invite_users view_dashboard manage_reports)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#permissions_as_keys=' do
|
2023-11-07 10:11:04 +01:00
|
|
|
let(:input) { nil }
|
2022-07-05 02:41:40 +02:00
|
|
|
|
|
|
|
before do
|
|
|
|
subject.permissions_as_keys = input
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a single value' do
|
|
|
|
let(:input) { %w(manage_users) }
|
|
|
|
|
|
|
|
it 'sets permission flags' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.permissions).to eq described_class::FLAGS[:manage_users]
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple values' do
|
|
|
|
let(:input) { %w(manage_users manage_reports) }
|
|
|
|
|
|
|
|
it 'sets permission flags' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.permissions).to eq described_class::FLAGS[:manage_users] | described_class::FLAGS[:manage_reports]
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an unknown value' do
|
|
|
|
let(:input) { %w(foo) }
|
|
|
|
|
|
|
|
it 'does not set permission flags' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.permissions).to eq described_class::Flags::NONE
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#computed_permissions' do
|
|
|
|
context 'when the role is nobody' do
|
2023-07-12 09:49:33 +02:00
|
|
|
subject { described_class.nobody }
|
2022-07-05 02:41:40 +02:00
|
|
|
|
|
|
|
it 'returns none' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.computed_permissions).to eq described_class::Flags::NONE
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the role is everyone' do
|
2023-07-12 09:49:33 +02:00
|
|
|
subject { described_class.everyone }
|
2022-07-05 02:41:40 +02:00
|
|
|
|
|
|
|
it 'returns permissions' do
|
|
|
|
expect(subject.computed_permissions).to eq subject.permissions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when role has the administrator flag' do
|
|
|
|
before do
|
2024-03-13 16:42:39 +01:00
|
|
|
subject.permissions = described_class::FLAGS[:administrator]
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all permissions' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.computed_permissions).to eq described_class::Flags::ALL
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-06 15:51:42 +02:00
|
|
|
it 'returns permissions combined with the everyone role' do
|
|
|
|
expect(subject.computed_permissions).to eq described_class.everyone.permissions
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.everyone' do
|
|
|
|
subject { described_class.everyone }
|
|
|
|
|
|
|
|
it 'returns a role' do
|
2023-02-18 03:24:16 +01:00
|
|
|
expect(subject).to be_a(described_class)
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is identified as the everyone role' do
|
|
|
|
expect(subject.everyone?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has default permissions' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.permissions).to eq described_class::FLAGS[:invite_users]
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has negative position' do
|
2024-03-01 12:05:24 +01:00
|
|
|
expect(subject.position).to eq(described_class::NOBODY_POSITION)
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.nobody' do
|
|
|
|
subject { described_class.nobody }
|
|
|
|
|
|
|
|
it 'returns a role' do
|
2023-02-18 03:24:16 +01:00
|
|
|
expect(subject).to be_a(described_class)
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is identified as the nobody role' do
|
|
|
|
expect(subject.nobody?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has no permissions' do
|
2024-03-13 16:42:39 +01:00
|
|
|
expect(subject.permissions).to eq described_class::Flags::NONE
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'has negative position' do
|
2024-03-01 12:05:24 +01:00
|
|
|
expect(subject.position).to eq(described_class::NOBODY_POSITION)
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#everyone?' do
|
2024-02-19 12:09:43 +01:00
|
|
|
it 'returns true when id matches the everyone id' do
|
|
|
|
subject.id = described_class::EVERYONE_ROLE_ID
|
2022-07-05 02:41:40 +02:00
|
|
|
expect(subject.everyone?).to be true
|
|
|
|
end
|
|
|
|
|
2024-02-19 12:09:43 +01:00
|
|
|
it 'returns false when id does not match the everyone id' do
|
2022-07-05 02:41:40 +02:00
|
|
|
subject.id = 123
|
|
|
|
expect(subject.everyone?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#nobody?' do
|
|
|
|
it 'returns true when id is nil' do
|
|
|
|
subject.id = nil
|
|
|
|
expect(subject.nobody?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when id is not nil' do
|
|
|
|
subject.id = 123
|
|
|
|
expect(subject.nobody?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|