settings_spec.rb 7.64 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
RSpec.describe API::Settings, 'EE Settings' do
6 7
  include StubENV

8 9
  let(:user) { create(:user) }
  let(:admin) { create(:admin) }
10
  let(:project) { create(:project) }
11

12 13 14 15
  before do
    stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
  end

16 17
  describe "PUT /application/settings" do
    it 'sets EE specific settings' do
18 19
      stub_licensed_features(custom_file_templates: true)

Imre Farkas's avatar
Imre Farkas committed
20
      put api("/application/settings", admin),
blackst0ne's avatar
blackst0ne committed
21 22 23 24
        params: {
          help_text: 'Help text',
          file_template_project_id: project.id
        }
25

26
      expect(response).to have_gitlab_http_status(:ok)
27
      expect(json_response['help_text']).to eq('Help text')
28
      expect(json_response['file_template_project_id']).to eq(project.id)
29
    end
30 31 32 33 34 35 36 37 38 39 40 41 42

    context 'elasticsearch settings' do
      it 'limits namespaces and projects properly' do
        namespace_ids = create_list(:namespace, 2).map(&:id)
        project_ids = create_list(:project, 2).map(&:id)

        put api('/application/settings', admin),
            params: {
              elasticsearch_limit_indexing: true,
              elasticsearch_project_ids: project_ids.join(','),
              elasticsearch_namespace_ids: namespace_ids.join(',')
            }

43
        expect(response).to have_gitlab_http_status(:ok)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        expect(json_response['elasticsearch_limit_indexing']).to eq(true)
        expect(json_response['elasticsearch_project_ids']).to eq(project_ids)
        expect(json_response['elasticsearch_namespace_ids']).to eq(namespace_ids)
        expect(ElasticsearchIndexedNamespace.count).to eq(2)
        expect(ElasticsearchIndexedProject.count).to eq(2)
      end

      it 'removes namespaces and projects properly' do
        stub_ee_application_setting(elasticsearch_limit_indexing: true)
        create(:elasticsearch_indexed_namespace).namespace.id
        create(:elasticsearch_indexed_project).project.id

        put api('/application/settings', admin),
            params: {
              elasticsearch_namespace_ids: []
            }.to_json,
            headers: {
              'CONTENT_TYPE' => 'application/json'
            }

64
        expect(response).to have_gitlab_http_status(:ok)
65 66 67 68 69
        expect(json_response['elasticsearch_namespace_ids']).to eq([])
        expect(ElasticsearchIndexedNamespace.count).to eq(0)
        expect(ElasticsearchIndexedProject.count).to eq(1)
      end
    end
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    context 'secret_detection_token_revocation_enabled is true' do
      context 'secret_detection_token_revocation_url value is present' do
        it 'updates secret_detection_token_revocation_url' do
          put api('/application/settings', admin),
            params: {
              secret_detection_token_revocation_enabled: true,
              secret_detection_token_revocation_url: 'https://example.com/secret_detection_token_revocation'
            }

          expect(response).to have_gitlab_http_status(:ok)
          expect(json_response['secret_detection_token_revocation_enabled']).to be(true)
          expect(json_response['secret_detection_token_revocation_url']).to eq('https://example.com/secret_detection_token_revocation')
        end
      end

      context 'missing secret_detection_token_revocation_url value' do
        it 'returns a blank parameter error message' do
          put api('/application/settings', admin), params: { secret_detection_token_revocation_enabled: true }

          expect(response).to have_gitlab_http_status(:bad_request)
          expect(json_response['error']).to include('secret_detection_token_revocation_url is missing')
        end
      end
    end
95
  end
96

97 98 99
  shared_examples 'settings for licensed features' do
    let(:attribute_names) { settings.keys.map(&:to_s) }

100
    before do
101
      # Make sure the settings exist before the specs
102 103 104
      get api("/application/settings", admin)
    end

105 106 107 108
    context 'when the feature is not available' do
      before do
        stub_licensed_features(feature => false)
      end
109

110 111
      it 'hides the attributes in the API' do
        get api("/application/settings", admin)
112

113
        expect(response).to have_gitlab_http_status(:ok)
114 115 116 117
        attribute_names.each do |attribute|
          expect(json_response.keys).not_to include(attribute)
        end
      end
118

119
      it 'does not update application settings' do
blackst0ne's avatar
blackst0ne committed
120
        expect { put api("/application/settings", admin), params: settings }
121 122
          .not_to change { ApplicationSetting.current.reload.attributes.slice(*attribute_names) }
      end
123 124
    end

125 126 127 128
    context 'when the feature is available' do
      before do
        stub_licensed_features(feature => true)
      end
129

130 131 132
      it 'includes the attributes in the API' do
        get api("/application/settings", admin)

133
        expect(response).to have_gitlab_http_status(:ok)
134 135 136 137 138 139
        attribute_names.each do |attribute|
          expect(json_response.keys).to include(attribute)
        end
      end

      it 'allows updating the settings' do
blackst0ne's avatar
blackst0ne committed
140
        put api("/application/settings", admin), params: settings
141
        expect(response).to have_gitlab_http_status(:ok)
142 143 144 145 146

        settings.each do |attribute, value|
          expect(ApplicationSetting.current.public_send(attribute)).to eq(value)
        end
      end
147
    end
148 149 150 151 152
  end

  context 'mirroring settings' do
    let(:settings) { { mirror_max_capacity: 15 } }
    let(:feature) { :repository_mirrors }
153

154 155
    it_behaves_like 'settings for licensed features'
  end
156

157 158 159 160 161 162
  context 'custom email footer' do
    let(:settings) { { email_additional_text: 'this is a scary legal footer' } }
    let(:feature) { :email_additional_text }

    it_behaves_like 'settings for licensed features'
  end
Imre Farkas's avatar
Imre Farkas committed
163

164
  context 'default project deletion protection' do
165 166 167 168 169 170
    let(:settings) { { default_project_deletion_protection: true } }
    let(:feature) { :default_project_deletion_protection }

    it_behaves_like 'settings for licensed features'
  end

Manoj M J's avatar
Manoj M J committed
171 172 173 174 175 176 177
  context 'group_owners_can_manage_default_branch_protection setting' do
    let(:settings) { { group_owners_can_manage_default_branch_protection: false } }
    let(:feature) { :default_branch_protection_restriction_in_groups }

    it_behaves_like 'settings for licensed features'
  end

178
  context 'delayed deletion period' do
Gosia Ksionek's avatar
Gosia Ksionek committed
179
    let(:settings) { { deletion_adjourned_period: 5 } }
180
    let(:feature) { :adjourned_deletion_for_projects_and_groups }
Gosia Ksionek's avatar
Gosia Ksionek committed
181 182 183 184

    it_behaves_like 'settings for licensed features'
  end

185 186 187 188 189 190
  context 'custom file template project' do
    let(:settings) { { file_template_project_id: project.id } }
    let(:feature) { :custom_file_templates }

    it_behaves_like 'settings for licensed features'
  end
191 192 193 194 195 196 197

  context 'updating name disabled for users' do
    let(:settings) { { updating_name_disabled_for_users: true } }
    let(:feature) { :disable_name_update_for_users }

    it_behaves_like 'settings for licensed features'
  end
Małgorzata Ksionek's avatar
Małgorzata Ksionek committed
198 199 200 201 202 203 204 205 206

  context 'merge request approvers rules' do
    let(:settings) do
      {
        disable_overriding_approvers_per_merge_request: true,
        prevent_merge_requests_author_approval: true,
        prevent_merge_requests_committers_approval: true
      }
    end
207

Małgorzata Ksionek's avatar
Małgorzata Ksionek committed
208
    let(:feature) { :admin_merge_request_approvers_rules }
Małgorzata Ksionek's avatar
Małgorzata Ksionek committed
209

Tan Le's avatar
Tan Le committed
210
    it_behaves_like 'settings for licensed features'
Małgorzata Ksionek's avatar
Małgorzata Ksionek committed
211
  end
212 213 214

  context 'updating npm packages request forwarding' do
    let(:settings) { { npm_package_requests_forwarding: true } }
Steve Abrams's avatar
Steve Abrams committed
215
    let(:feature) { :package_forwarding }
216 217 218

    it_behaves_like 'settings for licensed features'
  end
219 220

  context 'maintenance mode' do
221 222 223 224
    before do
      stub_feature_flags(maintenance_mode: true)
    end

225 226 227 228 229 230
    let(:settings) do
      {
        maintenance_mode: true,
        maintenance_mode_message: 'GitLab is in maintenance'
      }
    end
231

232 233 234 235
    let(:feature) { :geo }

    it_behaves_like 'settings for licensed features'
  end
236
end