summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/requests/projects/google_cloud/google_oauth2_token_examples.rb
blob: 379327be0db77f867f29d4ed7122951f7e15061a (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
# frozen_string_literal: true

RSpec.shared_examples 'requires valid Google Oauth2 token' do
  context 'when a valid Google OAuth2 token does not exist' do
    before do
      project.add_maintainer(user)
      sign_in(user)
    end

    it 'triggers Google OAuth2 flow on request' do
      subject

      expect(response).to redirect_to(assigns(:authorize_url))
    end

    context 'and a valid Google OAuth2 token gets created' do
      before do
        allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
          allow(client).to receive(:validate_token).and_return(true)
          allow(client).to receive(:list_projects).and_return(mock_gcp_projects) if mock_gcp_projects
        end

        allow_next_instance_of(BranchesFinder) do |finder|
          allow(finder).to receive(:execute).and_return(mock_branches) if mock_branches
        end

        allow_next_instance_of(TagsFinder) do |finder|
          allow(finder).to receive(:execute).and_return(mock_branches) if mock_branches
        end
      end

      it 'renders template as expected' do
        if renders_template
          subject
          expect(response).to render_template(renders_template)
        end
      end

      it 'redirects as expected' do
        if redirects_to
          subject
          expect(response).to redirect_to(redirects_to)
        end
      end
    end
  end
end