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.

Fold the feature specs into the files that already cover their subjects

Damian Legawiecd07b32375e53
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb
index 6ca531edec..89f09ce679 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb
@@ -120,4 +120,131 @@ RSpec.describe Spree::Api::V3::Admin::StockLevelsController, type: :controller d
expect(response).to have_http_status(:no_content)
end
end
+
+ describe 'POST #bulk_upsert' do
+ # Shadows the file-level rows: the feed scenarios need a location owned by
+ # this store and a known starting count, with no leftover levels.
+ let!(:stock_location) { create(:stock_location, store: store) }
+
+ before do
+ variant.stock_levels.destroy_all
+ create(:stock_level, variant: variant, stock_location: stock_location,
+ count_on_hand: 5, adjust_count_on_hand: false)
+ variant.stock_levels.reload
+ end
+
+ def stock_level
+ variant.stock_levels.find_by(stock_location: stock_location)
+ end
+
+ it 'sets the level a feed reports' do
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
+ count_on_hand: 12 }]
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['stock_level_count']).to eq(1)
+ expect(stock_level.count_on_hand).to eq(12)
+ end
+
+ # A merchant has to be able to see why a number moved, so a feed writes
+ # history rather than overwriting the figure.
+ it 'records the change as a movement rather than a silent overwrite' do
+ expect do
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
+ count_on_hand: 12 }]
+ }, as: :json
+ end.to change(Spree::StockMovement, :count).by(1)
+
+ expect(Spree::StockMovement.last.quantity).to eq(7)
+ end
+
+ it 'accepts a relative adjustment as well as an absolute level' do
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
+ adjustment: -2 }]
+ }, as: :json
+
+ expect(stock_level.count_on_hand).to eq(3)
+ end
+
+ it 'addresses records by the keys the external system holds' do
+ variant.set_external_id('erp', 'MAT-100')
+ stock_location.set_external_id('erp', 'WH-1')
+
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant: { external_id: { erp: 'MAT-100' } },
+ stock_location: { external_id: { erp: 'WH-1' } },
+ count_on_hand: 9 }]
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(stock_level.count_on_hand).to eq(9)
+ end
+
+ it 'writes many pairs in one call' do
+ second = create(:variant)
+ second.stock_levels.destroy_all
+ create(:stock_level, variant: second, stock_location: stock_location, count_on_hand: 0,
+ adjust_count_on_hand: false)
+
+ post :bulk_upsert, params: {
+ stock_levels: [
+ { variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id, count_on_hand: 1 },
+ { variant_id: second.prefixed_id, stock_location_id: stock_location.prefixed_id, count_on_hand: 2 }
+ ]
+ }, as: :json
+
+ expect(json_response['stock_level_count']).to eq(2)
+ expect(stock_level.count_on_hand).to eq(1)
+ expect(second.stock_levels.find_by(stock_location: stock_location).count_on_hand).to eq(2)
+ end
+
+ it 'sets the backorder flag when the feed states it' do
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
+ count_on_hand: 5, backorderable: false }]
+ }, as: :json
+
+ expect(stock_level.backorderable).to be(false)
+ end
+
+ it 'rejects a row that names neither a level nor an adjustment' do
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id }]
+ }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(json_response['error']['details']['rows'].first['missing']).to include('count_on_hand')
+ end
+
+ it 'refuses to write another store stock' do
+ other_store = create(:store)
+ other_location = create(:stock_location, store: other_store)
+
+ post :bulk_upsert, params: {
+ stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: other_location.prefixed_id,
+ count_on_hand: 99 }]
+ }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(stock_level.count_on_hand).to eq(5)
+ end
+
+ it 'asks for the parameter rather than silently doing nothing' do
+ post :bulk_upsert, params: {}, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(json_response['error']['code']).to eq('missing_stock_levels')
+ end
+
+ it 'accepts an empty batch as a no-op' do
+ post :bulk_upsert, params: { stock_levels: [] }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['stock_level_count']).to eq(0)
+ end
+ end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb
index fd449f6709..354476ace2 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb
@@ -401,4 +401,62 @@ RSpec.describe Spree::Api::V3::Admin::StoreController, type: :controller do
end
end
end
+
+ describe 'GET #data_sources' do
+ it 'lists Spree own engines as available' do
+ get :data_sources, as: :json
+
+ expect(response).to have_http_status(:ok)
+ pricing = json_response['data']['pricing_providers']
+ expect(pricing.map { |row| row['key'] }).to include('internal')
+ expect(pricing.find { |row| row['key'] == 'internal' }['available']).to be(true)
+ end
+
+ it 'offers the failure policies a merchant can choose between' do
+ get :data_sources, as: :json
+
+ expect(json_response['data']['failure_policies']).to contain_exactly('fallback', 'strict')
+ end
+
+ # A connector gem's provider is listed but not selectable until its
+ # integration is connected, so the dashboard can say why rather than
+ # leaving the merchant wondering where their ERP went.
+ it 'marks a provider whose integration is not connected as unavailable' do
+ needs_credentials = Class.new(Spree::PricingProvider::Base) do
+ def self.key = 'acme_erp'
+ def self.provider_name = 'Acme ERP'
+ def self.integration_class = 'SpreeAcmeErp::Integration'
+ end
+ Spree.pricing_providers << needs_credentials
+
+ get :data_sources, as: :json
+
+ row = json_response['data']['pricing_providers'].find { |item| item['key'] == 'acme_erp' }
+ expect(row['available']).to be(false)
+ expect(row['integration_class']).to eq('SpreeAcmeErp::Integration')
+ ensure
+ Spree.pricing_providers.delete(needs_credentials)
+ end
+ end
+
+ describe 'PATCH #update' do
+ it 'saves the chosen providers and policies' do
+ patch :update, params: {
+ preferred_pricing_provider: 'internal',
+ preferred_inventory_provider: 'internal',
+ preferred_pricing_provider_failure_policy: 'fallback',
+ preferred_inventory_provider_failure_policy: 'strict'
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['preferred_pricing_provider_failure_policy']).to eq('fallback')
+ expect(store.reload.preferred_inventory_provider_failure_policy).to eq('strict')
+ end
+
+ it 'refuses a policy that is neither falling back nor strict' do
+ patch :update, params: { preferred_pricing_provider_failure_policy: 'ignore' }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+ end
end
diff --git a/spree/core/spec/workflows/spree/carts/add_item_spec.rb b/spree/core/spec/workflows/spree/carts/add_item_spec.rb
index 15be638d1f..42595702b5 100644
--- a/spree/core/spec/workflows/spree/carts/add_item_spec.rb
+++ b/spree/core/spec/workflows/spree/carts/add_item_spec.rb
@@ -335,5 +335,41 @@ module Spree
end
end
end
+
+ context 'with an external pricing provider' do
+ let(:store) { @default_store }
+ let(:cart) { create(:cart, store: store) }
+ let(:provider_class) do
+ Class.new(Spree::PricingProvider::Base) do
+ class << self; attr_accessor :calls; end
+ self.calls = 0
+ def self.key = 'contract'
+ def price_for(context)
+ self.class.calls += 1
+ Spree::Price.new(variant: context.variant, currency: context.currency, amount: 5)
+ end
+ end
+ end
+
+ before do
+ Spree.pricing_providers << provider_class
+ stub_store_preferences(store, pricing_provider: 'contract')
+ end
+
+ after { Spree.pricing_providers.delete(provider_class) }
+
+ # The second add increments an existing line; the quantity-change
+ # callback used to ask the provider again from inside the transaction.
+ it 'asks the provider once per add, even when incrementing an existing line' do
+ subject.call(cart: cart, variant: variant, quantity: 1)
+ provider_class.calls = 0
+
+ subject.call(cart: cart, variant: variant, quantity: 1)
+
+ expect(provider_class.calls).to eq(1)
+ expect(cart.line_items.reload.first.price).to eq(5)
+ expect(cart.line_items.first.quantity).to eq(2)
+ end
+ end
end
end