From 71eeabe55c1c2d8ed2c92fbf6f07fcf99832281f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volker=20Z=C3=B6pfel?= Date: Tue, 3 Dec 2013 08:30:55 +0100 Subject: [PATCH] adds specs for file_store --- .../api_client/auth/storages/file_store.rb | 6 ++-- .../files/auth_stored_credentials.json | 18 +++++------ .../auth/storages/file_store_spec.rb | 32 ++++++++++++++++--- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/google/api_client/auth/storages/file_store.rb b/lib/google/api_client/auth/storages/file_store.rb index 850dee2d0..cd3eae710 100644 --- a/lib/google/api_client/auth/storages/file_store.rb +++ b/lib/google/api_client/auth/storages/file_store.rb @@ -37,7 +37,7 @@ module Google ## # Attempt to read in credentials from the specified file. def load_credentials - File.open(path, 'r') { |f| JSON.parse(f.read) } + open(path, 'r') { |f| JSON.parse(f.read) } rescue nil end @@ -49,8 +49,8 @@ module Google # Optional authorization instance. If not provided, the authorization # already associated with this instance will be written. def write_credentials(credentials_hash) - File.open(self.path, 'w') do |file| - file.write(credentials_hash.to_json) + open(self.path, 'w+') do |f| + f.write(credentials_hash.to_json) end end end diff --git a/spec/fixtures/files/auth_stored_credentials.json b/spec/fixtures/files/auth_stored_credentials.json index 56ff4ec34..4cd786e4a 100644 --- a/spec/fixtures/files/auth_stored_credentials.json +++ b/spec/fixtures/files/auth_stored_credentials.json @@ -1,10 +1,8 @@ -{ - "access_token":"my_access_token", - "authorization_uri":"https://accounts.google.com/o/oauth2/auth", - "client_id":"123456_test_client_id@.apps.googleusercontent.com", - "client_secret":"123456_client_secret", - "expires_in":3600, - "refresh_token":"my_refresh_token", - "token_credential_uri":"https://accounts.google.com/o/oauth2/token", - "issued_at":1384440275 -} \ No newline at end of file +{ "access_token":"access_token_123456789", + "authorization_uri":"https://accounts.google.com/o/oauth2/auth", + "client_id":"123456789p.apps.googleusercontent.com", + "client_secret":"very_secret", + "expires_in":3600, + "refresh_token":"refresh_token_12345679", + "token_credential_uri":"https://accounts.google.com/o/oauth2/token", + "issued_at":1386053761} \ No newline at end of file diff --git a/spec/google/api_client/auth/storages/file_store_spec.rb b/spec/google/api_client/auth/storages/file_store_spec.rb index b2cee7c8d..e2b165f57 100644 --- a/spec/google/api_client/auth/storages/file_store_spec.rb +++ b/spec/google/api_client/auth/storages/file_store_spec.rb @@ -2,13 +2,37 @@ require 'spec_helper' require_relative '../../../../../lib/google/api_client/auth/storages/file_store' describe Google::APIClient::FileStore do - let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) } + let(:root_path) { File.expand_path(File.join(__FILE__, '..','..','..', '..','..')) } let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) } - it 'should initialize' + let(:credentials_hash) {{ + "access_token"=>"my_access_token", + "authorization_uri"=>"https://accounts.google.com/o/oauth2/auth", + "client_id"=>"123456_test_client_id@.apps.googleusercontent.com", + "client_secret"=>"123456_client_secret", + "expires_in"=>3600, + "refresh_token"=>"my_refresh_token", + "token_credential_uri"=>"https://accounts.google.com/o/oauth2/token", + "issued_at"=>1384440275 + }} - it 'should load credentials' + subject{Google::APIClient::FileStore.new('a file path')} - it 'should write credentials' + it 'should have a path' do + subject.path.should == 'a file path' + subject.path = 'an other file path' + subject.path.should == 'an other file path' + end + it 'should load credentials' do + subject.path = json_file + credentials = subject.load_credentials + credentials.should include('access_token', 'authorization_uri', 'refresh_token') + end + + it 'should write credentials' do + io_stub = StringIO.new + subject.should_receive(:open).and_return(io_stub) + subject.write_credentials(credentials_hash) + end end