discourse/spec/system/admin_api_keys_spec.rb
Régis Hanol 8830c1bb32
FIX: API key scopes display and navigation issues (#36844)
Two issues were occurring with the admin API keys page:

1. Clicking "Edit" on an API key in the list failed to navigate properly
because the full apiKey object was passed to the route instead of just
the ID.

2. Viewing an API key with scopes would error when a scope's
allowed_parameters was null/undefined, as the template tried to call
`get` on a nullish value.

**BEFORE**

https://github.com/user-attachments/assets/a1c3a54f-7d26-4498-95e3-fd734b2e9a61

**AFTER**

https://github.com/user-attachments/assets/d560a189-566f-4f85-a76a-266b9ac95bed
2025-12-23 17:56:12 +01:00

83 lines
2.3 KiB
Ruby

#frozen_string_literal: true
describe "Admin API Keys Page", type: :system do
fab!(:current_user, :admin)
let(:api_keys_page) { PageObjects::Pages::AdminApiKeys.new }
let(:dialog) { PageObjects::Components::Dialog.new }
before do
Fabricate(:api_key, description: "Integration")
sign_in(current_user)
end
it "shows a list of API keys" do
api_keys_page.visit_page
expect(api_keys_page).to have_api_key_listed("Integration")
end
it "can add a new API key" do
api_keys_page.visit_page
api_keys_page.add_api_key(description: "Second Integration")
expect(api_keys_page).to have_generated_api_key
api_keys_page.click_continue
expect(api_keys_page).to have_api_key_listed("Second Integration")
end
it "can edit existing API keys" do
api_keys_page.visit_page
api_keys_page.click_edit("Integration")
api_keys_page.edit_description("Old Integration")
api_keys_page.click_back
expect(api_keys_page).to have_api_key_listed("Old Integration")
end
it "can revoke API keys" do
api_keys_page.visit_page
api_keys_page.click_edit("Integration")
api_keys_page.click_revoke
api_keys_page.click_back
expect(api_keys_page).to have_revoked_api_key_listed("Integration")
end
it "can undo revocation of API keys" do
api_keys_page.visit_page
api_keys_page.click_edit("Integration")
api_keys_page.click_revoke
api_keys_page.click_unrevoke
api_keys_page.click_back
expect(api_keys_page).to have_unrevoked_api_key_listed("Integration")
end
it "can permanently delete revoked API keys" do
api_keys_page.visit_page
api_keys_page.click_edit("Integration")
api_keys_page.click_revoke
api_keys_page.click_delete
expect(api_keys_page).to have_current_path("/admin/api/keys")
expect(api_keys_page).to have_no_api_key_listed("Integration")
end
it "displays scopes when viewing a granular API key from the list" do
Fabricate(
:api_key,
description: "Granular Key",
api_key_scopes: [Fabricate.build(:api_key_scope, resource: "topics", action: "read")],
)
api_keys_page.visit_page
api_keys_page.click_edit("Granular Key")
expect(api_keys_page).to have_scopes_displayed
expect(api_keys_page).to have_scope(resource: "topics", action: "read")
end
end