summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/alerting/notification_payload_parser_spec.rb
blob: f32095b3c8694d5f2e7f68dabd25d58d58d66703 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true

require 'fast_spec_helper'

describe Gitlab::Alerting::NotificationPayloadParser do
  describe '.call' do
    let(:starts_at) { Time.current.change(usec: 0) }
    let(:payload) do
      {
        'title' => 'alert title',
        'start_time' => starts_at.rfc3339,
        'description' => 'Description',
        'monitoring_tool' => 'Monitoring tool name',
        'service' => 'Service',
        'hosts' => ['gitlab.com'],
        'severity' => 'low'
      }
    end

    subject { described_class.call(payload) }

    it 'returns Prometheus-like payload' do
      is_expected.to eq(
        {
          'annotations' => {
            'title' => 'alert title',
            'description' => 'Description',
            'monitoring_tool' => 'Monitoring tool name',
            'service' => 'Service',
            'hosts' => ['gitlab.com'],
            'severity' => 'low'
          },
          'startsAt' => starts_at.rfc3339
        }
      )
    end

    context 'when title is blank' do
      before do
        payload[:title] = ''
      end

      it 'sets a predefined title' do
        expect(subject.dig('annotations', 'title')).to eq('New: Incident')
      end
    end

    context 'when hosts attribute is a string' do
      before do
        payload[:hosts] = 'gitlab.com'
      end

      it 'returns hosts as an array of one element' do
        expect(subject.dig('annotations', 'hosts')).to eq(['gitlab.com'])
      end
    end

    context 'when the time is in unsupported format' do
      before do
        payload[:start_time] = 'invalid/date/format'
      end

      it 'sets startsAt to a current time in RFC3339 format' do
        expect(subject['startsAt']).to eq(starts_at.rfc3339)
      end
    end

    context 'when payload is blank' do
      let(:payload) { {} }

      it 'returns default parameters' do
        is_expected.to match(
          'annotations' => {
            'title' => described_class::DEFAULT_TITLE,
            'severity' => described_class::DEFAULT_SEVERITY
          },
          'startsAt' => starts_at.rfc3339
        )
      end

      context 'when severity is blank' do
        before do
          payload[:severity] = ''
        end

        it 'sets severity to the default ' do
          expect(subject.dig('annotations', 'severity')).to eq(described_class::DEFAULT_SEVERITY)
        end
      end
    end

    context 'when payload attributes have blank lines' do
      let(:payload) do
        {
          'title' => '',
          'start_time' => '',
          'description' => '',
          'monitoring_tool' => '',
          'service' => '',
          'hosts' => ['']
        }
      end

      it 'returns default parameters' do
        is_expected.to eq(
          'annotations' => {
            'title' => 'New: Incident',
            'severity' => described_class::DEFAULT_SEVERITY
          },
          'startsAt' => starts_at.rfc3339
        )
      end
    end

    context 'when payload has secondary params' do
      let(:payload) do
        {
          'description' => 'Description',
          'additional' => {
            'params' => {
              '1' => 'Some value 1',
              '2' => 'Some value 2',
              'blank' => ''
            }
          }
        }
      end

      it 'adds secondary params to annotations' do
        is_expected.to eq(
          'annotations' => {
            'title' => 'New: Incident',
            'severity' => described_class::DEFAULT_SEVERITY,
            'description' => 'Description',
            'additional.params.1' => 'Some value 1',
            'additional.params.2' => 'Some value 2'
          },
          'startsAt' => starts_at.rfc3339
        )
      end
    end

    context 'when secondary params hash is too big' do
      before do
        allow(Gitlab::Utils::SafeInlineHash).to receive(:merge_keys!).and_raise(ArgumentError)
      end

      it 'catches and re-raises an error' do
        expect { subject }.to raise_error Gitlab::Alerting::NotificationPayloadParser::BadPayloadError, 'The payload is too big'
      end
    end
  end
end