[#10799] Declare api_entreprise_token_expires_at attribute and feed it on save

This commit is contained in:
Mathieu Magnin 2024-09-24 14:08:06 +02:00
parent 222038d9de
commit 2d9854dc01
No known key found for this signature in database
GPG key ID: 8DCAFC82D7BA654E
6 changed files with 84 additions and 1 deletions

View file

@ -17,6 +17,10 @@ class APIEntrepriseToken
decoded_token.key?("exp") && decoded_token["exp"] <= Time.zone.now.to_i
end
def expiration
Time.zone.at(decoded_token["exp"])
end
def role?(role)
roles.include?(role)
end

View file

@ -288,6 +288,7 @@ class Procedure < ApplicationRecord
validates :api_particulier_token, format: { with: /\A[A-Za-z0-9\-_=.]{15,}\z/ }, allow_blank: true
validate :validate_auto_archive_on_in_the_future, if: :will_save_change_to_auto_archive_on?
before_save :set_api_entreprise_token_expires_at, if: :will_save_change_to_api_entreprise_token?
before_save :update_juridique_required
after_save :extend_conservation_for_dossiers
@ -973,6 +974,10 @@ class Procedure < ApplicationRecord
monavis_embed.gsub('nd_source=button', "nd_source=#{source}").gsub('<a ', '<a target="_blank" rel="noopener noreferrer" ')
end
def set_api_entreprise_token_expires_at
self.api_entreprise_token_expires_at = APIEntrepriseToken.new(api_entreprise_token).expiration
end
private
def published_revisions_types_de_champ(parent = nil)

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddAPIEntrepriseTokenExpiresAtToProcedures < ActiveRecord::Migration[7.0]
def change
add_column :procedures, :api_entreprise_token_expires_at, :datetime, precision: nil
end
end

View file

@ -242,8 +242,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_09_29_141825) do
t.integer "dossier_count"
t.string "dossier_state"
t.bigint "instructeur_id", null: false
t.datetime "sent_at", precision: nil, null: false
t.bigint "procedure_id"
t.datetime "sent_at", precision: nil, null: false
t.datetime "updated_at", null: false
end
@ -934,6 +934,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_09_29_141825) do
t.boolean "allow_expert_messaging", default: true, null: false
t.boolean "allow_expert_review", default: true, null: false
t.string "api_entreprise_token"
t.datetime "api_entreprise_token_expires_at", precision: nil
t.text "api_particulier_scopes", default: [], array: true
t.jsonb "api_particulier_sources", default: {}
t.boolean "ask_birthday", default: false, null: false

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

@ -1860,6 +1860,42 @@ describe Procedure do
end
end
describe '#set_api_entreprise_token_expires_at (before_save)' do
let(:procedure) { create(:procedure) }
before do
procedure.api_entreprise_token = api_entreprise_token
end
subject { procedure.save }
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
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
describe "#parsed_latest_zone_labels" do
let!(:draft_procedure) { create(:procedure) }
let!(:published_procedure) { create(:procedure_with_dossiers, :published, dossiers_count: 2) }