alerts_spec.rb 3.76 KB
Newer Older
1 2 3 4 5 6
# frozen_string_literal: true
require 'spec_helper'

describe 'getting Alert Management Alerts' do
  include GraphqlHelpers

7
  let_it_be(:payload) { { 'custom' => { 'alert' => 'payload' } } }
8 9
  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:current_user) { create(:user) }
10
  let_it_be(:alert_1) { create(:alert_management_alert, :all_fields, :resolved, project: project, severity: :low) }
11 12
  let_it_be(:alert_2) { create(:alert_management_alert, :all_fields, project: project, severity: :critical, payload: payload) }
  let_it_be(:other_project_alert) { create(:alert_management_alert, :all_fields) }
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

  let(:fields) do
    <<~QUERY
      nodes {
        #{all_graphql_fields_for('AlertManagementAlert'.classify)}
      }
    QUERY
  end

  let(:query) do
    graphql_query_for(
      'project',
      { 'fullPath' => project.full_path },
      query_graphql_field('alertManagementAlerts', {}, fields)
    )
  end

  context 'with alert data' do
    let(:alerts) { graphql_data.dig('project', 'alertManagementAlerts', 'nodes') }

    context 'without project permissions' do
      let(:user) { create(:user) }

      before do
        post_graphql(query, current_user: current_user)
      end

      it_behaves_like 'a working graphql query'

      it { expect(alerts).to be nil }
    end

    context 'with project permissions' do
      before do
        project.add_developer(current_user)
        post_graphql(query, current_user: current_user)
      end

      let(:first_alert) { alerts.first }
52
      let(:second_alert) { alerts.second }
53 54 55 56

      it_behaves_like 'a working graphql query'

      it { expect(alerts.size).to eq(2) }
57 58

      it 'returns the correct properties of the alerts' do
59 60 61
        expect(first_alert).to include(
          'iid' => alert_2.iid.to_s,
          'title' => alert_2.title,
62
          'description' => alert_2.description,
63
          'severity' => alert_2.severity.upcase,
64
          'status' => 'TRIGGERED',
65 66
          'monitoringTool' => alert_2.monitoring_tool,
          'service' => alert_2.service,
67
          'hosts' => alert_2.hosts,
68 69
          'eventCount' => alert_2.events,
          'startedAt' => alert_2.started_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
70
          'endedAt' => nil,
71 72 73
          'details' => { 'custom.alert' => 'payload' },
          'createdAt' => alert_2.created_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
          'updatedAt' => alert_2.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
74
        )
75 76 77 78 79

        expect(second_alert).to include(
          'status' => 'RESOLVED',
          'endedAt' => alert_1.ended_at.strftime('%Y-%m-%dT%H:%M:%SZ')
        )
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      end

      context 'with iid given' do
        let(:query) do
          graphql_query_for(
            'project',
            { 'fullPath' => project.full_path },
            query_graphql_field('alertManagementAlerts', { iid: alert_1.iid.to_s }, fields)
          )
        end

        it_behaves_like 'a working graphql query'

        it { expect(alerts.size).to eq(1) }
        it { expect(first_alert['iid']).to eq(alert_1.iid.to_s) }
      end
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

      context 'sorting data given' do
        let(:query) do
          graphql_query_for(
            'project',
            { 'fullPath' => project.full_path },
            query_graphql_field('alertManagementAlerts', params, fields)
          )
        end

        let(:params) { 'sort: SEVERITY_DESC' }
        let(:iids) { alerts.map { |a| a['iid'] } }

        it_behaves_like 'a working graphql query'

        it 'sorts in the correct order' do
          expect(iids).to eq [alert_1.iid.to_s, alert_2.iid.to_s]
        end

        context 'ascending order' do
          let(:params) { 'sort: SEVERITY_ASC' }

          it 'sorts in the correct order' do
            expect(iids).to eq [alert_2.iid.to_s, alert_1.iid.to_s]
          end
        end
      end
123 124 125
    end
  end
end