Commit 22e5c7ac authored by James Lopez's avatar James Lopez

Merge branch '326931-sync-condition' into 'master'

Cloud License: seat link sync even when historical data is absent

See merge request gitlab-org/gitlab!59096
parents e056ec1c 70d78a8f
......@@ -13,7 +13,8 @@ module Gitlab
# like for SyncSeatLinkRequestWorker, the params are passed because the values from when
# the job was enqueued are necessary.
def initialize(timestamp: nil, key: default_key, max_users: nil, active_users: nil)
@timestamp = timestamp || historical_data&.recorded_at
@current_time = Time.current
@timestamp = timestamp || historical_data&.recorded_at || current_time
@key = key
@max_users = max_users || default_max_count
@active_users = active_users || default_active_count
......@@ -25,22 +26,32 @@ module Gitlab
SyncSeatLinkWorker.perform_async
end
# Only sync paid licenses from start date until 14 days after expiration
# when seat link feature is enabled.
def should_sync_seats?
Gitlab::CurrentSettings.seat_link_enabled? &&
!license&.trial? &&
license&.expires_at && # Skip sync if license has no expiration
historical_data.present? && # Skip sync if there is no historical data
timestamp.between?(license.starts_at.beginning_of_day, license.expires_at.end_of_day + 14.days)
return false unless license
if license.cloud?
!license.trial? &&
license.expires_at && # Skip sync if license has no expiration
timestamp.between?(license.starts_at.beginning_of_day, license.expires_at.end_of_day + 14.days)
else
# Only sync paid licenses from start date until 14 days after expiration
# when seat link feature is enabled.
Gitlab::CurrentSettings.seat_link_enabled? &&
!license.trial? &&
license.expires_at && # Skip sync if license has no expiration
historical_data.present? && # Skip sync if there is no historical data
timestamp.between?(license.starts_at.beginning_of_day, license.expires_at.end_of_day + 14.days)
end
end
private
attr_reader :current_time
def data
{
timestamp: timestamp&.iso8601,
date: timestamp&.to_date&.to_s,
timestamp: timestamp.iso8601,
date: timestamp.to_date.to_s,
license_key: key,
max_historical_user_count: max_users,
active_users: active_users
......@@ -61,7 +72,7 @@ module Gitlab
def historical_data
strong_memoize(:historical_data) do
to_timestamp = timestamp || Time.current
to_timestamp = timestamp || current_time
license&.historical_data(to: to_timestamp)&.order(:recorded_at)&.last
end
......
......@@ -15,6 +15,10 @@ FactoryBot.define do
expires_at { 3.weeks.ago.to_date }
end
trait :cloud do
type { 'cloud' }
end
transient do
plan { License::STARTER_PLAN }
end
......@@ -48,10 +52,15 @@ FactoryBot.define do
trial { false }
end
trait :cloud do
cloud { true }
end
data do
attrs = [:gitlab_license]
attrs << :trial if trial
attrs << :expired if expired
attrs << :cloud if cloud
build(*attrs, plan: plan).export
end
......
......@@ -120,40 +120,82 @@ RSpec.describe Gitlab::SeatLinkData do
describe '#should_sync_seats?' do
let_it_be(:historical_data) { create(:historical_data, recorded_at: timestamp) }
let(:license) { build(:license, :cloud) }
before do
allow(License).to receive(:current).and_return(license)
end
subject { super().should_sync_seats? }
context 'when all the pre conditions are valid' do
it { is_expected.to eq(true) }
end
context 'when seat link is disabled' do
before do
allow(Settings.gitlab).to receive(:seat_link_enabled).and_return(false)
end
context 'when license key is missing' do
let(:license) { nil }
it { is_expected.to be_falsey }
end
context 'when license key is missing' do
before do
allow(License).to receive(:current).and_return(nil)
end
context 'when expires_at is not set' do
let(:license) { build(:license, expires_at: nil) }
it { is_expected.to be_falsey }
end
context 'when license is trial' do
before do
allow(License).to receive(:current).and_return(create(:license, trial: true))
context 'cloud license' do
context 'when license is trial' do
let(:license) { build(:license, trial: true) }
it { is_expected.to be_falsey }
end
it { is_expected.to be_falsey }
context 'when timestamp is out of the range' do
let(:timestamp) { license.starts_at - 1.day }
it { is_expected.to be_falsey }
end
context 'when historical data not found' do
before do
historical_data.destroy!
end
it { is_expected.to eq(true) }
end
end
context 'when timestamp is out of the range' do
let(:timestamp) { Time.iso8601('2015-03-22T06:09:18Z') }
context 'legacy license' do
let(:license) { build(:license) }
it { is_expected.to be_falsey }
context 'when seat link is disabled' do
before do
allow(Settings.gitlab).to receive(:seat_link_enabled).and_return(false)
end
it { is_expected.to be_falsey }
end
context 'when license is trial' do
let(:license) { build(:license, trial: true) }
it { is_expected.to be_falsey }
end
context 'when timestamp is out of the range' do
let(:timestamp) { license.starts_at - 1.day }
it { is_expected.to be_falsey }
end
context 'when historical data not found' do
before do
historical_data.destroy!
end
it { is_expected.to eq(false) }
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