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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Point the moved agent tool specs at the derived map
Damian Legawieccad4d4de4ec9
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rbindex 8040677fcb..6237bc2377 100644--- a/spree/api/lib/spree/api/agent_resource_map.rb+++ b/spree/api/lib/spree/api/agent_resource_map.rb@@ -82,6 +82,20 @@ module SpreeSpree::GiftCard].freeze+ # Controllers that resolve their scope per request rather than declaring+ # one, because the scope belongs to the resource being imported or+ # exported — creating a product import is a product write, and reading+ # its rows exposes the uploaded data. A single static key cannot say+ # that, so these are registered read-only under the broadest key the+ # controller can demand, and the write path stays with the endpoint.+ DYNAMIC_SCOPE_RESOURCES = {+ 'imports' => { model_name: 'Spree::Import', permission: 'read_settings',+ serializer_name: 'Spree::Api::V3::Admin::ImportSerializer',+ dashboard_path: '/settings/imports' },+ 'exports' => { model_name: 'Spree::Export', permission: 'read_settings',+ serializer_name: 'Spree::Api::V3::Admin::ExportSerializer' }+ }.freeze+class << self# Walks every admin resource controller and registers what it serves.#@@ -97,6 +111,8 @@ module Spreemap.register(**entry)end+ DYNAMIC_SCOPE_RESOURCES.each { |key, attributes| map.register(key: key, **attributes) }+map.all.sizeenddiff --git a/spree/core/spec/lib/spree/agent_tools/authorization_spec.rb b/spree/api/spec/lib/spree/agent_tools/authorization_spec.rbsimilarity index 65%rename from spree/core/spec/lib/spree/agent_tools/authorization_spec.rbrename to spree/api/spec/lib/spree/agent_tools/authorization_spec.rbindex 9b7a989476..d91e322fd3 100644--- a/spree/core/spec/lib/spree/agent_tools/authorization_spec.rb+++ b/spree/api/spec/lib/spree/agent_tools/authorization_spec.rb@@ -7,7 +7,7 @@ require 'spec_helper'RSpec.describe 'assistant authorization' dolet(:store) { @default_store }let(:admin) { create(:admin_user) }- let(:context) { Spree::Assistant::Context.new(store: store, user: admin, ability: ability) }+ let(:context) { Spree::AgentTools::Context.new(store: store, user: admin, ability: ability) }let!(:visible) { create(:product, store: store, name: 'Visible Product', status: 'active') }let!(:hidden) { create(:product, store: store, name: 'Hidden Product', status: 'active') }@@ -30,7 +30,7 @@ RSpec.describe 'assistant authorization' dodescribe 'reads' doit 'hides records the ability excludes' do- result = Spree::Assistant::Tools::SearchResources.new(context).call(resource: 'products', limit: 25)+ result = Spree::AgentTools::SearchResources.new(context).call(resource: 'products', limit: 25)titles = result[:records].map { |record| record[:title] }expect(titles).to include('Visible Product')@@ -38,7 +38,7 @@ RSpec.describe 'assistant authorization' doendit 'counts only what the admin may see' do- result = Spree::Assistant::Tools::SearchResources.new(context).call(resource: 'products')+ result = Spree::AgentTools::SearchResources.new(context).call(resource: 'products')# The assistant states this number out loud, so a leak here is a leak the# merchant reads.@@ -46,7 +46,7 @@ RSpec.describe 'assistant authorization' doendit 'cannot fetch an excluded record directly' do- result = Spree::Assistant::Tools::GetResource.new(context).+ result = Spree::AgentTools::GetResource.new(context).call(resource: 'products', id: hidden.prefixed_id)expect(result[:error]).to be_present@@ -54,20 +54,27 @@ RSpec.describe 'assistant authorization' doendend- describe 'changes' do+ # The hand-written status tool is gone: the products workflows are tools by+ # the allowlist, so a change runs the same workflow the dashboard runs. What+ # the retired tool's specs asserted — that a record the ability excludes+ # cannot be changed, and one it includes can — is asserted here against the+ # workflow tool that replaced it.+ describe 'changes, through the workflow tool' do+ let(:tool) do+ Spree.agent_tools.available_for(context).find { |candidate| candidate.tool_name == 'products_draft' }+ end+it 'refuses a record the ability excludes' do- result = Spree::Assistant::Tools::UpdateProductStatus.new(context).- call(id: hidden.slug, status: 'draft')+ result = tool.call(product: hidden.prefixed_id)expect(result[:error]).to be_presentexpect(hidden.reload.status).to eq('active')endit 'allows a record the ability includes' do- result = Spree::Assistant::Tools::UpdateProductStatus.new(context).- call(id: visible.slug, status: 'draft')+ result = tool.call(product: visible.prefixed_id)- expect(result[:ok]).to be(true)+ expect(result[:error]).to be_nilexpect(visible.reload.status).to eq('draft')endenddiff --git a/spree/core/spec/lib/spree/agent_tools/resource_map_spec.rb b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rbsimilarity index 56%rename from spree/core/spec/lib/spree/agent_tools/resource_map_spec.rbrename to spree/api/spec/lib/spree/agent_tools/resource_map_spec.rbindex 9636cf74ae..012ab0b5a5 100644--- a/spree/core/spec/lib/spree/agent_tools/resource_map_spec.rb+++ b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb@@ -1,10 +1,10 @@require 'spec_helper'-RSpec.describe Spree::Assistant::ResourceMap do+RSpec.describe Spree::AgentTools::ResourceMap dolet(:store) { @default_store }let(:admin) { create(:admin_user) }let(:context) do- Spree::Assistant::Context.new(store: store, user: admin, ability: full_ability)+ Spree::AgentTools::Context.new(store: store, user: admin, ability: full_ability)endlet(:full_ability) doClass.new do@@ -18,7 +18,7 @@ RSpec.describe Spree::Assistant::ResourceMap do# exists, is searchable, and links somewhere real. A typo in any of the four# fields is invisible until a merchant asks the question that needs it.describe 'every registered resource' do- Spree::Assistant::ResourceMap.all.each do |entry|+ Spree::AgentTools::ResourceMap.all.each do |entry|context "`#{entry.key}`" doit 'names a model that exists' doexpect(entry.model_name.safe_constantize).to be_present@@ -44,13 +44,23 @@ RSpec.describe Spree::Assistant::ResourceMap doendend- describe 'a resource that is a slice of a model' do- it 'narrows through its scope' do- draft_orders = described_class.find('draft_orders')+ # The map is derived from the controllers, one entry per model, so a+ # resource that used to be a hand-written slice of another (draft orders+ # among orders) is now reached by filtering the model's own entry. The+ # `scope_name` mechanism stays for a registration that needs it.+ describe 'a resource narrowed by a scope' do+ it 'narrows through its scope when one is registered' do+ described_class.register(key: 'draft_orders', model_name: 'Spree::Order', scope_name: :drafts,+ permission: 'read_orders',+ serializer_name: 'Spree::Api::V3::Admin::OrderSerializer')++ drafts = described_class.find('draft_orders')orders = described_class.find('orders')- expect(draft_orders.model_name).to eq(orders.model_name)- expect(draft_orders.scope_for(context).to_sql).not_to eq(orders.scope_for(context).to_sql)+ expect(drafts.model_name).to eq(orders.model_name)+ expect(drafts.scope_for(context).to_sql).not_to eq(orders.scope_for(context).to_sql)+ ensure+ Spree::Api::AgentResourceMap.installendend@@ -63,7 +73,7 @@ RSpec.describe Spree::Assistant::ResourceMap doendend-RSpec.describe Spree::Assistant::ResourceMap, 'permission alignment' do+RSpec.describe Spree::AgentTools::ResourceMap, 'permission alignment' do# A resource must not be reachable through the assistant on a looser key than# the Admin API demands for the same records — otherwise an admin can ask the# assistant for something the dashboard would refuse them, and the permission@@ -82,20 +92,29 @@ RSpec.describe Spree::Assistant::ResourceMap, 'permission alignment' doDYNAMIC_PERMISSION = %w[imports exports].freezeCONTROLLER_OVERRIDES = { 'draft_orders' => 'OrdersController' }.freeze- described_class.all.each do |entry|- next if DYNAMIC_PERMISSION.include?(entry.key)-- controller_name = CONTROLLER_OVERRIDES.fetch(entry.key) { "#{entry.key.camelize}Controller" }-- it "`#{entry.key}` uses the same permission as #{controller_name}" do- controller = "Spree::Api::V3::Admin::#{controller_name}".safe_constantize- skip("#{controller_name} not found") if controller.nil?+ # Permissions no longer need comparing entry by entry: each one IS the+ # invoking controller's `scoped_resource`, read straight off the class. What+ # is worth asserting is that the derivation reached the controllers at all,+ # and that nothing arrived with a permission outside the catalog.+ it 'derives every entry from a controller that declares the same scope' do+ # What each controller actually declares, read the way the derivation+ # reads it, so this compares the map against its own source rather than+ # against a second list.+ declared = Spree::Api::AgentResourceMap.controllers.filter_map do |controller|+ scope = controller._scoped_resource+ model = Spree::Api::AgentResourceMap.send(:safely, controller.allocate, :model_class)+ next if scope.blank? || model.nil?++ [model.name, "read_#{scope}"]+ end- scoped = controller._scoped_resource- skip("#{controller_name} declares no scoped_resource") if scoped.blank?+ mismatched = described_class.all.+ # Imports and exports resolve their scope per request, so they+ # are registered explicitly rather than derived.+ reject { |entry| Spree::Api::AgentResourceMap::DYNAMIC_SCOPE_RESOURCES.key?(entry.key) }.+ reject { |entry| declared.include?([entry.model_name, entry.permission]) }- expect(entry.permission).to eq("read_#{scoped}")- end+ expect(mismatched.map(&:key)).to be_emptyendit 'points every resource at a permission the catalog actually defines' dodiff --git a/spree/core/spec/tools/spree/agent_tools/create_export_spec.rb b/spree/api/spec/tools/spree/agent_tools/create_export_spec.rbsimilarity index 92%rename from spree/core/spec/tools/spree/agent_tools/create_export_spec.rbrename to spree/api/spec/tools/spree/agent_tools/create_export_spec.rbindex e270f30ecf..3277c5fa50 100644--- a/spree/core/spec/tools/spree/agent_tools/create_export_spec.rb+++ b/spree/api/spec/tools/spree/agent_tools/create_export_spec.rb@@ -1,11 +1,11 @@require 'spec_helper'-RSpec.describe Spree::Assistant::Tools::CreateExport do+RSpec.describe Spree::AgentTools::CreateExport dosubject(:tool) { described_class.new(context) }let(:store) { @default_store }let(:admin) { create(:admin_user) }- let(:context) { Spree::Assistant::Context.new(store: store, user: admin, ability: ability) }+ let(:context) { Spree::AgentTools::Context.new(store: store, user: admin, ability: ability) }let(:ability) doClass.new doinclude CanCan::Abilitydiff --git a/spree/core/spec/tools/spree/agent_tools/import_tools_spec.rb b/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rbsimilarity index 92%rename from spree/core/spec/tools/spree/agent_tools/import_tools_spec.rbrename to spree/api/spec/tools/spree/agent_tools/import_tools_spec.rbindex 5df9d1ef7f..4c912b0cd8 100644--- a/spree/core/spec/tools/spree/agent_tools/import_tools_spec.rb+++ b/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb@@ -4,7 +4,7 @@ require 'csv'RSpec.describe 'assistant import tools' dolet(:store) { @default_store }let(:admin) { create(:admin_user) }- let(:context) { Spree::Assistant::Context.new(store: store, user: admin, ability: ability) }+ let(:context) { Spree::AgentTools::Context.new(store: store, user: admin, ability: ability) }let(:ability) doClass.new doinclude CanCan::Ability@@ -30,7 +30,7 @@ RSpec.describe 'assistant import tools' dorecordend- describe Spree::Assistant::Tools::DescribeImportMapping do+ describe Spree::AgentTools::DescribeImportMapping doit 'hands the model the real headings and the real field names' doresult = described_class.new(context).call(id: import.prefixed_id)@@ -41,7 +41,7 @@ RSpec.describe 'assistant import tools' doendend- describe Spree::Assistant::Tools::ProposeImportMapping do+ describe Spree::AgentTools::ProposeImportMapping dosubject(:tool) { described_class.new(context) }it 'is held for approval rather than applied outright' do@@ -86,7 +86,7 @@ RSpec.describe 'assistant import tools' doendend- describe Spree::Assistant::Tools::GetImportStatus do+ describe Spree::AgentTools::GetImportStatus doit 'reports row counts for the latest import' doimportdiff --git a/spree/core/spec/tools/spree/agent_tools/search_resources_spec.rb b/spree/api/spec/tools/spree/agent_tools/search_resources_spec.rbsimilarity index 93%rename from spree/core/spec/tools/spree/agent_tools/search_resources_spec.rbrename to spree/api/spec/tools/spree/agent_tools/search_resources_spec.rbindex dd8a066852..e1d02f7f15 100644--- a/spree/core/spec/tools/spree/agent_tools/search_resources_spec.rb+++ b/spree/api/spec/tools/spree/agent_tools/search_resources_spec.rb@@ -1,11 +1,11 @@require 'spec_helper'-RSpec.describe Spree::Assistant::Tools::SearchResources do+RSpec.describe Spree::AgentTools::SearchResources dosubject(:tool) { described_class.new(context) }let(:store) { @default_store }let(:admin) { create(:admin_user) }- let(:context) { Spree::Assistant::Context.new(store: store, user: admin, ability: ability) }+ let(:context) { Spree::AgentTools::Context.new(store: store, user: admin, ability: ability) }let(:ability) { full_ability_for(admin) }# A real ability rather than a double: the tools now call `accessible_by`,# which needs CanCanCan's actual machinery. `admin` here is a full admin, so@@ -102,12 +102,12 @@ RSpec.describe Spree::Assistant::Tools::SearchResources doendend-RSpec.describe Spree::Assistant::Tools::SearchResources, 'filter validation' do+RSpec.describe Spree::AgentTools::SearchResources, 'filter validation' dosubject(:tool) { described_class.new(context) }let(:store) { @default_store }let(:admin) { create(:admin_user) }- let(:context) { Spree::Assistant::Context.new(store: store, user: admin, ability: ability) }+ let(:context) { Spree::AgentTools::Context.new(store: store, user: admin, ability: ability) }let(:ability) doClass.new doinclude CanCan::Abilitydiff --git a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rbindex 8019995756..a480ff399f 100644--- a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb+++ b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb@@ -1,6 +1,6 @@require 'spec_helper'-RSpec.describe Spree::Assistant::RecordSummary do+RSpec.describe Spree::AgentTools::RecordSummary do# Every record the assistant emits crosses the wire to a third-party AI vendor# and is stored in chat history, so this filter is the last thing standing# between a serializer that gains a credential field and a live secret leaving