Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Api tokens counters to support api token index page #10294

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/support_interface/api_tokens_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def index
VendorAPIToken.arel_table[:last_used_at].desc.nulls_last,
created_at: :desc,
)
@api_tokens_last_3_months_count = VendorAPIToken.used_in_last_3_months.count
end

def new
Expand Down
2 changes: 2 additions & 0 deletions app/models/vendor_api_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class VendorAPIToken < ApplicationRecord

audited associated_with: :provider

scope :used_in_last_3_months, -> { where('last_used_at >= ?', 3.months.ago) }

def self.create_with_random_token!(provider:)
unhashed_token, hashed_token = Devise.token_generator.generate(VendorAPIToken, :hashed_token)
create!(hashed_token:, provider:)
Expand Down
17 changes: 17 additions & 0 deletions app/views/support_interface/api_tokens/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

<%= govuk_button_link_to 'Add a token', new_support_interface_api_token_path %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-one-half">
<%= render SupportInterface::TileComponent.new(
count: @api_tokens.count,
label: 'API tokens issued',
colour: :blue,
) %>
</div>
<div class="govuk-grid-column-one-half">
<%= render SupportInterface::TileComponent.new(
count: @api_tokens_last_3_months_count,
label: 'API tokens used in the last 3 months',
colour: :blue,
) %>
</div>
</div>

<table class='govuk-table'>
<thead class='govuk-table__head'>
<tr class='govuk-table__row'>
Expand Down
40 changes: 40 additions & 0 deletions spec/system/support_interface/api_tokens/view_tokens_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rails_helper'

RSpec.describe 'API tokens' do
include DfESignInHelpers

scenario 'Support views vendors with api tokens' do
given_i_am_signed_in
and_api_tokens_exist
when_i_visit_the_tokens_page
then_i_see_the_count_of_providers_with_api_tokens
end

def given_i_am_signed_in
sign_in_as_support_user
end

def and_api_tokens_exist
vendor = create(:vendor, name: 'vendor_1')
provider_1 = create(:provider, name: 'Provider 1', vendor:)
provider_2 = create(:provider, name: 'Provider 2', vendor:)

create(:vendor_api_token, provider: provider_1, last_used_at: 1.month.ago)
create(:vendor_api_token, provider: provider_2)
end

def when_i_visit_the_tokens_page
visit support_interface_api_tokens_path
end

def then_i_see_the_count_of_providers_with_api_tokens
expect(page).to have_content '2 API tokens issued'
expect(page).to have_content '1 API tokens used in the last 3 months'

within '.govuk-table' do
expect(page).to have_content 'Provider 1'
expect(page).to have_content 'Provider 2'
expect(page).to have_content 'vendor_1'
end
end
end
Loading