summaryrefslogtreecommitdiff
path: root/spec/lib/google_api/auth_spec.rb
blob: 87a3f43274ff903b3399ac8fd9a5f5ce5e1848ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'spec_helper'

describe GoogleApi::Auth do
  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
    subject { client.authorize_url }

    it 'returns authorize_url' do
      is_expected.to start_with('https://accounts.google.com/o/oauth2')
      is_expected.to include(URI.encode(redirect_uri, URI::PATTERN::RESERVED))
      is_expected.to include(URI.encode(redirect_to, URI::PATTERN::RESERVED))
    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
      allow_any_instance_of(OAuth2::Strategy::AuthCode)
        .to receive(:get_token).and_return(token)
    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
  end
end