From 655052a932d1999726f5fd2bde4098b05321d75a Mon Sep 17 00:00:00 2001 From: michold Date: Wed, 26 Apr 2023 12:54:04 +0200 Subject: [PATCH] Add maintenance mode to registration features Add maintenance mode to registration features Changelog: added EE: true --- .../admin_area/settings/usage_statistics.md | 4 ++ .../admin/application_settings_controller.rb | 2 +- .../models/gitlab_subscriptions/features.rb | 1 + .../registration_features/maintenance_mode.rb | 9 +++ .../_maintenance_mode_settings_form.html.haml | 2 +- ee/lib/ee/api/entities/application_setting.rb | 4 +- ee/lib/ee/api/settings.rb | 2 +- .../application_settings_controller_spec.rb | 25 +++++++++ ee/spec/requests/api/settings_spec.rb | 52 ++++++++++++++++++ .../maintenance_mode_spec.rb | 55 +++++++++++++++++++ 10 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 ee/app/services/registration_features/maintenance_mode.rb create mode 100644 ee/spec/services/registration_features/maintenance_mode_spec.rb diff --git a/doc/user/admin_area/settings/usage_statistics.md b/doc/user/admin_area/settings/usage_statistics.md index 12b9a9ff17301a..0580dc5f45206b 100644 --- a/doc/user/admin_area/settings/usage_statistics.md +++ b/doc/user/admin_area/settings/usage_statistics.md @@ -50,6 +50,10 @@ tier. Users can continue to access the features in a paid tier without sharing u - [Repository size limit](../settings/account_and_limit_settings.md#repository-size-limit). - [Group access restriction by IP address](../../group/access_and_permissions.md#restrict-group-access-by-ip-address). +### Features available in 16.0 and later + +- [Maintenance mode](../../../administration/maintenance_mode/index.md). + NOTE: Registration is not yet required for participation, but may be added in a future milestone. diff --git a/ee/app/controllers/ee/admin/application_settings_controller.rb b/ee/app/controllers/ee/admin/application_settings_controller.rb index 8b17f2652e21a5..44a095b445ff04 100644 --- a/ee/app/controllers/ee/admin/application_settings_controller.rb +++ b/ee/app/controllers/ee/admin/application_settings_controller.rb @@ -126,7 +126,7 @@ def visible_application_setting_attributes ] end - if ::Gitlab::Geo.license_allows? + if RegistrationFeatures::MaintenanceMode.feature_available? attrs << :maintenance_mode attrs << :maintenance_mode_message end diff --git a/ee/app/models/gitlab_subscriptions/features.rb b/ee/app/models/gitlab_subscriptions/features.rb index e5f6b2ac92190b..187d1fca585539 100644 --- a/ee/app/models/gitlab_subscriptions/features.rb +++ b/ee/app/models/gitlab_subscriptions/features.rb @@ -254,6 +254,7 @@ class Features STARTER_FEATURES_WITH_USAGE_PING = %i[ send_emails_from_admin_area repository_size_limit + maintenance_mode ].freeze PREMIUM_FEATURES_WITH_USAGE_PING = %i[ diff --git a/ee/app/services/registration_features/maintenance_mode.rb b/ee/app/services/registration_features/maintenance_mode.rb new file mode 100644 index 00000000000000..3d19f9565efe09 --- /dev/null +++ b/ee/app/services/registration_features/maintenance_mode.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module RegistrationFeatures + class MaintenanceMode + def self.feature_available? + ::Gitlab::Geo.license_allows? || ::GitlabSubscriptions::Features.usage_ping_feature?(:maintenance_mode) + end + end +end diff --git a/ee/app/views/admin/application_settings/_maintenance_mode_settings_form.html.haml b/ee/app/views/admin/application_settings/_maintenance_mode_settings_form.html.haml index 2d5cb6e9514e34..d32e727ac5b10f 100644 --- a/ee/app/views/admin/application_settings/_maintenance_mode_settings_form.html.haml +++ b/ee/app/views/admin/application_settings/_maintenance_mode_settings_form.html.haml @@ -1,4 +1,4 @@ -- return unless ::Gitlab::Geo.license_allows? +- return unless RegistrationFeatures::MaintenanceMode.feature_available? %section.settings.no-animate#js-maintenance-mode-toggle{ class: ('expanded' if expanded_by_default?) } .settings-header diff --git a/ee/lib/ee/api/entities/application_setting.rb b/ee/lib/ee/api/entities/application_setting.rb index 073e84bf220d8e..63096690daa245 100644 --- a/ee/lib/ee/api/entities/application_setting.rb +++ b/ee/lib/ee/api/entities/application_setting.rb @@ -28,8 +28,8 @@ module ApplicationSetting expose :npm_package_requests_forwarding, if: ->(_instance, _opts) { ::License.feature_available?(:package_forwarding) } expose :pypi_package_requests_forwarding, if: ->(_instance, _opts) { ::License.feature_available?(:package_forwarding) } expose :group_owners_can_manage_default_branch_protection, if: ->(_instance, _opts) { ::License.feature_available?(:default_branch_protection_restriction_in_groups) } - expose :maintenance_mode, if: ->(_instance, _opts) { ::Gitlab::Geo.license_allows? } - expose :maintenance_mode_message, if: ->(_instance, _opts) { ::Gitlab::Geo.license_allows? } + expose :maintenance_mode, if: ->(_instance, _opts) { RegistrationFeatures::MaintenanceMode.feature_available? } + expose :maintenance_mode_message, if: ->(_instance, _opts) { RegistrationFeatures::MaintenanceMode.feature_available? } expose :git_two_factor_session_expiry, if: ->(_instance, _opts) { License.feature_available?(:git_two_factor_enforcement) && ::Feature.enabled?(:two_factor_for_cli) } expose(*EE::ApplicationSettingsHelper.git_abuse_rate_limit_attributes, if: ->(_instance, _options) do ::License.feature_available?(:git_abuse_rate_limit) diff --git a/ee/lib/ee/api/settings.rb b/ee/lib/ee/api/settings.rb index fea4947cc089e7..f99dbc2f2ceb18 100644 --- a/ee/lib/ee/api/settings.rb +++ b/ee/lib/ee/api/settings.rb @@ -61,7 +61,7 @@ def filter_attributes_using_license(attrs) attrs = attrs.except(:git_two_factor_session_expiry) end - unless ::Gitlab::Geo.license_allows? + unless RegistrationFeatures::MaintenanceMode.feature_available? attrs = attrs.except(:maintenance_mode, :maintenance_mode_message) end diff --git a/ee/spec/controllers/admin/application_settings_controller_spec.rb b/ee/spec/controllers/admin/application_settings_controller_spec.rb index 6bb5013157f5c3..08bbd01169f36f 100644 --- a/ee/spec/controllers/admin/application_settings_controller_spec.rb +++ b/ee/spec/controllers/admin/application_settings_controller_spec.rb @@ -176,6 +176,31 @@ let(:feature) { :geo } it_behaves_like 'settings for licensed features' + + context "with geo feature disabled" do + context "with registration features disabled" do + it 'does not update settings' do + stub_application_setting(usage_ping_features_enabled: false) + + attribute_names = settings.keys.map(&:to_s) + + expect { put :update, params: { application_setting: settings } } + .not_to change { ApplicationSetting.current.reload.attributes.slice(*attribute_names) } + end + end + + context "with registration features enabled" do + it 'updates settings' do + stub_application_setting(usage_ping_features_enabled: true) + + put :update, params: { application_setting: settings } + + settings.each do |attribute, value| + expect(ApplicationSetting.current.public_send(attribute)).to eq(value) + end + end + end + end end context 'deletion adjourned period' do diff --git a/ee/spec/requests/api/settings_spec.rb b/ee/spec/requests/api/settings_spec.rb index e1601a798828d0..683974e843e841 100644 --- a/ee/spec/requests/api/settings_spec.rb +++ b/ee/spec/requests/api/settings_spec.rb @@ -313,6 +313,58 @@ let(:feature) { :geo } it_behaves_like 'settings for licensed features' + + context 'when geo feature is disabled' do + let(:attribute_names) { settings.keys.map(&:to_s) } + + context "with registration features disabled" do + before do + stub_application_setting(usage_ping_features_enabled: false) + end + + it 'hides the attributes in the API' do + get api(path, admin, admin_mode: true) + + expect(response).to have_gitlab_http_status(:ok) + attribute_names.each do |attribute| + expect(json_response.keys).not_to include(attribute) + end + end + + it 'does not update application settings' do + put api(path, admin, admin_mode: true), params: settings + + expect(response).to have_gitlab_http_status(:ok) + + expect { put api(path, admin, admin_mode: true), params: settings } + .not_to change { ApplicationSetting.current.reload.attributes.slice(*attribute_names) } + end + end + + context "with registration features enabled" do + before do + stub_application_setting(usage_ping_features_enabled: true) + end + + it 'includes the attributes in the API' do + get api(path, admin, admin_mode: true) + + expect(response).to have_gitlab_http_status(:ok) + attribute_names.each do |attribute| + expect(json_response.keys).to include(attribute) + end + end + + it 'allows updating the settings' do + put api(path, admin, admin_mode: true), params: settings + expect(response).to have_gitlab_http_status(:ok) + + settings.each do |attribute, value| + expect(ApplicationSetting.current.public_send(attribute)).to eq(value) + end + end + end + end end context 'password complexity settings' do diff --git a/ee/spec/services/registration_features/maintenance_mode_spec.rb b/ee/spec/services/registration_features/maintenance_mode_spec.rb new file mode 100644 index 00000000000000..898b8d04471e69 --- /dev/null +++ b/ee/spec/services/registration_features/maintenance_mode_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe RegistrationFeatures::MaintenanceMode, feature_category: :service_ping do + describe '.feature_available?' do + subject { described_class.feature_available? } + + context 'when geo feature is available' do + before do + stub_licensed_features(geo: true) + end + + it { is_expected.to be_truthy } + end + + context 'when geo feature is disabled' do + before do + stub_licensed_features(geo: false) + end + + it { is_expected.to be_falsey } + end + + context 'when usage ping is enabled' do + before do + stub_application_setting(usage_ping_enabled: true) + end + + context 'when usage_ping_features is enabled' do + before do + stub_application_setting(usage_ping_features_enabled: true) + end + + it { is_expected.to be_truthy } + end + + context 'when usage_ping_features is disabled' do + before do + stub_application_setting(usage_ping_features_enabled: false) + end + + it { is_expected.to be_falsey } + end + end + + context 'when usage ping is disabled' do + before do + stub_application_setting(usage_ping_enabled: false) + end + + it { is_expected.to be_falsey } + end + end +end -- GitLab