Skip to content

Commit

Permalink
Fix token renewal (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
alejanderl authored Aug 14, 2023
1 parent c45a5f8 commit 952e0c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/icd/api/authorize_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class AuthorizeToken
setting :client_secret, default: ENV['ICD_API_CLIENT_SECRET']

class << self
attr_reader :access_token

def call
return @access_token if valid?

Expand All @@ -23,15 +25,15 @@ def call
client_secret: config.client_secret
}

@access_token ||= AccessToken.new(::JSON.parse(response.body))
@access_token = AccessToken.new(::JSON.parse(response.body))
end

private

def valid?
return false if @access_token.nil?

@access_token.expires_at > Time.now
@access_token.expires_at.to_i > Time.now.to_i
end

def http_adapter
Expand Down
14 changes: 14 additions & 0 deletions spec/icd/api/authorize_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ module Api
expect(subject.expires_at.to_i).to be_within(5).of(Time.now.to_i + 3600)
end
end

context 'when the token is expired' do
let(:expired_token) { AccessToken.new({ 'expires_in' => 0, 'access_token' => 'expired_token' }) }
before do
described_class.instance_variable_set(:@access_token, expired_token)
end

it 'gets a new token' do
VCR.use_cassette('icd/api/authorize_token') do
expect { subject }.to change(described_class, :access_token)
expect(subject.expires_at.to_i).to be_within(5).of(Time.now.to_i + 3600)
end
end
end
end
end
end
Expand Down

0 comments on commit 952e0c2

Please sign in to comment.