2020-05-27 14:08:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-30 20:10:15 -04:00
|
|
|
require 'spec_helper'
|
2020-05-27 14:08:14 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Limitable do
|
2020-05-27 14:08:14 -04:00
|
|
|
let(:minimal_test_class) do
|
|
|
|
Class.new do
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
'TestClass'
|
|
|
|
end
|
|
|
|
|
|
|
|
include Limitable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2021-07-30 20:10:15 -04:00
|
|
|
stub_const('MinimalTestClass', minimal_test_class)
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(MinimalTestClass.limit_name).to eq('test_classes') }
|
|
|
|
|
|
|
|
context 'with scoped limit' do
|
|
|
|
before do
|
|
|
|
MinimalTestClass.limit_scope = :project
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(MinimalTestClass.limit_scope).to eq(:project) }
|
|
|
|
|
|
|
|
it 'triggers scoped validations' do
|
|
|
|
instance = MinimalTestClass.new
|
|
|
|
|
|
|
|
expect(instance).to receive(:validate_scoped_plan_limit_not_exceeded)
|
|
|
|
|
|
|
|
instance.valid?(:create)
|
|
|
|
end
|
2021-06-02 11:09:59 -04:00
|
|
|
|
2021-07-30 20:10:15 -04:00
|
|
|
context 'with custom relation and feature flags' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:limit_feature_flag, :limit_feature_flag_value, :limit_feature_flag_for_override, :limit_feature_flag_override_value, :expect_limit_applied?) do
|
|
|
|
nil | nil | nil | nil | true
|
|
|
|
:some_feature_flag | false | nil | nil | false
|
|
|
|
:some_feature_flag | true | nil | nil | true
|
|
|
|
:some_feature_flag | true | :some_feature_flag_disable | false | true
|
|
|
|
:some_feature_flag | false | :some_feature_flag_disable | false | false
|
|
|
|
:some_feature_flag | false | :some_feature_flag_disable | true | false
|
|
|
|
:some_feature_flag | true | :some_feature_flag_disable | true | false
|
2021-06-02 11:09:59 -04:00
|
|
|
end
|
|
|
|
|
2021-07-30 20:10:15 -04:00
|
|
|
with_them do
|
|
|
|
let(:instance) { MinimalTestClass.new }
|
2021-06-02 11:09:59 -04:00
|
|
|
|
2021-07-30 20:10:15 -04:00
|
|
|
before do
|
|
|
|
def instance.project
|
|
|
|
@project ||= stub_feature_flag_gate('CustomActor')
|
|
|
|
end
|
|
|
|
|
|
|
|
stub_feature_flags("#{limit_feature_flag}": limit_feature_flag_value ? [instance.project] : false) if limit_feature_flag
|
|
|
|
stub_feature_flags("#{limit_feature_flag_for_override}": limit_feature_flag_override_value ? [instance.project] : false) if limit_feature_flag_for_override
|
|
|
|
skip_feature_flags_yaml_validation
|
|
|
|
skip_default_enabled_yaml_check
|
2021-06-02 11:09:59 -04:00
|
|
|
|
2021-07-30 20:10:15 -04:00
|
|
|
MinimalTestClass.limit_relation = :custom_relation
|
|
|
|
MinimalTestClass.limit_feature_flag = limit_feature_flag
|
|
|
|
MinimalTestClass.limit_feature_flag_for_override = limit_feature_flag_for_override
|
|
|
|
end
|
2021-06-02 11:09:59 -04:00
|
|
|
|
2021-07-30 20:10:15 -04:00
|
|
|
it 'acts according to the feature flag settings' do
|
|
|
|
limits = Object.new
|
|
|
|
custom_relation = Object.new
|
|
|
|
if expect_limit_applied?
|
|
|
|
expect(instance).to receive(:custom_relation).and_return(custom_relation)
|
|
|
|
expect(instance.project).to receive(:actual_limits).and_return(limits)
|
|
|
|
expect(limits).to receive(:exceeded?).with(instance.class.name.demodulize.tableize, custom_relation).and_return(false)
|
|
|
|
else
|
|
|
|
expect(instance).not_to receive(:custom_relation)
|
|
|
|
end
|
|
|
|
|
|
|
|
instance.valid?(:create)
|
|
|
|
end
|
2021-06-02 11:09:59 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-27 14:08:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with global limit' do
|
|
|
|
before do
|
|
|
|
MinimalTestClass.limit_scope = Limitable::GLOBAL_SCOPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(MinimalTestClass.limit_scope).to eq(Limitable::GLOBAL_SCOPE) }
|
|
|
|
|
|
|
|
it 'triggers scoped validations' do
|
|
|
|
instance = MinimalTestClass.new
|
|
|
|
|
|
|
|
expect(instance).to receive(:validate_global_plan_limit_not_exceeded)
|
|
|
|
|
|
|
|
instance.valid?(:create)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|