summaryrefslogtreecommitdiff
path: root/spec/graphql/types/environment_type_spec.rb
blob: 2220f847e4eb6daef7141b7a0989fa1155f5a14e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe GitlabSchema.types['Environment'] do
  specify { expect(described_class.graphql_name).to eq('Environment') }

  it 'has the expected fields' do
    expected_fields = %w[
      name id state metrics_dashboard latest_opened_most_severe_alert path
    ]

    expect(described_class).to have_graphql_fields(*expected_fields)
  end

  specify { expect(described_class).to require_graphql_authorizations(:read_environment) }

  context 'when there is an environment' do
    let_it_be(:project) { create(:project) }
    let_it_be(:environment) { create(:environment, project: project) }
    let_it_be(:user) { create(:user) }

    subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }

    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            environment(name: "#{environment.name}") {
              name
              path
              state
            }
          }
        }
      )
    end

    before do
      project.add_developer(user)
    end

    it 'returns an environment' do
      expect(subject['data']['project']['environment']['name']).to eq(environment.name)
    end

    it 'returns the path when the feature is enabled' do
      expect(subject['data']['project']['environment']['path']).to eq(
        Gitlab::Routing.url_helpers.project_environment_path(project, environment)
      )
    end

    it 'does not return the path when the feature is disabled' do
      stub_feature_flags(expose_environment_path_in_alert_details: false)

      expect(subject['data']['project']['environment']['path']).to be_nil
    end

    context 'when query alert data for the environment' do
      let_it_be(:query) do
        %(
          query {
            project(fullPath: "#{project.full_path}") {
              environment(name: "#{environment.name}") {
                name
                state
                latestOpenedMostSevereAlert {
                  severity
                  title
                  detailsUrl
                  prometheusAlert {
                    humanizedText
                  }
                }
              }
            }
          }
        )
      end

      it 'does not return alert information' do
        expect(subject['data']['project']['environment']['latestOpenedMostSevereAlert']).to be_nil
      end

      context 'when alert is raised on the environment' do
        let!(:prometheus_alert) { create(:prometheus_alert, project: project, environment: environment) }
        let!(:alert) { create(:alert_management_alert, :triggered, :prometheus, project: project, environment: environment, prometheus_alert: prometheus_alert) }

        it 'returns alert information' do
          expect(subject['data']['project']['environment']['latestOpenedMostSevereAlert']['severity']).to eq(alert.severity.upcase)
        end
      end
    end
  end
end