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 ...@@ -230,8 +230,10 @@ class License < ApplicationRecord
after_create :reset_current after_create :reset_current
after_destroy :reset_current after_destroy :reset_current
after_commit :reset_future_dated, on: [:create, :destroy]
scope :recent, -> { reorder(id: :desc) } scope :recent, -> { reorder(id: :desc) }
scope :last_hundred, -> { recent.limit(100) }
class << self class << self
def features_for_plan(plan) def features_for_plan(plan)
...@@ -267,7 +269,21 @@ class License < ApplicationRecord ...@@ -267,7 +269,21 @@ class License < ApplicationRecord
def load_license def load_license
return unless self.table_exists? 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 end
def global_feature?(feature) def global_feature?(feature)
...@@ -291,6 +307,12 @@ class License < ApplicationRecord ...@@ -291,6 +307,12 @@ class License < ApplicationRecord
def history def history
all.sort_by { |license| [license.starts_at, license.created_at, license.expires_at] }.reverse all.sort_by { |license| [license.starts_at, license.created_at, license.expires_at] }.reverse
end end
private
def load_future_dated
self.last_hundred.find { |license| license.valid? && license.future_dated? }
end
end end
def data_filename def data_filename
...@@ -470,6 +492,10 @@ class License < ApplicationRecord ...@@ -470,6 +492,10 @@ class License < ApplicationRecord
starts_at <= Date.current starts_at <= Date.current
end end
def future_dated?
starts_at > Date.current
end
private private
def restricted_attr(name, default = nil) def restricted_attr(name, default = nil)
...@@ -482,6 +508,10 @@ class License < ApplicationRecord ...@@ -482,6 +508,10 @@ class License < ApplicationRecord
self.class.reset_current self.class.reset_current
end end
def reset_future_dated
self.class.reset_future_dated
end
def reset_license def reset_license
@license = nil @license = nil
end end
......
...@@ -273,6 +273,34 @@ describe License do ...@@ -273,6 +273,34 @@ describe License do
end end
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 describe "Class methods" do
before do before do
described_class.reset_current described_class.reset_current
...@@ -338,7 +366,7 @@ describe License do ...@@ -338,7 +366,7 @@ describe License do
end end
end end
describe ".current" do describe '.current' do
context 'when licenses table does not exist' do context 'when licenses table does not exist' do
it 'returns nil' do it 'returns nil' do
allow(described_class).to receive(:table_exists?).and_return(false) allow(described_class).to receive(:table_exists?).and_return(false)
...@@ -347,25 +375,25 @@ describe License do ...@@ -347,25 +375,25 @@ describe License do
end end
end end
context "when there is no license" do context 'when there is no license' do
it "returns nil" do it 'returns nil' do
allow(described_class).to receive(:order).and_return(double(limit: [])) allow(described_class).to receive(:last_hundred).and_return([])
expect(described_class.current).to be_nil expect(described_class.current).to be_nil
end end
end end
context "when the license is invalid" do context 'when the license is invalid' do
it "returns nil" do it 'returns nil' do
allow(described_class).to receive(:order).and_return(double(limit: [license])) allow(described_class).to receive(:last_hundred).and_return([license])
allow(license).to receive(:valid?).and_return(false) allow(license).to receive(:valid?).and_return(false)
expect(described_class.current).to be_nil expect(described_class.current).to be_nil
end end
end end
context "when the license is valid" do context 'when the license is valid' do
it "returns the license" do it 'returns the license' do
current_license = create_list(:license, 2).last current_license = create_list(:license, 2).last
create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export) create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export)
...@@ -374,6 +402,98 @@ describe License do ...@@ -374,6 +402,98 @@ describe License do
end end
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 describe ".block_changes?" do
before do before do
allow(License).to receive(:current).and_return(license) allow(License).to receive(:current).and_return(license)
...@@ -562,7 +682,7 @@ describe License do ...@@ -562,7 +682,7 @@ describe License do
let(:license) { create(:license, trial: true, expired: true) } let(:license) { create(:license, trial: true, expired: true) }
before(:all) do before(:all) do
described_class.destroy_all # rubocop: disable DestroyAll described_class.delete_all
end end
::License::EES_FEATURES.each do |feature| ::License::EES_FEATURES.each do |feature|
...@@ -829,4 +949,24 @@ describe License do ...@@ -829,4 +949,24 @@ describe License do
end end
end 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 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