Skip to content

Commit 71eeabe

Browse files
committed
adds specs for file_store
1 parent b1f8ac3 commit 71eeabe

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

lib/google/api_client/auth/storages/file_store.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initialize(path)
3737
##
3838
# Attempt to read in credentials from the specified file.
3939
def load_credentials
40-
File.open(path, 'r') { |f| JSON.parse(f.read) }
40+
open(path, 'r') { |f| JSON.parse(f.read) }
4141
rescue
4242
nil
4343
end
@@ -49,8 +49,8 @@ def load_credentials
4949
# Optional authorization instance. If not provided, the authorization
5050
# already associated with this instance will be written.
5151
def write_credentials(credentials_hash)
52-
File.open(self.path, 'w') do |file|
53-
file.write(credentials_hash.to_json)
52+
open(self.path, 'w+') do |f|
53+
f.write(credentials_hash.to_json)
5454
end
5555
end
5656
end
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
{
2-
"access_token":"my_access_token",
3-
"authorization_uri":"https://accounts.google.com/o/oauth2/auth",
4-
"client_id":"[email protected]",
5-
"client_secret":"123456_client_secret",
6-
"expires_in":3600,
7-
"refresh_token":"my_refresh_token",
8-
"token_credential_uri":"https://accounts.google.com/o/oauth2/token",
9-
"issued_at":1384440275
10-
}
1+
{ "access_token":"access_token_123456789",
2+
"authorization_uri":"https://accounts.google.com/o/oauth2/auth",
3+
"client_id":"123456789p.apps.googleusercontent.com",
4+
"client_secret":"very_secret",
5+
"expires_in":3600,
6+
"refresh_token":"refresh_token_12345679",
7+
"token_credential_uri":"https://accounts.google.com/o/oauth2/token",
8+
"issued_at":1386053761}

spec/google/api_client/auth/storages/file_store_spec.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,37 @@
22
require_relative '../../../../../lib/google/api_client/auth/storages/file_store'
33

44
describe Google::APIClient::FileStore do
5-
let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) }
5+
let(:root_path) { File.expand_path(File.join(__FILE__, '..','..','..', '..','..')) }
66
let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
77

8-
it 'should initialize'
8+
let(:credentials_hash) {{
9+
"access_token"=>"my_access_token",
10+
"authorization_uri"=>"https://accounts.google.com/o/oauth2/auth",
11+
"client_id"=>"[email protected]",
12+
"client_secret"=>"123456_client_secret",
13+
"expires_in"=>3600,
14+
"refresh_token"=>"my_refresh_token",
15+
"token_credential_uri"=>"https://accounts.google.com/o/oauth2/token",
16+
"issued_at"=>1384440275
17+
}}
918

10-
it 'should load credentials'
19+
subject{Google::APIClient::FileStore.new('a file path')}
1120

12-
it 'should write credentials'
21+
it 'should have a path' do
22+
subject.path.should == 'a file path'
23+
subject.path = 'an other file path'
24+
subject.path.should == 'an other file path'
25+
end
1326

27+
it 'should load credentials' do
28+
subject.path = json_file
29+
credentials = subject.load_credentials
30+
credentials.should include('access_token', 'authorization_uri', 'refresh_token')
31+
end
32+
33+
it 'should write credentials' do
34+
io_stub = StringIO.new
35+
subject.should_receive(:open).and_return(io_stub)
36+
subject.write_credentials(credentials_hash)
37+
end
1438
end

0 commit comments

Comments
 (0)