[#10799] Handle the case when api_entreprise_token is not nil then set to nil

This commit is contained in:
Mathieu Magnin 2024-09-24 18:33:35 +02:00
parent 7009eed9d7
commit 703a722c54
No known key found for this signature in database
GPG key ID: 8DCAFC82D7BA654E
2 changed files with 38 additions and 19 deletions

View file

@ -17,6 +17,10 @@ module APIEntrepriseTokenConcern
self[:api_entreprise_token].presence || Rails.application.secrets.api_entreprise[:key]
end
def has_custom_api_entreprise_token?
self[:api_entreprise_token].present?
end
def api_entreprise_token_expired?
APIEntrepriseToken.new(api_entreprise_token).expired?
end
@ -26,7 +30,7 @@ module APIEntrepriseTokenConcern
end
def set_api_entreprise_token_expires_at
self.api_entreprise_token_expires_at = APIEntrepriseToken.new(api_entreprise_token).expiration
self.api_entreprise_token_expires_at = has_custom_api_entreprise_token? ? APIEntrepriseToken.new(api_entreprise_token).expiration : nil
end
end
end

View file

@ -1861,7 +1861,7 @@ describe Procedure do
end
describe '#set_api_entreprise_token_expires_at (before_save)' do
let(:procedure) { create(:procedure) }
let(:procedure) { create(:procedure, api_entreprise_token: initial_api_entreprise_token) }
before do
procedure.api_entreprise_token = api_entreprise_token
@ -1869,29 +1869,44 @@ describe Procedure do
subject { procedure.save }
context 'when the api_entreprise_token is nil' do
let(:api_entreprise_token) { nil }
context "when procedure had no api_entreprise_token" do
let(:initial_api_entreprise_token) { nil }
it 'does not set the api_entreprise_token_expires_at' do
expect { subject }.not_to change { procedure.api_entreprise_token_expires_at }.from(nil)
context 'when the api_entreprise_token is nil' do
let(:api_entreprise_token) { nil }
it 'does not set the api_entreprise_token_expires_at' do
expect { subject }.not_to change { procedure.api_entreprise_token_expires_at }.from(nil)
end
end
context 'when the api_entreprise_token is not valid' do
let(:api_entreprise_token) { "not a token" }
it do
expect { subject }.not_to change { procedure.api_entreprise_token_expires_at }.from(nil)
end
end
context 'when the api_entreprise_token is valid' do
let(:expiration_date) { Time.zone.now.beginning_of_minute }
let(:api_entreprise_token) { JWT.encode({ exp: expiration_date.to_i }, nil, 'none') }
it do
expect { subject }.to change { procedure.api_entreprise_token_expires_at }.from(nil).to(expiration_date)
end
end
end
context 'when the api_entreprise_token is not valid' do
let(:api_entreprise_token) { "not a token" }
context "when procedure had an api_entreprise_token" do
let(:initial_api_entreprise_token) { JWT.encode({ exp: 2.months.from_now.to_i }, nil, "none") }
it do
expect { subject }.not_to change { procedure.api_entreprise_token_expires_at }.from(nil)
end
end
context 'when the api_entreprise_token is set to nil' do
let(:api_entreprise_token) { nil }
context 'when the api_entreprise_token is valid' do
let(:expiration_date) { Time.zone.now.beginning_of_minute }
let(:api_entreprise_token) { JWT.encode({ exp: expiration_date.to_i }, nil, 'none') }
it do
puts "expiration_date: #{expiration_date.to_i}"
expect { subject }.to change { procedure.api_entreprise_token_expires_at }.from(nil).to(expiration_date)
it do
expect { subject }.to change { procedure.api_entreprise_token_expires_at }.to(nil)
end
end
end
end