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.

Update examples. Include Spree::Webhooks when initializing.

Sebastian Palma2f200693eb60
diff --git a/api/lib/spree/api/engine.rb b/api/lib/spree/api/engine.rb
index 242a7ec7c1..cb76b69d40 100644
--- a/api/lib/spree/api/engine.rb
+++ b/api/lib/spree/api/engine.rb
@@ -20,6 +20,10 @@ module Spree
# FIXME: we should only notify about deprecated preferences that are in use, not all of them
# warn "[DEPRECATION] Spree::Api::Config[:#{pref[:name]}] is deprecated. #{pref[:message]}"
end
+ end
+
+ initializer 'Spree::Webhooks' do
+ Spree::Base.include(Spree::Webhooks::HasWebhooks)
Spree::Order.prepend(Spree::Api::Order)
Spree::Payment.prepend(Spree::Api::Payment)
Spree::Shipment.prepend(Spree::Api::Shipment)
diff --git a/api/spec/models/spree/order_spec.rb b/api/spec/models/spree/order_spec.rb
index d45c24344d..4c7f428f45 100644
--- a/api/spec/models/spree/order_spec.rb
+++ b/api/spec/models/spree/order_spec.rb
@@ -5,7 +5,7 @@ end
describe Spree::Order do
describe 'sending webhooks after transitioning from states' do
- let(:order) { Spree::Order.create(store: build(:store, default: true)) }
+ let(:order) { Spree::Order.create }
let(:payload) { {} }
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
diff --git a/api/spec/models/spree/payment_spec.rb b/api/spec/models/spree/payment_spec.rb
index bffc042079..b9c2d9f412 100644
--- a/api/spec/models/spree/payment_spec.rb
+++ b/api/spec/models/spree/payment_spec.rb
@@ -4,12 +4,7 @@ class TextFormatter
end
describe Spree::Payment do
- let(:payment) do
- create(:payment,
- source: build(:credit_card),
- order: build(:order),
- payment_method: Spree::Gateway::Bogus.create(active: true))
- end
+ let(:payment) { create(:payment) }
describe '#after_void' do
let(:payload) { {} }
diff --git a/api/spec/models/spree/shipment_spec.rb b/api/spec/models/spree/shipment_spec.rb
index 66d1f2fdaf..376464ec8b 100644
--- a/api/spec/models/spree/shipment_spec.rb
+++ b/api/spec/models/spree/shipment_spec.rb
@@ -4,16 +4,7 @@ class TextFormatter
end
describe Spree::Shipment do
- let(:order) do
- mock_model Spree::Order, backordered?: false,
- canceled?: false,
- can_ship?: true,
- currency: 'USD',
- number: 'S12345',
- paid?: false,
- touch_later: false
- end
- let(:shipment) { create(:shipment, stock_location: create(:stock_location)) }
+ let(:shipment) { create(:shipment) }
context '#after_ship' do
before do
@@ -24,10 +15,8 @@ describe Spree::Shipment do
end
end
- allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)
allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
allow(queue_requests).to receive(:call).with(any_args)
- allow(shipment).to receive(:determine_state).and_return('shipped')
end
let(:payload) { {} }
@@ -39,7 +28,8 @@ describe Spree::Shipment do
double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
)
)
- shipment.update!(order)
+ shipment.cancel # previous state that allows the object be shipped
+ shipment.ship
expect(queue_requests).to have_received(:call).with(event: 'shipment.ship', payload: payload).once
end
end
diff --git a/api/spec/models/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/spree/webhooks/has_webhooks_spec.rb
index 14221a6d6c..c498b9f28d 100644
--- a/api/spec/models/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/spree/webhooks/has_webhooks_spec.rb
@@ -10,10 +10,8 @@ end
describe Spree::Webhooks::HasWebhooks do
let(:connection) { ActiveRecord::Base.connection }
- let(:url) { 'https://google.com' }
before do
- stub_request(:post, url)
connection.create_table :test_products, force: true do |t|
t.string :name
end
@@ -56,11 +54,10 @@ describe Spree::Webhooks::HasWebhooks do
context 'when able to infer the serializer from the class' do
let!(:images) { create_list(:image, 2) }
let!(:product) do
- create(:product_in_stock,
- name: 'Test Product',
- price: 10.00,
- compare_at_price: 15.00,
- variants_including_master: [create(:variant, images: images), create(:variant)])
+ create(
+ :product_in_stock,
+ variants_including_master: [create(:variant, images: images), build(:variant)]
+ )
end
let(:payload) do
Spree::Api::V2::Platform::ProductSerializer.new(
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 671fb17002..db9b71d70a 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -10,11 +10,6 @@ describe Spree::Order, type: :model do
let(:user) { stub_model(Spree::LegacyUser, email: '[email protected]') }
let!(:store) { create(:store, default: true) }
let(:order) { stub_model(Spree::Order, user: user, store: store) }
- let(:url) { 'https://google.com' }
-
- before(:all) do
- stub_request(:any, 'https://google.com')
- end
before do
allow(Spree::LegacyUser).to receive_messages(current: mock_model(Spree::LegacyUser, id: 123))
@@ -70,16 +65,6 @@ describe Spree::Order, type: :model do
expect(order.payments.first).to be_void
end
-
- it 'executes a webhook callback' do
- order.cancel
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(72)
- )
- end
end
context '#canceled_by' do
@@ -191,16 +176,6 @@ describe Spree::Order, type: :model do
order.finalize!
end
- it 'executes a webhook callback' do
- order.finalize!
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(6)
- )
- end
-
context 'order is considered risky' do
before do
allow(order).to receive_messages is_risky?: true
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index 1f6b731292..a6b6cdbe7f 100644
--- a/core/spec/models/spree/shipment_spec.rb
+++ b/core/spec/models/spree/shipment_spec.rb
@@ -22,10 +22,6 @@ describe Spree::Shipment, type: :model do
let(:variant) { mock_model(Spree::Variant) }
let(:line_item) { mock_model(Spree::LineItem, variant: variant) }
- before(:all) do
- stub_request(:any, 'https://google.com')
- end
-
def create_shipment(order, stock_location)
order.shipments.create(stock_location_id: stock_location.id).inventory_units.create(
order_id: order.id,
@@ -373,18 +369,6 @@ describe Spree::Shipment, type: :model do
shipment.update!(order)
end
- it 'executes a webhook callback' do
- allow(shipment).to receive_messages determine_state: 'shipped'
- shipment.update!(order)
-
- expect(WebMock).to(
- have_requested(:post, 'https://google.com').with(
- body: {foo: :bar}.to_json,
- headers: {'Content-Type' => 'application/json'}
- ).times(13)
- )
- end
-
context 'when using the default shipment handler' do
it "calls the 'perform' method" do
shipment.state = 'pending'
diff --git a/core/spec/spec_helper.rb b/core/spec/spec_helper.rb
index 493c9787a8..0623d3f17e 100644
--- a/core/spec/spec_helper.rb
+++ b/core/spec/spec_helper.rb
@@ -34,7 +34,6 @@ end
require 'rspec/rails'
require 'database_cleaner'
require 'ffaker'
-require 'webmock/rspec'
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }