debian-mirror-gitlab/spec/models/concerns/featurable_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
2.8 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Featurable do
2022-06-21 17:19:12 +05:30
let!(:klass) do
Class.new(ApplicationRecord) do
include Featurable
2021-04-29 21:17:54 +05:30
2022-06-21 17:19:12 +05:30
self.table_name = 'project_features'
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
set_available_features %i(feature1 feature2 feature3)
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
def feature1_access_level
Featurable::DISABLED
end
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
def feature2_access_level
Featurable::ENABLED
end
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
def feature3_access_level
Featurable::PRIVATE
end
2020-06-23 00:09:42 +05:30
end
end
2022-06-21 17:19:12 +05:30
subject { klass.new }
2021-11-11 11:23:49 +05:30
2022-06-21 17:19:12 +05:30
describe '.set_available_features' do
it { expect(klass.available_features).to match_array [:feature1, :feature2, :feature3] }
end
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
describe '#*_enabled?' do
it { expect(subject.feature1_enabled?).to be_falsey }
it { expect(subject.feature2_enabled?).to be_truthy }
end
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
describe '.quoted_access_level_column' do
it 'returns the table name and quoted column name for a feature' do
expect(klass.quoted_access_level_column(:feature1)).to eq('"project_features"."feature1_access_level"')
2020-06-23 00:09:42 +05:30
end
end
2022-06-21 17:19:12 +05:30
describe '.access_level_attribute' do
it { expect(klass.access_level_attribute(:feature1)).to eq :feature1_access_level }
it 'raises error for unspecified feature' do
expect { klass.access_level_attribute(:unknown) }
.to raise_error(ArgumentError, /invalid feature: unknown/)
end
2020-06-23 00:09:42 +05:30
end
describe '#access_level' do
it 'returns access level' do
2022-06-21 17:19:12 +05:30
expect(subject.access_level(:feature1)).to eq(subject.feature1_access_level)
2020-06-23 00:09:42 +05:30
end
end
describe '#feature_available?' do
context 'when features are disabled' do
2022-06-21 17:19:12 +05:30
it 'returns false' do
expect(subject.feature_available?(:feature1)).to eq(false)
2020-06-23 00:09:42 +05:30
end
end
context 'when features are enabled only for team members' do
2022-06-21 17:19:12 +05:30
let_it_be(:user) { create(:user) }
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
before do
expect(subject).to receive(:member?).and_call_original
2020-06-23 00:09:42 +05:30
end
2022-06-21 17:19:12 +05:30
context 'when user is not present' do
it 'returns false' do
expect(subject.feature_available?(:feature3)).to eq(false)
2020-06-23 00:09:42 +05:30
end
end
2022-06-21 17:19:12 +05:30
context 'when user can read all resources' do
it 'returns true' do
allow(user).to receive(:can_read_all_resources?).and_return(true)
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
expect(subject.feature_available?(:feature3, user)).to eq(true)
2020-06-23 00:09:42 +05:30
end
end
2022-06-21 17:19:12 +05:30
context 'when user cannot read all resources' do
it 'raises NotImplementedError exception' do
expect(subject).to receive(:resource_member?).and_call_original
2020-06-23 00:09:42 +05:30
2022-06-21 17:19:12 +05:30
expect { subject.feature_available?(:feature3, user) }.to raise_error(NotImplementedError)
2020-06-23 00:09:42 +05:30
end
end
end
context 'when feature is enabled for everyone' do
2022-06-21 17:19:12 +05:30
it 'returns true' do
expect(subject.feature_available?(:feature2)).to eq(true)
2020-06-23 00:09:42 +05:30
end
end
end
end