Commit 9d1e7ef8 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '3525_allow_removal_for_cloud_licenses' into 'master'

Allow removal of cloud licenses

See merge request gitlab-org/gitlab!71481
parents 87e7ec3e 7f7e8de7
...@@ -93,7 +93,7 @@ export default { ...@@ -93,7 +93,7 @@ export default {
return this.licenseUploadPath && this.isLicenseFileType; return this.licenseUploadPath && this.isLicenseFileType;
}, },
canRemoveLicense() { canRemoveLicense() {
return this.licenseRemovePath && this.isLicenseFileType; return this.licenseRemovePath;
}, },
hasSubscription() { hasSubscription() {
return Boolean(Object.keys(this.subscription).length); return Boolean(Object.keys(this.subscription).length);
......
...@@ -43,10 +43,6 @@ class Admin::LicensesController < Admin::ApplicationController ...@@ -43,10 +43,6 @@ class Admin::LicensesController < Admin::ApplicationController
flash[:alert] = _('The license was removed. GitLab now no longer has a valid license.') flash[:alert] = _('The license was removed. GitLab now no longer has a valid license.')
end end
redirect_to admin_subscription_path, status: :found
rescue Licenses::DestroyService::DestroyCloudLicenseError => e
flash[:error] = e.message
redirect_to admin_subscription_path, status: :found redirect_to admin_subscription_path, status: :found
end end
......
...@@ -4,13 +4,10 @@ module Licenses ...@@ -4,13 +4,10 @@ module Licenses
class DestroyService < ::Licenses::BaseService class DestroyService < ::Licenses::BaseService
extend ::Gitlab::Utils::Override extend ::Gitlab::Utils::Override
DestroyCloudLicenseError = Class.new(StandardError)
override :execute override :execute
def execute def execute
raise ActiveRecord::RecordNotFound unless license raise ActiveRecord::RecordNotFound unless license
raise Gitlab::Access::AccessDeniedError unless can?(user, :destroy_licenses) raise Gitlab::Access::AccessDeniedError unless can?(user, :destroy_licenses)
raise DestroyCloudLicenseError, _('Cloud licenses can not be removed.') if license.cloud_license?
license.destroy license.destroy
end end
......
...@@ -6,10 +6,6 @@ module API ...@@ -6,10 +6,6 @@ module API
feature_category :license feature_category :license
rescue_from Licenses::DestroyService::DestroyCloudLicenseError do |e|
render_api_error!(e.message, 422)
end
resource :license do resource :license do
desc 'Get information on the currently active license' do desc 'Get information on the currently active license' do
success EE::API::Entities::GitlabLicenseWithActiveUsers success EE::API::Entities::GitlabLicenseWithActiveUsers
......
...@@ -124,28 +124,26 @@ RSpec.describe Admin::LicensesController do ...@@ -124,28 +124,26 @@ RSpec.describe Admin::LicensesController do
allow(License).to receive(:current).and_return(create(:license, cloud: is_cloud_license)) allow(License).to receive(:current).and_return(create(:license, cloud: is_cloud_license))
end end
context 'with a cloud license' do shared_examples 'license removal' do
let(:is_cloud_license) { true } it 'removes the license' do
it 'is can not be removed' do
delete :destroy delete :destroy
expect(response).to redirect_to(admin_subscription_path) expect(response).to redirect_to(admin_subscription_path)
expect(flash[:error]).to match('Cloud licenses can not be removed.') expect(flash[:notice]).to match('The license was removed. GitLab has fallen back on the previous license.')
expect(cloud_licenses).to be_present expect(cloud_licenses).to be_empty
end end
end end
context 'with a cloud license' do
let(:is_cloud_license) { true }
it_behaves_like 'license removal'
end
context 'with a legacy license' do context 'with a legacy license' do
let(:is_cloud_license) { false } let(:is_cloud_license) { false }
it 'is can be removed' do it_behaves_like 'license removal'
delete :destroy
expect(response).to redirect_to(admin_subscription_path)
expect(flash[:notice]).to match('The license was removed. GitLab has fallen back on the previous license.')
expect(cloud_licenses).to be_empty
end
end end
end end
end end
...@@ -16,6 +16,22 @@ RSpec.describe 'Admin views Subscription', :js do ...@@ -16,6 +16,22 @@ RSpec.describe 'Admin views Subscription', :js do
end end
end end
shared_examples 'license removal' do
context 'when removing a license file' do
before do
accept_alert do
click_on 'Remove license'
end
end
it 'shows a message saying the license was correctly removed' do
page.within(find('#content-body', match: :first)) do
expect(page).to have_content('The license was removed.')
end
end
end
end
context 'with a cloud license' do context 'with a cloud license' do
let!(:license) { create_current_license(cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN) } let!(:license) { create_current_license(cloud_licensing_enabled: true, plan: License::ULTIMATE_PLAN) }
...@@ -51,6 +67,7 @@ RSpec.describe 'Admin views Subscription', :js do ...@@ -51,6 +67,7 @@ RSpec.describe 'Admin views Subscription', :js do
end end
it_behaves_like 'an "Export license usage file" button' it_behaves_like 'an "Export license usage file" button'
it_behaves_like 'license removal'
end end
end end
...@@ -62,20 +79,7 @@ RSpec.describe 'Admin views Subscription', :js do ...@@ -62,20 +79,7 @@ RSpec.describe 'Admin views Subscription', :js do
end end
it_behaves_like 'an "Export license usage file" button' it_behaves_like 'an "Export license usage file" button'
it_behaves_like 'license removal'
context 'when removing a license file' do
before do
accept_alert do
click_on 'Remove license'
end
end
it 'shows a message saying the license was correctly removed' do
page.within(find('#content-body', match: :first)) do
expect(page).to have_content('The license was removed.')
end
end
end
context 'when activating another subscription' do context 'when activating another subscription' do
before do before do
......
...@@ -255,7 +255,7 @@ describe('Subscription Breakdown', () => { ...@@ -255,7 +255,7 @@ describe('Subscription Breakdown', () => {
it.each` it.each`
url | type | shouldShow url | type | shouldShow
${licenseRemovePath} | ${subscriptionTypes.LICENSE_FILE} | ${true} ${licenseRemovePath} | ${subscriptionTypes.LICENSE_FILE} | ${true}
${licenseRemovePath} | ${subscriptionTypes.CLOUD} | ${false} ${licenseRemovePath} | ${subscriptionTypes.CLOUD} | ${true}
${''} | ${subscriptionTypes.LICENSE_FILE} | ${false} ${''} | ${subscriptionTypes.LICENSE_FILE} | ${false}
${''} | ${subscriptionTypes.CLOUD} | ${false} ${''} | ${subscriptionTypes.CLOUD} | ${false}
${undefined} | ${subscriptionTypes.LICENSE_FILE} | ${false} ${undefined} | ${subscriptionTypes.LICENSE_FILE} | ${false}
......
...@@ -86,14 +86,18 @@ RSpec.describe API::License, api: true do ...@@ -86,14 +86,18 @@ RSpec.describe API::License, api: true do
let(:endpoint) { "/license/#{license.id}" } let(:endpoint) { "/license/#{license.id}" }
it 'destroys a license and returns 204' do shared_examples 'license removal' do
delete api(endpoint, admin) it 'destroys a license and returns 204' do
delete api(endpoint, admin)
expect(response).to have_gitlab_http_status(:no_content) expect(response).to have_gitlab_http_status(:no_content)
expect(response.message).to eq('No Content') expect(response.message).to eq('No Content')
expect(License.where(id: license.id)).not_to exist expect(License.where(id: license.id)).not_to exist
end
end end
it_behaves_like 'license removal'
it "returns an error if the license doesn't exist" do it "returns an error if the license doesn't exist" do
delete api("/license/0", admin) delete api("/license/0", admin)
...@@ -111,12 +115,7 @@ RSpec.describe API::License, api: true do ...@@ -111,12 +115,7 @@ RSpec.describe API::License, api: true do
context 'with a cloud license' do context 'with a cloud license' do
let(:cloud_licensing_enabled) { true } let(:cloud_licensing_enabled) { true }
it 'returns 422 and does not delete the license' do it_behaves_like 'license removal'
delete api(endpoint, admin)
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(License.where(id: license.id)).to exist
end
end end
end end
......
...@@ -16,6 +16,16 @@ RSpec.describe Licenses::DestroyService do ...@@ -16,6 +16,16 @@ RSpec.describe Licenses::DestroyService do
expect(License.where(id: license.id)).not_to exist expect(License.where(id: license.id)).not_to exist
end end
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)
expect(License.where(id: license.id)).not_to exist
end
end
end end
context 'when admin mode is disabled' do context 'when admin mode is disabled' do
......
...@@ -7204,9 +7204,6 @@ msgstr "" ...@@ -7204,9 +7204,6 @@ msgstr ""
msgid "Cloud Run description and apps that are suitable for this deployment target" msgid "Cloud Run description and apps that are suitable for this deployment target"
msgstr "" msgstr ""
msgid "Cloud licenses can not be removed."
msgstr ""
msgid "Cluster" msgid "Cluster"
msgstr "" msgstr ""
......
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