The personal finance app for everyone (by everyone)
Ruby
Allow multiple active API keys per user (#2077)
Will Wilson9487e6cbfb31
diff --git a/app/controllers/settings/api_keys_controller.rb b/app/controllers/settings/api_keys_controller.rbindex cb8b2c3db7..ac5d53aa4c 100644--- a/app/controllers/settings/api_keys_controller.rb+++ b/app/controllers/settings/api_keys_controller.rb@@ -5,18 +5,24 @@ class Settings::ApiKeysController < ApplicationControllerbefore_action :set_api_key, only: [ :show, :destroy ]+ def index+ @api_keys = Current.user.api_keys.active.visible.order(created_at: :desc)+ @breadcrumbs = [+ [ t("breadcrumbs.home"), root_path ],+ [ t("breadcrumbs.api_keys"), nil ]+ ]+ end+def show+ @newly_created = params[:newly_created].present?@breadcrumbs = [[ t("breadcrumbs.home"), root_path ],- [ t("breadcrumbs.api_key"), nil ]+ [ t("breadcrumbs.api_keys"), settings_api_keys_path ],+ [ @api_key.name, nil ]]- @current_api_key = @api_keyenddef new- # Allow regeneration by not redirecting if user explicitly wants to create a new key- # Only redirect if user stumbles onto new page without explicit intent- redirect_to settings_api_key_path if Current.user.api_keys.active.visible.exists? && !params[:regenerate]@api_key = ApiKey.newend@@ -25,42 +31,33 @@ class Settings::ApiKeysController < ApplicationController@api_key = Current.user.api_keys.build(api_key_params)@api_key.key = @plain_key- # Temporarily revoke existing visible keys for validation to pass- # (demo monitoring key is excluded and remains active)- existing_keys = Current.user.api_keys.active.visible- existing_keys.each { |key| key.update_column(:revoked_at, Time.current) }-if @api_key.saveflash[:notice] = t(".success")- redirect_to settings_api_key_path+ redirect_to settings_api_key_path(@api_key, newly_created: true)else- # Restore existing keys if new key creation failed- existing_keys.each { |key| key.update_column(:revoked_at, nil) }render :new, status: :unprocessable_entityendenddef destroy- if @api_key.nil?- flash[:alert] = t(".not_found")- elsif @api_key.demo_monitoring_key?- flash[:alert] = t(".cannot_revoke")- elsif @api_key.revoke!- flash[:notice] = t(".revoked_successfully")- else- flash[:alert] = t(".revoke_failed")- end- redirect_to settings_api_key_path+ @api_key.revoke!+ flash[:notice] = t(".revoked_successfully")+ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotDestroyed+ flash[:alert] = t(".revoke_failed")+ ensure+ redirect_to settings_api_keys_pathendprivate+ # `.visible` excludes the demo monitoring key, so a demo key id 404s here+ # before #destroy can revoke it — this is intentional (see the SECURITY note+ # on ApiKey's `visible` scope).def set_api_key- @api_key = Current.user.api_keys.active.visible.first+ @api_key = Current.user.api_keys.active.visible.find(params[:id])enddef api_key_params- # Convert single scope value to array for storagepermitted_params = params.require(:api_key).permit(:name, :scopes)if permitted_params[:scopes].present?permitted_params[:scopes] = [ permitted_params[:scopes] ]diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rbindex 6d821ecdb1..658b9199de 100644--- a/app/helpers/settings_helper.rb+++ b/app/helpers/settings_helper.rb@@ -18,7 +18,7 @@ module SettingsHelper# Advanced section{ name: -> { t("settings.settings_nav.ai_prompts_label") }, path: :settings_ai_prompts_path, condition: :admin_user? },{ name: -> { t("settings.settings_nav.llm_usage_label") }, path: :settings_llm_usage_path, condition: :admin_user? },- { name: -> { t("settings.settings_nav.api_key_label") }, path: :settings_api_key_path, condition: :admin_user? },+ { name: -> { t("settings.settings_nav.api_key_label") }, path: :settings_api_keys_path, condition: :admin_user? },{ name: -> { t("settings.settings_nav.self_hosting_label") }, path: :settings_hosting_path, condition: :self_hosted_and_admin? },{ name: -> { t("settings.settings_nav.imports_label") }, path: :imports_path, condition: :admin_user? },{ name: -> { t("settings.settings_nav.exports_label") }, path: :family_exports_path, condition: :admin_user? },diff --git a/app/models/api_key.rb b/app/models/api_key.rbindex 4c12b4668f..1b5543763d 100644--- a/app/models/api_key.rb+++ b/app/models/api_key.rb@@ -18,7 +18,7 @@ class ApiKey < ApplicationRecordvalidates :scopes, presence: truevalidates :source, presence: true, inclusion: { in: SOURCES }validate :scopes_not_empty- validate :one_active_key_per_user_per_source, on: :create+ validate :name_unique_among_active_keys, on: :create# Callbacksbefore_validation :set_display_key@@ -26,6 +26,10 @@ class ApiKey < ApplicationRecord# Scopesscope :active, -> { where(revoked_at: nil).where("expires_at IS NULL OR expires_at > ?", Time.current) }+ # SECURITY: excluding the demo monitoring key here is also the revocation guard+ # in Settings::ApiKeysController#destroy — a demo key id 404s in `set_api_key`+ # (it is not `.visible`) before it can be revoked. Do NOT broaden this scope to+ # include monitoring keys without adding another explicit destroy guard.scope :visible, -> { where.not(display_key: DEMO_MONITORING_KEY) }# Class methods@@ -103,10 +107,11 @@ class ApiKey < ApplicationRecordendend- def one_active_key_per_user_per_source- if user&.api_keys&.active&.where(source: source)&.where&.not(id: id)&.exists?- errors.add(:user, "can only have one active API key per source (#{source})")- end+ def name_unique_among_active_keys+ return if name.blank? || user.blank?+ scope = user.api_keys.active.visible.where(name: name)+ scope = scope.where.not(id: id) if persisted?+ errors.add(:name, :taken) if scope.exists?enddef prevent_demo_monitoring_key_destroy!diff --git a/app/views/reports/google_sheets_instructions.html.erb b/app/views/reports/google_sheets_instructions.html.erbindex ba8e79a9f0..3eb8f4bb24 100644--- a/app/views/reports/google_sheets_instructions.html.erb+++ b/app/views/reports/google_sheets_instructions.html.erb@@ -51,7 +51,7 @@text: t("reports.google_sheets_instructions.go_to_api_keys"),variant: "primary",full_width: true,- href: settings_api_key_path,+ href: settings_api_keys_path,frame: "_top") %><% end %>diff --git a/app/views/settings/_settings_nav.html.erb b/app/views/settings/_settings_nav.html.erbindex b65101ead9..2af887c634 100644--- a/app/views/settings/_settings_nav.html.erb+++ b/app/views/settings/_settings_nav.html.erb@@ -29,7 +29,7 @@ nav_sections = [items: [{ label: t(".ai_prompts_label"), path: settings_ai_prompts_path, icon: "bot" },{ label: t(".llm_usage_label"), path: settings_llm_usage_path, icon: "activity" },- { label: t(".api_keys_label"), path: settings_api_key_path, icon: "key" },+ { label: t(".api_keys_label"), path: settings_api_keys_path, icon: "key" },{ label: t(".mcp_label"), path: settings_mcp_path, icon: "plug" },{ label: t(".debug_label", default: "Debug"), path: settings_debug_path, icon: "bug", if: Current.user&.super_admin? },{ label: t(".self_hosting_label"), path: settings_hosting_path, icon: "database", if: self_hosted? },diff --git a/app/views/settings/api_keys/_key_meta.html.erb b/app/views/settings/api_keys/_key_meta.html.erbnew file mode 100644index 0000000000..df515e7dc7--- /dev/null+++ b/app/views/settings/api_keys/_key_meta.html.erb@@ -0,0 +1,9 @@+<%# locals: (api_key:) %>+<p class="text-secondary">+ <%= t("settings.api_keys.shared.created_ago", time: time_ago_in_words(api_key.created_at)) %>+ <% if api_key.last_used_at %>+ • <%= t("settings.api_keys.shared.last_used_ago", time: time_ago_in_words(api_key.last_used_at)) %>+ <% else %>+ • <%= t("settings.api_keys.shared.never_used") %>+ <% end %>+</p>diff --git a/app/views/settings/api_keys/_key_reveal.html.erb b/app/views/settings/api_keys/_key_reveal.html.erbnew file mode 100644index 0000000000..8852b00216--- /dev/null+++ b/app/views/settings/api_keys/_key_reveal.html.erb@@ -0,0 +1,17 @@+<%# locals: (api_key:, heading:, description:, copy_label:) %>+<div class="bg-surface-inset rounded-xl p-4">+ <h4 class="font-medium text-primary mb-3"><%= heading %></h4>+ <p class="text-secondary text-sm mb-3"><%= description %></p>++ <div class="@container bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">+ <div class="flex flex-col items-start gap-3 @lg:flex-row @lg:items-center @lg:justify-between">+ <code class="font-mono text-sm text-primary break-all min-w-0" data-clipboard-target="source"><%= api_key.plain_key %></code>+ <%= render DS::Button.new(+ text: copy_label,+ variant: "ghost",+ icon: "copy",+ data: { action: "clipboard#copy" }+ ) %>+ </div>+ </div>+</div>diff --git a/app/views/settings/api_keys/_scope_badges.html.erb b/app/views/settings/api_keys/_scope_badges.html.erbnew file mode 100644index 0000000000..bb7ef4ecda--- /dev/null+++ b/app/views/settings/api_keys/_scope_badges.html.erb@@ -0,0 +1,11 @@+<%# locals: (scopes:) %>+<div class="flex flex-wrap gap-2">+ <% scopes.each do |scope| %>+ <% label = case scope+ when "read" then t("settings.api_keys.shared.scope_read_only")+ when "read_write" then t("settings.api_keys.shared.scope_read_write")+ else t("settings.api_keys.shared.scopes.#{scope}", default: scope.humanize)+ end %>+ <%= render DS::Pill.new(label: label, icon: "shield-check", tone: :indigo, style: :soft, marker: false) %>+ <% end %>+</div>diff --git a/app/views/settings/api_keys/_status_indicator.html.erb b/app/views/settings/api_keys/_status_indicator.html.erbnew file mode 100644index 0000000000..ec4bedc3e0--- /dev/null+++ b/app/views/settings/api_keys/_status_indicator.html.erb@@ -0,0 +1 @@+<%= render DS::Pill.new(label: t("settings.api_keys.shared.active"), tone: :success, show_dot: true, marker: false) %>diff --git a/app/views/settings/api_keys/_usage.html.erb b/app/views/settings/api_keys/_usage.html.erbnew file mode 100644index 0000000000..2b1f41602b--- /dev/null+++ b/app/views/settings/api_keys/_usage.html.erb@@ -0,0 +1,8 @@+<%# locals: (api_key:, title:, description:) %>+<div class="bg-surface-inset rounded-xl p-4">+ <h4 class="font-medium text-primary mb-3"><%= title %></h4>+ <p class="text-secondary text-sm mb-3"><%= description %></p>+ <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary break-all">+ curl -H "X-Api-Key: <%= api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts+ </div>+</div>diff --git a/app/views/settings/api_keys/created.html.erb b/app/views/settings/api_keys/created.html.erbdeleted file mode 100644index d0c4e7bcab..0000000000--- a/app/views/settings/api_keys/created.html.erb+++ /dev/null@@ -1,87 +0,0 @@-<%= content_for :page_title, t(".page_title") %>--<%= settings_section title: t(".success_title"), subtitle: t(".success_description") do %>- <div class="space-y-4">- <div class="p-3 shadow-border-xs bg-container rounded-lg">- <div class="flex items-start gap-3">- <%= render DS::FilledIcon.new(- icon: "check-circle",- rounded: true,- size: "lg",- variant: :success- ) %>- <div class="flex-1">- <h3 class="font-medium text-primary"><%= t(".success_title") %></h3>- <p class="text-secondary text-sm mt-1"><%= t(".key_ready", name: @api_key.name) %></p>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".your_api_key") %></h4>- <p class="text-secondary text-sm mb-3"><%= t(".copy_store_securely") %></p>-- <div class="bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">- <div class="flex items-center justify-between gap-3">- <code id="api-key-display" class="font-mono text-sm text-primary break-all" data-clipboard-target="source"><%= @api_key.plain_key %></code>- <%= render DS::Button.new(- text: t(".copy_key"),- variant: "ghost",- icon: "copy",- data: { action: "clipboard#copy" }- ) %>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".key_details_title") %></h4>- <div class="space-y-2 text-sm">- <div class="flex justify-between">- <span class="text-secondary"><%= t(".key_name_label") %></span>- <span class="text-primary font-medium"><%= @api_key.name %></span>- </div>- <div class="flex justify-between">- <span class="text-secondary"><%= t(".permissions_label") %></span>- <span class="text-primary">- <%= @api_key.scopes.map { |scope|- case scope- when "read_accounts" then t("settings.api_keys_controller.scope_descriptions.read_accounts")- when "read_transactions" then t("settings.api_keys_controller.scope_descriptions.read_transactions")- when "read_balances" then t("settings.api_keys_controller.scope_descriptions.read_balances")- when "write_transactions" then t("settings.api_keys_controller.scope_descriptions.write_transactions")- else scope.humanize- end- }.join(", ") %>- </span>- </div>- <div class="flex justify-between">- <span class="text-secondary"><%= t(".created_label") %></span>- <span class="text-primary"><%= @api_key.created_at.strftime("%B %d, %Y at %I:%M %p") %></span>- </div>- </div>- </div>-- <%= render DS::Alert.new(title: t(".security_note_title"), variant: :warning) do %>- <p>- <%= t(".security_note_body") %>- </p>- <% end %>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".usage_instructions_title") %></h4>- <p class="text-secondary text-sm mb-3"><%= t("settings.api_keys.show.current_api_key.usage_instructions", product_name: product_name) %></p>- <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary">- curl -H "X-Api-Key: <%= @api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts- </div>- </div>-- <div class="flex justify-end pt-4 border-t border-primary">- <%= render DS::Link.new(- text: t(".continue"),- href: settings_api_key_path,- variant: "primary"- ) %>- </div>- </div>-<% end %>diff --git a/app/views/settings/api_keys/created.turbo_stream.erb b/app/views/settings/api_keys/created.turbo_stream.erbdeleted file mode 100644index f187a6d310..0000000000--- a/app/views/settings/api_keys/created.turbo_stream.erb+++ /dev/null@@ -1,95 +0,0 @@-<%= turbo_stream.update "main" do %>- <div class="relative max-w-4xl mx-auto flex flex-col w-full h-full">- <div class="grow space-y-4 overflow-y-auto -mx-1 px-1 pb-12">- <h1 class="text-primary text-3xl md:text-xl font-medium">- <%= t("settings.api_keys.created.page_title") %>- </h1>-- <%= settings_section title: t("settings.api_keys.created.success_title"), subtitle: t("settings.api_keys.created.success_description") do %>- <div class="space-y-4">- <div class="p-3 shadow-border-xs bg-container rounded-lg">- <div class="flex items-start gap-3">- <%= render DS::FilledIcon.new(- icon: "check-circle",- rounded: true,- size: "lg",- variant: :success- ) %>- <div class="flex-1">- <h3 class="font-medium text-primary"><%= t("settings.api_keys.created.success_title") %></h3>- <p class="text-secondary text-sm mt-1"><%= t("settings.api_keys.created.key_ready", name: @api_key.name) %></p>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t("settings.api_keys.created.your_api_key") %></h4>- <p class="text-secondary text-sm mb-3"><%= t("settings.api_keys.created.copy_store_securely") %></p>-- <div class="bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">- <div class="flex items-center justify-between gap-3">- <code id="api-key-display" class="font-mono text-sm text-primary break-all" data-clipboard-target="source"><%= @api_key.plain_key %></code>- <%= render DS::Button.new(- text: t("settings.api_keys.created.copy_key"),- variant: "ghost",- icon: "copy",- data: { action: "clipboard#copy" }- ) %>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t("settings.api_keys.created.key_details_title") %></h4>- <div class="space-y-2 text-sm">- <div class="flex justify-between">- <span class="text-secondary"><%= t("settings.api_keys.created.key_name_label") %></span>- <span class="text-primary font-medium"><%= @api_key.name %></span>- </div>- <div class="flex justify-between">- <span class="text-secondary"><%= t("settings.api_keys.created.permissions_label") %></span>- <span class="text-primary">- <%= @api_key.scopes.map { |scope|- case scope- when "read_accounts" then t("settings.api_keys_controller.scope_descriptions.read_accounts")- when "read_transactions" then t("settings.api_keys_controller.scope_descriptions.read_transactions")- when "read_balances" then t("settings.api_keys_controller.scope_descriptions.read_balances")- when "write_transactions" then t("settings.api_keys_controller.scope_descriptions.write_transactions")- else scope.humanize- end- }.join(", ") %>- </span>- </div>- <div class="flex justify-between">- <span class="text-secondary"><%= t("settings.api_keys.created.created_label") %></span>- <span class="text-primary"><%= @api_key.created_at.strftime("%B %d, %Y at %I:%M %p") %></span>- </div>- </div>- </div>-- <%= render DS::Alert.new(title: t("settings.api_keys.created.security_note_title"), variant: :warning) do %>- <p>- <%= t("settings.api_keys.created.security_note_body") %>- </p>- <% end %>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t("settings.api_keys.created.usage_instructions_title") %></h4>- <p class="text-secondary text-sm mb-3"><%= t("settings.api_keys.show.current_api_key.usage_instructions", product_name: product_name) %></p>- <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary">- curl -H "X-Api-Key: <%= @api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts- </div>- </div>-- <div class="flex justify-end pt-4 border-t border-primary">- <%= render DS::Link.new(- text: t("settings.api_keys.created.continue"),- href: settings_api_key_path,- variant: "primary"- ) %>- </div>- </div>- <% end %>- </div>- </div>-<% end %>diff --git a/app/views/settings/api_keys/index.html.erb b/app/views/settings/api_keys/index.html.erbnew file mode 100644index 0000000000..7a6cbd2a47--- /dev/null+++ b/app/views/settings/api_keys/index.html.erb@@ -0,0 +1,72 @@+<%= content_for :page_title, t(".title") %>+<% unless @api_keys.empty? %>+ <%= content_for :page_actions do %>+ <%= render DS::Link.new(+ text: t(".new_key"),+ href: new_settings_api_key_path,+ variant: "primary",+ icon: "plus"+ ) %>+ <% end %>+<% end %>++<% if @api_keys.empty? %>+ <div class="bg-container rounded-xl shadow-border-xs p-4">+ <div class="bg-surface-inset rounded-xl p-8 text-center">+ <div class="flex justify-center mb-4">+ <%= render DS::FilledIcon.new(+ icon: "key",+ rounded: true,+ size: "lg"+ ) %>+ </div>+ <h3 class="font-medium text-primary"><%= t(".empty_heading") %></h3>+ <p class="text-secondary text-sm mt-1 mb-4"><%= t(".empty_description") %></p>+ <div class="flex justify-center">+ <%= render DS::Link.new(+ text: t(".new_key"),+ href: new_settings_api_key_path,+ variant: "primary",+ icon: "plus"+ ) %>+ </div>+ </div>+ </div>+<% else %>+ <div class="space-y-3">+ <% @api_keys.each do |api_key| %>+ <div class="bg-container rounded-xl shadow-border-xs p-4">+ <div class="flex items-start justify-between gap-3">+ <div class="flex items-start gap-3 min-w-0">+ <%= render DS::FilledIcon.new(+ icon: "key",+ rounded: true,+ size: "lg"+ ) %>+ <div class="text-sm space-y-1 min-w-0">+ <%= link_to api_key.name, settings_api_key_path(api_key), class: "text-primary font-medium hover:underline break-all" %>+ <%= render "settings/api_keys/key_meta", api_key: api_key %>+ <div class="pt-1">+ <%= render "settings/api_keys/scope_badges", scopes: api_key.scopes %>+ </div>+ </div>+ </div>++ <div class="flex items-center gap-3 shrink-0">+ <%= render "settings/api_keys/status_indicator" %>+ <%= render DS::Button.new(+ text: t(".revoke_key"),+ href: settings_api_key_path(api_key),+ method: :delete,+ variant: "destructive",+ size: "sm",+ data: {+ turbo_confirm: t(".revoke_confirmation", name: api_key.name)+ }+ ) %>+ </div>+ </div>+ </div>+ <% end %>+ </div>+<% end %>diff --git a/app/views/settings/api_keys/new.html.erb b/app/views/settings/api_keys/new.html.erbindex d37a24f701..6566404369 100644--- a/app/views/settings/api_keys/new.html.erb+++ b/app/views/settings/api_keys/new.html.erb@@ -1,7 +1,7 @@<%= content_for :page_title, t(".create_new_api_key") %><%= settings_section title: nil, subtitle: t(".subtitle") do %>- <%= styled_form_with model: @api_key, url: settings_api_key_path, class: "space-y-4" do |form| %>+ <%= styled_form_with model: @api_key, url: settings_api_keys_path, class: "space-y-4" do |form| %><%= form.text_field :name,placeholder: t(".name_placeholder"),label: t(".name_label"),@@ -39,7 +39,7 @@<div class="flex justify-end gap-3 pt-4 border-t border-primary"><%= render DS::Link.new(text: t(".cancel"),- href: settings_api_key_path,+ href: settings_api_keys_path,variant: "ghost") %>diff --git a/app/views/settings/api_keys/show.html.erb b/app/views/settings/api_keys/show.html.erbindex c32e87349b..f27dc41664 100644--- a/app/views/settings/api_keys/show.html.erb+++ b/app/views/settings/api_keys/show.html.erb@@ -1,4 +1,13 @@-<% if @newly_created && @plain_key %>+<%= content_for :page_actions do %>+ <%= render DS::Link.new(+ text: t(".current_api_key.back_to_keys"),+ href: settings_api_keys_path,+ variant: "secondary",+ icon: "chevron-left"+ ) %>+<% end %>++<% if @newly_created %><%= content_for :page_title, t(".newly_created.page_title") %><div class="bg-container rounded-xl shadow-border-xs p-4">@@ -13,54 +22,33 @@) %><div class="flex-1"><h3 class="font-medium text-primary"><%= t(".newly_created.heading") %></h3>- <p class="text-secondary text-sm mt-1"><%= t(".newly_created.key_ready", name: @current_api_key.name) %></p>+ <p class="text-secondary text-sm mt-1"><%= t(".newly_created.key_ready", name: @api_key.name) %></p></div></div></div>- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".newly_created.your_api_key") %></h4>- <p class="text-secondary text-sm mb-3"><%= t(".newly_created.copy_store_securely") %></p>+ <%= render "settings/api_keys/key_reveal",+ api_key: @api_key,+ heading: t(".newly_created.your_api_key"),+ description: t(".newly_created.copy_store_securely"),+ copy_label: t(".newly_created.copy_api_key") %>- <div class="bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">- <div class="flex items-center justify-between gap-3">- <code id="api-key-display" class="font-mono text-sm text-primary break-all" data-clipboard-target="source"><%= @current_api_key.plain_key %></code>- <%= render DS::Button.new(- text: t(".newly_created.copy_api_key"),- variant: "ghost",- icon: "copy",- data: { action: "clipboard#copy" }- ) %>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".newly_created.how_to_use") %></h4>- <p class="text-secondary text-sm mb-3"><%= t(".current_api_key.usage_instructions", product_name: product_name) %></p>- <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary">- curl -H "X-Api-Key: <%= @current_api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts- </div>- </div>+ <%= render "settings/api_keys/usage",+ api_key: @api_key,+ title: t(".newly_created.how_to_use"),+ description: t(".current_api_key.usage_instructions", product_name: product_name) %><div class="flex justify-end pt-4 border-t border-primary"><%= render DS::Link.new(text: t(".newly_created.continue"),- href: settings_api_key_path,+ href: settings_api_keys_path,variant: "primary") %></div></div></div>-<% elsif @current_api_key %>+<% else %><%= content_for :page_title, t(".current_api_key.title") %>- <%= content_for :page_actions do %>- <%= render DS::Link.new(- text: t(".current_api_key.regenerate_key"),- href: new_settings_api_key_path(regenerate: true),- variant: "secondary"- ) %>- <% end %><div class="bg-container rounded-xl shadow-border-xs p-4"><div class="space-y-4">@@ -73,68 +61,34 @@) %><div class="text-sm space-y-1">- <p class="text-primary font-medium"><%= @current_api_key.name %></p>- <p class="text-secondary">- <%= t(".current_api_key.created_ago", time: time_ago_in_words(@current_api_key.created_at)) %>- <% if @current_api_key.last_used_at %>- • <%= t(".current_api_key.last_used_ago", time: time_ago_in_words(@current_api_key.last_used_at)) %>- <% else %>- • <%= t(".current_api_key.never_used") %>- <% end %>- </p>+ <p class="text-primary font-medium"><%= @api_key.name %></p>+ <%= render "settings/api_keys/key_meta", api_key: @api_key %></div></div>- <div class="rounded-md bg-success px-2 py-1">- <p class="text-success-foreground font-medium text-xs"><%= t(".current_api_key.active") %></p>- </div>+ <%= render "settings/api_keys/status_indicator" %></div><div class="bg-surface-inset rounded-xl p-4"><h4 class="font-medium text-primary mb-3"><%= t(".current_api_key.permissions") %></h4>- <div class="flex flex-wrap gap-2">- <% @current_api_key.scopes.each do |scope| %>- <span class="inline-flex items-center gap-1 px-2 py-1 button-bg-primary text-inverse rounded-full text-xs font-medium">- <%= icon("shield-check", class: "w-3 h-3") %>- <%= case scope- when "read" then t(".current_api_key.scope_read_only")- when "read_write" then t(".current_api_key.scope_read_write")- else scope.humanize- end %>- </span>- <% end %>- </div>+ <%= render "settings/api_keys/scope_badges", scopes: @api_key.scopes %></div>- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".current_api_key.title") %></h4>- <p class="text-secondary text-sm mb-3"><%= t(".current_api_key.copy_store_securely") %></p>+ <%= render "settings/api_keys/key_reveal",+ api_key: @api_key,+ heading: t(".current_api_key.title"),+ description: t(".current_api_key.copy_store_securely"),+ copy_label: t(".current_api_key.copy_api_key") %>- <div class="bg-container rounded-lg p-3 border border-primary" data-controller="clipboard">- <div class="flex items-center justify-between gap-3">- <code id="api-key-display" class="font-mono text-sm text-primary break-all" data-clipboard-target="source"><%= @current_api_key.plain_key %></code>- <%= render DS::Button.new(- text: t(".current_api_key.copy_api_key"),- variant: "ghost",- icon: "copy",- data: { action: "clipboard#copy" }- ) %>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".current_api_key.usage_instructions_title") %></h4>- <p class="text-secondary text-sm mb-3"><%= t(".current_api_key.usage_instructions", product_name: product_name) %></p>- <div class="bg-container rounded-lg p-3 font-mono text-sm text-primary border border-primary">- curl -H "X-Api-Key: <%= @current_api_key.plain_key %>" <%= request.base_url %>/api/v1/accounts- </div>- </div>+ <%= render "settings/api_keys/usage",+ api_key: @api_key,+ title: t(".current_api_key.usage_instructions_title"),+ description: t(".current_api_key.usage_instructions", product_name: product_name) %><div class="flex justify-end pt-4 border-t border-primary"><%= render DS::Button.new(text: t(".current_api_key.revoke_key"),- href: settings_api_key_path,+ href: settings_api_key_path(@api_key),method: :delete,variant: "destructive",data: {@@ -144,54 +98,4 @@</div></div></div>-<% else %>- <%= content_for :page_title, t(".no_api_key.title") %>- <%= content_for :page_actions do %>- <%= render DS::Link.new(- text: t(".no_api_key.create_api_key"),- href: new_settings_api_key_path,- variant: "primary"- ) %>- <% end %>-- <div class="bg-container rounded-xl shadow-border-xs p-4">- <div class="space-y-4">- <div class="p-3 shadow-border-xs bg-container rounded-lg">- <div class="flex items-start gap-3">- <%= render DS::FilledIcon.new(- icon: "key",- rounded: true,- size: "lg"- ) %>- <div class="flex-1">- <h3 class="font-medium text-primary"><%= t(".no_api_key.heading", product_name: product_name) %></h3>- <p class="text-secondary text-sm mt-1"><%= t(".no_api_key.description") %></p>- </div>- </div>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-3"><%= t(".no_api_key.what_you_can_do") %></h4>- <ul class="space-y-2 text-sm text-secondary">- <li class="flex items-start gap-2">- <%= icon("check", class: "w-4 h-4 text-primary mt-0.5") %>- <span><%= t(".no_api_key.feature_1") %></span>- </li>- <li class="flex items-start gap-2">- <%= icon("check", class: "w-4 h-4 text-primary mt-0.5") %>- <span><%= t(".no_api_key.feature_2") %></span>- </li>- <li class="flex items-start gap-2">- <%= icon("check", class: "w-4 h-4 text-primary mt-0.5") %>- <span><%= t(".no_api_key.feature_3") %></span>- </li>- </ul>- </div>-- <div class="bg-surface-inset rounded-xl p-4">- <h4 class="font-medium text-primary mb-2"><%= t(".no_api_key.security_note_title") %></h4>- <p class="text-secondary text-sm"><%= t(".no_api_key.security_note") %></p>- </div>- </div>- </div><% end %>diff --git a/config/locales/views/settings/api_keys/en.yml b/config/locales/views/settings/api_keys/en.ymlindex ebc072b88b..c9fd6eba54 100644--- a/config/locales/views/settings/api_keys/en.yml+++ b/config/locales/views/settings/api_keys/en.yml@@ -5,32 +5,29 @@ en:success: "Your API key has been created successfully"revoked_successfully: "API key has been revoked successfully"revoke_failed: "Failed to revoke API key"- scope_descriptions:- read_accounts: "View Accounts"- read_transactions: "View Transactions"- read_balances: "View Balances"- write_transactions: "Create Transactions"api_keys:create:success: "Your API key has been created successfully"destroy:- not_found: "API key not found"- cannot_revoke: "This API key cannot be revoked"revoked_successfully: "API key has been revoked successfully"revoke_failed: "Failed to revoke API key"+ shared:+ active: "Active"+ created_ago: "Created %{time} ago"+ last_used_ago: "Last used %{time} ago"+ never_used: "Never used"+ scope_read_only: "Read Only"+ scope_read_write: "Read/Write"+ index:+ title: "API Keys"+ subtitle: "Manage API keys for programmatic access to your data."+ new_key: "New API Key"+ empty_heading: "No API keys yet"+ empty_description: "Create an API key to access your data programmatically."+ revoke_key: "Revoke"+ revoke_confirmation: "Are you sure you want to revoke \"%{name}\"? This will immediately disable any application using this key."show:title: "API Key Management"- no_api_key:- title: "API Key"- heading: "Access your account data programmatically"- description: "Get programmatic access to your Sure data with a secure API key."- what_you_can_do: "What you can do with the API:"- feature_1: "Access your account data programmatically"- feature_2: "Build custom integrations and applications"- feature_3: "Automate data retrieval and analysis"- security_note_title: "Security First"- security_note: "Your API key will have restricted permissions based on the scopes you select. You can only have one active API key at a time."- create_api_key: "Create API Key"newly_created:page_title: "API Key Created Successfully"heading: "API Key Created Successfully!"@@ -42,25 +39,12 @@ en:continue: "Continue to API Key Settings"current_api_key:title: "Your API Key"- description: "Your active API key is ready to use. Keep it secure and never share it publicly."- active: "Active"- key_name: "Name"- created_at: "Created"- created_ago: "Created %{time} ago"- last_used: "Last Used"- last_used_ago: "Last used %{time} ago"- expires: "Expires"- ago: "ago"- never_used: "Never used"- never_expires: "Never expires"permissions: "Permissions"- scope_read_only: "Read Only"- scope_read_write: "Read/Write"copy_api_key: "Copy API Key"copy_store_securely: "Copy and store this key securely. You'll need it to authenticate your API requests."usage_instructions_title: "How to use your API key"usage_instructions: "Include your API key in the X-Api-Key header when making requests to the %{product_name} API:"- regenerate_key: "Create New Key"+ back_to_keys: "Back to API Keys"revoke_key: "Revoke Key"revoke_confirmation: "Are you sure you want to revoke this API key? This action cannot be undone and will immediately disable all applications using this key."new:@@ -88,28 +72,4 @@ en:security_warning: "Your API key will be shown only once after creation. Store it securely and never share it publicly. If you lose it, you'll need to create a new one."create_key: "Create API Key"cancel: "Cancel"- save_api_key: "Save API Key"- created:- page_title: "API Key Created"- title: "API Key Created"- success_title: "API Key Created Successfully"- success_description: "Your new API key has been generated successfully."- key_ready: "Your new API key \"%{name}\" has been created and is ready to use."- your_api_key: "Your API Key"- copy_store_securely: "Copy and store this key securely. You'll need it to authenticate your API requests."- key_details_title: "Key Details"- key_name_label: "Name:"- permissions_label: "Permissions:"- created_label: "Created:"- security_note_title: "Important Security Note"- security_note_body: "This is the only time your API key will be displayed. Make sure to copy it now and store it securely. If you lose this key, you'll need to generate a new one."- usage_instructions_title: "How to use your API key"- key_name: "Name"- permissions: "Permissions"- critical_warning_title: "Critical: Save Your API Key Now"- critical_warning_1: "This is the only time you'll see your API key in plain text."- critical_warning_2: "Copy and store it securely in your password manager or application."- critical_warning_3: "If you lose this key, you'll need to create a new one."- usage_instructions: "Use your API key by including it in the X-Api-Key header:"- copy_key: "Copy API Key"- continue: "Continue to API Key Settings"\ No newline at end of file+ save_api_key: "Save API Key"\ No newline at end of filediff --git a/config/locales/views/settings/en.yml b/config/locales/views/settings/en.ymlindex c9913ee8e8..ee825a7d04 100644--- a/config/locales/views/settings/en.yml+++ b/config/locales/views/settings/en.yml@@ -262,7 +262,7 @@ en:accounts_label: Accountsadvanced_section_title: Advancedai_prompts_label: AI Prompts- api_key_label: API Key+ api_key_label: API Keyspayment_label: Paymentcategories_label: Categoriesfeedback_label: Feedback@@ -288,7 +288,7 @@ en:transactions_section_title: Transactionsusers_label: Userswhats_new_label: What's new- api_keys_label: API Key+ api_keys_label: API Keysappearance_label: Appearancebank_sync_label: Bank syncdebug_label: Debugdiff --git a/config/routes.rb b/config/routes.rbindex 3570ce392e..8dbef6d5a1 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -258,7 +258,7 @@ Rails.application.routes.draw dopost :options, on: :collectionendresources :sso_identities, only: :destroy- resource :api_key, only: [ :show, :new, :create, :destroy ]+ resources :api_keys, only: [ :index, :show, :new, :create, :destroy ]resource :mcp, controller: "mcp", only: :show dodelete "tokens/:token_id", to: "mcp#revoke", as: :revoke_tokenenddiff --git a/test/controllers/settings/api_keys_controller_test.rb b/test/controllers/settings/api_keys_controller_test.rbindex 1905a4cab3..df20dd3131 100644--- a/test/controllers/settings/api_keys_controller_test.rb+++ b/test/controllers/settings/api_keys_controller_test.rb@@ -7,29 +7,20 @@ class Settings::ApiKeysControllerTest < ActionDispatch::IntegrationTestsign_in @userend- test "should show no API key page when user has no active keys" do- get settings_api_key_path- assert_response :success- end-- test "should show current API key when user has active key" do- @api_key = ApiKey.create!(+ test "index shows api keys list" do+ ApiKey.create!(user: @user,- name: "Test API Key",- display_key: "test_key_123",+ name: "Listed Key",+ display_key: "listed_key_123",scopes: [ "read" ])- get settings_api_key_path- assert_response :success- end-- test "should show new API key form" do- get new_settings_api_key_path+ get settings_api_keys_pathassert_response :success+ assert_includes response.body, "Listed Key"end- test "should redirect to show when user already has active key and tries to visit new" do+ test "new always renders form (no redirect when key exists)" doApiKey.create!(user: @user,name: "Existing API Key",@@ -38,60 +29,54 @@ class Settings::ApiKeysControllerTest < ActionDispatch::IntegrationTest)get new_settings_api_key_path- assert_redirected_to settings_api_key_path+ assert_response :successend- test "should create new API key with valid parameters" do+ test "create makes a new key without revoking existing keys" do+ existing = ApiKey.create!(+ user: @user,+ name: "Existing API Key",+ display_key: "existing_key_123",+ scopes: [ "read" ]+ )+assert_difference "ApiKey.count", 1 do- post settings_api_key_path, params: {+ post settings_api_keys_path, params: {api_key: {- name: "Test Integration Key",+ name: "Brand New Key",scopes: "read_write"}}end- assert_redirected_to settings_api_key_path- follow_redirect!- assert_response :success+ new_key = @user.api_keys.active.visible.find_by(name: "Brand New Key")+ assert new_key.present?+ assert_redirected_to settings_api_key_path(new_key, newly_created: true)- api_key = @user.api_keys.active.first- assert_equal "Test Integration Key", api_key.name- assert_includes api_key.scopes, "read_write"+ existing.reload+ refute existing.revoked?+ assert_includes new_key.scopes, "read_write"end- test "should revoke existing key when creating new one" do- old_key = ApiKey.create!(- user: @user,- name: "Old API Key",- display_key: "old_key_123",- scopes: [ "read" ]- )-- post settings_api_key_path, params: {- api_key: {- name: "New API Key",- scopes: "read_write"+ test "create rejects blank name" do+ assert_no_difference "ApiKey.count" do+ post settings_api_keys_path, params: {+ api_key: {+ name: "",+ scopes: "read"+ }}- }-- assert_redirected_to settings_api_key_path- follow_redirect!- assert_response :success-- old_key.reload- assert old_key.revoked?+ end- new_key = @user.api_keys.active.first- assert_equal "New API Key", new_key.name+ assert_response :unprocessable_entityend- test "should not create API key without name" do+ test "create rejects blank scopes" doassert_no_difference "ApiKey.count" do- post settings_api_key_path, params: {+ post settings_api_keys_path, params: {api_key: {- name: "",- scopes: "read"+ name: "No Scopes Key",+ scopes: []}}end@@ -99,93 +84,120 @@ class Settings::ApiKeysControllerTest < ActionDispatch::IntegrationTestassert_response :unprocessable_entityend- test "should not create API key without scopes" do- # Ensure clean state for this specific test- @user.api_keys.destroy_all- initial_user_count = @user.api_keys.count+ test "create rejects duplicate active name" do+ ApiKey.create!(+ user: @user,+ name: "Dup",+ display_key: "dup_key_123",+ scopes: [ "read" ]+ )- assert_no_difference "@user.api_keys.count" do- post settings_api_key_path, params: {- api_key: {- name: "Test Key",- scopes: []+ assert_no_difference "ApiKey.count" do+ post settings_api_keys_path, params: {+ api_key: {+ name: "Dup",+ scopes: "read_write"+ }}- }- end+ end- assert_response :unprocessable_entity- assert_equal initial_user_count, @user.api_keys.reload.count-end+ assert_response :unprocessable_entity+ end- test "should revoke API key" do- @api_key = ApiKey.create!(+ test "show renders a key" do+ created_key = ApiKey.create!(user: @user,name: "Test API Key",display_key: "test_key_123",scopes: [ "read" ])- delete settings_api_key_path-- assert_redirected_to settings_api_key_path- follow_redirect!+ get settings_api_key_path(created_key)assert_response :success+ assert_includes response.body, "Test API Key"+ end++ test "show renders the newly created confirmation" do+ created_key = ApiKey.create!(+ user: @user,+ name: "Fresh Key",+ display_key: "fresh_key_123",+ scopes: [ "read" ]+ )- @api_key.reload- assert @api_key.revoked?+ get settings_api_key_path(created_key, newly_created: true)+ assert_response :success+ assert_includes response.body, created_key.plain_key+ assert_select "h3", text: I18n.t("settings.api_keys.show.newly_created.heading")end- test "should handle revoke when no API key exists" do- delete settings_api_key_path+ test "show 404s on another user's key" do+ other_user = users(:family_member)+ other_user.api_keys.destroy_all+ other_key = ApiKey.create!(+ user: other_user,+ name: "Other User Key",+ display_key: "other_user_key_123",+ scopes: [ "read" ]+ )- assert_redirected_to settings_api_key_path- # Should not error even when no API key exists+ get settings_api_key_path(other_key)+ assert_response :not_foundend- test "should only allow one active API key per user" do- # Create first API key- post settings_api_key_path, params: {- api_key: {- name: "First Key",- scopes: "read"- }- }+ test "destroy revokes the targeted key only" do+ key1 = ApiKey.create!(+ user: @user,+ name: "Key One",+ display_key: "key_one_123",+ scopes: [ "read" ]+ )+ key2 = ApiKey.create!(+ user: @user,+ name: "Key Two",+ display_key: "key_two_123",+ scopes: [ "read_write" ]+ )- first_key = @user.api_keys.active.first+ delete settings_api_key_path(key1)+ assert_redirected_to settings_api_keys_path- # Create second API key- post settings_api_key_path, params: {- api_key: {- name: "Second Key",- scopes: "read_write"- }- }+ key1.reload+ key2.reload+ assert key1.revoked?+ refute key2.revoked?+ end- # First key should be revoked- first_key.reload- assert first_key.revoked?+ test "destroy cannot revoke demo monitoring key" do+ # set_api_key scopes to .visible which EXCLUDES the demo key, so the+ # demo key id is not found by the controller and the request 404s+ # before reaching the cannot_revoke branch.+ demo_key = ApiKey.create!(+ user: @user,+ name: "Demo Monitoring Key",+ display_key: ApiKey::DEMO_MONITORING_KEY,+ scopes: [ "read" ]+ )++ delete settings_api_key_path(demo_key)+ assert_response :not_found- # Only one active key should exist- assert_equal 1, @user.api_keys.active.count- assert_equal "Second Key", @user.api_keys.active.first.name+ demo_key.reload+ refute demo_key.revoked?end- test "should generate secure random API key" do- post settings_api_key_path, params: {+ test "create generates a secure random API key" do+ post settings_api_keys_path, params: {api_key: {name: "Random Key Test",scopes: "read"}}- assert_redirected_to settings_api_key_path- follow_redirect!- assert_response :success-- # Verify the API key was created with expected properties- api_key = @user.api_keys.active.first- assert api_key.present?- assert_equal "Random Key Test", api_key.name- assert_includes api_key.scopes, "read"+ created_key = @user.api_keys.active.visible.find_by(name: "Random Key Test")+ assert created_key.present?+ assert_redirected_to settings_api_key_path(created_key, newly_created: true)+ assert_includes created_key.scopes, "read"+ assert_equal 64, created_key.plain_key.lengthendenddiff --git a/test/models/api_key_test.rb b/test/models/api_key_test.rbindex edd60389a6..46087f7908 100644--- a/test/models/api_key_test.rb+++ b/test/models/api_key_test.rb@@ -135,7 +135,7 @@ class ApiKeyTest < ActiveSupport::TestCaseassert @api_key.last_used_at > (original_time || Time.at(0))end- test "should prevent user from having multiple active api keys" do+ test "allows multiple active api keys for the same user" do@api_key.save!second_key = ApiKey.new(@@ -145,22 +145,69 @@ class ApiKeyTest < ActiveSupport::TestCasescopes: [ "read" ])- assert_not second_key.valid?- assert_includes second_key.errors[:user], "can only have one active API key per source (web)"+ assert second_key.valid?+ assert second_key.save+ assert second_key.active?+ assert_equal 2, @user.api_keys.active.countend- test "should allow user to have new active key after revoking old one" do- @api_key.save!- @api_key.revoke!+ test "rejects duplicate name among active keys for the same user" do+ ApiKey.create!(+ user: @user,+ name: "Dup",+ key: "dup_key_123",+ scopes: [ "read" ]+ )- second_key = ApiKey.new(+ duplicate = ApiKey.new(user: @user,- name: "Second API Key",- key: "another_key_123",+ name: "Dup",+ key: "dup_key_456",scopes: [ "read" ])- assert second_key.valid?+ assert_not duplicate.valid?+ assert_includes duplicate.errors.attribute_names, :name+ end++ test "allows reusing the name of a revoked key" do+ first = ApiKey.create!(+ user: @user,+ name: "Reuse",+ key: "reuse_key_123",+ scopes: [ "read" ]+ )+ first.revoke!++ reused = ApiKey.new(+ user: @user,+ name: "Reuse",+ key: "reuse_key_456",+ scopes: [ "read" ]+ )++ assert reused.valid?+ end++ test "allows the same name across different users" do+ ApiKey.create!(+ user: @user,+ name: "Shared",+ key: "shared_key_123",+ scopes: [ "read" ]+ )++ other_user = users(:family_member)+ other_user.api_keys.destroy_all++ other_key = ApiKey.new(+ user: other_user,+ name: "Shared",+ key: "shared_key_456",+ scopes: [ "read" ]+ )++ assert other_key.valid?endtest "should allow active monitoring key alongside active web key" do@@ -177,6 +224,21 @@ class ApiKeyTest < ActiveSupport::TestCaseassert monitoring_key.valid?end+ test "visible scope excludes the demo monitoring key" do+ # The demo key's exclusion from `.visible` is the revocation guard used by+ # Settings::ApiKeysController#destroy. Lock the invariant so broadening the+ # scope can't silently make the demo key revocable.+ demo_key = ApiKey.create!(+ user: @user,+ name: "Demo Monitoring Key",+ display_key: ApiKey::DEMO_MONITORING_KEY,+ scopes: [ "read" ]+ )++ refute_includes ApiKey.visible, demo_key+ refute_includes @user.api_keys.active.visible, demo_key+ end+test "should include active api keys in active scope" do@api_key.save!active_keys = ApiKey.activediff --git a/test/system/settings/api_keys_test.rb b/test/system/settings/api_keys_test.rbindex e558cb945c..bf296309fd 100644--- a/test/system/settings/api_keys_test.rb+++ b/test/system/settings/api_keys_test.rb@@ -7,200 +7,75 @@ class Settings::ApiKeysTest < ApplicationSystemTestCaselogin_as @userend- test "should show no API key state when user has no active keys" do- visit settings_api_key_path+ test "should show empty state when user has no API keys" do+ visit settings_api_keys_path- assert_text "API Key"- assert_link "Create API Key", href: new_settings_api_key_path- assert_text "Access your account data programmatically"+ assert_text "API Keys"+ assert_text "No API keys yet"+ assert_link "New API Key", href: new_settings_api_key_pathend- test "should navigate to create new API key form" do- visit settings_api_key_path- click_link "Create API Key"+ test "should create multiple keys, list them, and revoke one" do+ # Create the first key+ visit settings_api_keys_path+ click_link "New API Key"assert_current_path new_settings_api_key_pathassert_text "Create New API Key"- assert_field "API Key Name"- assert_text "Read Only"- assert_text "Read/Write"- end-- test "should create a new API key with selected scopes" do- visit new_settings_api_key_path- fill_in "API Key Name", with: "Test Integration Key"+ fill_in "API Key Name", with: "First Key"choose "Read/Write"-click_button "Save API Key"- # Should redirect to show page with the API key details- assert_current_path settings_api_key_path- assert_text "Test Integration Key"- assert_text "Your API Key"-- # Should show the actual API key value- api_key_display = find("#api-key-display")+ # Newly-created confirmation page shows the plain key+ assert_text "API Key Created Successfully"+ assert_text "First Key"+ api_key_display = find("[data-clipboard-target='source']")assert api_key_display.text.length > 30 # Should be a long hex string- # Should show copy buttons- assert_button "Copy API Key"- assert_link "Create New Key"- end-- test "should show current API key details after creation" do- # Create an API key first- api_key = ApiKey.create!(- user: @user,- name: "Production API Key",- display_key: "test_plain_key_123",- scopes: [ "read_write" ]- )-- visit settings_api_key_path-- assert_text "Your API Key"- assert_text "Production API Key"- assert_text "Active"- assert_text "Read/Write"- assert_text "Never used"- assert_link "Create New Key"- assert_button "Revoke Key"- end-- test "should show usage instructions and example curl command" do- api_key = ApiKey.create!(- user: @user,- name: "Test API Key",- display_key: "test_key_123",- scopes: [ "read" ]- )-- visit settings_api_key_path-- assert_text "How to use your API key"- assert_text "curl -H \"X-Api-Key: test_key_123\""- assert_text "/api/v1/accounts"- end-- test "should allow regenerating API key" do- api_key = ApiKey.create!(- user: @user,- name: "Old API Key",- display_key: "old_key_123",- scopes: [ "read" ]- )-- visit settings_api_key_path- click_link "Create New Key"-- # Should be on the new API key form- assert_text "Create New API Key"+ # Back to the list, the first key is shown+ click_link "Continue to API Key Settings"+ assert_current_path settings_api_keys_path+ assert_text "First Key"- fill_in "API Key Name", with: "New API Key"+ # Create a SECOND key with a different name+ click_link "New API Key"+ fill_in "API Key Name", with: "Second Key"choose "Read Only"click_button "Save API Key"- # Should redirect to show page with new key- assert_text "New API Key"- assert_text "Your API Key"+ assert_text "API Key Created Successfully"+ click_link "Continue to API Key Settings"- # Old key should be revoked- api_key.reload- assert api_key.revoked?- end-- test "should allow revoking API key with confirmation" do- api_key = ApiKey.create!(- user: @user,- name: "Test API Key",- display_key: "test_key_123",- scopes: [ "read" ]- )-- visit settings_api_key_path+ # Both keys appear in the list+ assert_current_path settings_api_keys_path+ assert_text "First Key"+ assert_text "Second Key"- # Click the revoke button to open the modal- click_button "Revoke Key"+ # Revoke the first key via the custom confirmation dialog+ within all(".bg-container", text: "First Key").first do+ click_button "Revoke"+ end- # Wait for the dialog and then confirmassert_selector "#confirm-dialog", visible: truewithin "#confirm-dialog" doclick_button "Confirm"end-- # Wait for redirect after revokeassert_no_selector "#confirm-dialog"- assert_text "API Key"- assert_text "Access your account data programmatically"+ # First key gone, second remains+ assert_no_text "First Key"+ assert_text "Second Key"- # Key should be revoked in the database- api_key.reload- assert api_key.revoked?+ assert @user.api_keys.where(name: "First Key").first.revoked?+ refute @user.api_keys.active.where(name: "Second Key").empty?end- test "should redirect to show when user already has active key and tries to visit new" do- api_key = ApiKey.create!(- user: @user,- name: "Existing API Key",- display_key: "existing_key_123",- scopes: [ "read" ]- )-- visit new_settings_api_key_path-- assert_current_path settings_api_key_path- end-- test "should show API key in navigation" do- visit settings_api_key_path+ test "should show API keys in navigation" do+ visit settings_api_keys_pathwithin("nav") do- assert_text "API Key"+ assert_text "API Keys"endend-- test "should validate API key name is required" do- visit new_settings_api_key_path-- # Try to submit without name- choose "Read Only"- click_button "Save API Key"-- # Should stay on form with validation error- assert_current_path new_settings_api_key_path- assert_field "API Key Name" # Form should still be visible- # The form might not show the validation error inline, but should remain on the form- end-- test "should show last used timestamp when API key has been used" do- api_key = ApiKey.create!(- user: @user,- name: "Used API Key",- display_key: "used_key_123",- scopes: [ "read" ],- last_used_at: 2.hours.ago- )-- visit settings_api_key_path-- assert_text "2 hours ago"- assert_no_text "Never used"- end-- test "should show expiration date when API key has expiration" do- api_key = ApiKey.create!(- user: @user,- name: "Expiring API Key",- display_key: "expiring_key_123",- scopes: [ "read" ],- expires_at: 30.days.from_now- )-- visit settings_api_key_path-- # Should show some indication of expiration (exact format may vary)- assert_no_text "Never expires"- endenddiff --git a/test/system/settings_test.rb b/test/system/settings_test.rbindex 96ec24dcce..954c152668 100644--- a/test/system/settings_test.rb+++ b/test/system/settings_test.rb@@ -30,7 +30,7 @@ class SettingsTest < ApplicationSystemTestCase@settings_links.insert(merchants_index + 1, [ "Statement Vault", account_statements_path ])@settings_links += [[ "AI Prompts", settings_ai_prompts_path ],- [ "API Key", settings_api_key_path ]+ [ "API Keys", settings_api_keys_path ]]endend@@ -93,7 +93,7 @@ class SettingsTest < ApplicationSystemTestCase# Assert that admin-only settings are not present in the navigationassert_no_selector "li", text: "AI Prompts"- assert_no_selector "li", text: "API Key"+ assert_no_selector "li", text: "API Keys"assert_no_selector "li", text: "Bank sync"assert_no_selector "li", text: "Statement Vault"end