summaryrefslogtreecommitdiff
path: root/spec/requests/projects/google_cloud/gcp_regions_controller_spec.rb
blob: e77bcdb40b84c6344210dec412f5f5b71b92399c (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::GoogleCloud::GcpRegionsController do
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:repository) { project.repository }

  let_it_be(:user_guest) { create(:user) }
  let_it_be(:user_maintainer) { create(:user) }

  RSpec.shared_examples "should track not_found event" do
    it "tracks event" do
      is_expected.to be(404)
      expect_snowplow_event(
        category: 'Projects::GoogleCloud::GcpRegionsController',
        action: 'error_invalid_user',
        label: nil,
        project: project,
        user: nil
      )
    end
  end

  RSpec.shared_examples "should track access_denied event" do
    it "tracks event" do
      is_expected.to be(404)
      expect_snowplow_event(
        category: 'Projects::GoogleCloud::GcpRegionsController',
        action: 'error_invalid_user',
        label: nil,
        project: project,
        user: nil
      )
    end
  end

  RSpec.shared_examples "should track feature_flag_disabled event" do |user|
    it "tracks event" do
      is_expected.to be(404)
      expect_snowplow_event(
        category: 'Projects::GoogleCloud::GcpRegionsController',
        action: 'error_feature_flag_not_enabled',
        label: nil,
        project: project,
        user: user_maintainer
      )
    end
  end

  RSpec.shared_examples "should track gcp_error event" do |config|
    it "tracks event" do
      is_expected.to be(403)
      expect_snowplow_event(
        category: 'Projects::GoogleCloud::GcpRegionsController',
        action: 'error_google_oauth2_not_enabled',
        label: nil,
        project: project,
        user: user_maintainer
      )
    end
  end

  RSpec.shared_examples "should be not found" do
    it 'returns not found' do
      is_expected.to be(404)
    end
  end

  RSpec.shared_examples "should be forbidden" do
    it 'returns forbidden' do
      is_expected.to be(403)
    end
  end

  RSpec.shared_examples "public request should 404" do
    it_behaves_like "should be not found"
    it_behaves_like "should track not_found event"
  end

  RSpec.shared_examples "unauthorized access should 404" do
    before do
      project.add_guest(user_guest)
    end

    it_behaves_like "should be not found"
    it_behaves_like "should track access_denied event"
  end

  describe 'GET #index', :snowplow do
    subject { get project_google_cloud_gcp_regions_path(project) }

    it_behaves_like "public request should 404"
    it_behaves_like "unauthorized access should 404"

    context 'when authorized members make requests' do
      before do
        project.add_maintainer(user_maintainer)
        sign_in(user_maintainer)
      end

      it 'renders gcp_regions' do
        is_expected.to render_template('projects/google_cloud/gcp_regions/index')
      end

      context 'but gitlab instance is not configured for google oauth2' do
        unconfigured_google_oauth2 = Struct.new(:app_id, :app_secret)
                                           .new('', '')

        before do
          allow(Gitlab::Auth::OAuth::Provider).to receive(:config_for)
                                                    .with('google_oauth2')
                                                    .and_return(unconfigured_google_oauth2)
        end

        it_behaves_like "should be forbidden"
        it_behaves_like "should track gcp_error event", unconfigured_google_oauth2
      end

      context 'but feature flag is disabled' do
        before do
          stub_feature_flags(incubation_5mp_google_cloud: false)
        end

        it_behaves_like "should be not found"
        it_behaves_like "should track feature_flag_disabled event"
      end
    end
  end

  describe 'POST #index', :snowplow do
    subject { post project_google_cloud_gcp_regions_path(project), params: { gcp_region: 'region1', environment: 'env1' } }

    it_behaves_like "public request should 404"
    it_behaves_like "unauthorized access should 404"

    context 'when authorized members make requests' do
      before do
        project.add_maintainer(user_maintainer)
        sign_in(user_maintainer)
      end

      it 'redirects to google cloud configurations' do
        is_expected.to redirect_to(project_google_cloud_configuration_path(project))
      end
    end
  end
end