diff --git a/spec/features/profile_spec.rb b/spec/features/profile_spec.rb index 9fe2e610555ddcfa30f30d517ae737bf193cd7e2..c80253fead8c7dd280eaa2036d22f244c09a0bc8 100644 --- a/spec/features/profile_spec.rb +++ b/spec/features/profile_spec.rb @@ -9,8 +9,7 @@ describe 'Profile account page', feature: true do describe 'when signup is enabled' do before do - allow_any_instance_of(ApplicationSetting). - to receive(:signup_enabled?).and_return(true) + stub_application_setting(signup_enabled: true) visit profile_account_path end @@ -24,8 +23,7 @@ describe 'Profile account page', feature: true do describe 'when signup is disabled' do before do - allow_any_instance_of(ApplicationSetting). - to receive(:signup_enabled?).and_return(false) + stub_application_setting(signup_enabled: false) visit profile_account_path end diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index e9ff832603fdbab91f89627611bdb2f6fc161233..4178bb2e83666f9756cd4300d2a837476022f35d 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -220,9 +220,7 @@ describe API::API, api: true do context 'when a visibility level is restricted' do before do @project = attributes_for(:project, { public: true }) - allow_any_instance_of(ApplicationSetting).to( - receive(:restricted_visibility_levels).and_return([20]) - ) + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) end it 'should not allow a non-admin to use a restricted visibility level' do diff --git a/spec/services/create_snippet_service_spec.rb b/spec/services/create_snippet_service_spec.rb index 08689c15ca8ac937677691c73bd00bf21834ba80..8edabe9450bcf7243bae886dde5a0bffbc8e012d 100644 --- a/spec/services/create_snippet_service_spec.rb +++ b/spec/services/create_snippet_service_spec.rb @@ -14,11 +14,7 @@ describe CreateSnippetService do context 'When public visibility is restricted' do before do - allow_any_instance_of(ApplicationSetting).to( - receive(:restricted_visibility_levels).and_return( - [Gitlab::VisibilityLevel::PUBLIC] - ) - ) + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) @opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) end diff --git a/spec/services/git_push_service_spec.rb b/spec/services/git_push_service_spec.rb index 3373b97bfd4ed7323bd79ddb178cfc2ed83b00fe..62cef9db5344f1754c156cf4ca551287d44b0598 100644 --- a/spec/services/git_push_service_spec.rb +++ b/spec/services/git_push_service_spec.rb @@ -124,9 +124,7 @@ describe GitPushService do end it "when pushing a branch for the first time with default branch protection disabled" do - allow(ApplicationSetting.current_application_settings). - to receive(:default_branch_protection). - and_return(Gitlab::Access::PROTECTION_NONE) + stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_NONE) expect(project).to receive(:execute_hooks) expect(project.default_branch).to eq("master") @@ -135,9 +133,7 @@ describe GitPushService do end it "when pushing a branch for the first time with default branch protection set to 'developers can push'" do - allow(ApplicationSetting.current_application_settings). - to receive(:default_branch_protection). - and_return(Gitlab::Access::PROTECTION_DEV_CAN_PUSH) + stub_application_setting(default_branch_protection: Gitlab::Access::PROTECTION_DEV_CAN_PUSH) expect(project).to receive(:execute_hooks) expect(project.default_branch).to eq("master") diff --git a/spec/services/projects/create_service_spec.rb b/spec/services/projects/create_service_spec.rb index 337dae592dd488024653093b2c918ec94880c2bc..97b206c9854739bd310cd1bd38f5bd849e4ac69d 100644 --- a/spec/services/projects/create_service_spec.rb +++ b/spec/services/projects/create_service_spec.rb @@ -58,9 +58,7 @@ describe Projects::CreateService do context 'restricted visibility level' do before do - allow_any_instance_of(ApplicationSetting).to( - receive(:restricted_visibility_levels).and_return([20]) - ) + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) @opts.merge!( visibility_level: Gitlab::VisibilityLevel.options['Public'] diff --git a/spec/services/projects/update_service_spec.rb b/spec/services/projects/update_service_spec.rb index 0dd6980a44fd79838fc2a74a20518213e6922535..b347fa15f878ba1226b56a5d44eabf8904e89599 100644 --- a/spec/services/projects/update_service_spec.rb +++ b/spec/services/projects/update_service_spec.rb @@ -47,9 +47,7 @@ describe Projects::UpdateService do context 'respect configured visibility restrictions setting' do before(:each) do - allow_any_instance_of(ApplicationSetting).to( - receive(:restricted_visibility_levels).and_return([20]) - ) + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) end context 'should be private when updated to private' do diff --git a/spec/services/update_snippet_service_spec.rb b/spec/services/update_snippet_service_spec.rb index 841ef9bfed11e5e037b686e80a5f0ecfbae2e97a..d7c516e3934e6d2925189373f40e1410aa867e6d 100644 --- a/spec/services/update_snippet_service_spec.rb +++ b/spec/services/update_snippet_service_spec.rb @@ -14,11 +14,7 @@ describe UpdateSnippetService do context 'When public visibility is restricted' do before do - allow_any_instance_of(ApplicationSetting).to( - receive(:restricted_visibility_levels).and_return( - [Gitlab::VisibilityLevel::PUBLIC] - ) - ) + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) @snippet = create_snippet(@project, @user, @opts) @opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) diff --git a/spec/support/stub_configuration.rb b/spec/support/stub_configuration.rb index ad86abdbb413a6758da2805b270624541cdfbdc2..e4004ec8f79b88eb43298b6b9b54319ce18e68bd 100644 --- a/spec/support/stub_configuration.rb +++ b/spec/support/stub_configuration.rb @@ -1,5 +1,10 @@ module StubConfiguration def stub_application_setting(messages) + add_predicates(messages) + + # Stubbing both of these because we're not yet consistent with how we access + # current application settings + allow_any_instance_of(ApplicationSetting).to receive_messages(messages) allow(Gitlab::CurrentSettings.current_application_settings). to receive_messages(messages) end @@ -11,4 +16,25 @@ module StubConfiguration def stub_gravatar_setting(messages) allow(Gitlab.config.gravatar).to receive_messages(messages) end + + private + + # Modifies stubbed messages to also stub possible predicate versions + # + # Examples: + # + # add_predicates(foo: true) + # # => {foo: true, foo?: true} + # + # add_predicates(signup_enabled?: false) + # # => {signup_enabled? false} + def add_predicates(messages) + # Only modify keys that aren't already predicates + keys = messages.keys.map(&:to_s).reject { |k| k.end_with?('?') } + + keys.each do |key| + predicate = key + '?' + messages[predicate.to_sym] = messages[key.to_sym] + end + end end