2019-04-15 06:17:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-05 08:29:22 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-03 23:08:05 -04:00
|
|
|
RSpec.describe GoogleApi::AuthorizationsController do
|
2017-10-05 08:29:22 -04:00
|
|
|
describe 'GET|POST #callback' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:token) { 'token' }
|
|
|
|
let(:expires_at) { 1.hour.since.strftime('%s') }
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
subject { get :callback, params: { code: 'xxx', state: state } }
|
2017-10-05 08:29:22 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
shared_examples_for 'access denied' do
|
|
|
|
it 'returns a 404' do
|
|
|
|
subject
|
2017-10-05 08:29:22 -04:00
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
expect(session[GoogleApi::CloudPlatform::Client.session_key_for_token]).to be_nil
|
2020-02-06 13:08:54 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:not_found)
|
2019-02-12 19:11:28 -05:00
|
|
|
end
|
2017-10-05 08:29:22 -04:00
|
|
|
end
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
context 'session key is present' do
|
|
|
|
let(:session_key) { 'session-key' }
|
|
|
|
let(:redirect_uri) { 'example.com' }
|
2017-10-06 08:28:40 -04:00
|
|
|
|
|
|
|
before do
|
2019-02-12 19:11:28 -05:00
|
|
|
session[GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(session_key)] = redirect_uri
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'session key matches state param' do
|
|
|
|
let(:state) { session_key }
|
|
|
|
|
2020-05-18 08:08:08 -04:00
|
|
|
before do
|
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |instance|
|
|
|
|
allow(instance).to receive(:get_token).and_return([token, expires_at])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
it 'sets token and expires_at in session' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(session[GoogleApi::CloudPlatform::Client.session_key_for_token])
|
|
|
|
.to eq(token)
|
|
|
|
expect(session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at])
|
|
|
|
.to eq(expires_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to the URL stored in state param' do
|
|
|
|
expect(subject).to redirect_to(redirect_uri)
|
2017-10-06 08:28:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
context 'session key does not match state param' do
|
|
|
|
let(:state) { 'bad-key' }
|
|
|
|
|
|
|
|
it_behaves_like 'access denied'
|
2017-10-05 08:29:22 -04:00
|
|
|
end
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
context 'state param is blank' do
|
|
|
|
let(:state) { '' }
|
|
|
|
|
|
|
|
it_behaves_like 'access denied'
|
2017-10-05 08:29:22 -04:00
|
|
|
end
|
2020-05-18 08:08:08 -04:00
|
|
|
|
|
|
|
context 'when a Faraday exception occurs' do
|
|
|
|
let(:state) { session_key }
|
|
|
|
|
|
|
|
[::Faraday::TimeoutError, ::Faraday::ConnectionFailed].each do |error|
|
|
|
|
it "sets a flash alert on #{error}" do
|
|
|
|
allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |instance|
|
|
|
|
allow(instance).to receive(:get_token).and_raise(error.new(nil))
|
|
|
|
end
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(flash[:alert]).to eq('Timeout connecting to the Google API. Please try again.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-10-05 08:29:22 -04:00
|
|
|
end
|
2019-02-12 19:11:28 -05:00
|
|
|
|
|
|
|
context 'state param is present, but session key is blank' do
|
|
|
|
let(:state) { 'session-key' }
|
|
|
|
|
|
|
|
it_behaves_like 'access denied'
|
|
|
|
end
|
2017-10-05 08:29:22 -04:00
|
|
|
end
|
|
|
|
end
|