Sha256: 9fc8dcc7e73287548fb761d1a3ecd64db91c8d97bf2b4b22079c67bfd4982b2c

Contents?: true

Size: 1.46 KB

Versions: 23

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

describe LHC::Auth do
  let(:initial_token) { '123456' }
  let(:refresh_token) { 'abcdef' }
  let(:options) { { bearer: initial_token, refresh_client_token: -> { refresh_token } } }
  let!(:auth_failing) do
    stub_request(:get, 'http://local.ch')
      .with(headers: { 'Authorization' => "Bearer #{initial_token}" })
      .to_return(status: 401, body: "{}") # LHC::Unauthorized
  end
  let!(:auth_suceeding_after_recovery) do
    stub_request(:get, 'http://local.ch')
      .with(headers: { 'Authorization' => "Bearer #{refresh_token}" })
  end

  before(:each) do
    LHC.config.interceptors = [LHC::Auth, LHC::Retry]
  end

  it "recovery is attempted" do
    LHC.config.endpoint(:local, 'http://local.ch', auth: options)
    # the retried request (with updated Bearer), that should work
    LHC.get(:local)
    expect(auth_suceeding_after_recovery).to have_been_made.once
  end

  it "recovery is not attempted again when the request has reauthenticated: true " do
    LHC.config.endpoint(:local, 'http://local.ch', auth: options.merge(reauthenticated: true))
    expect { LHC.get(:local) }.to raise_error(LHC::Unauthorized)
  end

  context 'token format' do
    let(:initial_token) { 'BAsZ-98-ZZZ' }

    it 'refreshes tokens with various formats' do
      LHC.config.endpoint(:local, 'http://local.ch', auth: options)
      LHC.get(:local)
      expect(auth_suceeding_after_recovery).to have_been_made.once
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
lhc-15.1.2 spec/interceptors/auth/reauthentication_spec.rb
lhc-15.1.1 spec/interceptors/auth/reauthentication_spec.rb
lhc-15.1.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-15.0.1 spec/interceptors/auth/reauthentication_spec.rb
lhc-15.0.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-14.0.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-13.4.0.pre.pro1766.1 spec/interceptors/auth/reauthentication_spec.rb
lhc-13.2.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-13.1.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-13.0.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.3.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.2.1 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.2.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.1.3 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.1.2 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.1.1 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.1.0 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.0.3 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.0.2 spec/interceptors/auth/reauthentication_spec.rb
lhc-12.0.1 spec/interceptors/auth/reauthentication_spec.rb