Commit 996ffa8e authored by Josianne Hyson's avatar Josianne Hyson

Refactor license validation specs

Prior to this change there were several different styles for declaring
the top level describe block in the License validation specs. This made
it difficult to easily see which validation is being tested where.

This commit re-groups the existing validation specs in this file to be
under a consistent title (the validation method name).

Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/334115
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65793
parent e70e3415
...@@ -5,28 +5,94 @@ require "spec_helper" ...@@ -5,28 +5,94 @@ require "spec_helper"
RSpec.describe License do RSpec.describe License do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
subject(:license) { build(:license, data: gl_license.export) }
let(:gl_license) { build(:gitlab_license) } let(:gl_license) { build(:gitlab_license) }
let(:license) { build(:license, data: gl_license.export) }
describe "Validation" do describe 'validations' do
describe "Valid license" do describe '#valid_license' do
context "when the license is provided" do context 'when the license is provided' do
it "is valid" do it 'is valid' do
expect(license).to be_valid expect(license).to be_valid
end end
end end
context "when no license is provided" do context 'when no license is provided' do
before do before do
license.data = nil license.data = nil
end end
it "is invalid" do it 'is invalid' do
expect(license).not_to be_valid expect(license).not_to be_valid
end end
end end
end end
describe '#check_trueup' do
let(:active_user_count) { described_class.current.daily_billable_users_count + 10 }
let(:date) { described_class.current.starts_at }
before do
create(:historical_data, recorded_at: date, active_user_count: active_user_count)
end
context 'when quantity is ok' do
before do
set_restrictions(restricted_user_count: 5, trueup_quantity: 10)
end
it 'is valid' do
expect(license).to be_valid
end
context 'but active users exceeds restricted user count' do
it 'is invalid' do
create_list(:user, 6)
expect(license).not_to be_valid
end
end
end
context 'when quantity is wrong' do
it 'is invalid' do
set_restrictions(restricted_user_count: 5, trueup_quantity: 8)
expect(license).not_to be_valid
end
end
context 'when previous user count is not present' do
before do
set_restrictions(restricted_user_count: 5, trueup_quantity: 7)
end
it 'uses current active user count to calculate the expected true-up' do
create_list(:user, 3)
expect(license).to be_valid
end
context 'with wrong true-up quantity' do
it 'is invalid' do
create_list(:user, 2)
expect(license).not_to be_valid
end
end
end
context 'when previous user count is present' do
before do
set_restrictions(restricted_user_count: 5, trueup_quantity: 6, previous_user_count: 4)
end
it 'uses it to calculate the expected true-up' do
expect(license).to be_valid
end
end
end
describe '#check_users_limit' do describe '#check_users_limit' do
context 'for each plan' do context 'for each plan' do
before do before do
...@@ -131,184 +197,126 @@ RSpec.describe License do ...@@ -131,184 +197,126 @@ RSpec.describe License do
end end
end end
end end
end
describe "Historical active user count" do describe 'Historical active user count' do
let(:active_user_count) { described_class.current.daily_billable_users_count + 10 } let(:active_user_count) { described_class.current.daily_billable_users_count + 10 }
let(:date) { described_class.current.starts_at } let(:date) { described_class.current.starts_at }
let!(:historical_data) { create(:historical_data, recorded_at: date, active_user_count: active_user_count) } let!(:historical_data) { create(:historical_data, recorded_at: date, active_user_count: active_user_count) }
context "when there is no active user count restriction" do context 'when there is no active user count restriction' do
it "is valid" do it 'is valid' do
expect(license).to be_valid expect(license).to be_valid
end
end
context 'without historical data' do
before do
create_list(:user, 2)
gl_license.restrictions = {
previous_user_count: 1,
active_user_count: described_class.current.daily_billable_users_count - 1
}
HistoricalData.delete_all
end
context 'with previous_user_count and active users above of license limit' do
it 'is invalid' do
expect(license).to be_invalid
end end
end
it 'shows the proper error message' do context 'without historical data' do
license.valid? before do
create_list(:user, 2)
error_msg = "This GitLab installation currently has 2 active users, exceeding this license's limit of 1 by 1 user. " \ gl_license.restrictions = {
"Please upload a license for at least 2 users or contact sales at https://about.gitlab.com/sales/" previous_user_count: 1,
active_user_count: described_class.current.daily_billable_users_count - 1
}
expect(license.errors[:base].first).to eq(error_msg) HistoricalData.delete_all
end end
end
end
context "when the active user count restriction is exceeded" do context 'with previous_user_count and active users above of license limit' do
before do it 'is invalid' do
gl_license.restrictions = { active_user_count: active_user_count - 1 } expect(license).to be_invalid
end end
context "when the license started" do it 'shows the proper error message' do
it "is invalid" do license.valid?
expect(license).not_to be_valid
end
end
context "after the license started" do error_msg = "This GitLab installation currently has 2 active users, exceeding this license's limit of 1 by 1 user. " \
let(:date) { Date.current } "Please upload a license for at least 2 users or contact sales at https://about.gitlab.com/sales/"
it "is valid" do expect(license.errors[:base].first).to eq(error_msg)
expect(license).to be_valid end
end end
end end
context "in the year before the license started" do context 'when the active user count restriction is exceeded' do
let(:date) { described_class.current.starts_at - 6.months } before do
gl_license.restrictions = { active_user_count: active_user_count - 1 }
it "is invalid" do
expect(license).not_to be_valid
end end
end
context "earlier than a year before the license started" do
let(:date) { described_class.current.starts_at - 2.years }
it "is valid" do context 'when the license started' do
expect(license).to be_valid it 'is invalid' do
expect(license).not_to be_valid
end
end end
end
end
context "when the active user count restriction is not exceeded" do
before do
gl_license.restrictions = { active_user_count: active_user_count + 1 }
end
it "is valid" do context 'after the license started' do
expect(license).to be_valid let(:date) { Date.current }
end
end
context "when the active user count is met exactly" do
it "is valid" do
active_user_count = 100
gl_license.restrictions = { active_user_count: active_user_count }
expect(license).to be_valid
end
end
context 'with true-up info' do it 'is valid' do
context 'when quantity is ok' do expect(license).to be_valid
before do end
set_restrictions(restricted_user_count: 5, trueup_quantity: 10)
end end
it 'is valid' do context 'in the year before the license started' do
expect(license).to be_valid let(:date) { described_class.current.starts_at - 6.months }
end
context 'but active users exceeds restricted user count' do
it 'is invalid' do it 'is invalid' do
create_list(:user, 6)
expect(license).not_to be_valid expect(license).not_to be_valid
end end
end end
end
context 'when quantity is wrong' do context 'earlier than a year before the license started' do
it 'is invalid' do let(:date) { described_class.current.starts_at - 2.years }
set_restrictions(restricted_user_count: 5, trueup_quantity: 8)
expect(license).not_to be_valid it 'is valid' do
expect(license).to be_valid
end
end end
end end
context 'when previous user count is not present' do context 'when the active user count restriction is not exceeded' do
before do before do
set_restrictions(restricted_user_count: 5, trueup_quantity: 7) gl_license.restrictions = { active_user_count: active_user_count + 1 }
end end
it 'uses current active user count to calculate the expected true-up' do it 'is valid' do
create_list(:user, 3)
expect(license).to be_valid expect(license).to be_valid
end end
context 'with wrong true-up quantity' do
it 'is invalid' do
create_list(:user, 2)
expect(license).not_to be_valid
end
end
end end
context 'when previous user count is present' do context 'when the active user count is met exactly' do
before do it 'is valid' do
set_restrictions(restricted_user_count: 5, trueup_quantity: 6, previous_user_count: 4) active_user_count = 100
end gl_license.restrictions = { active_user_count: active_user_count }
it 'uses it to calculate the expected true-up' do
expect(license).to be_valid expect(license).to be_valid
end end
end end
end end
end end
describe "Not expired" do describe '#not_expired' do
context "when the license doesn't expire" do context "when the license doesn't expire" do
it "is valid" do it 'is valid' do
expect(license).to be_valid expect(license).to be_valid
end end
end end
context "when the license has expired" do context 'when the license has expired' do
before do before do
gl_license.expires_at = Date.yesterday gl_license.expires_at = Date.yesterday
end end
it "is invalid" do it 'is invalid' do
expect(license).not_to be_valid expect(license).not_to be_valid
end end
end end
context "when the license has yet to expire" do context 'when the license has yet to expire' do
before do before do
gl_license.expires_at = Date.tomorrow gl_license.expires_at = Date.tomorrow
end end
it "is valid" do it 'is valid' do
expect(license).to be_valid expect(license).to be_valid
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