summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/browser_ui/8_monitor/alert_management/email_notification_for_alert_spec.rb
blob: 70874e46f276409503cf6f459961eb70066a6c84 (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
# frozen_string_literal: true

module QA
  RSpec.describe 'Monitor', :orchestrated, :smtp, :requires_admin, product_group: :respond do
    describe 'Alert' do
      shared_examples 'notification on new alert', :aggregate_failures do
        it 'sends email to user' do
          expect { email_subjects }.to eventually_include(alert_email_subject).within(max_duration: 60)
          expect(recipient_email_addresses).to include(user.email)
        end
      end

      let!(:admin_api_client) { Runtime::API::Client.as_admin }

      let!(:user) do
        Resource::User.fabricate_via_api! do |user|
          user.api_client = admin_api_client
          user.hard_delete_on_api_removal = true
        end
      end

      let(:project) do
        Resource::Project.fabricate_via_api! do |project|
          project.name = 'project-for-alerts'
          project.description = 'Project for alerts'
        end
      end

      let(:alert_title) { Faker::Lorem.word }
      let(:mail_hog_api) { Vendor::MailHog::API.new }
      let(:alert_email_subject) { "#{project.name} | Alert: #{alert_title}" }
      let(:http_payload) { { title: alert_title, description: alert_title } }

      let(:prometheus_payload) do
        {
          version: '4',
          groupKey: nil,
          status: 'firing',
          receiver: '',
          groupLabels: {},
          commonLabels: {},
          commonAnnotations: {},
          externalURL: '',
          alerts: [
            {
              startsAt: Time.now,
              generatorURL: Faker::Internet.url,
              endsAt: nil,
              status: 'firing',
              labels: { gitlab_environment_name: Faker::Lorem.word },
              annotations:
                {
                  title: alert_title,
                  gitlab_y_label: 'status'
                }
            }
          ]
        }
      end

      before do
        Flow::Login.sign_in
        project.visit!
        Flow::AlertSettings.go_to_monitor_settings
        Flow::AlertSettings.enable_email_notification
      end

      context 'when user is a maintainer' do
        before do
          project.add_member(user, Resource::Members::AccessLevel::MAINTAINER)
        end

        context(
          'when using HTTP endpoint integration',
          testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/389993'
        ) do
          before do
            send_http_alert
          end

          it_behaves_like 'notification on new alert'
        end

        context(
          'when using Prometheus integration',
          testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/389994'
        ) do
          before do
            send_prometheus_alert
          end

          it_behaves_like 'notification on new alert'
        end
      end

      context 'when user is an owner' do
        before do
          project.add_member(user, Resource::Members::AccessLevel::OWNER)
        end

        context(
          'when using HTTP endpoint integration',
          testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/390145'
        ) do
          before do
            send_http_alert
          end

          it_behaves_like 'notification on new alert'
        end

        context(
          'when using Prometheus integration',
          testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/390144'
        ) do
          before do
            send_prometheus_alert
          end

          it_behaves_like 'notification on new alert'
        end
      end

      private

      def send_http_alert
        Flow::AlertSettings.setup_http_endpoint_integration
        Flow::AlertSettings.send_test_alert(payload: http_payload)
      end

      def send_prometheus_alert
        Flow::AlertSettings.setup_prometheus_integration
        Flow::AlertSettings.send_test_alert(payload: prometheus_payload)
      end

      def mail_hog_messages
        mail_hog_api.fetch_messages
      end

      def email_subjects
        mail_hog_messages.map(&:subject)
      end

      def recipient_email_addresses
        mail_hog_messages.map(&:to)
      end
    end
  end
end