2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-05 05:17:21 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe GoogleApi::Auth do
|
2017-10-05 05:17:21 -04:00
|
|
|
let(:redirect_uri) { 'http://localhost:3000/google_api/authorizations/callback' }
|
|
|
|
let(:redirect_to) { 'http://localhost:3000/namaspace/project/clusters' }
|
|
|
|
|
|
|
|
let(:client) do
|
|
|
|
GoogleApi::CloudPlatform::Client
|
|
|
|
.new(nil, redirect_uri, state: redirect_to)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#authorize_url' do
|
2020-09-22 14:09:54 -04:00
|
|
|
subject { Addressable::URI.parse(client.authorize_url) }
|
2017-10-05 05:17:21 -04:00
|
|
|
|
|
|
|
it 'returns authorize_url' do
|
2020-09-22 14:09:54 -04:00
|
|
|
expect(subject.to_s).to start_with('https://accounts.google.com/o/oauth2')
|
|
|
|
expect(subject.query_values['state']).to eq(redirect_to)
|
|
|
|
expect(subject.query_values['redirect_uri']).to eq(redirect_uri)
|
2017-10-05 05:17:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#get_token' do
|
|
|
|
let(:token) do
|
|
|
|
double.tap do |dbl|
|
|
|
|
allow(dbl).to receive(:token).and_return('token')
|
|
|
|
allow(dbl).to receive(:expires_at).and_return('expires_at')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2019-11-20 01:06:16 -05:00
|
|
|
allow_next_instance_of(OAuth2::Strategy::AuthCode) do |instance|
|
|
|
|
allow(instance).to receive(:get_token).and_return(token)
|
|
|
|
end
|
2017-10-05 05:17:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns token and expires_at' do
|
|
|
|
token, expires_at = client.get_token('xxx')
|
|
|
|
expect(token).to eq('token')
|
|
|
|
expect(expires_at).to eq('expires_at')
|
|
|
|
end
|
2020-05-18 08:08:08 -04:00
|
|
|
|
|
|
|
it 'expects the client to receive default options' do
|
|
|
|
config = Gitlab::Auth::OAuth::Provider.config_for('google_oauth2')
|
|
|
|
|
|
|
|
expect(OAuth2::Client).to receive(:new).with(
|
|
|
|
config.app_id,
|
|
|
|
config.app_secret,
|
|
|
|
hash_including(
|
|
|
|
**config.args.client_options.deep_symbolize_keys
|
|
|
|
)
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
client.get_token('xxx')
|
|
|
|
end
|
2017-10-05 05:17:21 -04:00
|
|
|
end
|
|
|
|
end
|