summaryrefslogtreecommitdiff
path: root/spec/requests/projects/incident_management/pagerduty_incidents_spec.rb
blob: c246aacb4c731a35a40b22134aff95f057324287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'PagerDuty webhook' do
  let_it_be(:project) { create(:project) }

  describe 'POST /incidents/pagerduty' do
    let(:payload) { Gitlab::Json.parse(fixture_file('pager_duty/webhook_incident_trigger.json')) }
    let(:webhook_processor_class) { ::IncidentManagement::PagerDuty::ProcessWebhookService }
    let(:webhook_processor) { instance_double(webhook_processor_class) }

    def make_request
      headers = { 'Content-Type' => 'application/json' }
      post project_incidents_pagerduty_url(project, token: 'VALID-TOKEN'), params: payload.to_json, headers: headers
    end

    before do
      allow(webhook_processor_class).to receive(:new).and_return(webhook_processor)
      allow(webhook_processor).to receive(:execute).and_return(ServiceResponse.success(http_status: :accepted))
    end

    it 'calls PagerDuty webhook processor with correct parameters' do
      make_request

      expect(webhook_processor_class).to have_received(:new).with(project, nil, payload)
      expect(webhook_processor).to have_received(:execute).with('VALID-TOKEN')
    end

    it 'responds with 202 Accepted' do
      make_request

      expect(response).to have_gitlab_http_status(:accepted)
    end
  end
end