2017-10-05 08:29:22 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe GoogleApi::AuthorizationsController do
|
|
|
|
describe 'GET|POST #callback' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:token) { 'token' }
|
|
|
|
let(:expires_at) { 1.hour.since.strftime('%s') }
|
|
|
|
|
2017-10-06 08:28:40 -04:00
|
|
|
subject { get :callback, code: 'xxx', state: @state }
|
2017-10-05 08:29:22 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
|
|
|
|
.to receive(:get_token).and_return([token, expires_at])
|
|
|
|
end
|
|
|
|
|
2017-10-06 08:28:40 -04:00
|
|
|
it 'sets token and expires_at in session' do
|
2017-10-05 08:29:22 -04:00
|
|
|
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
|
|
|
|
|
2017-10-06 10:14:14 -04:00
|
|
|
context 'when redirect uri key is stored in state' do
|
2017-10-06 08:28:40 -04:00
|
|
|
set(:project) { create(:project) }
|
2017-10-06 10:14:14 -04:00
|
|
|
let(:redirect_uri) { project_clusters_url(project).to_s }
|
2017-10-06 08:28:40 -04:00
|
|
|
|
|
|
|
before do
|
2017-10-06 10:14:14 -04:00
|
|
|
@state = GoogleApi::CloudPlatform::Client
|
|
|
|
.new_session_key_for_redirect_uri do |key|
|
|
|
|
session[key] = redirect_uri
|
2017-10-06 08:28:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-05 08:29:22 -04:00
|
|
|
it 'redirects to the URL stored in state param' do
|
2017-10-06 10:14:14 -04:00
|
|
|
expect(subject).to redirect_to(redirect_uri)
|
2017-10-05 08:29:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when redirection url is not stored in state' do
|
|
|
|
it 'redirects to root_path' do
|
|
|
|
expect(subject).to redirect_to(root_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|