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