21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / spec / google / api_client / auth / storages / redis_store_spec.rb
1 require 'spec_helper'
2
3 require 'google/api_client'
4 require 'google/api_client/version'
5
6
7 describe Google::APIClient::RedisStore do
8   let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..', '..', '..')) }
9   let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
10   let(:redis) {double}
11
12   let(:credentials_hash) { {
13       "access_token" => "my_access_token",
14       "authorization_uri" => "https://accounts.google.com/o/oauth2/auth",
15       "client_id" => "123456_test_client_id@.apps.googleusercontent.com",
16       "client_secret" => "123456_client_secret",
17       "expires_in" => 3600,
18       "refresh_token" => "my_refresh_token",
19       "token_credential_uri" => "https://accounts.google.com/o/oauth2/token",
20       "issued_at" => 1384440275
21   } }
22
23   subject { Google::APIClient::RedisStore.new('a redis instance') }
24
25   it 'should have a redis instance' do
26     expect(subject.redis).to be == 'a redis instance'
27     subject.redis = 'an other redis instance'
28     expect(subject.redis).to be == 'an other redis instance'
29   end
30
31   describe 'load_credentials' do
32
33     it 'should load credentials' do
34       subject.redis= redis
35       expect(redis).to receive(:get).and_return(credentials_hash.to_json)
36       expect(subject.load_credentials).to be == credentials_hash
37     end
38
39     it 'should return nil' do
40       subject.redis= redis
41       expect(redis).to receive(:get).and_return(nil)
42       expect(subject.load_credentials).to be_nil
43     end
44   end
45
46   describe 'redis_credentials_key' do
47     context 'without given key' do
48       it 'should return default key' do
49         expect(subject.redis_credentials_key).to be == "google_api_credentials"
50       end
51     end
52     context 'with given key' do
53       let(:redis_store) { Google::APIClient::RedisStore.new('a redis instance', 'another_google_api_credentials') }
54       it 'should use given key' do
55         expect(redis_store.redis_credentials_key).to be == "another_google_api_credentials"
56       end
57     end
58
59   end
60
61   describe 'write credentials' do
62
63     it 'should write credentials' do
64       subject.redis= redis
65       expect(redis).to receive(:set).and_return('ok')
66       expect(subject.write_credentials(credentials_hash)).to be_truthy
67     end
68   end
69
70 end