Commit 8ef2d715 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Call NotifyService from NotificationsController

Connecting the dots by calling
`Projects::Alerting::NotifyService`
from `Projects::Alerting::NotificationsController`
parent a4e3f81f
...@@ -11,7 +11,10 @@ module Projects ...@@ -11,7 +11,10 @@ module Projects
before_action :check_generic_alert_endpoint_feature_flag! before_action :check_generic_alert_endpoint_feature_flag!
def create def create
head :ok token = extract_alert_manager_token(request)
result = notify_service.execute(token)
head(response_status(result))
end end
private private
...@@ -24,6 +27,26 @@ module Projects ...@@ -24,6 +27,26 @@ module Projects
def check_generic_alert_endpoint_feature_flag! def check_generic_alert_endpoint_feature_flag!
render_404 unless Feature.enabled?(:generic_alert_endpoint, @project) render_404 unless Feature.enabled?(:generic_alert_endpoint, @project)
end 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, params.permit!)
end
def response_status(result)
case result.http_status
when 401
:unauthorized
when 403
:forbidden
else
:ok
end
end
end end
end end
end end
...@@ -7,6 +7,13 @@ describe Projects::Alerting::NotificationsController do ...@@ -7,6 +7,13 @@ describe Projects::Alerting::NotificationsController do
set(:environment) { create(:environment, project: project) } set(:environment) { create(:environment, project: project) }
describe 'POST #create' do 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 = {}) def make_request(opts = {})
post :create, params: project_params, session: { as: :json } post :create, params: project_params, session: { as: :json }
end end
...@@ -16,6 +23,7 @@ describe Projects::Alerting::NotificationsController do ...@@ -16,6 +23,7 @@ describe Projects::Alerting::NotificationsController do
stub_feature_flags(generic_alert_endpoint: true) stub_feature_flags(generic_alert_endpoint: true)
end end
context 'when notification service succeeds' do
it 'responds with ok' do it 'responds with ok' do
make_request make_request
...@@ -23,6 +31,45 @@ describe Projects::Alerting::NotificationsController do ...@@ -23,6 +31,45 @@ describe Projects::Alerting::NotificationsController do
end end
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 context 'when feature flag is off' do
before do before do
stub_feature_flags(generic_alert_endpoint: false) stub_feature_flags(generic_alert_endpoint: false)
......
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