summaryrefslogtreecommitdiff
path: root/spec/graphql/resolvers/alert_management/integrations_resolver_spec.rb
blob: fb0fb6729d4a80fb6d8743e07c26153d4cb6054b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Resolvers::AlertManagement::IntegrationsResolver do
  include GraphqlHelpers

  let_it_be(:current_user) { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:project2) { create(:project) }
  let_it_be(:prometheus_integration) { create(:prometheus_service, project: project) }
  let_it_be(:active_http_integration) { create(:alert_management_http_integration, project: project) }
  let_it_be(:inactive_http_integration) { create(:alert_management_http_integration, :inactive, project: project) }
  let_it_be(:other_proj_integration) { create(:alert_management_http_integration, project: project2) }
  let_it_be(:other_proj_prometheus_integration) { create(:prometheus_service, project: project2) }

  let(:params) { {} }

  subject { sync(resolve_http_integrations(params)) }

  specify do
    expect(described_class).to have_nullable_graphql_type(Types::AlertManagement::IntegrationType.connection_type)
  end

  context 'user does not have permission' do
    it { is_expected.to be_empty }
  end

  context 'user has permission' do
    before do
      project.add_maintainer(current_user)
      project2.add_maintainer(current_user)
    end

    it { is_expected.to contain_exactly(active_http_integration, prometheus_integration) }

    context 'when HTTP Integration ID is given' do
      context 'when integration is from the current project' do
        let(:params) { { id: global_id_of(inactive_http_integration) } }

        it { is_expected.to contain_exactly(inactive_http_integration) }
      end

      context 'when integration is from other project' do
        let(:params) { { id: global_id_of(other_proj_integration) } }

        it { is_expected.to be_empty }
      end
    end

    context 'when Prometheus Integration ID is given' do
      context 'when integration is from the current project' do
        let(:params) { { id: global_id_of(prometheus_integration) } }

        it { is_expected.to contain_exactly(prometheus_integration) }
      end

      context 'when integration is from other project' do
        let(:params) { { id: global_id_of(other_proj_prometheus_integration) } }

        it { is_expected.to be_empty }
      end
    end
  end

  private

  def resolve_http_integrations(args = {}, context = { current_user: current_user })
    resolve(described_class, obj: project, args: args, ctx: context)
  end
end