Welcome to mirror list, hosted at ThFree Co, Russian Federation.

revoke_oauth_controller_spec.rb « google_cloud « projects « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07590d3710e75ad03c5e6e8cbeda3886eb56f68a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::GoogleCloud::RevokeOauthController do
  include SessionHelpers

  describe 'POST #create', :snowplow, :clean_gitlab_redis_sessions, :aggregate_failures do
    let_it_be(:project) { create(:project, :public) }
    let_it_be(:url) { project_google_cloud_revoke_oauth_index_path(project).to_s }

    let(:user) { project.creator }

    before do
      sign_in(user)

      stub_session(GoogleApi::CloudPlatform::Client.session_key_for_token => 'token')

      allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
        allow(client).to receive(:validate_token).and_return(true)
      end
    end

    context 'when GCP token is invalid' do
      before do
        allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
          allow(client).to receive(:validate_token).and_return(false)
        end
      end

      it 'redirects to Google OAuth2 authorize URL' do
        sign_in(user)

        post url

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

    context 'when revocation is successful' do
      before do
        stub_request(:post, "https://oauth2.googleapis.com/revoke")
          .to_return(status: 200, body: "", headers: {})
      end

      it 'calls revoke endpoint and redirects' do
        post url

        expect(request.session[GoogleApi::CloudPlatform::Client.session_key_for_token]).to be_nil
        expect(response).to redirect_to(project_google_cloud_index_path(project))
        expect(flash[:notice]).to eq('Google OAuth2 token revocation requested')
        expect_snowplow_event(
          category: 'Projects::GoogleCloud',
          action: 'revoke_oauth#create',
          label: 'create',
          property: 'success',
          project: project,
          user: user
        )
      end
    end

    context 'when revocation fails' do
      before do
        stub_request(:post, "https://oauth2.googleapis.com/revoke")
          .to_return(status: 400, body: "", headers: {})
      end

      it 'calls revoke endpoint and redirects' do
        post url

        expect(request.session[GoogleApi::CloudPlatform::Client.session_key_for_token]).to be_nil
        expect(response).to redirect_to(project_google_cloud_index_path(project))
        expect(flash[:alert]).to eq('Google OAuth2 token revocation request failed')
        expect_snowplow_event(
          category: 'Projects::GoogleCloud',
          action: 'revoke_oauth#create',
          label: 'create',
          property: 'failed',
          project: project,
          user: user
        )
      end
    end
  end
end