Commit 80b5526d authored by Vladlena Shumilo's avatar Vladlena Shumilo Committed by Mark Chao

Clear future subscriptions info on license destroy

Update the Licenses::DestroyService to clear the
info related to future subscriptions when
the license is destroyed

Changelog: changed
EE: true
parent 7e3945be
......@@ -598,6 +598,10 @@ class License < ApplicationRecord
!!license&.operational_metrics?
end
def current?
self == License.current
end
def license_type
cloud_license? ? CLOUD_LICENSE_TYPE : LICENSE_FILE_TYPE
end
......
......@@ -9,7 +9,16 @@ module Licenses
raise ActiveRecord::RecordNotFound unless license
raise Gitlab::Access::AccessDeniedError unless can?(user, :destroy_licenses)
clear_future_subscriptions
license.destroy
end
private
def clear_future_subscriptions
return unless license.current?
Gitlab::CurrentSettings.current_application_settings.update(future_subscriptions: [])
end
end
end
......@@ -1599,6 +1599,40 @@ RSpec.describe License do
end
end
describe '#current?' do
subject { license.current? }
context 'when the license is not persisted' do
it { is_expected.to be false }
end
context 'when the license is persisted' do
before do
license.save!
end
context 'when the license is the current license' do
it { is_expected.to be true }
end
context 'when the license is not the current license' do
before do
allow(License).to receive(:current).and_return(create(:license))
end
it { is_expected.to be false }
end
context 'when there is no current license' do
before do
allow(License).to receive(:current).and_return(nil)
end
it { is_expected.to be false }
end
end
end
describe '#license_type' do
subject { license.license_type }
......
......@@ -10,20 +10,48 @@ RSpec.describe Licenses::DestroyService do
described_class.new(license, user).execute
end
context 'when admin mode is enabled', :enable_admin_mode do
shared_examples 'license destroy' do
it 'destroys a license' do
destroy_with(user)
expect(License.where(id: license.id)).not_to exist
end
end
shared_examples 'clear future subscriptions application setting' do
it 'clears the future_subscriptions application setting' do
expect(Gitlab::CurrentSettings.current_application_settings).to receive(:update)
.with(future_subscriptions: [])
destroy_with(user)
end
end
context 'when admin mode is enabled', :enable_admin_mode do
it_behaves_like 'license destroy'
it_behaves_like 'clear future subscriptions application setting'
context 'with cloud license' do
let(:license) { create(:license, cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN) }
it 'destroys a license' do
destroy_with(user)
it_behaves_like 'license destroy'
it_behaves_like 'clear future subscriptions application setting'
end
context 'with an active license that is not the current one' do
before do
if Gitlab.ee?
allow(License).to receive(:current).and_return(create(:license))
end
end
it_behaves_like 'license destroy'
expect(License.where(id: license.id)).not_to exist
it 'does not clear the future_subscriptions application setting' do
expect(Gitlab::CurrentSettings.current_application_settings).not_to receive(:update)
.with(future_subscriptions: [])
destroy_with(user)
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