PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/requests/projects/google_cloud/revoke_oauth_controller_spec.rb

https://gitlab.com/523/gitlab-ce
Ruby | 86 lines | 68 code | 17 blank | 1 comment | 0 complexity | 38433484089bdea4b81e84da57c9e780 MD5 | raw file
  1. # frozen_string_literal: true
  2. require 'spec_helper'
  3. RSpec.describe Projects::GoogleCloud::RevokeOauthController do
  4. include SessionHelpers
  5. describe 'POST #create', :snowplow, :clean_gitlab_redis_sessions, :aggregate_failures do
  6. let_it_be(:project) { create(:project, :public) }
  7. let_it_be(:url) { project_google_cloud_revoke_oauth_index_path(project).to_s }
  8. let(:user) { project.creator }
  9. before do
  10. sign_in(user)
  11. stub_session(GoogleApi::CloudPlatform::Client.session_key_for_token => 'token')
  12. allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
  13. allow(client).to receive(:validate_token).and_return(true)
  14. end
  15. end
  16. context 'when GCP token is invalid' do
  17. before do
  18. allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |client|
  19. allow(client).to receive(:validate_token).and_return(false)
  20. end
  21. end
  22. it 'redirects to Google OAuth2 authorize URL' do
  23. sign_in(user)
  24. post url
  25. expect(response).to redirect_to(assigns(:authorize_url))
  26. end
  27. end
  28. context 'when revocation is successful' do
  29. before do
  30. stub_request(:post, "https://oauth2.googleapis.com/revoke")
  31. .to_return(status: 200, body: "", headers: {})
  32. end
  33. it 'calls revoke endpoint and redirects' do
  34. post url
  35. expect(request.session[GoogleApi::CloudPlatform::Client.session_key_for_token]).to be_nil
  36. expect(response).to redirect_to(project_google_cloud_index_path(project))
  37. expect(flash[:notice]).to eq('Google OAuth2 token revocation requested')
  38. expect_snowplow_event(
  39. category: 'Projects::GoogleCloud',
  40. action: 'revoke_oauth#create',
  41. label: 'create',
  42. property: 'success',
  43. project: project,
  44. user: user
  45. )
  46. end
  47. end
  48. context 'when revocation fails' do
  49. before do
  50. stub_request(:post, "https://oauth2.googleapis.com/revoke")
  51. .to_return(status: 400, body: "", headers: {})
  52. end
  53. it 'calls revoke endpoint and redirects' do
  54. post url
  55. expect(request.session[GoogleApi::CloudPlatform::Client.session_key_for_token]).to be_nil
  56. expect(response).to redirect_to(project_google_cloud_index_path(project))
  57. expect(flash[:alert]).to eq('Google OAuth2 token revocation request failed')
  58. expect_snowplow_event(
  59. category: 'Projects::GoogleCloud',
  60. action: 'revoke_oauth#create',
  61. label: 'create',
  62. property: 'failed',
  63. project: project,
  64. user: user
  65. )
  66. end
  67. end
  68. end
  69. end