Skip to content

Commit

Permalink
AuthorizationRequest: Show - Displays replaced banned token when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Un3x committed Oct 20, 2023
1 parent 69f53ce commit bf85f79
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
16 changes: 10 additions & 6 deletions app/controllers/concerns/authorization_requests_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ module AuthorizationRequestsManagement
extend ActiveSupport::Concern

def show
@authorization_request = current_user
.authorization_requests
.where(api:)
.viewable_by_users
.find(params[:id])

@authorization_request = extract_authorization_request
@main_token = TokenShowDecorator.new(@authorization_request.token)
@banned_tokens = TokenShowDecorator.decorate_collection(@authorization_request.tokens.blacklisted_later)

render 'shared/authorization_requests/show'
rescue ActiveRecord::RecordNotFound
Expand All @@ -19,6 +15,14 @@ def show

private

def extract_authorization_request
current_user
.authorization_requests
.where(api:)
.viewable_by_users
.find(params[:id])
end

def api
namespace.slice(4..-1)
end
Expand Down
13 changes: 10 additions & 3 deletions app/decorators/token_show_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class TokenShowDecorator < ApplicationDecorator
delegate_all

def end_timestamp
return blacklisted_at.to_i if blacklisted_at.present?

exp
end

def passed_time_as_ratio
total_duration = exp - created_at.to_i
total_duration = end_timestamp - created_at.to_i
time_passed = Time.zone.now.to_i - created_at.to_i

return 100 if time_passed > total_duration
Expand All @@ -12,13 +18,14 @@ def passed_time_as_ratio

def status
return 'expired' if expired?
return 'revoked' if blacklisted? || blacklisted_later?
return 'revoked' if blacklisted?
return 'revoked_later' if blacklisted_later?

'active'
end

def progress_bar_color
day_left = (exp - Time.zone.now.to_i) / 86_400
day_left = (end_timestamp - Time.zone.now.to_i) / 86_400
if day_left < 30
'red'
elsif day_left < 90
Expand Down
10 changes: 10 additions & 0 deletions app/views/shared/authorization_requests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
token: @main_token,
}
%>

<% @banned_tokens.each do |banned_token| %>
<%=
render partial: 'shared/tokens/detail',
locals: {
title: t('.banned_tokens.title'),
token: banned_token,
}
%>
<% end %>
</div>
</div>
</div>
6 changes: 3 additions & 3 deletions app/views/shared/tokens/_detail.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</h2>
</div>
<% end %>
<div class="fr-card__desc">
<div class="fr-card__desc fr-pb-6v">
<div class="fr-pb-2v">
<p class="fr-badge fr-badge--<%= t(".status.color.#{token.status}") %>">
<%= t(".status.label.#{token.status}") %>
Expand All @@ -19,7 +19,7 @@
</div>
<p>
<%= t(".remaining_time.#{token.status}",
remaining_time: distance_of_time_in_words(Time.zone.now, token.exp)
remaining_time: distance_of_time_in_words(Time.zone.now, token.end_timestamp)
).html_safe %>
</p>
<div class="progress-bar-light-grey progress-bar-round">
Expand All @@ -29,7 +29,7 @@
</div>
</div>
<span class=""><%= friendly_format_from_timestamp(token.created_at)%></span>
<span class="pull-right"><%= friendly_format_from_timestamp(token.exp)%></span>
<span class="pull-right"><%= friendly_format_from_timestamp(token.end_timestamp)%></span>
</div>


3 changes: 3 additions & 0 deletions config/locales/api_entreprise/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fr:
remaining_time:
active: "Durée restante du jeton : <strong>%{remaining_time}</strong>"
expired: "Durée restante du jeton : <strong>Expiré</strong> ☠️"
revoked_later: "Durée restante du jeton : 🔥 <strong>%{remaining_time}</strong>"
revoked: "Durée restante du jeton : <strong>Banni</strong> ☠️"
call_for_a_week:
zero: "%{count} appel les 7 derniers jours"
Expand All @@ -22,10 +23,12 @@ fr:
status:
color:
revoked: pink-tuile
revoked_later: pink-tuile
expired: no-specific-color
active: green-emeraude
label:
revoked: Banni
revoked_later: Banni
expired: Expiré
active: Actif
authorization_requests:
Expand Down
7 changes: 7 additions & 0 deletions spec/features/api_particulier/authorization_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
create(:token, authorization_request:, exp:, blacklisted_at:)
end

let!(:banned_token) { create(:token, authorization_request:, exp:, blacklisted_at: 1.day.from_now) }

let(:exp) { 1.day.from_now.to_i }
let(:blacklisted_at) { nil }

Expand Down Expand Up @@ -112,6 +114,11 @@
expect(page).to have_content(token.id)
expect(page).to have_content('4 appels les 7 derniers jours')
expect(page).to have_content(distance_of_time_in_words(Time.zone.now, token.exp))

expect(page).to have_content(banned_token.id)
expect(page).to have_content('Banni')
expect(page).to have_content(distance_of_time_in_words(Time.zone.now, banned_token.blacklisted_at))
expect(page).to have_content('0 appel les 7 derniers jours')
end
end
end
Expand Down

0 comments on commit bf85f79

Please sign in to comment.