[#10799] Handle the case when api_entreprise_token is not nil then set to nil
This commit is contained in:
parent
7009eed9d7
commit
703a722c54
2 changed files with 38 additions and 19 deletions
|
@ -17,6 +17,10 @@ module APIEntrepriseTokenConcern
|
||||||
self[:api_entreprise_token].presence || Rails.application.secrets.api_entreprise[:key]
|
self[:api_entreprise_token].presence || Rails.application.secrets.api_entreprise[:key]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def has_custom_api_entreprise_token?
|
||||||
|
self[:api_entreprise_token].present?
|
||||||
|
end
|
||||||
|
|
||||||
def api_entreprise_token_expired?
|
def api_entreprise_token_expired?
|
||||||
APIEntrepriseToken.new(api_entreprise_token).expired?
|
APIEntrepriseToken.new(api_entreprise_token).expired?
|
||||||
end
|
end
|
||||||
|
@ -26,7 +30,7 @@ module APIEntrepriseTokenConcern
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_api_entreprise_token_expires_at
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1861,7 +1861,7 @@ describe Procedure do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#set_api_entreprise_token_expires_at (before_save)' do
|
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
|
before do
|
||||||
procedure.api_entreprise_token = api_entreprise_token
|
procedure.api_entreprise_token = api_entreprise_token
|
||||||
|
@ -1869,29 +1869,44 @@ describe Procedure do
|
||||||
|
|
||||||
subject { procedure.save }
|
subject { procedure.save }
|
||||||
|
|
||||||
context 'when the api_entreprise_token is nil' do
|
context "when procedure had no api_entreprise_token" do
|
||||||
let(:api_entreprise_token) { nil }
|
let(:initial_api_entreprise_token) { nil }
|
||||||
|
|
||||||
it 'does not set the api_entreprise_token_expires_at' do
|
context 'when the api_entreprise_token is nil' do
|
||||||
expect { subject }.not_to change { procedure.api_entreprise_token_expires_at }.from(nil)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the api_entreprise_token is not valid' do
|
context "when procedure had an api_entreprise_token" do
|
||||||
let(:api_entreprise_token) { "not a token" }
|
let(:initial_api_entreprise_token) { JWT.encode({ exp: 2.months.from_now.to_i }, nil, "none") }
|
||||||
|
|
||||||
it do
|
context 'when the api_entreprise_token is set to nil' do
|
||||||
expect { subject }.not_to change { procedure.api_entreprise_token_expires_at }.from(nil)
|
let(:api_entreprise_token) { nil }
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when the api_entreprise_token is valid' do
|
it do
|
||||||
let(:expiration_date) { Time.zone.now.beginning_of_minute }
|
expect { subject }.to change { procedure.api_entreprise_token_expires_at }.to(nil)
|
||||||
let(:api_entreprise_token) { JWT.encode({ exp: expiration_date.to_i }, nil, 'none') }
|
end
|
||||||
|
|
||||||
it do
|
|
||||||
puts "expiration_date: #{expiration_date.to_i}"
|
|
||||||
expect { subject }.to change { procedure.api_entreprise_token_expires_at }.from(nil).to(expiration_date)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue