diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-04-24 19:28:04 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-05-04 14:52:55 +0300 |
commit | 3d6d0a09b65f032bbe1bd5ad4736dd764195bbe1 (patch) | |
tree | 72225f4bd77687266020a1391b5604478b67318c /spec | |
parent | cf37bef287d7dd5d2dce3e2276489767b8c0671f (diff) |
Store application wide terms
This allows admins to define terms in the application settings.
Every time the terms are adjusted, a new version is stored and becomes
the 'active' version. This allows tracking which specific version was
accepted by a user.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/terms.rb | 5 | ||||
-rw-r--r-- | spec/features/admin/admin_settings_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/application_setting/term_spec.rb | 15 | ||||
-rw-r--r-- | spec/models/application_setting_spec.rb | 15 | ||||
-rw-r--r-- | spec/requests/api/settings_spec.rb | 6 | ||||
-rw-r--r-- | spec/services/application_settings/update_service_spec.rb | 57 |
6 files changed, 109 insertions, 1 deletions
diff --git a/spec/factories/terms.rb b/spec/factories/terms.rb new file mode 100644 index 00000000000..5ffca365a5f --- /dev/null +++ b/spec/factories/terms.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :term, class: ApplicationSetting::Term do + terms "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + end +end diff --git a/spec/features/admin/admin_settings_spec.rb b/spec/features/admin/admin_settings_spec.rb index 7853d2952ea..b74643bac55 100644 --- a/spec/features/admin/admin_settings_spec.rb +++ b/spec/features/admin/admin_settings_spec.rb @@ -85,6 +85,18 @@ feature 'Admin updates settings' do expect(page).to have_content "Application settings saved successfully" end + scenario 'Terms of Service' do + page.within('.as-terms') do + check 'Require all users to accept Terms of Service when they access GitLab.' + fill_in 'Terms of Service Agreement', with: 'Be nice!' + click_button 'Save changes' + end + + expect(Gitlab::CurrentSettings.enforce_terms).to be(true) + expect(Gitlab::CurrentSettings.terms).to eq 'Be nice!' + expect(page).to have_content 'Application settings saved successfully' + end + scenario 'Modify oauth providers' do expect(Gitlab::CurrentSettings.disabled_oauth_sign_in_sources).to be_empty diff --git a/spec/models/application_setting/term_spec.rb b/spec/models/application_setting/term_spec.rb new file mode 100644 index 00000000000..1eddf3c56ff --- /dev/null +++ b/spec/models/application_setting/term_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe ApplicationSetting::Term do + describe 'validations' do + it { is_expected.to validate_presence_of(:terms) } + end + + describe '.latest' do + it 'finds the latest terms' do + terms = create(:term) + + expect(described_class.latest).to eq(terms) + end + end +end diff --git a/spec/models/application_setting_spec.rb b/spec/models/application_setting_spec.rb index ae2d34750a7..10d6109cae7 100644 --- a/spec/models/application_setting_spec.rb +++ b/spec/models/application_setting_spec.rb @@ -301,6 +301,21 @@ describe ApplicationSetting do expect(subject).to be_invalid end end + + describe 'enforcing terms' do + it 'requires the terms to present when enforcing users to accept' do + subject.enforce_terms = true + + expect(subject).to be_invalid + end + + it 'is valid when terms are created' do + create(:term) + subject.enforce_terms = true + + expect(subject).to be_valid + end + end end describe '.current' do diff --git a/spec/requests/api/settings_spec.rb b/spec/requests/api/settings_spec.rb index 015d4b9a491..8b22d1e72f3 100644 --- a/spec/requests/api/settings_spec.rb +++ b/spec/requests/api/settings_spec.rb @@ -54,7 +54,9 @@ describe API::Settings, 'Settings' do dsa_key_restriction: 2048, ecdsa_key_restriction: 384, ed25519_key_restriction: 256, - circuitbreaker_check_interval: 2 + circuitbreaker_check_interval: 2, + enforce_terms: true, + terms: 'Hello world!' expect(response).to have_gitlab_http_status(200) expect(json_response['default_projects_limit']).to eq(3) @@ -76,6 +78,8 @@ describe API::Settings, 'Settings' do expect(json_response['ecdsa_key_restriction']).to eq(384) expect(json_response['ed25519_key_restriction']).to eq(256) expect(json_response['circuitbreaker_check_interval']).to eq(2) + expect(json_response['enforce_terms']).to be(true) + expect(json_response['terms']).to eq('Hello world!') end end diff --git a/spec/services/application_settings/update_service_spec.rb b/spec/services/application_settings/update_service_spec.rb new file mode 100644 index 00000000000..fb07ecc6ae8 --- /dev/null +++ b/spec/services/application_settings/update_service_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe ApplicationSettings::UpdateService do + let(:application_settings) { Gitlab::CurrentSettings.current_application_settings } + let(:admin) { create(:user, :admin) } + let(:params) { {} } + + subject { described_class.new(application_settings, admin, params) } + + before do + # So the caching behaves like it would in production + stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false') + end + + describe 'updating terms' do + context 'when the passed terms are blank' do + let(:params) { { terms: '' } } + + it 'does not create terms' do + expect { subject.execute }.not_to change { ApplicationSetting::Term.count } + end + end + + context 'when passing terms' do + let(:params) { { terms: 'Be nice! ' } } + + it 'creates the terms' do + expect { subject.execute }.to change { ApplicationSetting::Term.count }.by(1) + end + + it 'does not create terms if they are the same as the existing ones' do + create(:term, terms: 'Be nice!') + + expect { subject.execute }.not_to change { ApplicationSetting::Term.count } + end + + it 'updates terms if they already existed' do + create(:term, terms: 'Other terms') + + subject.execute + + expect(application_settings.terms).to eq('Be nice!') + end + + it 'Only queries once when the terms are changed' do + create(:term, terms: 'Other terms') + expect(application_settings.terms).to eq('Other terms') + + subject.execute + + expect(application_settings.terms).to eq('Be nice!') + expect { 2.times { application_settings.terms } } + .not_to exceed_query_limit(0) + end + end + end +end |