summaryrefslogtreecommitdiff
path: root/spec/services/metrics/dashboard/gitlab_alert_embed_service_spec.rb
blob: d5928b1b5afd8a5bff0ea9b0b797db06353c6d37 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Metrics::Dashboard::GitlabAlertEmbedService do
  include MetricsDashboardHelpers

  let_it_be(:alert) { create(:prometheus_alert) }
  let_it_be(:project) { alert.project }
  let_it_be(:user) { create(:user) }
  let(:alert_id) { alert.id }

  before_all do
    project.add_maintainer(user)
  end

  describe '.valid_params?' do
    let(:valid_params) do
      {
        embedded: true,
        prometheus_alert_id: alert_id
      }
    end

    subject { described_class.valid_params?(params) }

    let(:params) { valid_params }

    it { is_expected.to be_truthy }

    context 'missing embedded' do
      let(:params) { valid_params.except(:embedded) }

      it { is_expected.to be_falsey }
    end

    context 'not embedded' do
      let(:params) { valid_params.merge(embedded: 'false') }

      it { is_expected.to be_falsey }
    end

    context 'missing alert id' do
      let(:params) { valid_params.except(:prometheus_alert_id) }

      it { is_expected.to be_falsey }
    end

    context 'missing alert id' do
      let(:params) { valid_params.merge(prometheus_alert_id: 'none') }

      it { is_expected.to be_falsey }
    end
  end

  describe '#get_dashboard' do
    let(:service_params) do
      [
        project,
        user,
        {
          embedded: true,
          prometheus_alert_id: alert_id
        }
      ]
    end

    let(:service_call) { described_class.new(*service_params).get_dashboard }

    context 'when alerting is available' do
      it_behaves_like 'valid embedded dashboard service response'
      it_behaves_like 'raises error for users with insufficient permissions'

      it 'generates an panel based on the alert' do
        result = service_call
        panel = result[:dashboard][:panel_groups][0][:panels][0]
        metric = panel[:metrics].first

        expect(panel[:metrics].length).to eq 1
        expect(panel).to include(
          title: alert.prometheus_metric.title,
          y_label: alert.prometheus_metric.y_label,
          type: 'area-chart'
        )
        expect(metric[:metric_id]).to eq alert.prometheus_metric_id
      end

      context 'when the metric does not exist' do
        let(:alert_id) { -4 }

        it_behaves_like 'misconfigured dashboard service response', :not_found
      end

      it 'does not cache the unprocessed dashboard' do
        # Fail spec if any method of Cache class is called.
        stub_const('Gitlab::Metrics::Dashboard::Cache', double)

        described_class.new(*service_params).get_dashboard
      end
    end
  end
end