Sha256: c13c7e7820fb331b58b60dec88a3b145eddb1fece265d245cbb50935be77551f

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

require 'spec_helper'

RSpec.describe TeslaApi::Client do
  subject(:tesla_api) { TeslaApi::Client.new(ENV['TESLA_EMAIL']) }

  describe '#new client' do
    it 'has no expiry date' do
      expect(tesla_api.expired_at).to eq(nil)
    end

    it 'has a expiry status set to true' do
      expect(tesla_api.expired?).to eq(true)
    end
  end

  describe '#token=' do
    it 'sets a Bearer token' do
      tesla_api.token = Faker::Lorem.characters(32)
      expect(tesla_api.class.headers).to include({'Authorization' => /Bearer [a-z0-9]{32}/})
    end
  end

  describe '#login!', vcr: { cassette_name: 'client-login' } do
    it { is_expected.to be_a(TeslaApi::Client) }

    it 'logs into the API' do
      tesla_api.login!(ENV['TESLA_PASS'])
      expect(a_request(:post, "https://#{URI.parse(tesla_api.class.base_uri).host}/oauth/token")).to have_been_made.once
    end

    it 'set a expiry date' do
      tesla_api.login!(ENV['TESLA_PASS'])
      expect(tesla_api.expired_at).to eq(Time.at(1475777133 + 7776000).to_datetime)
    end

    it 'expose expiry status' do
      tesla_api.login!(ENV['TESLA_PASS'])
      tesla_api.created_at = (Time.now - 1).to_i
      expect(tesla_api.expired?).to eq(false)
    end

    it 'is expired when has a 90+ days old date' do
      tesla_api.login!(ENV['TESLA_PASS'])
      tesla_api.created_at = (Time.now - 7776000 - 1).to_i
      expect(tesla_api.expired?).to eq(true)
    end

    it 'sets a Bearer token header' do
      tesla_api.login!(ENV['TESLA_PASS'])
      expect(tesla_api.class.headers).to include({'Authorization' => /Bearer [a-z0-9]{32}/})
    end

    it 'obtains a Bearer token' do
      tesla_api.login!(ENV['TESLA_PASS'])
      expect(tesla_api.token).to match(/[a-z0-9]{32}/)
    end
  end

  describe '#vehicles', vcr: {cassette_name: 'client-vehicles'} do
    it 'lists the vehicles on the account' do
      expect(tesla_api.vehicles).to include(TeslaApi::Vehicle)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tesla_api-1.4.1 spec/lib/tesla_api/client_spec.rb
tesla_api-1.4.0 spec/lib/tesla_api/client_spec.rb
tesla_api-1.3.0 spec/lib/tesla_api/client_spec.rb