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.

Use webhooks in test suite unless DISABLE_SPREE_WEBHOOKS equals true.

Sebastian Palmaf6734b2cef7f
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 91aa3fd481..cb3dd1fb60 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -4,17 +4,16 @@ module Spree
extend ActiveSupport::Concern
included do
- after_create_commit proc { queue_webhooks_requests!(event_name(:create)) },
- unless: proc { webhooks_descendant? }
- after_destroy_commit proc { queue_webhooks_requests!(event_name(:destroy)) },
- unless: proc { webhooks_descendant? }
- after_update_commit proc { queue_webhooks_requests!(event_name(:update)) },
- unless: proc { webhooks_descendant? }
+ after_create_commit proc { queue_webhooks_requests!(event_name(:create)) }
+ after_destroy_commit proc { queue_webhooks_requests!(event_name(:destroy)) }
+ after_update_commit proc { queue_webhooks_requests!(event_name(:update)) }
def queue_webhooks_requests!(event)
- unless payload.blank?
- Spree::Webhooks::Endpoints::QueueRequests.call(event: event, payload: payload)
- end
+ return if disable_spree_webhooks?
+ return if webhooks_descendant?
+ return if payload.blank?
+
+ Spree::Webhooks::Endpoints::QueueRequests.call(event: event, payload: payload)
end
end
@@ -25,7 +24,11 @@ module Spree
end
def webhooks_descendant?
- self.class.ancestors.include?(Spree::Webhooks)
+ if Rails::VERSION::MAJOR < 6
+ self.class.parent == Spree::Webhooks
+ else
+ self.class.module_parent == Spree::Webhooks
+ end
end
def payload
@@ -38,6 +41,10 @@ module Spree
demodulized_class_name = self.class.to_s.demodulize
"Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".constantize
end
+
+ def disable_spree_webhooks?
+ ENV['DISABLE_SPREE_WEBHOOKS'] == 'true'
+ end
end
end
end
diff --git a/api/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rb
index f8b1e68b1b..77e5bb198b 100644
--- a/api/app/services/spree/webhooks/endpoints/queue_requests.rb
+++ b/api/app/services/spree/webhooks/endpoints/queue_requests.rb
@@ -5,13 +5,20 @@ module Spree
prepend Spree::ServiceModule::Base
def call(event:, payload:)
+ return failure(false) if ENV['DISABLE_SPREE_WEBHOOKS'] == 'true'
+
+ run :queue_requests
+ end
+
+ private
+
+ def queue_requests(event:, payload:)
urls_subscribed_to(event).each do |url|
Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(payload, url)
end
+ success(true)
end
- private
-
def urls_subscribed_to(event)
Spree::Webhooks::Endpoint.
where(enabled: true).
diff --git a/api/lib/spree/api/testing_support/jobs.rb b/api/lib/spree/api/testing_support/jobs.rb
new file mode 100644
index 0000000000..2964951978
--- /dev/null
+++ b/api/lib/spree/api/testing_support/jobs.rb
@@ -0,0 +1,18 @@
+RSpec.configure do |config|
+ # Force jobs to be executed in a synchronous way (see http://archive.today/xcb1E)
+ config.around do |example|
+ (ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
+ ActiveJob::Base.queue_adapter = :inline
+ example.run
+ (ActiveJob::Base.descendants << ActiveJob::Base).each { |a| a.enable_test_adapter(ActiveJob::QueueAdapters::TestAdapter.new) }
+ ActiveJob::Base.queue_adapter = :test
+ end
+
+ config.before(:each, :job) do
+ ActiveJob::Base.queue_adapter = :test
+ end
+
+ config.after(:each, :job) do
+ ActiveJob::Base.queue_adapter = :inline
+ end
+end
diff --git a/api/spec/controllers/spree/api/v1/products_controller_spec.rb b/api/spec/controllers/spree/api/v1/products_controller_spec.rb
index a810c346fd..a55bdc9271 100644
--- a/api/spec/controllers/spree/api/v1/products_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v1/products_controller_spec.rb
@@ -134,7 +134,6 @@ module Spree
it 'gets a single product' do
create_image(product.master, image('thinking-cat.jpg'))
create(:variant, product: product)
- product.reload
create_image(product.variants.first, image('thinking-cat.jpg'))
product.set_property('spree', 'rocks')
product.taxons << create(:taxon)
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index f54b7e82c5..79d51b3f73 100644
--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
@@ -1,10 +1,12 @@
require 'spec_helper'
-module Test
- class Product < ActiveRecord::Base
- self.table_name = 'test_products'
+module Spree
+ module Webhooks
+ class TestProduct < ActiveRecord::Base
+ self.table_name = 'test_products'
- include Spree::Webhooks::HasWebhooks
+ include Spree::Webhooks::HasWebhooks
+ end
end
end
@@ -19,105 +21,138 @@ describe Spree::Webhooks::HasWebhooks do
ActiveRecord::Base.connection.drop_table :test_products, if_exists: true
end
- context 'with a Spree::Webhooks class including the module' do
- let!(:product) { Test::Product.create(name: 'test') }
+ before do
+ allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
+ allow(queue_requests).to receive(:call).with(any_args)
+ end
+
+ let(:images) { create_list(:image, 2) }
+ let(:product) do
+ build(
+ :product_in_stock,
+ variants_including_master: [create(:variant, images: images), build(:variant)],
+ stores: [store]
+ )
+ end
+ let(:store) { create(:store) }
+ let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
+
+ context 'with DISABLE_SPREE_WEBHOOKS equals "true"' do
+ before do
+ ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'
+ product.save
+ end
+
+ after { ENV['DISABLE_SPREE_WEBHOOKS'] = nil }
- context 'after commit on create' do
- it 'does not execute the webhook callback' do
- expect(Spree::Webhooks::Endpoints::QueueRequests).not_to receive(:new)
+ context 'after_create_commit' do
+ it 'does not queue a request' do
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'product.create'))
end
end
- context 'after commit on destroy' do
- it 'does not execute the webhook callback' do
+ context 'after_destroy_commit' do
+ it 'does not queue a request' do
product.destroy
- expect(Spree::Webhooks::Endpoints::QueueRequests).not_to receive(:new)
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'product.destroy'))
end
end
- context 'after commit on update' do
- it 'does not execute the webhook callback' do
+ context 'after_update_commit' do
+ it 'does not queue a request' do
product.update(name: 'updated')
- expect(Spree::Webhooks::Endpoints::QueueRequests).not_to receive(:new)
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'product.update'))
end
end
end
- context 'with a non Spree::Webhooks class including the module' do
- before do
- allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
- allow(queue_requests).to receive(:call).with(any_args)
- end
+ context 'without DISABLE_SPREE_WEBHOOKS' do
+ before { ENV['DISABLE_SPREE_WEBHOOKS'] = nil; product.save }
- context 'when able to infer the serializer from the class' do
- let!(:images) { create_list(:image, 2) }
- let!(:product) do
- create(
- :product_in_stock,
- variants_including_master: [create(:variant, images: images), build(:variant)]
- )
- end
- let(:payload) do
- Spree::Api::V2::Platform::ProductSerializer.new(
- product,
- params: { store: Spree::Store.default }
- ).serializable_hash
- end
- let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
+ after { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }
- context 'when the class name contains more than one word' do
- let(:product_property) { create(:product_property) }
- let(:payload) do
- Spree::Api::V2::Platform::ProductPropertySerializer.new(product_property).serializable_hash
- end
+ context 'with a Spree::Webhooks descendant class' do
+ let(:product) { Spree::Webhooks::TestProduct.new(name: 'test') }
- it 'splits the words and joins them with an underscore to create the event_name' do
- expect(queue_requests).to have_received(:call).with(event: 'product_property.create', payload: payload).once
+ context 'after_create_commit' do
+ it 'does not queue a request' do
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.create'))
end
end
- context 'after commit on create' do
- it 'executes QueueRequests.call with a create event and the product serializer as the payload' do
- expect(queue_requests).to have_received(:call).with(event: 'product.create', payload: payload).once
- end
- end
-
- context 'after commit on destroy' do
- it 'executes QueueRequests.call with a destroy event and the product serializer as the payload' do
+ context 'after_destroy_commit' do
+ it 'does not queue a request' do
product.destroy
- expect(queue_requests).to have_received(:call).with(event: 'product.destroy', payload: payload).once
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.destroy'))
end
end
- context 'after commit on update' do
- it 'executes QueueRequests.call with an update event and the product serializer as the payload' do
+ context 'after_update_commit' do
+ it 'does not queue a request' do
product.update(name: 'updated')
- expect(queue_requests).to have_received(:call).with(event: 'product.update', payload: payload).once
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.update'))
end
end
end
- context 'when unable to infer the serializer from the class' do
- let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
- let!(:shipment) { create(:shipment) }
+ context 'without a Spree::Webhooks descendant class' do
+ context 'with a resource serializer' do
+ let(:payload) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
- context 'after commit on create' do
- it 'does not queue HTTP requests' do
- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.create'))
+ context 'after_create_commit' do
+ it 'queues a request through QueueRequests for product.create' do
+ expect(queue_requests).to have_received(:call).with(event: 'product.create', payload: payload).once
+ end
end
- end
- context 'after commit on destroy' do
- it 'does not queue HTTP requests' do
- shipment.destroy
- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.destroy'))
+ context 'after_destroy_commit' do
+ it 'queues a request through QueueRequests for product.destroy' do
+ product.destroy
+ expect(queue_requests).to have_received(:call).with(event: 'product.destroy', payload: payload).once
+ end
+ end
+
+ context 'after_update_commit' do
+ it 'queues a request through QueueRequests for product.update' do
+ product.update(name: 'updated')
+ expect(queue_requests).to have_received(:call).with(event: 'product.update', payload: payload).once
+ end
+ end
+
+ context 'with a class name with multiple words' do
+ let!(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }
+ let(:payload) do
+ Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash
+ end
+ let(:underscore_event) { 'cms_page.create' }
+
+ it 'underscorize the event name' do
+ expect(queue_requests).to have_received(:call).with(event: underscore_event, payload: payload).once
+ end
end
end
- context 'after commit on update' do
- it 'does not queue HTTP requests' do
- shipment.update(tracking: 'U10001')
- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.update'))
+ context 'without a resource serializer' do
+ let!(:shipment) { create(:shipment) }
+
+ context 'after_create_commit' do
+ it 'does not queue a request' do
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.create'))
+ end
+ end
+
+ context 'after_destroy_commit' do
+ it 'does not queue a request' do
+ shipment.destroy
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.destroy'))
+ end
+ end
+
+ context 'after_update_commit' do
+ it 'does not queue a request' do
+ shipment.update(tracking: 'U10001')
+ expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'shipment.update'))
+ end
end
end
end
diff --git a/api/spec/requests/spree/api/v2/resource_includes_spec.rb b/api/spec/requests/spree/api/v2/resource_includes_spec.rb
index c0ff48ba7c..1ac07538ff 100644
--- a/api/spec/requests/spree/api/v2/resource_includes_spec.rb
+++ b/api/spec/requests/spree/api/v2/resource_includes_spec.rb
@@ -7,12 +7,7 @@ describe 'API v2 JSON API Resource Includes Spec', type: :request do
let!(:product_option_type) { create(:product_option_type, product: product) }
let!(:option_value) { create(:option_value, option_type: product_option_type.option_type) }
let(:primary_variant) { product.master }
- let(:default_variant) { create(:variant, product: product) }
-
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- default_variant
- end
+ let!(:default_variant) { create(:variant, product: product) }
shared_examples 'requested resources' do
it 'are returned' do
diff --git a/api/spec/requests/spree/api/v2/storefront/account_spec.rb b/api/spec/requests/spree/api/v2/storefront/account_spec.rb
index 11aae2aab9..620ab0d3c3 100644
--- a/api/spec/requests/spree/api/v2/storefront/account_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/account_spec.rb
@@ -3,14 +3,9 @@ require 'spec_helper'
describe 'Storefront API v2 Account spec', type: :request do
include_context 'API v2 tokens'
- let(:user) { create(:user_with_addresses) }
+ let!(:user) { create(:user_with_addresses) }
let(:headers) { headers_bearer }
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- user
- end
-
shared_examples 'mock tests for failed user saving' do
it { expect(service).to receive(:call).with(permitted_params).and_return(result) }
it { expect(result).to receive(:success?).and_return(false) }
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index e96c85325a..0737c0d07c 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -23,10 +23,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
let!(:product_with_property) { create(:product, properties: [property], stores: [store]) }
let!(:product_property) { create(:product_property, property: new_property, product: product_with_property, value: 'Some Value') }
- before do
- Spree::Api::Config[:api_v2_per_page_limit] = 4
- Rails.cache.clear
- end
+ before { Spree::Api::Config[:api_v2_per_page_limit] = 4 }
describe 'products#index' do
context 'with no params' do
diff --git a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
index e9d5b5e288..8761f122cc 100644
--- a/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/store_serializer_spec.rb
@@ -6,8 +6,6 @@ describe Spree::Api::V2::Platform::StoreSerializer do
let!(:store) { Spree::Store.default }
let!(:menus) { [create(:menu, store: store), create(:menu, location: 'Footer', store: store)] }
- before { store.reload }
-
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
it do
diff --git a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
index 13e6c7eedc..ab492f30dd 100644
--- a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
@@ -5,11 +5,6 @@ describe Spree::Api::V2::Platform::UserSerializer do
subject { described_class.new(user, params: serializer_params) }
- before do
- allow_any_instance_of(Spree::Order).to receive(:queue_webhooks_requests!)
- user
- end
-
let(:user) { create(:user_with_addresses) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/api/spec/serializers/spree/api/v2/platform/wishlist_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/wishlist_serializer_spec.rb
index cd75f10d3d..80ff048487 100644
--- a/api/spec/serializers/spree/api/v2/platform/wishlist_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/wishlist_serializer_spec.rb
@@ -3,14 +3,9 @@ require 'spec_helper'
describe Spree::Api::V2::Platform::WishlistSerializer do
subject { described_class.new(wishlist) }
- let(:user) { create(:user) }
- let(:wishlist) { create(:wishlist, user: user) }
- let(:wished_item) { create(:wished_item, wishlist: wishlist) }
-
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- wished_item
- end
+ let!(:user) { create(:user) }
+ let!(:wishlist) { create(:wishlist, user: user) }
+ let!(:wished_item) { create(:wished_item, wishlist: wishlist) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb
index ff97529f34..7e6d6aa491 100644
--- a/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/cms_section_serializer_spec.rb
@@ -5,11 +5,6 @@ describe Spree::V2::Storefront::CmsSectionSerializer do
let(:cms_section) { create(:cms_section) }
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- cms_section
- end
-
shared_examples 'returns proper hash' do
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/api/spec/serializers/spree/v2/storefront/menu_item_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/menu_item_serializer_spec.rb
index 93379b3772..bf7b8087ba 100644
--- a/api/spec/serializers/spree/v2/storefront/menu_item_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/menu_item_serializer_spec.rb
@@ -5,12 +5,7 @@ describe Spree::V2::Storefront::MenuItemSerializer do
let(:menu) { create(:menu) }
let(:menu_item) { create(:menu_item, menu: menu, linked_resource: create(:taxon)) }
- let(:children) { create(:menu_item, parent_id: menu_item.id, menu: menu) }
-
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- children
- end
+ let!(:children) { create(:menu_item, parent_id: menu_item.id, menu: menu) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb
index f22ad9983b..763d8b561e 100644
--- a/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb
@@ -5,11 +5,6 @@ describe Spree::V2::Storefront::MenuSerializer do
let(:menu) { create(:menu) }
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- menu
- end
-
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
it do
diff --git a/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb
index 069e9cc08c..cad305003d 100644
--- a/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/wishlist_serializer_spec.rb
@@ -3,13 +3,8 @@ require 'spec_helper'
describe Spree::V2::Storefront::WishlistSerializer do
subject { described_class.new(wishlist) }
- let(:wishlist) { create(:wishlist) }
- let(:wished_item) { create(:wished_item, wishlist: wishlist, variant: create(:variant), quantity: 1) }
-
- before do
- allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
- wished_item
- end
+ let!(:wishlist) { create(:wishlist) }
+ let!(:wished_item) { create(:wished_item, wishlist: wishlist, variant: create(:variant), quantity: 1) }
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb
index 73054255a6..e677307048 100644
--- a/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb
+++ b/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb
@@ -2,75 +2,125 @@ require 'spec_helper'
describe Spree::Webhooks::Endpoints::QueueRequests, :job do
describe '#call' do
+ subject { described_class.call(event: event, payload: payload) }
+
let(:event) { 'order.finalize' }
let(:payload) { {} }
- context 'without subscriptions for the given event' do
- it 'does not make any HTTP request' do
- expect { subject.call(event: event, payload: payload) }.not_to(
- have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequest).on_queue('spree_webhooks')
- )
+ context 'with DISABLE_SPREE_WEBHOOKS equals "true"' do
+ before do
+ ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'
+ Spree::Webhooks::Endpoint.create(url: url, subscriptions: ['*'], enabled: true)
end
- end
- context 'with subscriptions for the given event' do
- context 'when endpoint subscriptions includes all events (*)' do
- before { stub_request(:post, endpoint.url) }
+ after { ENV.delete('DISABLE_SPREE_WEBHOOKS') }
- let(:endpoint) do
- Spree::Webhooks::Endpoint.create(url: 'https://url1.com/', subscriptions: ['*'], enabled: true)
- end
+ let(:url) { 'https://url1.com/' }
- it 'makes a HTTP request to its URL' do
- expect { subject.call(event: event, payload: payload) }.to(
- have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)
- .with(payload, endpoint.url)
- .on_queue('spree_webhooks')
- )
- end
+ it 'returns early without querying for the subscribed events' do
+ instance = Spree::Webhooks::Endpoints::QueueRequests.new
+ expect(instance).to receive(:call).and_call_original
+ expect(instance).not_to receive(:urls_subscribed_to)
+ instance.call(event: event, payload: payload)
end
- context 'when endpoint subscriptions includes the specific event being used' do
- before { stub_request(:post, endpoint.url) }
+ it 'does not succeed' do
+ expect(subject.success).to eq(false)
+ end
- let(:endpoint) do
- Spree::Webhooks::Endpoint.create(url: 'https://url2.com/', subscriptions: [event], enabled: true)
- end
+ it 'returns a false value' do
+ expect(subject.value).to eq(false)
+ end
+ end
+
+ context 'without DISABLE_SPREE_WEBHOOKS' do
+ before { ENV['DISABLE_SPREE_WEBHOOKS'] = nil }
- it 'makes a HTTP request to its URL' do
- expect { subject.call(event: event, payload: payload) }.to(
- have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)
- .with(payload, endpoint.url)
- .on_queue('spree_webhooks')
+ after { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }
+
+ context 'without subscriptions for the given event' do
+ it 'does not queue a job to make a request' do
+ expect { subject }.not_to(
+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequest).on_queue('spree_webhooks')
)
end
end
- context 'when endpoint subscriptions are not enabled' do
- let(:endpoint) do
- Spree::Webhooks::Endpoint.create(url: 'https://url3.com/', subscriptions: [event], enabled: false)
+ context 'with subscriptions for the given event' do
+ context 'when endpoint subscriptions includes all events (*)' do
+ before { stub_request(:post, endpoint.url) }
+
+ let(:endpoint) do
+ Spree::Webhooks::Endpoint.create(
+ url: 'https://url1.com/',
+ subscriptions: ['*'],
+ enabled: true
+ )
+ end
+
+ it 'queues a job to make a request' do
+ expect { subject }.to(
+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob).
+ with(payload, endpoint.url).
+ on_queue('spree_webhooks')
+ )
+ end
end
- it 'does not make a HTTP request' do
- expect { subject.call(event: event, payload: payload) }.not_to(
- have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)
- .with(payload, endpoint.url)
- .on_queue('spree_webhooks')
- )
+ context 'when endpoint subscriptions includes the specific event being used' do
+ before { stub_request(:post, endpoint.url) }
+
+ let(:endpoint) do
+ Spree::Webhooks::Endpoint.create(
+ url: 'https://url2.com/',
+ subscriptions: [event],
+ enabled: true
+ )
+ end
+
+ it 'queues a job to make a request' do
+ expect { subject }.to(
+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob).
+ with(payload, endpoint.url).
+ on_queue('spree_webhooks')
+ )
+ end
end
- end
- context 'when endpoint subscriptions do not include the event or "*"' do
- let(:endpoint) do
- Spree::Webhooks::Endpoint.create(url: 'https://url4.com/', subscriptions: ['order.resume'], enabled: true)
+ context 'when endpoint subscriptions are not enabled' do
+ let(:endpoint) do
+ Spree::Webhooks::Endpoint.create(
+ url: 'https://url3.com/',
+ subscriptions: [event],
+ enabled: false
+ )
+ end
+
+ it 'does not queue a job to make a request' do
+ expect { subject }.not_to(
+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob).
+ with(payload, endpoint.url).
+ on_queue('spree_webhooks')
+ )
+ end
end
- it 'does not make a HTTP request' do
- expect { subject.call(event: event, payload: payload) }.not_to(
- have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)
- .with(endpoint.url)
- .on_queue('spree_webhooks')
- )
+ context 'when endpoint subscriptions do not include the event or "*"' do
+ let(:endpoint) do
+ Spree::Webhooks::Endpoint.create(
+ url: 'https://url4.com/',
+ subscriptions: ['order.resume'],
+ enabled: true
+ )
+ end
+
+ it 'does not queue a job to make a request' do
+ expect { subject }.not_to(
+ have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob).
+ with(endpoint.url).
+ on_queue('spree_webhooks')
+ )
+ end
end
end
end
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index 3ccfff7f29..f42793b3d9 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -40,6 +40,7 @@ require 'spree/testing_support/next_instance_of'
require 'spree/testing_support/rspec_retry_config'
require 'spree/api/testing_support/caching'
+require 'spree/api/testing_support/jobs'
require 'spree/api/testing_support/helpers'
require 'spree/api/testing_support/setup'
require 'spree/api/testing_support/v2/base'
@@ -65,6 +66,8 @@ RSpec.configure do |config|
config.include Spree::TestingSupport::ImageHelpers
config.before do
+ ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'
+
reset_spree_preferences
Spree::Api::Config[:requires_authentication] = true
@@ -75,23 +78,6 @@ RSpec.configure do |config|
create(:store, default: true)
end
- # Force jobs to be executed in a synchronous way (see http://archive.today/xcb1E)
- config.around do |example|
- (ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
- ActiveJob::Base.queue_adapter = :inline
- example.run
- (ActiveJob::Base.descendants << ActiveJob::Base).each { |a| a.enable_test_adapter(ActiveJob::QueueAdapters::TestAdapter.new) }
- ActiveJob::Base.queue_adapter = :test
- end
-
- config.before(:each, :job) do
- ActiveJob::Base.queue_adapter = :test
- end
-
- config.after(:each, :job) do
- ActiveJob::Base.queue_adapter = :inline
- end
-
config.order = :random
Kernel.srand config.seed
end
diff --git a/core/lib/spree/testing_support/jobs.rb b/core/lib/spree/testing_support/jobs.rb
new file mode 100644
index 0000000000..311dddab6d
--- /dev/null
+++ b/core/lib/spree/testing_support/jobs.rb
@@ -0,0 +1,9 @@
+RSpec.configure do |config|
+ config.before(:each, :job) do
+ ActiveJob::Base.queue_adapter = :test
+ end
+
+ config.after(:each, :job) do
+ ActiveJob::Base.queue_adapter = :inline
+ end
+end
diff --git a/core/spec/spec_helper.rb b/core/spec/spec_helper.rb
index 6172e5a809..bc938fa1fe 100644
--- a/core/spec/spec_helper.rb
+++ b/core/spec/spec_helper.rb
@@ -40,6 +40,7 @@ Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
require 'spree/testing_support/i18n' if ENV['CHECK_TRANSLATIONS']
require 'spree/testing_support/factories'
+require 'spree/testing_support/jobs'
require 'spree/testing_support/preferences'
require 'spree/testing_support/url_helpers'
require 'spree/testing_support/kernel'