[#10799] Display token error on related card

This commit is contained in:
Mathieu Magnin 2024-09-24 18:59:18 +02:00
parent 703a722c54
commit ec2c913ab4
No known key found for this signature in database
GPG key ID: 8DCAFC82D7BA654E
5 changed files with 42 additions and 6 deletions

View file

@ -1,8 +1,11 @@
.fr-col-6.fr-col-md-4.fr-col-lg-3 .fr-col-6.fr-col-md-4.fr-col-lg-3
= link_to jeton_admin_procedure_path(@procedure), class: 'fr-tile fr-enlarge-link' do = link_to jeton_admin_procedure_path(@procedure), class: 'fr-tile fr-enlarge-link' do
.fr-tile__body.flex.column.align-center.justify-between .fr-tile__body.flex.column.align-center.justify-between
- if @procedure.api_entreprise_token.present? - if @procedure.has_custom_api_entreprise_token?
%p.fr-badge.fr-badge--success Validé - if @procedure.api_entreprise_token_expired_or_expires_soon?
%p.fr-badge.fr-badge--error À renouveler
- else
%p.fr-badge.fr-badge--success Validé
- else - else
%p.fr-badge.fr-badge--info À configurer %p.fr-badge.fr-badge--info À configurer
%div %div

View file

@ -25,7 +25,7 @@ module APIEntrepriseTokenConcern
APIEntrepriseToken.new(api_entreprise_token).expired? APIEntrepriseToken.new(api_entreprise_token).expired?
end end
def api_entreprise_token_expires_soon? def api_entreprise_token_expired_or_expires_soon?
api_entreprise_token_expires_at && api_entreprise_token_expires_at <= SOON_TO_EXPIRE_DELAY.from_now api_entreprise_token_expires_at && api_entreprise_token_expires_at <= SOON_TO_EXPIRE_DELAY.from_now
end end

View file

@ -5,7 +5,7 @@
%p %p
Votre jeton API Entreprise est expiré. Votre jeton API Entreprise est expiré.
Merci de le renouveler. Merci de le renouveler.
- elsif procedure.api_entreprise_token_expires_soon? - elsif procedure.api_entreprise_token_expired_or_expires_soon?
= render Dsfr::AlertComponent.new(state: :warning, size: :sm, extra_class_names: 'fr-mb-2w') do |c| = render Dsfr::AlertComponent.new(state: :warning, size: :sm, extra_class_names: 'fr-mb-2w') do |c|
- c.with_body do - c.with_body do
%p %p

View file

@ -0,0 +1,27 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe Procedure::Card::APIEntrepriseComponent, type: :component do
subject { render_inline(described_class.new(procedure:)) }
let(:procedure) { create(:procedure, api_entreprise_token:) }
context "Token is not configured" do
let(:api_entreprise_token) { nil }
it { is_expected.to have_css('p.fr-badge.fr-badge--info', text: "À configurer") }
end
context "Token expires soon" do
let(:api_entreprise_token) { JWT.encode({ exp: 2.days.from_now.to_i }, nil, "none") }
it { is_expected.to have_css('p.fr-badge.fr-badge--error', text: "À renouveler") }
end
context "Token expires in a long time" do
let(:api_entreprise_token) { JWT.encode({ exp: 2.months.from_now.to_i }, nil, "none") }
it { is_expected.to have_css('p.fr-badge.fr-badge--success', text: "Validé") }
end
end

View file

@ -1,8 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
describe APIEntrepriseTokenConcern do describe APIEntrepriseTokenConcern do
describe "#api_entreprise_token_expires_soon?" do describe "#api_entreprise_token_expired_or_expires_soon?" do
subject { procedure.api_entreprise_token_expires_soon? } subject { procedure.api_entreprise_token_expired_or_expires_soon? }
let(:procedure) { create(:procedure, api_entreprise_token:) } let(:procedure) { create(:procedure, api_entreprise_token:) }
@ -23,5 +23,11 @@ describe APIEntrepriseTokenConcern do
it { is_expected.to be_truthy } it { is_expected.to be_truthy }
end 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 end
end end