BigW Consortium Gitlab

authorizations_controller_spec.rb 1.35 KB
Newer Older
1 2 3 4 5 6 7 8
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') }

9
    subject { get :callback, code: 'xxx', state: @state }
10 11 12 13 14 15 16 17

    before do
      sign_in(user)

      allow_any_instance_of(GoogleApi::CloudPlatform::Client)
        .to receive(:get_token).and_return([token, expires_at])
    end

18
    it 'sets token and expires_at in session' do
19 20 21 22 23 24 25 26
      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

27
    context 'when redirect uri key is stored in state' do
28
      set(:project) { create(:project) }
29
      let(:redirect_uri) { project_clusters_url(project).to_s }
30 31

      before do
32 33 34
        @state = GoogleApi::CloudPlatform::Client
          .new_session_key_for_redirect_uri do |key|
          session[key] = redirect_uri
35 36 37
        end
      end

38
      it 'redirects to the URL stored in state param' do
39
        expect(subject).to redirect_to(redirect_uri)
40 41 42 43 44 45 46 47 48 49
      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