diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-05 16:54:15 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-05 16:54:15 +0300 |
commit | be834a25982746ffd85252ff502df42bb88cb9d5 (patch) | |
tree | b4d6a8ba0931e12fac08f05abea33a3b8ec2c8a2 /spec/lib/pager_duty/webhook_payload_parser_spec.rb | |
parent | ee925a3597f27e92f83a50937a64068109675b3d (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc32
Diffstat (limited to 'spec/lib/pager_duty/webhook_payload_parser_spec.rb')
-rw-r--r-- | spec/lib/pager_duty/webhook_payload_parser_spec.rb | 84 |
1 files changed, 58 insertions, 26 deletions
diff --git a/spec/lib/pager_duty/webhook_payload_parser_spec.rb b/spec/lib/pager_duty/webhook_payload_parser_spec.rb index 0010165318d..54c61b9121c 100644 --- a/spec/lib/pager_duty/webhook_payload_parser_spec.rb +++ b/spec/lib/pager_duty/webhook_payload_parser_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'fast_spec_helper' +require 'json_schemer' RSpec.describe PagerDuty::WebhookPayloadParser do describe '.call' do @@ -8,36 +9,36 @@ RSpec.describe PagerDuty::WebhookPayloadParser do File.read(File.join(File.dirname(__FILE__), '../../fixtures/pager_duty/webhook_incident_trigger.json')) end + let(:triggered_event) do + { + 'event' => 'incident.trigger', + 'incident' => { + 'url' => 'https://webdemo.pagerduty.com/incidents/PRORDTY', + 'incident_number' => 33, + 'title' => 'My new incident', + 'status' => 'triggered', + 'created_at' => '2017-09-26T15:14:36Z', + 'urgency' => 'high', + 'incident_key' => nil, + 'assignees' => [{ + 'summary' => 'Laura Haley', + 'url' => 'https://webdemo.pagerduty.com/users/P553OPV' + }], + 'impacted_services' => [{ + 'summary' => 'Production XDB Cluster', + 'url' => 'https://webdemo.pagerduty.com/services/PN49J75' + }] + } + } + end + subject(:parse) { described_class.call(payload) } context 'when payload is a correct PagerDuty payload' do let(:payload) { Gitlab::Json.parse(fixture_file) } it 'returns parsed payload' do - is_expected.to eq( - [ - { - 'event' => 'incident.trigger', - 'incident' => { - 'url' => 'https://webdemo.pagerduty.com/incidents/PRORDTY', - 'incident_number' => 33, - 'title' => 'My new incident', - 'status' => 'triggered', - 'created_at' => '2017-09-26T15:14:36Z', - 'urgency' => 'high', - 'incident_key' => nil, - 'assignees' => [{ - 'summary' => 'Laura Haley', - 'url' => 'https://webdemo.pagerduty.com/users/P553OPV' - }], - 'impacted_services' => [{ - 'summary' => 'Production XDB Cluster', - 'url' => 'https://webdemo.pagerduty.com/services/PN49J75' - }] - } - } - ] - ) + is_expected.to eq([triggered_event]) end context 'when assignments summary and html_url are blank' do @@ -69,11 +70,42 @@ RSpec.describe PagerDuty::WebhookPayloadParser do end end - context 'when payload has no incidents' do + context 'when payload schema is invalid' do let(:payload) { { 'messages' => [{ 'event' => 'incident.trigger' }] } } it 'returns payload with blank incidents' do - is_expected.to eq([{ 'event' => 'incident.trigger', 'incident' => {} }]) + is_expected.to eq([]) + end + end + + context 'when payload consists of two messages' do + context 'when one of the messages has no incident data' do + let(:payload) do + valid_payload = Gitlab::Json.parse(fixture_file) + event = { 'event' => 'incident.trigger' } + valid_payload['messages'] = valid_payload['messages'].append(event) + valid_payload + end + + it 'returns parsed payload with valid events only' do + is_expected.to eq([triggered_event]) + end + end + + context 'when one of the messages has unknown event' do + let(:payload) do + valid_payload = Gitlab::Json.parse(fixture_file) + event = { 'event' => 'incident.unknown', 'incident' => valid_payload['messages'].first['incident'] } + valid_payload['messages'] = valid_payload['messages'].append(event) + valid_payload + end + + it 'returns parsed payload' do + unknown_event = triggered_event.dup + unknown_event['event'] = 'incident.unknown' + + is_expected.to contain_exactly(triggered_event, unknown_event) + end end end end |