diff options
Diffstat (limited to 'spec/controllers/projects/services_controller_spec.rb')
-rw-r--r-- | spec/controllers/projects/services_controller_spec.rb | 153 |
1 files changed, 85 insertions, 68 deletions
diff --git a/spec/controllers/projects/services_controller_spec.rb b/spec/controllers/projects/services_controller_spec.rb index f8474ab1082..baf3bde83bd 100644 --- a/spec/controllers/projects/services_controller_spec.rb +++ b/spec/controllers/projects/services_controller_spec.rb @@ -6,10 +6,12 @@ RSpec.describe Projects::ServicesController do include JiraServiceHelper include AfterNextHelpers - let(:project) { create(:project, :repository) } - let(:user) { create(:user) } - let(:service) { create(:jira_service, project: project) } - let(:service_params) { { username: 'username', password: 'password', url: 'http://example.com' } } + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + let_it_be(:jira_integration) { create(:jira_integration, project: project) } + + let(:integration) { jira_integration } + let(:integration_params) { { username: 'username', password: 'password', url: 'http://example.com' } } before do sign_in(user) @@ -17,9 +19,9 @@ RSpec.describe Projects::ServicesController do end describe '#test' do - context 'when can_test? returns false' do + context 'when the integration is not testable' do it 'renders 404' do - allow_any_instance_of(Integration).to receive(:can_test?).and_return(false) + allow_any_instance_of(Integration).to receive(:testable?).and_return(false) put :test, params: project_params @@ -28,10 +30,10 @@ RSpec.describe Projects::ServicesController do end context 'when validations fail' do - let(:service_params) { { active: 'true', url: '' } } + let(:integration_params) { { active: 'true', url: '' } } it 'returns error messages in JSON response' do - put :test, params: project_params(service: service_params) + put :test, params: project_params(service: integration_params) expect(json_response['message']).to eq 'Validations failed.' expect(json_response['service_response']).to include "Url can't be blank" @@ -39,15 +41,17 @@ RSpec.describe Projects::ServicesController do end end - context 'success' do + context 'when successful' do context 'with empty project' do - let(:project) { create(:project) } + let_it_be(:project) { create(:project) } + + context 'with chat notification integration' do + let_it_be(:teams_integration) { project.create_microsoft_teams_integration(webhook: 'http://webhook.com') } - context 'with chat notification service' do - let(:service) { project.create_microsoft_teams_service(webhook: 'http://webhook.com') } + let(:integration) { teams_integration } it 'returns success' do - allow_any_instance_of(::MicrosoftTeams::Notifier).to receive(:ping).and_return(true) + allow_next(::MicrosoftTeams::Notifier).to receive(:ping).and_return(true) put :test, params: project_params @@ -56,28 +60,28 @@ RSpec.describe Projects::ServicesController do end it 'returns success' do - stub_jira_service_test + stub_jira_integration_test expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original - put :test, params: project_params(service: service_params) + put :test, params: project_params(service: integration_params) expect(response).to be_successful end end it 'returns success' do - stub_jira_service_test + stub_jira_integration_test expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original - put :test, params: project_params(service: service_params) + put :test, params: project_params(service: integration_params) expect(response).to be_successful end context 'when service is configured for the first time' do - let(:service_params) do + let(:integration_params) do { 'active' => '1', 'push_events' => '1', @@ -108,17 +112,17 @@ RSpec.describe Projects::ServicesController do def do_put put :test, params: project_params(id: 'buildkite', - service: service_params) + service: integration_params) end end end - context 'failure' do + context 'when unsuccessful' do it 'returns an error response when the integration test fails' do stub_request(:get, 'http://example.com/rest/api/2/serverInfo') .to_return(status: 404) - put :test, params: project_params(service: service_params) + put :test, params: project_params(service: integration_params) expect(response).to be_successful expect(json_response).to eq( @@ -130,7 +134,7 @@ RSpec.describe Projects::ServicesController do end context 'with the Slack integration' do - let_it_be(:service) { build(:slack_service) } + let_it_be(:integration) { build(:integrations_slack) } it 'returns an error response when the URL is blocked' do put :test, params: project_params(service: { webhook: 'http://127.0.0.1' }) @@ -163,17 +167,17 @@ RSpec.describe Projects::ServicesController do describe 'PUT #update' do describe 'as HTML' do - let(:service_params) { { active: true } } - let(:params) { project_params(service: service_params) } + let(:integration_params) { { active: true } } + let(:params) { project_params(service: integration_params) } let(:message) { 'Jira settings saved and active.' } - let(:redirect_url) { edit_project_service_path(project, service) } + let(:redirect_url) { edit_project_service_path(project, integration) } before do put :update, params: params end - shared_examples 'service update' do + shared_examples 'integration update' do it 'redirects to the correct url with a flash message' do expect(response).to redirect_to(redirect_url) expect(flash[:notice]).to eq(message) @@ -181,61 +185,61 @@ RSpec.describe Projects::ServicesController do end context 'when param `active` is set to true' do - let(:params) { project_params(service: service_params, redirect_to: redirect) } + let(:params) { project_params(service: integration_params, redirect_to: redirect) } context 'when redirect_to param is present' do let(:redirect) { '/redirect_here' } let(:redirect_url) { redirect } - it_behaves_like 'service update' + it_behaves_like 'integration update' end context 'when redirect_to is an external domain' do let(:redirect) { 'http://examle.com' } - it_behaves_like 'service update' + it_behaves_like 'integration update' end context 'when redirect_to param is an empty string' do let(:redirect) { '' } - it_behaves_like 'service update' + it_behaves_like 'integration update' end end context 'when param `active` is set to false' do - let(:service_params) { { active: false } } - let(:message) { 'Jira settings saved, but not active.' } + let(:integration_params) { { active: false } } + let(:message) { 'Jira settings saved, but not active.' } - it_behaves_like 'service update' + it_behaves_like 'integration update' end - context 'wehn param `inherit_from_id` is set to empty string' do - let(:service_params) { { inherit_from_id: '' } } + context 'when param `inherit_from_id` is set to empty string' do + let(:integration_params) { { inherit_from_id: '' } } it 'sets inherit_from_id to nil' do - expect(service.reload.inherit_from_id).to eq(nil) + expect(integration.reload.inherit_from_id).to eq(nil) end end - context 'wehn param `inherit_from_id` is set to some value' do - let(:instance_service) { create(:jira_service, :instance) } - let(:service_params) { { inherit_from_id: instance_service.id } } + context 'when param `inherit_from_id` is set to some value' do + let(:instance_service) { create(:jira_integration, :instance) } + let(:integration_params) { { inherit_from_id: instance_service.id } } it 'sets inherit_from_id to value' do - expect(service.reload.inherit_from_id).to eq(instance_service.id) + expect(integration.reload.inherit_from_id).to eq(instance_service.id) end end end describe 'as JSON' do before do - stub_jira_service_test - put :update, params: project_params(service: service_params, format: :json) + stub_jira_integration_test + put :update, params: project_params(service: integration_params, format: :json) end context 'when update succeeds' do - let(:service_params) { { url: 'http://example.com' } } + let(:integration_params) { { url: 'http://example.com' } } it 'returns JSON response with no errors' do expect(response).to be_successful @@ -244,59 +248,67 @@ RSpec.describe Projects::ServicesController do end context 'when update fails' do - let(:service_params) { { url: '' } } + let(:integration_params) { { url: '' } } it 'returns JSON response with errors' do expect(response).to have_gitlab_http_status(:unprocessable_entity) expect(json_response).to include( 'active' => true, - 'errors' => { 'url' => ['must be a valid URL', %{can't be blank}] } + 'errors' => { 'url' => ['must be a valid URL', %(can't be blank)] } ) end end end - context 'Prometheus service' do - let!(:service) { create(:prometheus_service, project: project) } - let(:service_params) { { manual_configuration: '1', api_url: 'http://example.com' } } + context 'with Prometheus integration' do + let_it_be(:prometheus_integration) { create(:prometheus_integration, project: project) } - context 'feature flag :settings_operations_prometheus_service is enabled' do + let(:integration) { prometheus_integration } + let(:integration_params) { { manual_configuration: '1', api_url: 'http://example.com' } } + + context 'when feature flag :settings_operations_prometheus_service is enabled' do before do stub_feature_flags(settings_operations_prometheus_service: true) end it 'redirects user back to edit page with alert' do - put :update, params: project_params.merge(service: service_params) + put :update, params: project_params.merge(service: integration_params) - expect(response).to redirect_to(edit_project_service_path(project, service)) - expected_alert = "You can now manage your Prometheus settings on the <a href=\"#{project_settings_operations_path(project)}\">Operations</a> page. Fields on this page has been deprecated." + expect(response).to redirect_to(edit_project_service_path(project, integration)) + expected_alert = [ + "You can now manage your Prometheus settings on the", + %(<a href="#{project_settings_operations_path(project)}">Operations</a> page.), + "Fields on this page have been deprecated." + ].join(' ') expect(controller).to set_flash.now[:alert].to(expected_alert) end - it 'does not modify service' do - expect { put :update, params: project_params.merge(service: service_params) }.not_to change { project.prometheus_service.reload.attributes } + it 'does not modify integration' do + expect { put :update, params: project_params.merge(service: integration_params) } + .not_to change { project.prometheus_integration.reload.attributes } end end - context 'feature flag :settings_operations_prometheus_service is disabled' do + context 'when feature flag :settings_operations_prometheus_service is disabled' do before do stub_feature_flags(settings_operations_prometheus_service: false) end - it 'modifies service' do - expect { put :update, params: project_params.merge(service: service_params) }.to change { project.prometheus_service.reload.attributes } + it 'modifies integration' do + expect { put :update, params: project_params.merge(service: integration_params) } + .to change { project.prometheus_integration.reload.attributes } end end end end describe 'GET #edit' do - context 'Jira service' do - let(:service_param) { 'jira' } + context 'with Jira service' do + let(:integration_param) { 'jira' } before do - get :edit, params: project_params(id: service_param) + get :edit, params: project_params(id: integration_param) end context 'with approved services' do @@ -306,25 +318,30 @@ RSpec.describe Projects::ServicesController do end end - context 'Prometheus service' do - let(:service_param) { 'prometheus' } + context 'with Prometheus service' do + let(:integration_param) { 'prometheus' } - context 'feature flag :settings_operations_prometheus_service is enabled' do + context 'when feature flag :settings_operations_prometheus_service is enabled' do before do stub_feature_flags(settings_operations_prometheus_service: true) - get :edit, params: project_params(id: service_param) + get :edit, params: project_params(id: integration_param) end it 'renders deprecation warning notice' do - expected_alert = "You can now manage your Prometheus settings on the <a href=\"#{project_settings_operations_path(project)}\">Operations</a> page. Fields on this page has been deprecated." + expected_alert = [ + "You can now manage your Prometheus settings on the", + %(<a href="#{project_settings_operations_path(project)}">Operations</a> page.), + "Fields on this page have been deprecated." + ].join(' ') + expect(controller).to set_flash.now[:alert].to(expected_alert) end end - context 'feature flag :settings_operations_prometheus_service is disabled' do + context 'when feature flag :settings_operations_prometheus_service is disabled' do before do stub_feature_flags(settings_operations_prometheus_service: false) - get :edit, params: project_params(id: service_param) + get :edit, params: project_params(id: integration_param) end it 'does not render deprecation warning notice' do @@ -340,7 +357,7 @@ RSpec.describe Projects::ServicesController do opts.reverse_merge( namespace_id: project.namespace, project_id: project, - id: service.to_param + id: integration.to_param ) end end |