Commit 35708ab8 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'check_for_future_dated_licenses' into 'master'

Introduce check for future_dated licenses

See merge request gitlab-org/gitlab!31445
parents 61616728 740ead82
......@@ -230,8 +230,10 @@ class License < ApplicationRecord
after_create :reset_current
after_destroy :reset_current
after_commit :reset_future_dated, on: [:create, :destroy]
scope :recent, -> { reorder(id: :desc) }
scope :last_hundred, -> { recent.limit(100) }
class << self
def features_for_plan(plan)
......@@ -267,7 +269,21 @@ class License < ApplicationRecord
def load_license
return unless self.table_exists?
self.order(id: :desc).limit(100).find { |license| license.valid? && license.started? }
self.last_hundred.find { |license| license.valid? && license.started? }
end
def future_dated
Gitlab::SafeRequestStore.fetch(:future_dated_license) { load_future_dated }
end
def reset_future_dated
Gitlab::SafeRequestStore.delete(:future_dated_license)
end
def future_dated_only?
return false if current.present?
future_dated.present?
end
def global_feature?(feature)
......@@ -291,6 +307,12 @@ class License < ApplicationRecord
def history
all.sort_by { |license| [license.starts_at, license.created_at, license.expires_at] }.reverse
end
private
def load_future_dated
self.last_hundred.find { |license| license.valid? && license.future_dated? }
end
end
def data_filename
......@@ -470,6 +492,10 @@ class License < ApplicationRecord
starts_at <= Date.current
end
def future_dated?
starts_at > Date.current
end
private
def restricted_attr(name, default = nil)
......@@ -482,6 +508,10 @@ class License < ApplicationRecord
self.class.reset_current
end
def reset_future_dated
self.class.reset_future_dated
end
def reset_license
@license = nil
end
......
......@@ -273,6 +273,34 @@ describe License do
end
end
describe 'Callbacks' do
describe '#reset_future_dated', :request_store do
let!(:future_dated_license) { create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export) }
before do
described_class.future_dated
expect(Gitlab::SafeRequestStore.read(:future_dated_license)).to be_present
end
context 'when a license is created' do
it 'deletes the future_dated_license value in Gitlab::SafeRequestStore' do
create(:license)
expect(Gitlab::SafeRequestStore.read(:future_dated_license)).to be_nil
end
end
context 'when a license is destroyed' do
it 'deletes the future_dated_license value in Gitlab::SafeRequestStore' do
future_dated_license.destroy
expect(Gitlab::SafeRequestStore.read(:future_dated_license)).to be_nil
end
end
end
end
describe "Class methods" do
before do
described_class.reset_current
......@@ -338,7 +366,7 @@ describe License do
end
end
describe ".current" do
describe '.current' do
context 'when licenses table does not exist' do
it 'returns nil' do
allow(described_class).to receive(:table_exists?).and_return(false)
......@@ -347,25 +375,25 @@ describe License do
end
end
context "when there is no license" do
it "returns nil" do
allow(described_class).to receive(:order).and_return(double(limit: []))
context 'when there is no license' do
it 'returns nil' do
allow(described_class).to receive(:last_hundred).and_return([])
expect(described_class.current).to be_nil
end
end
context "when the license is invalid" do
it "returns nil" do
allow(described_class).to receive(:order).and_return(double(limit: [license]))
context 'when the license is invalid' do
it 'returns nil' do
allow(described_class).to receive(:last_hundred).and_return([license])
allow(license).to receive(:valid?).and_return(false)
expect(described_class.current).to be_nil
end
end
context "when the license is valid" do
it "returns the license" do
context 'when the license is valid' do
it 'returns the license' do
current_license = create_list(:license, 2).last
create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export)
......@@ -374,6 +402,98 @@ describe License do
end
end
describe '.future_dated_only?' do
before do
described_class.reset_future_dated
end
context 'when licenses table does not exist' do
it 'returns false' do
allow(described_class).to receive(:table_exists?).and_return(false)
expect(described_class.future_dated_only?).to be_falsey
end
end
context 'when there is no license' do
it 'returns false' do
allow(described_class).to receive(:last_hundred).and_return([])
expect(described_class.future_dated_only?).to be_falsey
end
end
context 'when the license is invalid' do
it 'returns false' do
license = build(:license, data: build(:gitlab_license, starts_at: Date.current + 1.month).export)
allow(described_class).to receive(:last_hundred).and_return([license])
allow(license).to receive(:valid?).and_return(false)
expect(described_class.future_dated_only?).to be_falsey
end
end
context 'when the license is valid' do
context 'when there is a current license' do
it 'returns the false' do
expect(described_class.future_dated_only?).to be_falsey
end
end
context 'when the license is future-dated' do
it 'returns the true' do
create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export)
allow(described_class).to receive(:current).and_return(nil)
expect(described_class.future_dated_only?).to be_truthy
end
end
end
end
describe '.future_dated' do
before do
described_class.reset_future_dated
end
context 'when licenses table does not exist' do
it 'returns nil' do
allow(described_class).to receive(:table_exists?).and_return(false)
expect(described_class.future_dated).to be_nil
end
end
context 'when there is no license' do
it 'returns nil' do
allow(described_class).to receive(:last_hundred).and_return([])
expect(described_class.future_dated).to be_nil
end
end
context 'when the license is invalid' do
it 'returns false' do
license = build(:license, data: build(:gitlab_license, starts_at: Date.current + 1.month).export)
allow(described_class).to receive(:last_hundred).and_return([license])
allow(license).to receive(:valid?).and_return(false)
expect(described_class.future_dated).to be_nil
end
end
context 'when the license is valid' do
it 'returns the true' do
future_dated_license = create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export)
expect(described_class.future_dated).to eq(future_dated_license)
end
end
end
describe ".block_changes?" do
before do
allow(License).to receive(:current).and_return(license)
......@@ -562,7 +682,7 @@ describe License do
let(:license) { create(:license, trial: true, expired: true) }
before(:all) do
described_class.destroy_all # rubocop: disable DestroyAll
described_class.delete_all
end
::License::EES_FEATURES.each do |feature|
......@@ -829,4 +949,24 @@ describe License do
end
end
end
describe '#future_dated?' do
using RSpec::Parameterized::TableSyntax
where(:starts_at, :result) do
Date.current - 1.month | false
Date.current | false
Date.current + 1.month | true
end
with_them do
let(:gl_license) { build(:gitlab_license, starts_at: starts_at) }
subject { license.future_dated? }
it do
is_expected.to eq(result)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment