diff --git a/app/components/procedure/card/api_entreprise_component/api_entreprise_component.html.haml b/app/components/procedure/card/api_entreprise_component/api_entreprise_component.html.haml index 8c5ed09f9..32797f8bd 100644 --- a/app/components/procedure/card/api_entreprise_component/api_entreprise_component.html.haml +++ b/app/components/procedure/card/api_entreprise_component/api_entreprise_component.html.haml @@ -1,8 +1,11 @@ .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 .fr-tile__body.flex.column.align-center.justify-between - - if @procedure.api_entreprise_token.present? - %p.fr-badge.fr-badge--success Validé + - if @procedure.has_custom_api_entreprise_token? + - 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 %p.fr-badge.fr-badge--info À configurer %div diff --git a/app/models/concerns/api_entreprise_token_concern.rb b/app/models/concerns/api_entreprise_token_concern.rb index 308e73249..3962b72f6 100644 --- a/app/models/concerns/api_entreprise_token_concern.rb +++ b/app/models/concerns/api_entreprise_token_concern.rb @@ -25,7 +25,7 @@ module APIEntrepriseTokenConcern APIEntrepriseToken.new(api_entreprise_token).expired? 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 end diff --git a/app/views/administrateurs/procedures/_api_entreprise_token_expiration_alert.html.haml b/app/views/administrateurs/procedures/_api_entreprise_token_expiration_alert.html.haml index c83fb1cec..877fb464b 100644 --- a/app/views/administrateurs/procedures/_api_entreprise_token_expiration_alert.html.haml +++ b/app/views/administrateurs/procedures/_api_entreprise_token_expiration_alert.html.haml @@ -5,7 +5,7 @@ %p Votre jeton API Entreprise est expiré. 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| - c.with_body do %p diff --git a/spec/components/procedure/card/api_entreprise_component_spec.rb b/spec/components/procedure/card/api_entreprise_component_spec.rb new file mode 100644 index 000000000..382d425ad --- /dev/null +++ b/spec/components/procedure/card/api_entreprise_component_spec.rb @@ -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 diff --git a/spec/models/concerns/api_entreprise_token_concern_spec.rb b/spec/models/concerns/api_entreprise_token_concern_spec.rb index 3eedc3c4d..7a6040af6 100644 --- a/spec/models/concerns/api_entreprise_token_concern_spec.rb +++ b/spec/models/concerns/api_entreprise_token_concern_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true describe APIEntrepriseTokenConcern do - describe "#api_entreprise_token_expires_soon?" do - subject { procedure.api_entreprise_token_expires_soon? } + 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:) } @@ -23,5 +23,11 @@ describe APIEntrepriseTokenConcern do 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 end