2018-12-13 09:10:32 +00:00
|
|
|
RSpec.shared_examples 'HasGroups' do |group_access_factory:|
|
2017-06-16 20:43:09 +00:00
|
|
|
context 'group' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject { create(group_access_factory) }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2017-06-16 20:43:09 +00:00
|
|
|
let(:group_full) { create(:group) }
|
|
|
|
let(:group_read) { create(:group) }
|
|
|
|
let(:group_inactive) { create(:group, active: false) }
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '.group_through_identifier' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to group_through_identifier' do
|
|
|
|
expect(described_class).to respond_to(:group_through_identifier)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Symbol as identifier' do
|
|
|
|
expect(described_class.group_through_identifier).to be_a(Symbol)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'instance responds to group_through_identifier method' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(described_class.group_through_identifier)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '.group_through' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to group_through' do
|
|
|
|
expect(described_class).to respond_to(:group_through)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the Reflection instance of the has_many :through relation' do
|
|
|
|
expect(described_class.group_through).to be_a(ActiveRecord::Reflection::HasManyReflection)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#groups' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to groups' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(:groups)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#groups.access' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to groups.access' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.groups).to respond_to(:access)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'result' do
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => 'read',
|
2018-01-12 12:44:02 +00:00
|
|
|
group_inactive.name => 'change',
|
2017-06-16 20:43:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all related Groups' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.groups.access.size).to eq(3)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds join table attribute(s like) access' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.groups.access.first).to respond_to(:access)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters for given access parameter' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.groups.access('read')).to include(group_read)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters for given access list parameter' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.groups.access('read', 'change')).to include(group_read, group_inactive)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'always includes full access groups' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.groups.access('read')).to include(group_full)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#group_access?' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
it 'responds to group_access?' do
|
|
|
|
expect(subject).to respond_to(:group_access?)
|
|
|
|
end
|
|
|
|
|
2017-06-16 20:43:09 +00:00
|
|
|
context 'Group ID parameter' do
|
|
|
|
include_examples '#group_access? call' do
|
|
|
|
let(:group_parameter) { group_read.id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Group parameter' do
|
|
|
|
include_examples '#group_access? call' do
|
|
|
|
let(:group_parameter) { group_read }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents inactive Group' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_inactive.name => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_access?(group_inactive.id, 'read')).to be false
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2017-06-19 08:16:46 +00:00
|
|
|
|
|
|
|
it 'prevents inactive instances' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.update!(active: false)
|
|
|
|
|
|
|
|
subject.group_names_access_map = {
|
2017-06-19 08:16:46 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_access?(group_read.id, 'read')).to be false
|
2017-06-19 08:16:46 +00:00
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#group_ids_access' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
it 'responds to group_ids_access' do
|
|
|
|
expect(subject).to respond_to(:group_ids_access)
|
|
|
|
end
|
|
|
|
|
2017-06-16 20:43:09 +00:00
|
|
|
it 'lists only active Group IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
group_inactive.name => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.group_ids_access('read')
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result).not_to include(group_inactive.id)
|
|
|
|
end
|
|
|
|
|
2017-06-19 08:16:46 +00:00
|
|
|
it "doesn't list for inactive instances" do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.update!(active: false)
|
|
|
|
|
|
|
|
subject.group_names_access_map = {
|
2017-06-19 08:16:46 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_ids_access('read')).to be_empty
|
2017-06-19 08:16:46 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 20:43:09 +00:00
|
|
|
context 'single access' do
|
|
|
|
|
|
|
|
it 'lists access Group IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.group_ids_access('read')
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result).to include(group_read.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't list for no access" do
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.group_ids_access('change')
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result).not_to include(group_read.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'access list' do
|
|
|
|
|
|
|
|
it 'lists access Group IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.group_ids_access(%w[read change])
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result).to include(group_read.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't list for no access" do
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.group_ids_access(%w[change create])
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result).not_to include(group_read.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#groups_access' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to groups_access' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(:groups_access)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'wraps #group_ids_access' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to receive(:group_ids_access)
|
|
|
|
subject.groups_access('read')
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns Groups' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.groups_access('read')
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result).to include(group_read)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#group_names_access_map=' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to group_names_access_map=' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(:group_names_access_map=)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
context 'existing instance' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'stores Hash with String values' do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(2)
|
|
|
|
end
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
it 'stores Hash with Array<String> values' do
|
2017-06-16 20:43:09 +00:00
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_full.name => 'full',
|
2018-02-15 18:01:39 +00:00
|
|
|
group_read.name => %w[read change],
|
2017-06-16 20:43:09 +00:00
|
|
|
}
|
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(3)
|
|
|
|
end
|
2018-02-19 19:48:09 +00:00
|
|
|
|
|
|
|
it 'allows empty Hash value' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2018-02-19 19:48:09 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => %w[read change],
|
|
|
|
}
|
|
|
|
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {}
|
2018-02-19 19:48:09 +00:00
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(-3)
|
|
|
|
end
|
2018-06-25 10:35:57 +00:00
|
|
|
|
|
|
|
it 'prevents having full and other privilege at the same time' do
|
|
|
|
|
|
|
|
invalid_combination = %w[full read change]
|
|
|
|
exception = ActiveRecord::RecordInvalid
|
|
|
|
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2018-06-25 10:35:57 +00:00
|
|
|
group_full.name => invalid_combination,
|
|
|
|
}
|
|
|
|
end.to raise_error(exception)
|
|
|
|
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2018-06-25 10:35:57 +00:00
|
|
|
group_full.name => invalid_combination.reverse,
|
|
|
|
}
|
|
|
|
end.to raise_error(exception)
|
|
|
|
end
|
2017-06-20 15:13:42 +00:00
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
context 'new instance' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject { build(group_access_factory) }
|
2017-06-20 15:13:42 +00:00
|
|
|
|
|
|
|
it "doesn't store directly" do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-20 15:13:42 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => 'read',
|
2017-06-16 20:43:09 +00:00
|
|
|
}
|
2017-06-20 15:13:42 +00:00
|
|
|
end.not_to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}
|
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
it 'stores after save' do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-20 15:13:42 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.save
|
2017-06-20 15:13:42 +00:00
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(2)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2018-02-19 19:48:09 +00:00
|
|
|
|
|
|
|
it 'allows empty Hash value' do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {}
|
2018-02-19 19:48:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.save
|
2018-02-19 19:48:09 +00:00
|
|
|
end.not_to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}
|
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#group_names_access_map' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to group_names_access_map' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(:group_names_access_map)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns instance Group name => access relations as Hash' do
|
|
|
|
expected = {
|
|
|
|
group_full.name => ['full'],
|
|
|
|
group_read.name => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = expected
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_names_access_map).to eq(expected)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2017-06-19 08:16:46 +00:00
|
|
|
|
|
|
|
it "doesn't map for inactive instances" do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.update!(active: false)
|
|
|
|
|
|
|
|
subject.group_names_access_map = {
|
2017-06-19 08:16:46 +00:00
|
|
|
group_full.name => ['full'],
|
|
|
|
group_read.name => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_names_access_map).to be_empty
|
2017-06-19 08:16:46 +00:00
|
|
|
end
|
2018-02-19 19:48:09 +00:00
|
|
|
|
|
|
|
it 'returns empty map if none is stored' do
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2018-02-19 19:48:09 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {}
|
2018-02-19 19:48:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_names_access_map).to be_blank
|
2018-02-19 19:48:09 +00:00
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#group_ids_access_map=' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to group_ids_access_map=' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(:group_ids_access_map=)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
context 'existing instance' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'stores Hash with String values' do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_full.id => 'full',
|
|
|
|
group_read.id => 'read',
|
|
|
|
}
|
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(2)
|
|
|
|
end
|
|
|
|
|
2018-02-19 19:48:09 +00:00
|
|
|
it 'stores Hash with Array<String> values' do
|
2017-06-16 20:43:09 +00:00
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_full.id => 'full',
|
2018-02-15 18:01:39 +00:00
|
|
|
group_read.id => %w[read change],
|
2017-06-16 20:43:09 +00:00
|
|
|
}
|
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(3)
|
|
|
|
end
|
2018-02-19 19:48:09 +00:00
|
|
|
|
|
|
|
it 'allows empty Hash value' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {
|
2018-02-19 19:48:09 +00:00
|
|
|
group_full.id => 'full',
|
|
|
|
group_read.id => %w[read change],
|
|
|
|
}
|
|
|
|
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {}
|
2018-02-19 19:48:09 +00:00
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(-3)
|
|
|
|
end
|
2017-06-20 15:13:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'new instance' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject { build(group_access_factory) }
|
2017-06-20 15:13:42 +00:00
|
|
|
|
|
|
|
it "doesn't store directly" do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {
|
2017-06-20 15:13:42 +00:00
|
|
|
group_full.id => 'full',
|
|
|
|
group_read.id => 'read',
|
|
|
|
}
|
|
|
|
end.not_to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}
|
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
it 'stores after save' do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {
|
2017-06-20 15:13:42 +00:00
|
|
|
group_full.id => 'full',
|
|
|
|
group_read.id => 'read',
|
2017-06-16 20:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.save
|
2017-06-20 15:13:42 +00:00
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(2)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2018-02-19 19:48:09 +00:00
|
|
|
|
|
|
|
it 'allows empty Hash value' do
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {}
|
2018-02-19 19:48:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.save
|
2018-02-19 19:48:09 +00:00
|
|
|
end.not_to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}
|
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#group_ids_access_map' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'responds to group_ids_access_map' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject).to respond_to(:group_ids_access_map)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns instance Group ID => access relations as Hash' do
|
|
|
|
expected = {
|
|
|
|
group_full.id => ['full'],
|
|
|
|
group_read.id => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = expected
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_ids_access_map).to eq(expected)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
2017-06-19 08:16:46 +00:00
|
|
|
|
|
|
|
it "doesn't map for inactive instances" do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.update!(active: false)
|
|
|
|
|
|
|
|
subject.group_ids_access_map = {
|
2017-06-19 08:16:46 +00:00
|
|
|
group_full.id => ['full'],
|
|
|
|
group_read.id => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_ids_access_map).to be_empty
|
2017-06-19 08:16:46 +00:00
|
|
|
end
|
2018-02-19 19:48:09 +00:00
|
|
|
|
|
|
|
it 'returns empty map if none is stored' do
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {
|
2018-02-19 19:48:09 +00:00
|
|
|
group_full.id => 'full',
|
|
|
|
group_read.id => 'read',
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = {}
|
2018-02-19 19:48:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_ids_access_map).to be_blank
|
2018-02-19 19:48:09 +00:00
|
|
|
end
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#associations_from_param' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'handles group_ids parameter as group_ids_access_map' do
|
|
|
|
expected = {
|
|
|
|
group_full.id => ['full'],
|
|
|
|
group_read.id => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.associations_from_param(group_ids: expected)
|
|
|
|
expect(subject.group_ids_access_map).to eq(expected)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles groups parameter as group_names_access_map' do
|
|
|
|
expected = {
|
|
|
|
group_full.name => ['full'],
|
|
|
|
group_read.name => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.associations_from_param(groups: expected)
|
|
|
|
expect(subject.group_names_access_map).to eq(expected)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#attributes_with_association_ids' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'includes group_ids as group_ids_access_map' do
|
|
|
|
expected = {
|
|
|
|
group_full.id => ['full'],
|
|
|
|
group_read.id => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = expected
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.attributes_with_association_ids
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result['group_ids']).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '#attributes_with_association_names' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
|
|
|
it 'includes group_ids as group_ids_access_map' do
|
|
|
|
expected = {
|
|
|
|
group_full.id => ['full'],
|
|
|
|
group_read.id => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_ids_access_map = expected
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.attributes_with_association_names
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result['group_ids']).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes groups as group_names_access_map' do
|
|
|
|
expected = {
|
|
|
|
group_full.name => ['full'],
|
|
|
|
group_read.name => ['read'],
|
|
|
|
}
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = expected
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
result = subject.attributes_with_association_names
|
2017-06-16 20:43:09 +00:00
|
|
|
expect(result['groups']).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '.group_access' do
|
2017-06-20 15:13:42 +00:00
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
before do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-04-15 01:41:17 +00:00
|
|
|
it 'responds to group_access' do
|
|
|
|
expect(described_class).to respond_to(:group_access)
|
|
|
|
end
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
it 'lists only active instances' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.update!(active: false)
|
|
|
|
|
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
result = described_class.group_access(group_read.id, 'read')
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(result).not_to include(subject)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'Group ID parameter' do
|
2017-06-20 15:13:42 +00:00
|
|
|
include_examples '.group_access call' do
|
2017-06-16 20:43:09 +00:00
|
|
|
let(:group_parameter) { group_read.id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Group parameter' do
|
2017-06-20 15:13:42 +00:00
|
|
|
include_examples '.group_access call' do
|
|
|
|
let(:group_parameter) { group_read }
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
describe '.group_access_ids' do
|
2017-06-16 20:43:09 +00:00
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
it 'responds to group_access_ids' do
|
|
|
|
expect(described_class).to respond_to(:group_access_ids)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
it 'wraps .group_access' do
|
|
|
|
expect(described_class).to receive(:group_access).and_call_original
|
|
|
|
described_class.group_access_ids(group_read, 'read')
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns class instances' do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_read.name => 'read',
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
result = described_class.group_access_ids(group_read, 'read')
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(result).to include(subject.id)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'destroys relations before instance gets destroyed' do
|
|
|
|
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.group_names_access_map = {
|
2017-06-16 20:43:09 +00:00
|
|
|
group_full.name => 'full',
|
|
|
|
group_read.name => 'read',
|
2018-01-12 12:44:02 +00:00
|
|
|
group_inactive.name => 'change',
|
2017-06-16 20:43:09 +00:00
|
|
|
}
|
|
|
|
expect do
|
2018-12-13 09:10:32 +00:00
|
|
|
subject.destroy
|
2017-06-16 20:43:09 +00:00
|
|
|
end.to change {
|
|
|
|
described_class.group_through.klass.count
|
|
|
|
}.by(-3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.shared_examples '#group_access? call' do
|
|
|
|
context 'single access' do
|
|
|
|
|
|
|
|
it 'checks positive' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_access?(group_parameter, 'read')).to be true
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks negative' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_access?(group_parameter, 'change')).to be false
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'access list' do
|
|
|
|
|
|
|
|
it 'checks positive' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_access?(group_parameter, %w[read change])).to be true
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'checks negative' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(subject.group_access?(group_parameter, %w[change create])).to be false
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-20 15:13:42 +00:00
|
|
|
RSpec.shared_examples '.group_access call' do
|
2017-06-16 20:43:09 +00:00
|
|
|
context 'single access' do
|
|
|
|
|
|
|
|
it 'lists access IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(described_class.group_access(group_parameter, 'read')).to include(subject)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes non access IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(described_class.group_access(group_parameter, 'change')).not_to include(subject)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'access list' do
|
|
|
|
|
|
|
|
it 'lists access IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(described_class.group_access(group_parameter, %w[read change])).to include(subject)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes non access IDs' do
|
2018-12-13 09:10:32 +00:00
|
|
|
expect(described_class.group_access(group_parameter, %w[change create])).not_to include(subject)
|
2017-06-16 20:43:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|