Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix API key scope handling and form checkbox styling (#13980)

Damian Legawiec68e518aaf08a
diff --git a/spree/admin/app/views/spree/admin/api_keys/_form.html.erb b/spree/admin/app/views/spree/admin/api_keys/_form.html.erb
index d911186ea9..4fbf1b173f 100644
--- a/spree/admin/app/views/spree/admin/api_keys/_form.html.erb
+++ b/spree/admin/app/views/spree/admin/api_keys/_form.html.erb
@@ -34,7 +34,7 @@
</div>
<div class="card-body">
<% Spree::ApiKey::SCOPES.each do |scope| %>
- <div class="custom-control custom-checkbox mb-1">
+ <div class="custom-control form-checkbox mb-1">
<%= f.check_box :scopes, { multiple: true, class: 'custom-control-input', id: "api_key_scopes_#{scope}" }, scope, nil %>
<%= f.label "scopes_#{scope}", scope, class: 'custom-control-label font-mono text-sm' %>
</div>
diff --git a/spree/admin/spec/features/spree/admin/api_keys_spec.rb b/spree/admin/spec/features/spree/admin/api_keys_spec.rb
new file mode 100644
index 0000000000..dc00356d11
--- /dev/null
+++ b/spree/admin/spec/features/spree/admin/api_keys_spec.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.feature 'API Keys', :js do
+ stub_authorization!
+
+ let!(:admin_user) { create(:admin_user) }
+
+ before do
+ allow_any_instance_of(Spree::Admin::BaseController).to receive(:try_spree_current_user).and_return(admin_user)
+ end
+
+ describe 'creating a secret API key' do
+ before { visit spree.new_admin_api_key_path }
+
+ it 'allows checking scope checkboxes and saves them without an "unknown scopes" error' do
+ # Scope checkboxes use the .form-checkbox style with the actual <input>
+ # visually hidden; clicks must be routed through the associated <label>.
+ expect(page).to have_css('.custom-control.form-checkbox input#api_key_scopes_read_orders', visible: :all)
+
+ fill_in 'api_key_name', with: 'My Integration'
+ select 'Secret', from: 'api_key_key_type'
+
+ check 'read_orders', allow_label_click: true
+ check 'write_products', allow_label_click: true
+
+ expect(page).to have_checked_field('api_key_scopes_read_orders', visible: :all)
+ expect(page).to have_checked_field('api_key_scopes_write_products', visible: :all)
+
+ click_button 'Create'
+
+ expect(page).not_to have_content('Scopes contains unknown scopes')
+
+ api_key = Spree::ApiKey.last
+ expect(api_key.name).to eq('My Integration')
+ expect(api_key.key_type).to eq('secret')
+ expect(api_key.scopes).to contain_exactly('read_orders', 'write_products')
+ end
+ end
+end
diff --git a/spree/core/app/models/spree/api_key.rb b/spree/core/app/models/spree/api_key.rb
index 42ef0b8457..0bb74834f6 100644
--- a/spree/core/app/models/spree/api_key.rb
+++ b/spree/core/app/models/spree/api_key.rb
@@ -29,6 +29,10 @@ module Spree
# The DB driver handles array <-> JSON conversion; no `serialize` needed.
attribute :scopes, default: []
+ def scopes=(value)
+ super(Array(value).map(&:to_s).reject(&:blank?))
+ end
+
# Returns the raw token value. For publishable keys this is the persisted
# +token+ column. For secret keys it is only available in memory immediately
# after creation (not persisted).
diff --git a/spree/core/spec/models/spree/api_key_spec.rb b/spree/core/spec/models/spree/api_key_spec.rb
index 44624f86d5..f8ff838c7d 100644
--- a/spree/core/spec/models/spree/api_key_spec.rb
+++ b/spree/core/spec/models/spree/api_key_spec.rb
@@ -280,6 +280,12 @@ RSpec.describe Spree::ApiKey, type: :model do
key = build(:api_key, :publishable, store: store, scopes: [])
expect(key).to be_valid
end
+
+ it 'filters out blank values from the scopes array (sentinel from form hidden field)' do
+ key = build(:api_key, :secret, store: store, scopes: ['', 'read_orders'])
+ expect(key.scopes).to eq(['read_orders'])
+ expect(key).to be_valid
+ end
end
describe '#has_scope?' do