debian-mirror-gitlab/spec/controllers/projects/alerting/notifications_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

172 lines
5.2 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::Alerting::NotificationsController do
2021-11-18 22:05:49 +05:30
include HttpBasicAuthHelpers
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
2021-04-29 21:17:54 +05:30
2021-01-29 00:20:46 +05:30
let(:params) { project_params }
2020-03-13 15:44:24 +05:30
describe 'POST #create' do
around do |example|
ForgeryProtection.with_forgery_protection { example.run }
end
2020-06-23 00:09:42 +05:30
shared_examples 'process alert payload' do |notify_service_class|
let(:notify_service) { instance_double(notify_service_class, execute: service_response) }
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
before do
allow(notify_service_class).to receive(:new).and_return(notify_service)
end
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
def make_request
2021-01-29 00:20:46 +05:30
post :create, params: params, body: payload.to_json, as: :json
2020-03-13 15:44:24 +05:30
end
2020-06-23 00:09:42 +05:30
context 'when notification service succeeds' do
let(:permitted_params) { ActionController::Parameters.new(payload).permit! }
2020-03-13 15:44:24 +05:30
2021-12-11 22:18:48 +05:30
it 'responds with the alert data' do
2020-06-23 00:09:42 +05:30
make_request
2020-03-13 15:44:24 +05:30
2023-01-13 00:05:48 +05:30
if service_response.payload.present?
expect(json_response).to contain_exactly(
{ 'iid' => alert_1.iid, 'title' => alert_1.title },
{ 'iid' => alert_2.iid, 'title' => alert_2.title }
)
end
expect(response).to have_gitlab_http_status(service_response.http_status)
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
it 'does not pass excluded parameters to the notify service' do
make_request
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
expect(notify_service_class)
.to have_received(:new)
2021-02-22 17:27:13 +05:30
.with(project, permitted_params)
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
end
2020-06-23 00:09:42 +05:30
context 'when notification service fails' do
let(:service_response) { ServiceResponse.error(message: 'Unauthorized', http_status: :unauthorized) }
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
it 'responds with the service response' do
make_request
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
end
2020-03-13 15:44:24 +05:30
end
2021-11-18 22:05:49 +05:30
shared_examples 'a working token' do
it 'extracts token' do
expect(notify_service).to receive(:execute).with('some token', nil)
2021-04-29 21:17:54 +05:30
2021-11-18 22:05:49 +05:30
make_request
end
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
context 'with a corresponding integration' do
context 'with integration parameters specified' do
let_it_be_with_reload(:integration) { create(:alert_management_http_integration, project: project) }
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
let(:params) { project_params(endpoint_identifier: integration.endpoint_identifier, name: integration.name) }
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
context 'the integration is active' do
it 'extracts and finds the integration' do
expect(notify_service).to receive(:execute).with('some token', integration)
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
make_request
2021-01-29 00:20:46 +05:30
end
2021-11-18 22:05:49 +05:30
end
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
context 'when the integration is inactive' do
before do
integration.update!(active: false)
end
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
it 'does not find an integration' do
expect(notify_service).to receive(:execute).with('some token', nil)
2021-01-29 00:20:46 +05:30
2021-11-18 22:05:49 +05:30
make_request
2021-01-29 00:20:46 +05:30
end
end
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
2021-11-18 22:05:49 +05:30
context 'without integration parameters specified' do
let_it_be(:integration) { create(:alert_management_http_integration, :legacy, project: project) }
2020-03-13 15:44:24 +05:30
2021-11-18 22:05:49 +05:30
it 'extracts and finds the legacy integration' do
expect(notify_service).to receive(:execute).with('some token', integration)
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
make_request
end
2020-06-23 00:09:42 +05:30
end
2020-03-13 15:44:24 +05:30
end
2021-11-18 22:05:49 +05:30
end
2020-03-13 15:44:24 +05:30
2021-11-18 22:05:49 +05:30
context 'with bearer token' do
context 'when set' do
before do
request.headers.merge(build_token_auth_header('some token'))
2020-06-23 00:09:42 +05:30
end
2021-11-18 22:05:49 +05:30
it_behaves_like 'a working token'
end
end
context 'with basic auth token' do
before do
request.headers.merge basic_auth_header(nil, 'some token')
end
it_behaves_like 'a working token'
end
context 'when inextractable token' do
it 'passes nil for a non-bearer token' do
request.headers['HTTP_AUTHORIZATION'] = 'some token'
expect(notify_service).to receive(:execute).with(nil, nil)
make_request
end
end
context 'when missing token' do
it 'passes nil' do
expect(notify_service).to receive(:execute).with(nil, nil)
make_request
2020-03-13 15:44:24 +05:30
end
end
end
2020-06-23 00:09:42 +05:30
2021-11-18 22:05:49 +05:30
context 'with generic alert payload' do
2020-06-23 00:09:42 +05:30
it_behaves_like 'process alert payload', Projects::Alerting::NotifyService do
2023-01-13 00:05:48 +05:30
let(:alert_1) { build(:alert_management_alert, project: project) }
let(:alert_2) { build(:alert_management_alert, project: project) }
let(:service_response) { ServiceResponse.success(payload: { alerts: [alert_1, alert_2] }) }
2020-06-23 00:09:42 +05:30
let(:payload) { { title: 'Alert title' } }
end
end
2021-11-18 22:05:49 +05:30
context 'with Prometheus alert payload' do
2020-06-23 00:09:42 +05:30
include PrometheusHelpers
it_behaves_like 'process alert payload', Projects::Prometheus::Alerts::NotifyService do
2023-01-13 00:05:48 +05:30
let(:service_response) { ServiceResponse.success(http_status: :created) }
2020-06-23 00:09:42 +05:30
let(:payload) { prometheus_alert_payload }
end
end
2020-03-13 15:44:24 +05:30
end
2020-06-23 00:09:42 +05:30
private
2020-03-13 15:44:24 +05:30
def project_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace, project_id: project)
end
end