summaryrefslogtreecommitdiff
path: root/spec/helpers/operations_helper_spec.rb
blob: e1bd477bc7544fe38a78b9364d56781afca6cf6b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe OperationsHelper do
  include Gitlab::Routing

  let_it_be(:user) { create(:user) }
  let_it_be(:project, reload: true) { create(:project) }

  before do
    helper.instance_variable_set(:@project, project)
    allow(helper).to receive(:current_user) { user }
  end

  describe '#alerts_settings_data' do
    subject { helper.alerts_settings_data }

    before do
      allow(helper).to receive(:can?).with(user, :admin_operations, project) { true }
    end

    context 'initial service configuration' do
      let_it_be(:prometheus_service) { PrometheusService.new(project: project) }

      before do
        allow(project).to receive(:find_or_initialize_service).and_call_original
        allow(project).to receive(:find_or_initialize_service).with('prometheus').and_return(prometheus_service)
      end

      it 'returns the correct values' do
        expect(subject).to eq(
          'alerts_setup_url' => help_page_path('operations/incident_management/integrations.md', anchor: 'configuration'),
          'alerts_usage_url' => project_alert_management_index_path(project),
          'prometheus_form_path' => project_service_path(project, prometheus_service),
          'prometheus_reset_key_path' => reset_alerting_token_project_settings_operations_path(project),
          'prometheus_authorization_key' => nil,
          'prometheus_api_url' => nil,
          'prometheus_activated' => 'false',
          'prometheus_url' => notify_project_prometheus_alerts_url(project, format: :json),
          'disabled' => 'false',
          'project_path' => project.full_path,
          'multi_integrations' => 'false',
          'templates' => '[]',
          'create_issue' => 'false',
          'issue_template_key' => '',
          'send_email' => 'false',
          'auto_close_incident' => 'true',
          'operations_settings_endpoint' => project_settings_operations_path(project),
          'pagerduty_reset_key_path' => reset_pagerduty_token_project_settings_operations_path(project)
        )
      end
    end

    context 'with external Prometheus configured' do
      let_it_be(:prometheus_service, reload: true) do
        create(:prometheus_service, project: project)
      end

      context 'with external Prometheus enabled' do
        it 'returns the correct values' do
          expect(subject).to include(
            'prometheus_activated' => 'true',
            'prometheus_api_url' => prometheus_service.api_url
          )
        end
      end

      context 'with external Prometheus disabled' do
        shared_examples 'Prometheus is disabled' do
          it 'returns the correct values' do
            expect(subject).to include(
              'prometheus_activated' => 'false',
              'prometheus_api_url' => prometheus_service.api_url
            )
          end
        end

        let(:cluster_managed) { false }

        before do
          allow(prometheus_service)
            .to receive(:prometheus_available?)
            .and_return(cluster_managed)

          prometheus_service.update!(manual_configuration: false)
        end

        include_examples 'Prometheus is disabled'

        context 'when cluster managed' do
          let(:cluster_managed) { true }

          include_examples 'Prometheus is disabled'
        end
      end

      context 'with project alert setting' do
        let_it_be(:project_alerting_setting) { create(:project_alerting_setting, project: project) }

        it 'returns the correct values' do
          expect(subject).to include(
            'prometheus_authorization_key' => project_alerting_setting.token,
            'prometheus_api_url' => prometheus_service.api_url
          )
        end
      end
    end
  end

  describe '#operations_settings_data' do
    let_it_be(:operations_settings) do
      create(
        :project_incident_management_setting,
        project: project,
        pagerduty_active: true
      )
    end

    subject { helper.operations_settings_data }

    it 'returns the correct set of data' do
      is_expected.to include(
        operations_settings_endpoint: project_settings_operations_path(project),
        pagerduty_active: 'true',
        pagerduty_token: operations_settings.pagerduty_token,
        pagerduty_webhook_url: project_incidents_integrations_pagerduty_url(project, token: operations_settings.pagerduty_token),
        pagerduty_reset_key_path: reset_pagerduty_token_project_settings_operations_path(project)
      )
    end
  end
end