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

104 lines
2.9 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
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
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(:service_response) { ServiceResponse.success }
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
post :create, params: project_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
2020-06-23 00:09:42 +05:30
it 'responds with ok' 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(:ok)
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)
.with(project, nil, permitted_params)
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
2020-06-23 00:09:42 +05:30
context 'bearer token' do
context 'when set' do
it 'extracts bearer token' do
request.headers['HTTP_AUTHORIZATION'] = 'Bearer some token'
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
expect(notify_service).to receive(:execute).with('some token')
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
make_request
end
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
it 'pass nil if cannot extract a non-bearer token' do
request.headers['HTTP_AUTHORIZATION'] = 'some token'
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
expect(notify_service).to receive(:execute).with(nil)
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
make_request
end
2020-03-13 15:44:24 +05:30
end
2020-06-23 00:09:42 +05:30
context 'when missing' do
it 'passes nil' do
expect(notify_service).to receive(:execute).with(nil)
2020-03-13 15:44:24 +05:30
2020-06-23 00:09:42 +05:30
make_request
end
2020-03-13 15:44:24 +05:30
end
end
end
2020-06-23 00:09:42 +05:30
context 'generic alert payload' do
it_behaves_like 'process alert payload', Projects::Alerting::NotifyService do
let(:payload) { { title: 'Alert title' } }
end
end
context 'Prometheus alert payload' do
include PrometheusHelpers
it_behaves_like 'process alert payload', Projects::Prometheus::Alerts::NotifyService do
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