Commit 8c5bed72 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'ee-call-notifyservice-from-generic-alert-endpoint' into 'master'

Call NotifyService from NotificationsController

See merge request gitlab-org/gitlab-ee!16508
parents 061dc361 986c6b13
......@@ -11,11 +11,16 @@ module Projects
before_action :check_generic_alert_endpoint_feature_flag!
def create
head :ok
token = extract_alert_manager_token(request)
result = notify_service.execute(token)
head(response_status(result))
end
private
PARAMS_TO_EXCLUDE = %w(controller action namespace_id project_id).freeze
def project_without_auth
@project ||= Project
.find_by_full_path("#{params[:namespace_id]}/#{params[:project_id]}")
......@@ -24,6 +29,25 @@ module Projects
def check_generic_alert_endpoint_feature_flag!
render_404 unless Feature.enabled?(:generic_alert_endpoint, @project)
end
def extract_alert_manager_token(request)
Doorkeeper::OAuth::Token.from_bearer_authorization(request)
end
def notify_service
Projects::Alerting::NotifyService
.new(project, current_user, permitted_params)
end
def response_status(result)
return :ok if result.success?
result.http_status
end
def permitted_params
params.except(*PARAMS_TO_EXCLUDE).permit! # rubocop:disable CodeReuse/ActiveRecord
end
end
end
end
......@@ -7,8 +7,15 @@ describe Projects::Alerting::NotificationsController do
set(:environment) { create(:environment, project: project) }
describe 'POST #create' do
let(:service_response) { ServiceResponse.success }
let(:notify_service) { instance_double(Projects::Alerting::NotifyService, execute: service_response) }
before do
allow(Projects::Alerting::NotifyService).to receive(:new).and_return(notify_service)
end
def make_request(opts = {})
post :create, params: project_params, session: { as: :json }
post :create, params: project_params(opts), session: { as: :json }
end
context 'when feature flag is on' do
......@@ -16,11 +23,68 @@ describe Projects::Alerting::NotificationsController do
stub_feature_flags(generic_alert_endpoint: true)
end
context 'when notification service succeeds' do
let(:payload) do
{
title: 'Alert title',
hosts: 'https://gitlab.com'
}
end
let(:permitted_params) { ActionController::Parameters.new(payload).permit! }
it 'responds with ok' do
make_request
expect(response).to have_gitlab_http_status(:ok)
end
it 'does not pass excluded parameters to the notify service' do
make_request(payload)
expect(Projects::Alerting::NotifyService)
.to have_received(:new)
.with(project, nil, permitted_params)
end
end
context 'when notification service fails' do
let(:service_response) { ServiceResponse.error(message: 'Unauthorized', http_status: 401) }
it 'responds with the service response' do
make_request
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'bearer token' do
context 'when set' do
it 'extracts bearer token' do
request.headers['HTTP_AUTHORIZATION'] = 'Bearer some token'
expect(notify_service).to receive(:execute).with('some token')
make_request
end
it 'pass nil if cannot extract a non-bearer token' do
request.headers['HTTP_AUTHORIZATION'] = 'some token'
expect(notify_service).to receive(:execute).with(nil)
make_request
end
end
context 'when missing' do
it 'passes nil' do
expect(notify_service).to receive(:execute).with(nil)
make_request
end
end
end
end
context 'when feature flag is off' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment