Merge pull request #10865 from demarches-simplifiees/fix-10799

ETQ Admin / Instructeur je veux être savoir si le jeton api entreprise d'une démarche a expiré ou va expirer prochainement
This commit is contained in:
Mathieu Magnin 2024-10-15 13:25:20 +00:00 committed by GitHub
commit 097074fdc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 340 additions and 50 deletions

View file

@ -138,4 +138,34 @@ describe APIEntrepriseToken, type: :model do
end
end
end
describe "#expiration" do
subject { api_entreprise_token.expiration }
context "without token" do
let(:token) { nil }
it { expect { subject }.to raise_exception(APIEntrepriseToken::TokenError) }
end
context "with a blank token" do
let(:token) { "" }
it { expect { subject }.to raise_exception(APIEntrepriseToken::TokenError) }
end
context "with an invalid token" do
let(:token) { "NOT-A-VALID-TOKEN" }
it { expect { subject }.to raise_exception(APIEntrepriseToken::TokenError) }
end
context "with a valid token" do
let(:token) { "eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiI2NjRkZWEyMS02YWFlLTQwZmYtYWM0Mi1kZmQ3ZGE4YjQ3NmUiLCJqdGkiOiJhcGktZW50cmVwcmlzZS1zdGFnaW5nIiwicm9sZXMiOlsiY2VydGlmaWNhdF9jbmV0cCIsInByb2J0cCIsImV0YWJsaXNzZW1lbnRzIiwicHJpdmlsZWdlcyIsInVwdGltZSIsImF0dGVzdGF0aW9uc19hZ2VmaXBoIiwiYWN0ZXNfaW5waSIsImJpbGFuc19pbnBpIiwiYWlkZXNfY292aWRfZWZmZWN0aWZzIiwiY2VydGlmaWNhdF9yZ2VfYWRlbWUiLCJhdHRlc3RhdGlvbnNfc29jaWFsZXMiLCJlbnRyZXByaXNlX2FydGlzYW5hbGUiLCJmbnRwX2NhcnRlX3BybyIsImNvbnZlbnRpb25zX2NvbGxlY3RpdmVzIiwiZXh0cmFpdHNfcmNzIiwiZXh0cmFpdF9jb3VydF9pbnBpIiwiY2VydGlmaWNhdF9hZ2VuY2VfYmlvIiwibXNhX2NvdGlzYXRpb25zIiwiZG9jdW1lbnRzX2Fzc29jaWF0aW9uIiwiZW9yaV9kb3VhbmVzIiwiYXNzb2NpYXRpb25zIiwiYmlsYW5zX2VudHJlcHJpc2VfYmRmIiwiZW50cmVwcmlzZXMiLCJxdWFsaWJhdCIsImNlcnRpZmljYXRfb3BxaWJpIiwiZW50cmVwcmlzZSIsImV0YWJsaXNzZW1lbnQiXSwic3ViIjoic3RhZ2luZyBkZXZlbG9wbWVudCIsImlhdCI6MTY0MTMwNDcxNCwidmVyc2lvbiI6IjEuMCIsImV4cCI6MTY4ODQ3NTUxNH0.xID66pIlMnBR5_6nG-GidFBzK4Tuuy5ZsWfkMEVB_Ek" }
it "returns the correct expiration time" do
expect(subject).to eq(Time.zone.at(1688475514))
end
end
end
end

View file

@ -0,0 +1,84 @@
# frozen_string_literal: true
describe APIEntrepriseTokenConcern do
describe "#api_entreprise_token_expired_or_expires_soon?" do
subject { procedure.api_entreprise_token_expired_or_expires_soon? }
let(:procedure) { create(:procedure, api_entreprise_token:) }
context "when there is no token" do
let(:api_entreprise_token) { nil }
it { is_expected.to be_falsey }
end
context "when the token expires in 2 months" do
let(:api_entreprise_token) { JWT.encode({ exp: 2.months.from_now.to_i }, nil, "none") }
it { is_expected.to be_falsey }
end
context "when the token expires tomorrow" do
let(:api_entreprise_token) { JWT.encode({ exp: 1.day.from_now.to_i }, nil, "none") }
it { is_expected.to be_truthy }
end
context "when the token is expired" do
let(:api_entreprise_token) { JWT.encode({ exp: 1.day.ago.to_i }, nil, "none") }
it { is_expected.to be_truthy }
end
end
describe '#set_api_entreprise_token_expires_at (before_save)' do
let(:procedure) { create(:procedure, api_entreprise_token: initial_api_entreprise_token) }
before do
procedure.api_entreprise_token = api_entreprise_token
end
subject { procedure.save }
context "when procedure had no api_entreprise_token" do
let(:initial_api_entreprise_token) { 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 procedure had an api_entreprise_token" do
let(:initial_api_entreprise_token) { JWT.encode({ exp: 2.months.from_now.to_i }, nil, "none") }
context 'when the api_entreprise_token is set to nil' do
let(:api_entreprise_token) { nil }
it do
expect { subject }.to change { procedure.api_entreprise_token_expires_at }.to(nil)
end
end
end
end
end

View file

@ -621,31 +621,6 @@ describe Procedure do
end
end
describe 'api_entreprise_token_expired?' do
let(:token) { "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }
let(:procedure) { create(:procedure, api_entreprise_token: token) }
let(:payload) {
[
{ "exp" => expiration_time }
]
}
let(:subject) { procedure.api_entreprise_token_expired? }
before do
allow(JWT).to receive(:decode).with(token, nil, false).and_return(payload)
end
context "with token expired" do
let(:expiration_time) { (1.day.ago).to_i }
it { is_expected.to be_truthy }
end
context "with token not expired" do
let(:expiration_time) { (1.day.from_now).to_i }
it { is_expected.to be_falsey }
end
end
describe 'clone' do
let(:service) { create(:service) }
let(:procedure) do