Commit cb140072 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'prevent_future_licenses_from_being_the_current' into 'master'

Change logic to find the current license

See merge request gitlab-org/gitlab!30296
parents daf89072 f4736106
...@@ -267,11 +267,7 @@ class License < ApplicationRecord ...@@ -267,11 +267,7 @@ class License < ApplicationRecord
def load_license def load_license
return unless self.table_exists? return unless self.table_exists?
license = self.last self.order(id: :desc).limit(100).find { |license| license.valid? && license.started? }
return unless license && license.valid?
license
end end
def global_feature?(feature) def global_feature?(feature)
......
---
title: Change logic to find the current license
merge_request: 30296
author:
type: changed
...@@ -19,8 +19,8 @@ FactoryBot.define do ...@@ -19,8 +19,8 @@ FactoryBot.define do
plan { License::STARTER_PLAN } plan { License::STARTER_PLAN }
end end
starts_at { Date.today - 1.month } starts_at { Date.new(1970, 1, 1) }
expires_at { Date.today + 11.months } expires_at { Date.current + 11.months }
block_changes_at { expires_at + 2.weeks } block_changes_at { expires_at + 2.weeks }
notify_users_at { expires_at } notify_users_at { expires_at }
notify_admins_at { expires_at } notify_admins_at { expires_at }
......
...@@ -102,7 +102,7 @@ describe HistoricalData do ...@@ -102,7 +102,7 @@ describe HistoricalData do
end end
context 'with data outside of the license period' do context 'with data outside of the license period' do
let!(:license) { create(:license) } let!(:license) { create(:license, starts_at: Date.current - 1.month) }
context 'with stats before the license period' do context 'with stats before the license period' do
before do before do
......
...@@ -116,7 +116,7 @@ describe License do ...@@ -116,7 +116,7 @@ describe License do
end end
context "after the license started" do context "after the license started" do
let(:date) { Date.today } let(:date) { Date.current }
it "is valid" do it "is valid" do
expect(license).to be_valid expect(license).to be_valid
...@@ -249,7 +249,7 @@ describe License do ...@@ -249,7 +249,7 @@ describe License do
describe 'downgrade' do describe 'downgrade' do
context 'when more users were added in previous period' do context 'when more users were added in previous period' do
before do before do
HistoricalData.create!(date: 6.months.ago, active_user_count: 15) HistoricalData.create!(date: described_class.current.starts_at - 6.months, active_user_count: 15)
set_restrictions(restricted_user_count: 5, previous_user_count: 10) set_restrictions(restricted_user_count: 5, previous_user_count: 10)
end end
...@@ -274,11 +274,8 @@ describe License do ...@@ -274,11 +274,8 @@ describe License do
end end
describe "Class methods" do describe "Class methods" do
let!(:license) { described_class.last }
before do before do
described_class.reset_current described_class.reset_current
allow(described_class).to receive(:last).and_return(license)
end end
describe '.features_for_plan' do describe '.features_for_plan' do
...@@ -343,46 +340,48 @@ describe License do ...@@ -343,46 +340,48 @@ describe License do
describe ".current" do describe ".current" do
context 'when licenses table does not exist' do context 'when licenses table does not exist' do
before 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)
end
it 'returns nil' do
expect(described_class.current).to be_nil expect(described_class.current).to be_nil
end end
end end
context "when there is no license" do context "when there is no license" do
let!(:license) { nil }
it "returns nil" do it "returns nil" do
allow(described_class).to receive(:order).and_return(double(limit: []))
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
before do it "returns nil" do
allow(described_class).to receive(:order).and_return(double(limit: [license]))
allow(license).to receive(:valid?).and_return(false) allow(license).to receive(:valid?).and_return(false)
end
it "returns nil" do
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
expect(described_class.current).to be_present current_license = create_list(:license, 2).last
create(:license, data: create(:gitlab_license, starts_at: Date.current + 1.month).export)
expect(described_class.current).to eq(current_license)
end end
end end
end end
describe ".block_changes?" do describe ".block_changes?" do
context "when there is no current license" do
before do before do
allow(described_class).to receive(:current).and_return(nil) allow(License).to receive(:current).and_return(license)
end end
context "when there is no current license" do
let(:license) { nil }
it "returns false" do it "returns false" do
expect(described_class.block_changes?).to be_falsey expect(described_class.block_changes?).to be_falsey
end end
...@@ -779,12 +778,14 @@ describe License do ...@@ -779,12 +778,14 @@ describe License do
end end
def set_restrictions(opts) def set_restrictions(opts)
date = described_class.current.starts_at
gl_license.restrictions = { gl_license.restrictions = {
active_user_count: opts[:restricted_user_count], active_user_count: opts[:restricted_user_count],
previous_user_count: opts[:previous_user_count], previous_user_count: opts[:previous_user_count],
trueup_quantity: opts[:trueup_quantity], trueup_quantity: opts[:trueup_quantity],
trueup_from: (Date.today - 1.year).to_s, trueup_from: (date - 1.year).to_s,
trueup_to: Date.today.to_s trueup_to: date.to_s
} }
end end
......
...@@ -32,7 +32,7 @@ describe API::License, api: true do ...@@ -32,7 +32,7 @@ describe API::License, api: true do
get api('/license', admin) get api('/license', admin)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['user_limit']).to eq 0 expect(json_response['user_limit']).to eq 0
expect(Date.parse(json_response['starts_at'])).to eq Date.today - 1.month expect(Date.parse(json_response['starts_at'])).to eq Date.new(1970, 1, 1)
expect(Date.parse(json_response['expires_at'])).to eq Date.today + 11.months expect(Date.parse(json_response['expires_at'])).to eq Date.today + 11.months
expect(json_response['active_users']).to eq 1 expect(json_response['active_users']).to eq 1
expect(json_response['licensee']).not_to be_empty expect(json_response['licensee']).not_to be_empty
...@@ -51,7 +51,7 @@ describe API::License, api: true do ...@@ -51,7 +51,7 @@ describe API::License, api: true do
expect(response).to have_gitlab_http_status(:created) expect(response).to have_gitlab_http_status(:created)
expect(json_response['user_limit']).to eq 0 expect(json_response['user_limit']).to eq 0
expect(Date.parse(json_response['starts_at'])).to eq Date.today - 1.month expect(Date.parse(json_response['starts_at'])).to eq Date.new(1970, 1, 1)
expect(Date.parse(json_response['expires_at'])).to eq Date.today + 11.months expect(Date.parse(json_response['expires_at'])).to eq Date.today + 11.months
expect(json_response['active_users']).to eq 1 expect(json_response['active_users']).to eq 1
expect(json_response['licensee']).not_to be_empty expect(json_response['licensee']).not_to be_empty
......
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