require 'spec_helper'

require 'google/api_client/auth/storages/file_store'

describe Google::APIClient::FileStore do
  let(:json_file) { File.join(FIXTURES_DIR, 'files', 'auth_stored_credentials.json') }

  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
  }}

  subject{Google::APIClient::FileStore.new('a file path')}

  it 'should have a path' do
    expect(subject.path).to be == 'a file path'
    subject.path = 'an other file path'
    expect(subject.path).to be == 'an other file path'
  end

  it 'should load credentials' do
    subject.path = json_file
    credentials = subject.load_credentials
    expect(credentials).to include(
      'access_token', 'authorization_uri', 'refresh_token', 'client_id',
      'client_secret', 'expires_in', 'token_credential_uri', 'issued_at')
  end

  it 'should write credentials' do
    io_stub = StringIO.new
    expect(subject).to receive(:open).and_return(io_stub)
    subject.write_credentials(credentials_hash)
  end
  
  it 'should not load credentials' do
    file = StringIO.new
    file.write "{invalid_hash}"
    invalid_subject = Google::APIClient::FileStore.new(file) 
    expect(invalid_subject.load_credentials).to be_nil
  end
  
end
