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.

Extracted Legacy Webhooks (#13485)

Damian Legawiece8116272add8
diff --git a/.circleci/config.yml b/.circleci/config.yml
index bdc38de163..1bdfa670ff 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -132,7 +132,7 @@ jobs:
<<: *test
environment:
<<: *environment
- PROJECTS: emails,sample,legacy_webhooks
+ PROJECTS: emails,sample
# mysql tests
test_core_mysql:
@@ -169,7 +169,7 @@ jobs:
<<: *test_mysql
environment:
<<: *mysql_environment
- PROJECTS: emails,sample,legacy_webhooks
+ PROJECTS: emails,sample
# brakeman
brakeman:
diff --git a/Rakefile b/Rakefile
index 0c1f06ee3c..15e77c902d 100644
--- a/Rakefile
+++ b/Rakefile
@@ -7,8 +7,8 @@ rescue LoadError
raise "Could not find spree/testing_support/common_rake. You need to run this command using Bundler."
end
-SPREE_GEMS = %w(core api cli emails sample admin storefront legacy_webhooks page_builder).freeze
-PACKAGES = %w(legacy_webhooks page_builder).freeze
+SPREE_GEMS = %w(core api cli emails sample admin storefront page_builder).freeze
+PACKAGES = %w(page_builder).freeze
task default: :test
diff --git a/bin/build-ci.rb b/bin/build-ci.rb
index aa62529e16..66fb431054 100755
--- a/bin/build-ci.rb
+++ b/bin/build-ci.rb
@@ -21,9 +21,9 @@ class Project
@name = name
end
- ALL = %w[emails api core sample admin storefront legacy_webhooks page_builder].freeze
+ ALL = %w[emails api core sample admin storefront page_builder].freeze
CORE_GEMS = %w[api core].freeze
- PACKAGES = %w[legacy_webhooks page_builder].freeze
+ PACKAGES = %w[page_builder].freeze
# Install subproject
#
diff --git a/packages/legacy_webhooks/Gemfile b/packages/legacy_webhooks/Gemfile
deleted file mode 100644
index 230f3baea0..0000000000
--- a/packages/legacy_webhooks/Gemfile
+++ /dev/null
@@ -1,9 +0,0 @@
-eval_gemfile('../../common_spree_dependencies.rb')
-
-gem 'spree_core', path: '../../core'
-gem 'spree_api', path: '../../api'
-gem 'spree_admin', path: '../../admin'
-
-gem 'sprockets-rails', '>= 3.5.2'
-
-gemspec
diff --git a/packages/legacy_webhooks/Rakefile b/packages/legacy_webhooks/Rakefile
deleted file mode 100644
index 217996c990..0000000000
--- a/packages/legacy_webhooks/Rakefile
+++ /dev/null
@@ -1,17 +0,0 @@
-require 'rubygems'
-require 'rake'
-require 'rake/testtask'
-require 'rspec/core/rake_task'
-require 'spree/testing_support/common_rake'
-
-RSpec::Core::RakeTask.new
-
-task default: :spec
-
-desc "Generates a dummy app for testing"
-task :test_app do
- ENV['LIB_NAME'] = 'spree/legacy_webhooks'
- Rake::Task['common:test_app'].execute(
- css: true
- )
-end
diff --git a/packages/legacy_webhooks/app/controllers/spree/admin/webhooks_subscribers_controller.rb b/packages/legacy_webhooks/app/controllers/spree/admin/webhooks_subscribers_controller.rb
deleted file mode 100644
index d7ec7b46fa..0000000000
--- a/packages/legacy_webhooks/app/controllers/spree/admin/webhooks_subscribers_controller.rb
+++ /dev/null
@@ -1,61 +0,0 @@
-module Spree
- module Admin
- class WebhooksSubscribersController < ResourceController
- include Spree::Admin::SettingsConcern
-
- before_action :set_supported_events, except: [:index, :show]
-
- create.before :process_subscriptions
- update.before :process_subscriptions
-
- def show
- @webhooks_subscriber = Webhooks::Subscriber.find(params[:id])
- events = @webhooks_subscriber.events.order(created_at: :desc)
- limit = params[:per_page] || Spree::Admin::RuntimeConfig.admin_records_per_page
- @pagy, @events = pagy(:offset, events, limit: limit)
- end
-
- private
-
- def resource
- @resource ||= Spree::Admin::Resource.new 'spree/admin/webhooks/subscribers', 'subscribers', nil
- end
-
- def process_subscriptions
- return if params[:webhooks_subscriber].blank?
-
- params[:webhooks_subscriber][:subscriptions] = if params[:subscribe_to_all_events] == 'true'
- ['*']
- else
- selected_events
- end
-
- params[:webhooks_subscriber] = params[:webhooks_subscriber].except(*supported_events.keys)
- end
-
- def selected_events
- @supported_events.select { |resource, _events| params[:webhooks_subscriber][resource] == 'true' }.values.flatten
- end
-
- def set_supported_events
- @supported_events ||= Spree::Webhooks::Subscriber.supported_events
- end
-
- def supported_events
- @supported_events
- end
-
- def permitted_resource_params
- params.require(:webhooks_subscriber).permit(:url, :active, subscriptions: [])
- end
-
- def location_after_create
- location_after_save
- end
-
- def location_after_save
- spree.admin_webhooks_subscriber_path(@object)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/controllers/spree/api/v2/platform/webhooks/events_controller.rb b/packages/legacy_webhooks/app/controllers/spree/api/v2/platform/webhooks/events_controller.rb
deleted file mode 100644
index 061314dcca..0000000000
--- a/packages/legacy_webhooks/app/controllers/spree/api/v2/platform/webhooks/events_controller.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Spree
- module Api
- module V2
- module Platform
- module Webhooks
- class EventsController < ResourceController
- private
-
- def model_class
- Spree::Webhooks::Event
- end
-
- def scope_includes
- %i[subscriber]
- end
-
- def resource_serializer
- Spree.api.platform_webhooks_event_serializer
- end
- end
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/controllers/spree/api/v2/platform/webhooks/subscribers_controller.rb b/packages/legacy_webhooks/app/controllers/spree/api/v2/platform/webhooks/subscribers_controller.rb
deleted file mode 100644
index 855a53482b..0000000000
--- a/packages/legacy_webhooks/app/controllers/spree/api/v2/platform/webhooks/subscribers_controller.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Spree
- module Api
- module V2
- module Platform
- module Webhooks
- class SubscribersController < ResourceController
- private
-
- def model_class
- Spree::Webhooks::Subscriber
- end
-
- def spree_permitted_attributes
- super + [{ subscriptions: [] }]
- end
-
- def resource_serializer
- Spree.api.platform_webhooks_subscriber_serializer
- end
- end
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/helpers/spree/admin/webhooks_subscribers_helper.rb b/packages/legacy_webhooks/app/helpers/spree/admin/webhooks_subscribers_helper.rb
deleted file mode 100644
index 7c58c9c273..0000000000
--- a/packages/legacy_webhooks/app/helpers/spree/admin/webhooks_subscribers_helper.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-module Spree
- module Admin
- module WebhooksSubscribersHelper
- def event_checkbox_for(resource_name, form)
- content_tag :div, class: 'col-span-12 sm:col-span-6 md:col-span-4 form-group' do
- content_tag :div, class: 'custom-control custom-switch' do
- (form.check_box resource_name, event_checkbox_opts(resource_name), true, nil) + ' ' +
- form.label(resource_name, Spree.t("#{resource_name.to_s.pluralize}"), class: 'custom-control-label')
- end
- end
- end
-
- def subscribe_to_all_events?
- @webhooks_subscriber.new_record? || @webhooks_subscriber.subscriptions == ['*']
- end
-
- private
-
- def event_checkbox_opts(resource_name)
- {
- class: 'events-checkbox custom-control-input',
- checked: subscribed_to_resource?(resource_name)
- }
- end
-
- def subscribed_to_resource?(resource_name)
- @webhooks_subscriber.subscriptions&.any? { |event| event.include? "#{resource_name}." }
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/jobs/spree/products/queue_status_changed_webhook_job.rb b/packages/legacy_webhooks/app/jobs/spree/products/queue_status_changed_webhook_job.rb
deleted file mode 100644
index 5bc0484eeb..0000000000
--- a/packages/legacy_webhooks/app/jobs/spree/products/queue_status_changed_webhook_job.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-module Spree
- module Products
- class QueueStatusChangedWebhookJob < Spree::BaseJob
- queue_as Spree.queues.webhooks
-
- def perform(product_id, event)
- product = Spree::Product.find(product_id)
- product.queue_webhooks_requests!("product.#{event}")
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/jobs/spree/webhooks/subscribers/make_request_job.rb b/packages/legacy_webhooks/app/jobs/spree/webhooks/subscribers/make_request_job.rb
deleted file mode 100644
index cfa8a15416..0000000000
--- a/packages/legacy_webhooks/app/jobs/spree/webhooks/subscribers/make_request_job.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-module Spree
- module Webhooks
- module Subscribers
- class MakeRequestJob < Spree::BaseJob
- queue_as :spree_webhooks
-
- def perform(webhook_payload_body, event_name, subscriber)
- Spree::Webhooks::Subscribers::HandleRequest.new(
- event_name: event_name,
- subscriber: subscriber,
- webhook_payload_body: webhook_payload_body
- ).call
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/concerns/spree/webhooks/has_webhooks.rb b/packages/legacy_webhooks/app/models/concerns/spree/webhooks/has_webhooks.rb
deleted file mode 100644
index c8c051742a..0000000000
--- a/packages/legacy_webhooks/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ /dev/null
@@ -1,99 +0,0 @@
-module Spree
- module Webhooks
- module HasWebhooks
- extend ActiveSupport::Concern
-
- included do
- after_create_commit(proc { queue_webhooks_requests!(inferred_event_name(:create)) })
- after_destroy_commit(proc { queue_webhooks_requests!(inferred_event_name(:delete)) })
- after_update_commit(proc { queue_webhooks_requests!(inferred_event_name(:update)) })
-
- def queue_webhooks_requests!(event_name)
- return if disable_spree_webhooks?
- return if no_webhooks_subscribers?(event_name)
- return if update_event?(event_name) && updating_only_ignored_attributes?
- return if webhook_payload_body.blank?
-
- Spree::Webhooks::Subscribers::QueueRequests.call(
- record: self,
- event_name: event_name,
- webhook_payload_body: webhook_payload_body,
- **webhooks_request_options
- )
- end
-
- def self.default_webhook_events
- model_name = name.demodulize.tableize.singularize
- %W[#{model_name}.create #{model_name}.delete #{model_name}.update]
- end
-
- def self.supported_webhook_events
- events = default_webhook_events
- events += custom_webhook_events if respond_to?(:custom_webhook_events)
- events
- end
- end
-
- private
-
- def webhook_payload_body
- resource_serializer.new(self, include: included_relationships).serializable_hash.to_json
- end
-
- def inferred_event_name(operation)
- "#{self.class.name.demodulize.tableize.singularize}.#{operation}"
- end
-
- def included_relationships
- if self.class.respond_to?(:webhook_included_relationships)
- self.class.webhook_included_relationships
- elsif resource_serializer.relationships_to_serialize
- resource_serializer.relationships_to_serialize.keys
- else
- []
- end
- end
-
- def resource_serializer
- @resource_serializer ||=
- begin
- demodulized_class_name = self.class.to_s.demodulize
- "Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".constantize
- end
- end
-
- def updating_only_ignored_attributes?
- (saved_changes.keys - ignored_attributes).empty?
- end
-
- def ignored_attributes
- timestamps = %w[created_at updated_at deleted_at]
- return timestamps unless self.class.respond_to?(:ignored_attributes_for_update_webhook_event)
-
- timestamps + self.class.ignored_attributes_for_update_webhook_event
- end
-
- def update_event?(event_name)
- event_name.end_with?('.update')
- end
-
- def disable_spree_webhooks?
- Spree::LegacyWebhooks.disabled?
- end
-
- def no_webhooks_subscribers?(event_name)
- active_webhooks_subscribers.none? do |subscriber|
- subscriber.supports_event?(event_name)
- end
- end
-
- def active_webhooks_subscribers
- Spree::Webhooks::Subscriber.active
- end
-
- def webhooks_request_options
- {}
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/order/webhooks.rb b/packages/legacy_webhooks/app/models/spree/order/webhooks.rb
deleted file mode 100644
index 6093b5911c..0000000000
--- a/packages/legacy_webhooks/app/models/spree/order/webhooks.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-module Spree
- class Order < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- included do
- after_update_commit :queue_webhooks_requests_for_order_resumed!
- end
-
- class_methods do
- def custom_webhook_events
- %w[order.canceled order.placed order.resumed order.shipped]
- end
- end
-
- def send_order_canceled_webhook
- queue_webhooks_requests!('order.canceled')
- end
-
- def send_order_placed_webhook
- queue_webhooks_requests!('order.placed')
- end
-
- def send_order_resumed_webhook
- queue_webhooks_requests!('order.resumed')
- self.state_machine_resumed = false # to not fire the same webhook twice
- end
-
- def queue_webhooks_requests_for_order_resumed!
- return if state_machine_resumed?
- return unless state_previously_changed?
- return unless state_previous_change&.last == 'resumed'
-
- send_order_resumed_webhook
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/payment/webhooks.rb b/packages/legacy_webhooks/app/models/spree/payment/webhooks.rb
deleted file mode 100644
index 733988aad7..0000000000
--- a/packages/legacy_webhooks/app/models/spree/payment/webhooks.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-module Spree
- class Payment < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- class_methods do
- def custom_webhook_events
- %w[payment.paid payment.voided]
- end
- end
-
- def send_payment_voided_webhook
- queue_webhooks_requests!('payment.voided')
- end
-
- def send_payment_completed_webhook
- queue_webhooks_requests!('payment.paid')
- order.queue_webhooks_requests!('order.paid') if order.paid?
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/product/webhooks.rb b/packages/legacy_webhooks/app/models/spree/product/webhooks.rb
deleted file mode 100644
index 0ed07d80c8..0000000000
--- a/packages/legacy_webhooks/app/models/spree/product/webhooks.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-module Spree
- class Product < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- included do
- after_update_commit :queue_webhooks_requests_for_product_discontinued!
- end
-
- class_methods do
- def custom_webhook_events
- %w[product.back_in_stock product.backorderable product.discontinued
- product.out_of_stock product.activated product.archived product.drafted]
- end
-
- def ignored_attributes_for_update_webhook_event
- %w[status]
- end
- end
-
- def send_product_activated_webhook
- queue_webhooks_requests!('product.activated')
- end
-
- def send_product_archived_webhook
- queue_webhooks_requests!('product.archived')
- end
-
- def send_product_drafted_webhook
- queue_webhooks_requests!('product.drafted')
- end
-
- def queue_webhooks_requests_for_product_discontinued!
- return unless discontinue_on_previously_changed?
- return if (change = discontinue_on_previous_change).blank? || change.last.blank?
-
- queue_webhooks_requests!('product.discontinued')
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/shipment/webhooks.rb b/packages/legacy_webhooks/app/models/spree/shipment/webhooks.rb
deleted file mode 100644
index febd1bff83..0000000000
--- a/packages/legacy_webhooks/app/models/spree/shipment/webhooks.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-module Spree
- class Shipment < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- class_methods do
- def custom_webhook_events
- %w[shipment.shipped]
- end
- end
-
- def send_shipment_shipped_webhook
- queue_webhooks_requests!('shipment.shipped')
- order.queue_webhooks_requests!('order.shipped') if order.fully_shipped?
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/stock_item/webhooks.rb b/packages/legacy_webhooks/app/models/spree/stock_item/webhooks.rb
deleted file mode 100644
index 6bf8d3e166..0000000000
--- a/packages/legacy_webhooks/app/models/spree/stock_item/webhooks.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-module Spree
- class StockItem < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- included do
- around_save :queue_webhooks_requests_for_variant_backorderable!
- around_save :queue_webhooks_requests_for_product_backorderable!
- end
-
- def queue_webhooks_requests_for_variant_backorderable!
- was_out_of_stock = !variant.in_stock_or_backorderable?
- was_not_backorderable = !variant_backorderable?
- yield
- if was_out_of_stock && was_not_backorderable && variant_backorderable?
- reload
- variant.queue_webhooks_requests!('variant.backorderable')
- end
- end
-
- def queue_webhooks_requests_for_product_backorderable!
- product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?
- product_was_not_backorderable = !product_backorderable?
- yield
- if product_was_out_of_stock && product_was_not_backorderable && product_backorderable?
- variant.product.queue_webhooks_requests!('product.backorderable')
- end
- end
-
- def product_backorderable?
- Spree::StockItem.exists?(backorderable: true, variant_id: variant.product.variants.ids)
- end
-
- def variant_backorderable?
- variant.stock_items.exists?(backorderable: true)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/stock_movement/webhooks.rb b/packages/legacy_webhooks/app/models/spree/stock_movement/webhooks.rb
deleted file mode 100644
index bcc65c4b4d..0000000000
--- a/packages/legacy_webhooks/app/models/spree/stock_movement/webhooks.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-module Spree
- class StockMovement < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- included do
- around_save :queue_webhooks_requests_for_variant_out_of_stock!
- around_save :queue_webhooks_requests_for_variant_back_in_stock!
- around_save :queue_webhooks_requests_for_product_out_of_stock!
- around_save :queue_webhooks_requests_for_product_back_in_stock!
- end
-
- def queue_webhooks_requests_for_variant_out_of_stock!
- variant_in_stock_before_update = variant.in_stock_or_backorderable?
- yield
- if variant_in_stock_before_update && !variant.in_stock_or_backorderable?
- reload
- stock_item.variant.queue_webhooks_requests!('variant.out_of_stock')
- end
- end
-
- def queue_webhooks_requests_for_variant_back_in_stock!
- variant_was_out_of_stock = !variant.in_stock_or_backorderable?
- yield
- if variant_was_out_of_stock && variant.in_stock_or_backorderable?
- reload
- variant.queue_webhooks_requests!('variant.back_in_stock')
- end
- end
-
- def queue_webhooks_requests_for_product_back_in_stock!
- product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?
- yield
- if product_was_out_of_stock && product.any_variant_in_stock_or_backorderable?
- product.queue_webhooks_requests!('product.back_in_stock')
- end
- end
-
- def queue_webhooks_requests_for_product_out_of_stock!
- product_was_in_stock = product.any_variant_in_stock_or_backorderable?
- yield
- if product_was_in_stock && !product.any_variant_in_stock_or_backorderable?
- product.queue_webhooks_requests!('product.out_of_stock')
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/variant/webhooks.rb b/packages/legacy_webhooks/app/models/spree/variant/webhooks.rb
deleted file mode 100644
index 2cfadda400..0000000000
--- a/packages/legacy_webhooks/app/models/spree/variant/webhooks.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Spree
- class Variant < Spree.base_class
- module Webhooks
- extend ActiveSupport::Concern
- include Spree::Webhooks::HasWebhooks
-
- included do
- after_update_commit :queue_webhooks_requests_for_variant_discontinued!
- end
-
- class_methods do
- def custom_webhook_events
- %w[variant.back_in_stock variant.backorderable variant.discontinued variant.out_of_stock]
- end
- end
-
- def queue_webhooks_requests_for_variant_discontinued!
- return unless discontinue_on_previously_changed?
- return if (change = discontinue_on_previous_change).blank? || change.last.blank?
-
- queue_webhooks_requests!('variant.discontinued')
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/webhooks/base.rb b/packages/legacy_webhooks/app/models/spree/webhooks/base.rb
deleted file mode 100644
index 97527fc316..0000000000
--- a/packages/legacy_webhooks/app/models/spree/webhooks/base.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-module Spree
- module Webhooks
- def self.table_name_prefix
- 'spree_webhooks_'
- end
-
- class Base < Spree.base_class
- self.abstract_class = true
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/webhooks/event.rb b/packages/legacy_webhooks/app/models/spree/webhooks/event.rb
deleted file mode 100644
index 66350dd9db..0000000000
--- a/packages/legacy_webhooks/app/models/spree/webhooks/event.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-module Spree
- module Webhooks
- class Event < Spree::Webhooks::Base
- validates :name, :subscriber, presence: true
-
- belongs_to :subscriber, inverse_of: :events, optional: false
-
- self.whitelisted_ransackable_associations = %w[subscriber]
- self.whitelisted_ransackable_attributes = %w[name request_errors response_code success url]
-
- # Computes the base64-encoded HMAC SHA256 signature of the event for the given payload.
- #
- # @param payload [Hash, String] The payload for to the webhook subscriber.
- # @return [String]
- def signature_for(payload)
- EventSignature.new(self, payload).computed_signature
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/webhooks/event_signature.rb b/packages/legacy_webhooks/app/models/spree/webhooks/event_signature.rb
deleted file mode 100644
index 69d478842d..0000000000
--- a/packages/legacy_webhooks/app/models/spree/webhooks/event_signature.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-require 'base64'
-require 'openssl'
-
-module Spree
- module Webhooks
- class EventSignature
- def initialize(event, payload)
- @event = event
- @payload = payload
- end
-
- # Generates a base64-encoded HMAC SHA256 signature for the payload of the event.
- #
- # By using the stringified payload, the signature is made tamper-proof as any
- # alterations of the data during transit will lead to an incorrect signature
- # comparison by the client.
- #
- # @return [String] The computed signature
- def computed_signature
- @computed_signature ||=
- Base64.strict_encode64(
- OpenSSL::HMAC.digest('sha256', @event.subscriber.secret_key, payload)
- )
- end
-
- private
-
- def payload
- @payload.is_a?(String) ? @payload : @payload.to_json
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/models/spree/webhooks/subscriber.rb b/packages/legacy_webhooks/app/models/spree/webhooks/subscriber.rb
deleted file mode 100644
index 7e01dd20c9..0000000000
--- a/packages/legacy_webhooks/app/models/spree/webhooks/subscriber.rb
+++ /dev/null
@@ -1,88 +0,0 @@
-module Spree
- module Webhooks
- class Subscriber < Spree::Webhooks::Base
- if defined?(Spree::VendorConcern)
- include Spree::VendorConcern
- end
-
- if Rails::VERSION::STRING >= '7.1.0'
- has_secure_token :secret_key, on: :save
- else
- has_secure_token :secret_key
- end
-
- has_many :events, inverse_of: :subscriber
-
- validates :url, 'spree/url': true, presence: true
- validate :check_uri_path
-
- self.whitelisted_ransackable_attributes = %w[active subscriptions url]
- self.whitelisted_ransackable_associations = %w[event]
-
- scope :active, -> { where(active: true) }
- scope :inactive, -> { where(active: false) }
-
- before_save :parse_subscriptions
-
- def latest_event_at
- events.order(:created_at).last&.created_at
- end
-
- # Returns true if the subscriber supports the given event
- #
- # @param event [String] The event to check, e.g. 'product.create'
- # @return [Boolean]
- def supports_event?(event)
- subscriptions.include?(event) || subscriptions.include?('*')
- end
-
- def self.with_urls_for(event)
- where(
- case ActiveRecord::Base.connection.adapter_name
- when 'Mysql2'
- ["('*' MEMBER OF(subscriptions) OR ? MEMBER OF(subscriptions))", event]
- when 'PostgreSQL'
- ["subscriptions @> '[\"*\"]' OR subscriptions @> ?", [event].to_json]
- when 'SQLite'
- ["subscriptions LIKE '%\"*\"%' OR subscriptions LIKE ?", "%#{event}%"]
- end
- )
- end
-
- def self.supported_events
- @supported_events ||= begin
- Rails.application.eager_load! if Rails.env.development?
- Spree::Base.descendants.
- select { |model| model.included_modules.include? Spree::Webhooks::HasWebhooks }.
- sort_by { |model| model.name.demodulize.underscore }.
- to_h do |model|
- model_name = model.name.demodulize.underscore.to_sym
- [model_name, model.supported_webhook_events]
- end
- end
- end
-
- def name
- url
- end
-
- private
-
- def check_uri_path
- uri = begin
- URI.parse(url)
- rescue URI::InvalidURIError
- return false
- end
-
- errors.add(:url, 'the URL must have a path') if uri.blank? || uri.path.blank?
- end
-
- def parse_subscriptions
- return if subscriptions.blank? || subscriptions.is_a?(Array)
-
- self.subscriptions = JSON.parse(subscriptions)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/serializers/spree/api/v2/platform/webhooks/event_serializer.rb b/packages/legacy_webhooks/app/serializers/spree/api/v2/platform/webhooks/event_serializer.rb
deleted file mode 100644
index 2a6d19f373..0000000000
--- a/packages/legacy_webhooks/app/serializers/spree/api/v2/platform/webhooks/event_serializer.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-module Spree
- module Api
- module V2
- module Platform
- module Webhooks
- class EventSerializer < BaseSerializer
- include ResourceSerializerConcern
-
- belongs_to :subscriber
- end
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/serializers/spree/api/v2/platform/webhooks/subscriber_serializer.rb b/packages/legacy_webhooks/app/serializers/spree/api/v2/platform/webhooks/subscriber_serializer.rb
deleted file mode 100644
index 63b82f4ab6..0000000000
--- a/packages/legacy_webhooks/app/serializers/spree/api/v2/platform/webhooks/subscriber_serializer.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-module Spree
- module Api
- module V2
- module Platform
- module Webhooks
- class SubscriberSerializer < BaseSerializer
- include ResourceSerializerConcern
- end
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/services/spree/products/queue_status_changed_webhook.rb b/packages/legacy_webhooks/app/services/spree/products/queue_status_changed_webhook.rb
deleted file mode 100644
index dbdb176357..0000000000
--- a/packages/legacy_webhooks/app/services/spree/products/queue_status_changed_webhook.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-module Spree
- module Products
- class QueueStatusChangedWebhook
- def self.call(ids:, event:)
- return false if ids.blank? || event.blank?
-
- # for ActiveJob 7.1+
- if ActiveJob.respond_to?(:perform_all_later)
- jobs = ids.map { |id| Spree::Products::QueueStatusChangedWebhookJob.new(id, event) }
- ActiveJob.perform_all_later(jobs)
- else
- ids.each { |id| Spree::Products::QueueStatusChangedWebhookJob.perform_later(id, event) }
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/handle_request.rb b/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/handle_request.rb
deleted file mode 100644
index d4d849221a..0000000000
--- a/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/handle_request.rb
+++ /dev/null
@@ -1,77 +0,0 @@
-# frozen_string_literal: true
-
-module Spree
- module Webhooks
- module Subscribers
- class HandleRequest
- def initialize(event_name:, subscriber:, webhook_payload_body:)
- @event_name = event_name
- @subscriber = subscriber
- @webhook_payload_body = JSON.parse(webhook_payload_body)
- end
-
- def call
- Rails.logger.debug(msg("sending to '#{url}'"))
- Rails.logger.debug(msg("webhook_payload_body: #{body_with_event_metadata}"))
-
- if request.unprocessable_uri?
- return process(:warn, msg("can not make a request to '#{url}'"))
- end
- return process(:warn, msg("failed for '#{url}'")) if request.failed_request?
-
- process
- end
-
- private
-
- attr_reader :webhook_payload_body, :event_name, :subscriber
-
- delegate :execution_time, :failed_request?, :response_code, :success?, :unprocessable_uri?, to: :request
- delegate :id, :url, to: :subscriber
- delegate :created_at, :id, to: :event, prefix: true
-
- def process(log_level = nil, msg = nil)
- Rails.logger.public_send(log_level, msg) if msg.present? && log_level.present?
- make_request
- update_event(msg)
- nil
- end
-
- def request
- @request ||=
- Spree::Webhooks::Subscribers::MakeRequest.new(
- signature: event.signature_for(body_with_event_metadata),
- url: url,
- webhook_payload_body: body_with_event_metadata
- )
- end
- alias make_request request
-
- def body_with_event_metadata
- webhook_payload_body.
- merge(event_created_at: event_created_at, event_id: event_id, event_type: event.name).
- to_json
- end
-
- def event
- @event ||= Spree::Webhooks::Event.create!(
- name: event_name, subscriber_id: subscriber.id, url: url
- )
- end
-
- def update_event(msg = nil)
- event.update(
- execution_time: execution_time,
- request_errors: msg,
- response_code: response_code,
- success: success?
- )
- end
-
- def msg(msg)
- "[SPREE WEBHOOKS] '#{event_name}' #{msg}"
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/make_request.rb b/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/make_request.rb
deleted file mode 100644
index 9f0faaa4d6..0000000000
--- a/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/make_request.rb
+++ /dev/null
@@ -1,83 +0,0 @@
-# frozen_string_literal: true
-
-module Spree
- module Webhooks
- module Subscribers
- class MakeRequest
- def initialize(signature:, url:, webhook_payload_body:)
- @execution_time_in_milliseconds = 0
- @signature = signature
- @url = url
- @webhook_payload_body = webhook_payload_body
- @webhooks_timeout = ENV['SPREE_WEBHOOKS_TIMEOUT']
- end
-
- def execution_time
- request
- @execution_time_in_milliseconds
- end
-
- def failed_request?
- (200...300).exclude?(response_code)
- end
-
- def response_code
- request.code.to_i
- end
-
- def success?
- !unprocessable_uri? && !failed_request?
- end
-
- def unprocessable_uri?
- uri_path == '' && uri_host.nil? && uri_port.nil?
- end
-
- private
-
- attr_reader :execution_time_in_milliseconds, :url, :webhook_payload_body, :webhooks_timeout
-
- HEADERS = { 'Content-Type' => 'application/json' }.freeze
- private_constant :HEADERS
-
- delegate :host, :path, :port, to: :uri, prefix: true
-
- def http
- http = Net::HTTP.new(uri_host, uri_port)
- http.read_timeout = webhooks_timeout.to_i if custom_read_timeout?
- http.use_ssl = use_ssl?
- http
- end
-
- def request
- req = Net::HTTP::Post.new(uri_path, HEADERS.merge('X-Spree-Hmac-SHA256' => @signature))
- req.body = webhook_payload_body
- @request ||= begin
- start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
- request_result = http.request(req)
- @execution_time_in_milliseconds = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time).in_milliseconds
- request_result
- end
- rescue Errno::ECONNREFUSED, Net::ReadTimeout, SocketError
- Class.new do
- def self.code
- '0'
- end
- end
- end
-
- def custom_read_timeout?
- webhooks_timeout.present?
- end
-
- def use_ssl?
- uri.scheme == 'https'
- end
-
- def uri
- URI(url)
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/queue_requests.rb b/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/queue_requests.rb
deleted file mode 100644
index 8fd90ccc7f..0000000000
--- a/packages/legacy_webhooks/app/services/spree/webhooks/subscribers/queue_requests.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-module Spree
- module Webhooks
- module Subscribers
- class QueueRequests
- prepend Spree::ServiceModule::Base
-
- def call(event_name:, webhook_payload_body:, record: nil, **options)
- filtered_subscribers(event_name, webhook_payload_body, record, options).each do |subscriber|
- Spree::Webhooks::Subscribers::MakeRequestJob.perform_later(
- webhook_payload_body, event_name, subscriber
- )
- end
- end
-
- private
-
- def filtered_subscribers(event_name, webhook_payload_body, record, options)
- Spree::Webhooks::Subscriber.active.map do |subscriber|
- if subscriber.supports_event?(event_name)
- subscriber
- end
- end.compact
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhook_events/_webhook_event.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhook_events/_webhook_event.html.erb
deleted file mode 100644
index 3ebae9fc08..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhook_events/_webhook_event.html.erb
+++ /dev/null
@@ -1,17 +0,0 @@
-<tr id="<%= spree_dom_id webhook_event %>">
- <td><span class="badge badge-light"><%= webhook_event.name %></span></td>
- <td><%= active_badge(webhook_event.success) %></td>
- <td><%= spree_time(webhook_event.created_at) %></td>
- <td>
- <% if webhook_event.response_code%>
- <code><%= webhook_event.response_code %></code></td>
- <% end %>
- <td>
- <% if webhook_event.execution_time %>
- <code><%= webhook_event.execution_time %>ms</code>
- <% end %>
- </td>
- <td>
- <%= sanitize webhook_event.request_errors&.gsub('[SPREE WEBHOOKS]', '') if webhook_event.request_errors.present? && !webhook_event.success? %>
- </td>
-</tr>
\ No newline at end of file
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_form.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_form.html.erb
deleted file mode 100644
index 50d4712675..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_form.html.erb
+++ /dev/null
@@ -1,68 +0,0 @@
-<div class="card mb-6">
- <div class="card-header">
- <h5 class="card-title">
- <%= Spree.t(:general_settings) %>
- </h5>
- </div>
- <div class="card-body">
- <%= f.spree_text_field :url, placeholder: 'https://example.com/endpoint' %>
- <%= f.spree_check_box :active %>
- </div>
-</div>
-<div class="card mb-6">
- <div class="card-header">
- <h5 class="card-title">
- <%= Spree.t('admin.webhooks_subscribers.webhooks_events') %>
- </h5>
- </div>
- <div class="card-body" data-controller="reveal">
- <div class="form-group">
- <div>
- <div class="custom-control custom-radio mb-12">
- <%= radio_button_tag :subscribe_to_all_events, true,
- subscribe_to_all_events?,
- {
- 'data-action' => 'click->reveal#hide',
- class: 'custom-control-input events' } %>
- <%= label_tag :subscribe_to_all_events_true, Spree.t('admin.webhooks_subscribers.subscribe_to_all_events'), class: 'custom-control-label' %>
- </div>
- <div class="custom-control custom-radio">
- <%= radio_button_tag :subscribe_to_all_events, false,
- !subscribe_to_all_events?,
- { 'data-action' => 'click->reveal#show',
- class: 'custom-control-input events' } %>
- <%= label_tag :subscribe_to_all_events_false, Spree.t('admin.webhooks_subscribers.select_events'), class: 'custom-control-label' %>
-
- <div data-reveal-target="item" class="<%= subscribe_to_all_events? ? 'hidden' : '' %>">
- <div class="bg-gray-25 rounded-xl p-12 mt-12">
- <% @supported_events.each do |event| %>
- <div class="form-group">
- <div class="custom-control custom-switch">
- <% resource_name = event.first %>
- <%= f.check_box resource_name,
- { class: 'events-checkbox custom-control-input',
- checked: @webhooks_subscriber.subscriptions&.any? { |event| event.include? "#{resource_name}." } },
- true,
- nil %>
-
- <%= f.label resource_name, class: 'custom-control-label w-full' do %>
- <%= Spree.t("#{resource_name.to_s.pluralize}") %>
- <p class="gap-2 mb-0 mt-1">
- <% event.last.each do |event| %>
- <span class="badge badge-light">
- <%= event %>
- </span>
- <% end %>
- </p>
- <% end %>
- </div>
- </div>
- <% end %>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
-</div>
-
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_table_header.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_table_header.html.erb
deleted file mode 100644
index 93bcea76ea..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_table_header.html.erb
+++ /dev/null
@@ -1,7 +0,0 @@
-<tr>
- <th scope="col"><%= Spree.t(:url) %></th>
- <th scope="col"><%= Spree.t(:active) %></th>
- <th scope="col"><%= Spree.t('admin.webhooks_subscribers.subscriptions') %></th>
- <th scope="col"><%= Spree.t('admin.webhooks_subscribers.time_of_last_event') %></th>
- <th scope="col"></th>
-</tr>
\ No newline at end of file
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_table_row.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_table_row.html.erb
deleted file mode 100644
index 719dd44d7b..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/_table_row.html.erb
+++ /dev/null
@@ -1,22 +0,0 @@
-<tr id="<%= spree_dom_id webhooks_subscriber %>" data-controller="row-link" class="cursor-pointer">
- <td class="w-40 whitespace-normal" data-action="click->row-link#openLink">
- <%= webhooks_subscriber.url %>
- </td>
- <td class="w-5" data-action="click->row-link#openLink"><%= active_badge(webhooks_subscriber.active) %></td>
- <td class="w-30 whitespace-normal" data-action="click->row-link#openLink">
- <% webhooks_subscriber.subscriptions&.sort&.each do |subscription| %>
- <span class="badge badge-light"><%= subscription %></span>
- <% end %>
- </td>
- <td class="w-10" data-action="click->row-link#openLink">
- <% if webhooks_subscriber.latest_event_at %>
- <%= spree_time(webhooks_subscriber.latest_event_at) %>
- <% else %>
- <span class="text-gray-600"><%= Spree.t(:not_available) %></span>
- <% end %>
- </td>
- <td class="w-15">
- <%= link_to_with_icon 'eye', Spree.t(:view), spree.admin_webhooks_subscriber_path(webhooks_subscriber), class: "btn btn-light btn-sm", data: { row_link_target: 'link', turbo_frame: '_top' } %>
- <%= link_to_edit(webhooks_subscriber, data: { turbo_frame: '_top' }) if can? :edit, webhooks_subscriber %>
- </td>
-</tr>
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/edit.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/edit.html.erb
deleted file mode 100644
index f5b8f8cb5d..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/edit.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= render 'spree/admin/shared/edit_resource' %>
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/index.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/index.html.erb
deleted file mode 100644
index 6b1a824b52..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/index.html.erb
+++ /dev/null
@@ -1,10 +0,0 @@
-<%= render 'spree/admin/shared/developers_nav' %>
-
-<% content_for :page_actions do %>
- <%= render_admin_partials(:webhooks_subscribers_actions_partials) %>
- <%= link_to_with_icon 'plus', Spree.t('admin.webhooks_subscribers.new_webhooks_subscriber'), new_object_url, class: "btn btn-primary" if can?(:create, Spree::Webhooks::Subscriber) %>
-<% end %>
-
-<%= render_admin_partials(:webhooks_subscribers_header_partials) %>
-
-<%= render 'spree/admin/shared/index_table' %>
\ No newline at end of file
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/new.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/new.html.erb
deleted file mode 100644
index 65a817edc9..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/new.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= render 'spree/admin/shared/new_resource' %>
diff --git a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/show.html.erb b/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/show.html.erb
deleted file mode 100644
index 9e7033fa59..0000000000
--- a/packages/legacy_webhooks/app/views/spree/admin/webhooks_subscribers/show.html.erb
+++ /dev/null
@@ -1,65 +0,0 @@
-<% content_for :title do %>
- <%= @webhooks_subscriber.url %>
-<% end %>
-
-<% content_for :page_title do %>
- <%= page_header_back_button spree.admin_webhooks_subscribers_path %>
- <%= @webhooks_subscriber.url %>
-<% end %>
-
-<% content_for :page_actions do %>
- <%= link_to_with_icon 'edit', Spree.t(:edit), spree.edit_admin_webhooks_subscriber_path(@webhooks_subscriber), class: 'btn btn-light' %>
-<% end %>
-
-<ul class="list-group bg-white mb-6 rounded-xl">
- <li class="list-group-item flex justify-between">
- <strong class="uppercase"><%= Spree.t(:url) %></strong>
- <%= external_link_to @webhooks_subscriber.url, @webhooks_subscriber.url, class: 'text-blue-500 no-underline' %>
- </li>
- <li class="list-group-item flex justify-between">
- <strong><%= Spree.t(:active) %></strong>
- <%= active_badge(@webhooks_subscriber.active) %>
- </li>
- <li class="list-group-item flex justify-between">
- <strong><%= Spree.t('admin.webhooks_subscribers.subscriptions') %></strong>
- <div>
- <% @webhooks_subscriber.subscriptions&.sort&.each do |subscription| %>
- <span class="badge badge-light"><%= subscription %></span>
- <% end %>
- </div>
- </li>
-</ul>
-
-<div class="card mb-6">
- <div class="card-header">
- <h5 class="card-title">
- <%= Spree.t('admin.webhooks_subscribers.webhooks_events') %>
- </h5>
- </div>
- <table class="table card-body pb-0">
- <thead>
- <tr>
- <th scope="col"><%= Spree.t('admin.name') %></th>
- <th scope="col"><%= Spree.t('admin.successful') %></th>
- <th scope="col"><%= Spree.t('admin.created_at') %></th>
- <th scope="col"><%= Spree.t('admin.response_code') %></th>
- <th scope="col"><%= Spree.t('admin.execution_time') %></th>
- <th scope="col"><%= Spree.t('admin.errors') %></th>
- <th scope="col"></th>
- </tr>
- </thead>
- <tbody>
- <% if @events.any? %>
- <%= render partial: 'spree/admin/webhook_events/webhook_event', collection: @events, cached: spree_base_cache_scope %>
- <% else %>
- <tr>
- <td colspan="7" class="text-center">
- <%= render 'spree/admin/shared/no_resource_found', new_object_url: nil, model_class: Spree::Webhooks::Event %>
- </td>
- </tr>
- <% end %>
- </tbody>
- </table>
- <%= render 'spree/admin/shared/index_table_options', collection: @events %>
-</div>
-
diff --git a/packages/legacy_webhooks/config/initializers/spree_admin_navigation.rb b/packages/legacy_webhooks/config/initializers/spree_admin_navigation.rb
deleted file mode 100644
index 0cee8746f3..0000000000
--- a/packages/legacy_webhooks/config/initializers/spree_admin_navigation.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-Rails.application.config.after_initialize do
- developers_tabs_nav = Spree.admin.navigation.developers_tabs
-
- developers_tabs_nav.add :webhooks,
- label: "#{Spree.t(:webhooks)} (Legacy))",
- url: :admin_webhooks_subscribers_path,
- position: 20,
- active: -> { controller_name == 'webhooks_subscribers' },
- if: -> { can?(:manage, Spree::Webhooks::Subscriber) }
-end
diff --git a/packages/legacy_webhooks/config/routes.rb b/packages/legacy_webhooks/config/routes.rb
deleted file mode 100644
index 1b82af1cad..0000000000
--- a/packages/legacy_webhooks/config/routes.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-# frozen_string_literal: true
-
-Spree::Core::Engine.add_routes do
- namespace :api, defaults: { format: 'json' } do
- namespace :v2 do
- namespace :platform do
- # Legacy Webhooks API
- namespace :webhooks do
- resources :events, only: :index
- resources :subscribers
- end
- end
- end
- end
-
- namespace :admin do
- resources :webhooks_subscribers
- end
-end
diff --git a/packages/legacy_webhooks/db/migrate/20210902162826_create_spree_webhooks_tables.rb b/packages/legacy_webhooks/db/migrate/20210902162826_create_spree_webhooks_tables.rb
deleted file mode 100644
index a2048967f9..0000000000
--- a/packages/legacy_webhooks/db/migrate/20210902162826_create_spree_webhooks_tables.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class CreateSpreeWebhooksTables < ActiveRecord::Migration[5.2]
- def change
- create_table :spree_webhooks_subscribers do |t|
- t.string :url, null: false
- t.boolean :active, default: false, index: true
-
- if t.respond_to? :jsonb
- t.jsonb :subscriptions
- else
- t.json :subscriptions
- end
-
- t.timestamps
- end
- end
-end
diff --git a/packages/legacy_webhooks/db/migrate/20211025162826_create_spree_webhooks_events.rb b/packages/legacy_webhooks/db/migrate/20211025162826_create_spree_webhooks_events.rb
deleted file mode 100644
index e4703fd107..0000000000
--- a/packages/legacy_webhooks/db/migrate/20211025162826_create_spree_webhooks_events.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-class CreateSpreeWebhooksEvents < ActiveRecord::Migration[5.2]
- def change
- create_table :spree_webhooks_events do |t|
- t.integer "execution_time"
- t.string "name", null: false
- t.string "request_errors"
- t.string "response_code", index: true
- t.belongs_to "subscriber", null: false, index: true
- t.boolean "success", index: true
- t.string "url", null: false
- t.timestamps
- end
- end
-end
diff --git a/packages/legacy_webhooks/db/migrate/20221221122100_add_secret_key_to_spree_webhooks_subscribers.rb b/packages/legacy_webhooks/db/migrate/20221221122100_add_secret_key_to_spree_webhooks_subscribers.rb
deleted file mode 100644
index f7a4ab01b5..0000000000
--- a/packages/legacy_webhooks/db/migrate/20221221122100_add_secret_key_to_spree_webhooks_subscribers.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class AddSecretKeyToSpreeWebhooksSubscribers < ActiveRecord::Migration[6.1]
- def change
- add_column :spree_webhooks_subscribers, :secret_key, :string, null: true
- end
-end
diff --git a/packages/legacy_webhooks/db/migrate/20230116204600_backfill_secret_key_for_spree_webhooks_subscribers.rb b/packages/legacy_webhooks/db/migrate/20230116204600_backfill_secret_key_for_spree_webhooks_subscribers.rb
deleted file mode 100644
index 4438b94247..0000000000
--- a/packages/legacy_webhooks/db/migrate/20230116204600_backfill_secret_key_for_spree_webhooks_subscribers.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class BackfillSecretKeyForSpreeWebhooksSubscribers < ActiveRecord::Migration[6.1]
- def change
- Spree::Webhooks::Subscriber.where(secret_key: nil).find_each(&:regenerate_secret_key)
- end
-end
diff --git a/packages/legacy_webhooks/db/migrate/20230116205000_change_secret_key_to_non_null_column.rb b/packages/legacy_webhooks/db/migrate/20230116205000_change_secret_key_to_non_null_column.rb
deleted file mode 100644
index 3aef2cf72d..0000000000
--- a/packages/legacy_webhooks/db/migrate/20230116205000_change_secret_key_to_non_null_column.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class ChangeSecretKeyToNonNullColumn < ActiveRecord::Migration[6.1]
- def change
- change_column_null :spree_webhooks_subscribers, :secret_key, false
- end
-end
diff --git a/packages/legacy_webhooks/lib/generators/spree/legacy_webhooks/install/install_generator.rb b/packages/legacy_webhooks/lib/generators/spree/legacy_webhooks/install/install_generator.rb
deleted file mode 100644
index 2c16e93fdf..0000000000
--- a/packages/legacy_webhooks/lib/generators/spree/legacy_webhooks/install/install_generator.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-# frozen_string_literal: true
-
-module Spree
- module LegacyWebhooks
- module Generators
- class InstallGenerator < Rails::Generators::Base
- class_option :migrate, type: :boolean, default: true
-
- def add_migrations
- run 'bundle exec rake railties:install:migrations FROM=spree_legacy_webhooks'
- end
-
- def run_migrations
- run_migrations = options[:migrate] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
- if run_migrations
- run 'bin/rails db:migrate'
- else
- puts 'Skipping rails db:migrate, don\'t forget to run it!'
- end
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/lib/spree/legacy_webhooks.rb b/packages/legacy_webhooks/lib/spree/legacy_webhooks.rb
deleted file mode 100644
index bf1a4abe81..0000000000
--- a/packages/legacy_webhooks/lib/spree/legacy_webhooks.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-require 'spree/core'
-require 'spree/api'
-require 'spree/admin'
-
-module Spree
- module LegacyWebhooks
- def self.disable_webhooks
- prev_value = disabled?
- RequestStore.store[:disable_spree_legacy_webhooks] = true
- yield
- ensure
- RequestStore.store[:disable_spree_legacy_webhooks] = prev_value
- end
-
- def self.disabled?
- RequestStore.fetch(:disable_spree_legacy_webhooks) { false }
- end
-
- def self.disabled=(value)
- RequestStore.store[:disable_spree_legacy_webhooks] = value
- end
- end
-end
-
-require 'spree/legacy_webhooks/engine'
diff --git a/packages/legacy_webhooks/lib/spree/legacy_webhooks/engine.rb b/packages/legacy_webhooks/lib/spree/legacy_webhooks/engine.rb
deleted file mode 100644
index 7ff8333b64..0000000000
--- a/packages/legacy_webhooks/lib/spree/legacy_webhooks/engine.rb
+++ /dev/null
@@ -1,78 +0,0 @@
-# frozen_string_literal: true
-
-require 'rails/engine'
-
-module Spree
- module LegacyWebhooks
- class Engine < Rails::Engine
- isolate_namespace Spree
- engine_name 'spree_legacy_webhooks'
-
- config.autoload_paths << "#{config.root}/app/models/concerns"
-
- # Include HasWebhooks concern in configured models
- config.after_initialize do
- # Models that should have legacy webhooks
- webhook_models = [
- 'Spree::Address',
- 'Spree::Asset',
- 'Spree::CreditCard',
- 'Spree::CustomerReturn',
- 'Spree::InventoryUnit',
- 'Spree::LineItem',
- 'Spree::OptionType',
- 'Spree::OptionValue',
- 'Spree::Order',
- 'Spree::Payment',
- 'Spree::PaymentCaptureEvent',
- 'Spree::Price',
- 'Spree::Product',
- 'Spree::Promotion',
- 'Spree::Property',
- 'Spree::Prototype',
- 'Spree::Refund',
- 'Spree::Reimbursement',
- 'Spree::ReturnAuthorization',
- 'Spree::ReturnItem',
- 'Spree::Shipment',
- 'Spree::ShippingCategory',
- 'Spree::ShippingMethod',
- 'Spree::StockItem',
- 'Spree::StockLocation',
- 'Spree::StockMovement',
- 'Spree::StockTransfer',
- 'Spree::Store',
- 'Spree::StoreCredit',
- 'Spree::TaxCategory',
- 'Spree::Taxon',
- 'Spree::Taxonomy',
- 'Spree::TaxRate',
- 'Spree::Variant',
- 'Spree::WishedItem',
- 'Spree::Wishlist',
- 'Spree::Zone'
- ]
-
- webhook_models.each do |model_name|
- model = model_name.safe_constantize
- next unless model
-
- model.include(Spree::Webhooks::HasWebhooks) unless model.included_modules.include?(Spree::Webhooks::HasWebhooks)
- end
-
- # Include model-specific webhook concerns
- Spree::Order.include(Spree::Order::Webhooks) if defined?(Spree::Order) && !Spree::Order.included_modules.include?(Spree::Order::Webhooks)
- Spree::Payment.include(Spree::Payment::Webhooks) if defined?(Spree::Payment) && !Spree::Payment.included_modules.include?(Spree::Payment::Webhooks)
- Spree::Product.include(Spree::Product::Webhooks) if defined?(Spree::Product) && !Spree::Product.included_modules.include?(Spree::Product::Webhooks)
- Spree::Variant.include(Spree::Variant::Webhooks) if defined?(Spree::Variant) && !Spree::Variant.included_modules.include?(Spree::Variant::Webhooks)
- Spree::Shipment.include(Spree::Shipment::Webhooks) if defined?(Spree::Shipment) && !Spree::Shipment.included_modules.include?(Spree::Shipment::Webhooks)
- Spree::StockItem.include(Spree::StockItem::Webhooks) if defined?(Spree::StockItem) && !Spree::StockItem.included_modules.include?(Spree::StockItem::Webhooks)
- Spree::StockMovement.include(Spree::StockMovement::Webhooks) if defined?(Spree::StockMovement) && !Spree::StockMovement.included_modules.include?(Spree::StockMovement::Webhooks)
- end
-
- def self.root
- @root ||= Pathname.new(File.expand_path('../../..', __dir__))
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories.rb b/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories.rb
deleted file mode 100644
index a36a5d0b8f..0000000000
--- a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-Dir["#{File.dirname(__FILE__)}/factories/**"].each do |f|
- load File.expand_path(f)
-end
diff --git a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories/webhook_event_factory.rb b/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories/webhook_event_factory.rb
deleted file mode 100644
index 1288a394c1..0000000000
--- a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories/webhook_event_factory.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-FactoryBot.define do
- factory :webhook_event, aliases: [:event], class: Spree::Webhooks::Event do
- subscriber
-
- execution_time { rand(1..99_999) }
- name { 'order.canceled' }
- request_errors { '' }
- sequence(:url) { |n| "https://www.url#{n}.com/" }
-
- trait :failed do
- response_code { '500' }
- success { false }
- end
-
- trait :successful do
- response_code { '200' }
- success { true }
- end
-
- trait :blank do
- execution_time { nil }
- request_errors { nil }
- subscriber_id { nil }
- url { nil }
- end
- end
-end
diff --git a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories/webhook_subscriber_factory.rb b/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories/webhook_subscriber_factory.rb
deleted file mode 100644
index 29a2d62db5..0000000000
--- a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/factories/webhook_subscriber_factory.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-FactoryBot.define do
- factory :webhook_subscriber, aliases: [:subscriber, :webhooks_subscriber], class: Spree::Webhooks::Subscriber do
- sequence(:url) { |n| "https://www.url#{n}.com/" }
-
- trait :active do
- active { true }
- end
-
- trait :inactive do
- active { false }
- end
- end
-end
diff --git a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/matchers/webhooks.rb b/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/matchers/webhooks.rb
deleted file mode 100644
index 9a9d86e249..0000000000
--- a/packages/legacy_webhooks/lib/spree/legacy_webhooks/testing_support/matchers/webhooks.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-# Passes if executing the code in the block there is a
-# `Spree::Webhooks::Subscribers::QueueRequests.call` method
-# call with the given `event` and `webhook_payload_body` arguments just once.
-#
-# @example
-# expect { order.complete }.to emit_webhook_event('order.paid')
-# expect do
-# order.start_processing
-# order.complete
-# end.to emit_webhook_event('order.paid')
-#
-# It can also be negated, resulting in the expectation
-# waiting to not receive a `call` method call with the
-# given `event` and `webhook_payload_body` (`once` isn't taken into consideration).
-#
-# @example
-# expect { order.complete }.not_to emit_webhook_event('order.paid')
-# expect do
-# order.start_processing
-# order.complete
-# end.not_to emit_webhook_event('order.paid')
-#
-# == Notes
-#
-# The matcher relies on a `webhook_payload_body` method previously defined which
-# isn't added to the matcher definition, because it acts in a different
-# way depending on what's the resource being tested.
-#
-RSpec::Matchers.define :emit_webhook_event do |event_to_emit, record = nil|
- match do |block|
- queue_requests = instance_double(Spree::Webhooks::Subscribers::QueueRequests)
-
- allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)
- allow(queue_requests).to receive(:call).with(any_args)
-
- with_webhooks_enabled { Timecop.freeze { block.call } }
-
- # Check that the webhook was called with the correct event name and record
- # The payload comparison is done separately to allow for minor serialization differences
- expect(queue_requests).to(
- have_received(:call).with(
- hash_including(event_name: event_to_emit, record: record)
- ).once
- )
- end
-
- def block_definition(obj_method)
- # positive look-behinds must have a fixed length, using a straightforward match instead
- obj_method.source.squish[/(expect *({|do) *)(.*?)( *(}|end).(not_)*to)/, 3]
- end
-
- failure_message do |obj_method|
- block_def = block_definition(obj_method)
- "Expected that executing `#{block_def}` emits the `#{event_to_emit}` Webhook event.\n" \
- "Check that `#{block_def}` does implement `queue_webhooks_requests!` for " \
- "`#{event_to_emit}` with the following webhook_payload_body: \n\n#{webhook_payload_body}."
- end
-
- failure_message_when_negated do |obj_method|
- "Expected that executing `#{block_definition(obj_method)}` does not " \
- "emit the `#{event_to_emit}` Webhook event with the following webhook_payload_body: #{webhook_payload_body}."
- end
-
- supports_block_expectations
-end
-
-def with_webhooks_enabled
- Spree::LegacyWebhooks.disabled = false
- yield
- Spree::LegacyWebhooks.disabled = true
-end
diff --git a/packages/legacy_webhooks/lib/spree_legacy_webhooks.rb b/packages/legacy_webhooks/lib/spree_legacy_webhooks.rb
deleted file mode 100644
index 42303a6955..0000000000
--- a/packages/legacy_webhooks/lib/spree_legacy_webhooks.rb
+++ /dev/null
@@ -1,3 +0,0 @@
-# frozen_string_literal: true
-
-require 'spree/legacy_webhooks'
diff --git a/packages/legacy_webhooks/spec/controllers/spree/admin/webhooks_subscribers_controller_spec.rb b/packages/legacy_webhooks/spec/controllers/spree/admin/webhooks_subscribers_controller_spec.rb
deleted file mode 100644
index 244efc28b4..0000000000
--- a/packages/legacy_webhooks/spec/controllers/spree/admin/webhooks_subscribers_controller_spec.rb
+++ /dev/null
@@ -1,124 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Admin::WebhooksSubscribersController, type: :controller do
- stub_authorization!
-
- describe '#index' do
- let!(:webhook_subscriber) { create(:webhook_subscriber) }
-
- it 'responds successfully' do
- get :index
- expect(response).to be_successful
- end
- end
-
- describe '#show' do
- let(:webhook_subscriber) { create(:webhook_subscriber) }
-
- it 'responds successfully' do
- get :show, params: { id: webhook_subscriber.id }
- expect(response).to be_successful
- end
-
- it 'assigns the subscriber' do
- get :show, params: { id: webhook_subscriber.id }
- expect(assigns(:webhooks_subscriber)).to eq webhook_subscriber
- end
- end
-
- describe '#new' do
- it 'responds successfully' do
- get :new
- expect(response).to be_successful
- end
- end
-
- describe '#create' do
- let(:params) do
- {
- webhooks_subscriber: {
- url: 'https://example.com/webhook',
- active: true
- },
- subscribe_to_all_events: 'true'
- }
- end
-
- it 'creates a new webhooks subscriber' do
- expect {
- post :create, params: params
- }.to change(Spree::Webhooks::Subscriber, :count).by(1)
-
- expect(response).to redirect_to spree.admin_webhooks_subscriber_path(Spree::Webhooks::Subscriber.last)
- end
-
- it 'sets the attributes' do
- post :create, params: params
- subscriber = Spree::Webhooks::Subscriber.last
- expect(subscriber.url).to eq('https://example.com/webhook')
- expect(subscriber.active).to be true
- expect(subscriber.subscriptions).to eq(['*'])
- end
-
- context 'with selected events' do
- let(:params) do
- {
- webhooks_subscriber: {
- url: 'https://example.com/webhook',
- active: true,
- order: 'true'
- },
- subscribe_to_all_events: 'false'
- }
- end
-
- before do
- allow(Spree::Webhooks::Subscriber).to receive(:supported_events).and_return(
- { order: ['order.created', 'order.updated'] }
- )
- end
-
- it 'sets the selected subscriptions' do
- post :create, params: params
- subscriber = Spree::Webhooks::Subscriber.last
- expect(subscriber.subscriptions).to eq(['order.created', 'order.updated'])
- end
- end
- end
-
- describe '#update' do
- let!(:webhook_subscriber) { create(:webhook_subscriber) }
- let(:params) do
- {
- id: webhook_subscriber.id,
- webhooks_subscriber: {
- url: 'https://example.com/webhook-updated',
- active: false
- },
- subscribe_to_all_events: 'true'
- }
- end
-
- it 'updates the webhooks subscriber' do
- put :update, params: params
- webhook_subscriber.reload
- expect(webhook_subscriber.url).to eq('https://example.com/webhook-updated')
- expect(webhook_subscriber.active).to be false
- expect(webhook_subscriber.subscriptions).to eq(['*'])
-
- expect(response).to redirect_to spree.admin_webhooks_subscriber_path(webhook_subscriber)
- end
- end
-
- describe '#destroy' do
- let!(:webhook_subscriber) { create(:webhook_subscriber) }
-
- it 'deletes the webhooks subscriber' do
- expect {
- delete :destroy, params: { id: webhook_subscriber.id }
- }.to change(Spree::Webhooks::Subscriber, :count).by(-1)
-
- expect(response).to redirect_to spree.admin_webhooks_subscribers_path
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/packages/legacy_webhooks/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb
deleted file mode 100644
index 3d65dd4390..0000000000
--- a/packages/legacy_webhooks/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::Subscribers::MakeRequestJob do
- let(:event) { 'order.cancel' }
- let(:queue) { 'spree_webhooks' }
- let(:subscriber) { create(:subscriber, :active) }
- let(:webhook_payload_body) { {} }
-
- it 'enqueues a HTTP request using Spree::Webhooks::Subscribers::HandleRequest', :job do
- expect { described_class.perform_later(webhook_payload_body, event, subscriber) }.to have_enqueued_job.on_queue(queue)
- end
-
- it 'does not raise if used with the expected arguments', :job do
- allow(Spree::Webhooks::Subscribers::HandleRequest).to receive_message_chain(:new, :call)
- expect { described_class.perform_now(webhook_payload_body, event, subscriber) }.not_to raise_error
- end
-end
diff --git a/packages/legacy_webhooks/spec/lib/legacy_webhooks_spec.rb b/packages/legacy_webhooks/spec/lib/legacy_webhooks_spec.rb
deleted file mode 100644
index 62892fd540..0000000000
--- a/packages/legacy_webhooks/spec/lib/legacy_webhooks_spec.rb
+++ /dev/null
@@ -1,72 +0,0 @@
-require 'spec_helper'
-
-describe Spree::LegacyWebhooks do
- describe '#disable_webhooks' do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::VariantSerializer.new(
- variant,
- include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:event_name) { 'variant.discontinued' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }
- let(:variant) { create(:variant) }
-
- before do
- allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)
- allow(queue_requests).to receive(:call).with(any_args)
- end
-
- after { Spree::LegacyWebhooks.disabled = true }
-
- describe 'the value of spree_webhooks_disabled request variable' do
- before { RequestStore.store[:disable_spree_webhooks] = false }
-
- describe 'when an error is not raised' do
- it 'sets it to the original value' do
- described_class.disable_webhooks { variant.discontinue! }
- expect(RequestStore.store[:disable_spree_webhooks]).to eq(false)
- end
- end
-
- describe 'when an error is raised' do
- it 'sets it to the original value' do
- begin
- described_class.disable_webhooks { raise StandardError }
- rescue StandardError
- end
- expect(RequestStore.store[:disable_spree_webhooks]).to eq(false)
- end
- end
- end
-
- describe 'when webhooks are already disabled' do
- before { Spree::LegacyWebhooks.disabled = true }
-
- it 'does not emit the event' do
- described_class.disable_webhooks { variant.discontinue! }
- expect(queue_requests).not_to have_received(:call).with(event: event_name, webhook_payload_body: webhook_payload_body)
- end
- end
-
- describe 'when webhooks are enabled' do
- before { Spree::LegacyWebhooks.disabled = false }
-
- describe 'when not using #disable_webhooks' do
- it 'emits the event' do
- expect do
- variant.discontinue!
- end.to emit_webhook_event(event_name, variant)
- end
- end
-
- describe 'when using #disable_webhooks' do
- it 'does not emit the event' do
- described_class.disable_webhooks { variant.discontinue! }
- expect(queue_requests).not_to have_received(:call).with(event: event_name, webhook_payload_body: webhook_payload_body)
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
deleted file mode 100644
index 00ca69d2f2..0000000000
--- a/packages/legacy_webhooks/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ /dev/null
@@ -1,158 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::HasWebhooks do
- let(:images) { create_list(:image, 2) }
- let(:store) { @default_store }
- let(:variant_with_images) { create(:variant, images: images) }
- let(:variant) { build(:variant) }
- let(:product) do
- build(
- :product_in_stock,
- variants_including_master: [variant_with_images, variant],
- stores: [store]
- )
- end
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: ['*']) }
-
- context 'with DISABLE_SPREE_WEBHOOKS equals "true" (set in spec_helper)' do
- let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }
-
- before do
- allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)
- allow(queue_requests).to receive(:call).with(any_args)
- product.save
- end
-
- shared_examples 'not queueing an event request' do |event_name|
- it { expect(queue_requests).not_to have_received(:call).with(hash_including(event: event_name)) }
- end
-
- context 'after_create_commit' do
- it_behaves_like 'not queueing an event request', 'product.create'
- end
-
- context 'after_destroy_commit' do
- before { product.destroy }
-
- it_behaves_like 'not queueing an event request', 'product.delete'
- end
-
- context 'after_update_commit' do
- before { product.update(name: 'updated') }
-
- it_behaves_like 'not queueing an event request', 'product.update'
- end
- end
-
- context 'without DISABLE_SPREE_WEBHOOKS' do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::ProductSerializer.new(
- product,
- include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
-
- context 'after_create_commit' do
- let(:event_name) { 'product.create' }
- it { expect { product.save }.to emit_webhook_event('product.create', product) }
- end
-
- context 'after_destroy_commit' do
- before { product.save }
-
- it { expect { product.destroy }.to emit_webhook_event('product.delete', product) }
- end
-
- context 'after_update_commit' do
- before { product.save }
-
- it { expect { product.update(name: 'updated') }.to emit_webhook_event('product.update', product) }
- end
-
- context 'when only timestamps change' do
- let(:event_name) { 'product.update' }
-
- before { product.save! }
-
- context 'on created_at change' do
- it do
- expect do
- product.update(created_at: Date.yesterday)
- end.not_to emit_webhook_event(event_name)
- end
- end
-
- context 'on updated_at change' do
- it do
- expect do
- product.update!(updated_at: Date.yesterday)
- end.not_to emit_webhook_event(event_name)
- end
- end
-
- context 'when using touch without arguments' do
- it do
- expect do
- # Doing product.touch in Rails 5.2 doesn't work at the first time.
- # It must be done twice in order to update the updated_at column.
- Spree::Product.find(product.id).touch
- end.not_to emit_webhook_event(event_name)
- end
- end
-
- context 'when using touch with an argument other than created_at/updated_at' do
- it do
- expect do
- product.touch(:available_on)
- end.to emit_webhook_event(event_name, product)
- end
- end
- end
-
- context 'on touch events from callbacks' do
- let!(:store2) { create(:store) }
- let!(:taxonomy) { create(:taxonomy, store: store2) }
- let(:body) do
- Spree::Api::V2::Platform::StoreSerializer.new(
- store2,
- include: Spree::Api::V2::Platform::StoreSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
-
- before { store2.changes_applied }
-
- it 'does not emit the touched model\'s update event' do
- expect { taxonomy.update(name: 'Updated Taxonomy') }.not_to emit_webhook_event('store.update')
- end
- end
-
- context 'when no subscribers are active' do
- before { webhook_subscriber.update(active: false) }
-
- it 'does not emit the event' do
- expect { product.save }.not_to emit_webhook_event('product.create')
- end
- end
- end
-
- describe '.default_webhook_events' do
- it 'return the default events' do
- expect(Spree::Order.default_webhook_events).to eq(%w[order.create order.delete order.update])
- end
- end
-
- describe '.supported_webhook_events' do
- context 'when there are custom supported events' do
- it 'returns the default and custom events' do
- expected_webhook_events = %w[order.create order.delete order.update order.canceled order.placed order.resumed order.shipped]
- expect(Spree::Order.supported_webhook_events).to contain_exactly(*expected_webhook_events)
- end
- end
-
- context 'when there are no custom supported events' do
- it 'return the default events' do
- expect(Spree::Zone.supported_webhook_events).to eq(Spree::Zone.default_webhook_events)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/order_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/order_webhooks_spec.rb
deleted file mode 100644
index 7e22b46732..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/order_webhooks_spec.rb
+++ /dev/null
@@ -1,73 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Order::Webhooks do
- let(:store) { @default_store }
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::OrderSerializer.new(
- order,
- include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
-
- describe 'order.canceled' do
- describe 'completed -> canceled' do
- let(:event_name) { 'order.canceled' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let(:order) { create(:completed_order_with_totals, store: store) }
-
- it { expect { Timecop.freeze { order.cancel } }.to emit_webhook_event(event_name, order) }
- end
- end
-
- describe 'order.placed' do
- describe 'checkout -> completed' do
- let(:event_name) { 'order.placed' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let(:order) { create(:order, email: '[email protected]', store: store) }
-
- it { expect { order.finalize! }.to emit_webhook_event(event_name, order) }
- end
- end
-
- describe 'order.resumed' do
- let(:event_name) { 'order.resumed' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let(:order) { create(:order, store: store, state: :canceled) }
-
- context 'when order state changes' do
- context 'when order state changes "resumed"' do
- context 'when manually setting the state' do
- # this case does not create a state change record
- subject { Timecop.freeze { order.update(state: 'resumed') } }
-
- it { expect { subject }.to emit_webhook_event(event_name, order) }
- end
-
- context 'when doing it through resume!' do
- subject { order.resume! }
-
- it { expect { subject }.to emit_webhook_event(event_name, order) }
-
- context 'after emitting the webhook' do
- it 'correctly sets state_machine_resumed used to avoid emitting the same event twice' do
- # it does so because an update is for setting the state
- # and another one is for setting the order state_changes
- order.state_machine_resumed = true
- expect(order.state_machine_resumed).to eq(true)
- subject
- expect(order.state_machine_resumed).to eq(false)
- end
- end
- end
- end
-
- context 'when order state does not change to "resumed"' do
- it do
- expect do
- order.update(email: '[email protected]')
- end.not_to emit_webhook_event(event_name, order)
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/payment_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/payment_webhooks_spec.rb
deleted file mode 100644
index bb5ad41cbf..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/payment_webhooks_spec.rb
+++ /dev/null
@@ -1,131 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Payment::Webhooks do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::PaymentSerializer.new(
- payment,
- include: Spree::Api::V2::Platform::PaymentSerializer.relationships_to_serialize.keys - [:state_changes]
- ).serializable_hash
- end
- let(:payment) { create(:payment) }
-
- before do
- allow(payment).to receive_message_chain(:state_changes, :create!)
- # because it state_changes is an instance of Double and can not be serialized
- allow(payment).to receive(:included_relationships).and_return(Spree::Api::V2::Platform::PaymentSerializer.relationships_to_serialize.keys - [:state_changes])
- end
-
- describe 'payment.paid' do
- let(:event_name) { 'payment.paid' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'processing -> completed' do
- before { payment.started_processing }
-
- it { expect { payment.complete }.to emit_webhook_event(event_name, payment) }
- end
-
- context 'pending -> completed' do
- before { payment.pend }
-
- it { expect { payment.complete }.to emit_webhook_event(event_name, payment) }
- end
-
- context 'checkout -> completed' do
- it { expect { payment.complete }.to emit_webhook_event(event_name, payment) }
- end
- end
-
- describe 'payment.voided' do
- let(:event_name) { 'payment.voided' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'pending -> void' do
- before { payment.pend }
-
- it { expect { payment.void }.to emit_webhook_event(event_name, payment) }
- end
-
- context 'processing -> void' do
- before { payment.started_processing }
-
- it { expect { payment.void }.to emit_webhook_event(event_name, payment) }
- end
-
- context 'completed -> void' do
- before { payment.complete }
-
- it { expect { payment.void }.to emit_webhook_event(event_name, payment) }
- end
-
- context 'checkout -> void' do
- it { expect { payment.void }.to emit_webhook_event(event_name, payment) }
- end
- end
-
- describe 'order.paid' do
- subject { Timecop.freeze { another_payment.complete } }
-
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::OrderSerializer.new(
- order,
- include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:order) { payment.order }
- let(:event_name) { 'order.paid' }
- let!(:another_payment) { create(:payment, order: order) }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'order.paid? == true' do
- context 'processing -> complete' do
- before do
- payment.started_processing
- payment.complete
- another_payment.started_processing
- end
-
- it { expect { subject }.to emit_webhook_event(event_name, order) }
- end
-
- context 'pending -> complete' do
- before do
- payment.pend
- payment.complete
- another_payment.pend
- end
-
- it { expect { subject }.to emit_webhook_event(event_name, order) }
- end
-
- context 'checkout -> complete' do
- before { payment.complete }
-
- it { expect { subject }.to emit_webhook_event(event_name, order) }
- end
- end
-
- context 'order.paid? == false' do
- before do
- allow(payment).to receive(:order).and_return(order)
- allow(order).to receive(:paid?).and_return(false)
- end
-
- context 'processing -> complete' do
- before { payment.started_processing }
-
- it { expect { subject }.not_to emit_webhook_event(event_name, order) }
- end
-
- context 'pending -> complete' do
- before { payment.pend }
-
- it { expect { subject }.not_to emit_webhook_event(event_name, order) }
- end
-
- context 'checkout -> complete' do
- it { expect { subject }.not_to emit_webhook_event(event_name, order) }
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/product_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/product_webhooks_spec.rb
deleted file mode 100644
index 15a35dc10f..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/product_webhooks_spec.rb
+++ /dev/null
@@ -1,72 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Product::Webhooks do
- let(:product) { create(:product) }
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::ProductSerializer.new(
- product,
- include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'emitting product.discontinued' do
- let(:event_name) { 'product.discontinued' }
-
- context 'when product discontinued_on changes' do
- context 'when the new value is "present"' do
- it do
- expect do
- product.discontinue!
- end.to emit_webhook_event(event_name, product)
- end
- end
-
- context 'when the new value is not "present"' do
- before { product.update(discontinue_on: Date.yesterday) }
-
- it do
- expect do
- product.update(discontinue_on: nil)
- end.not_to emit_webhook_event(event_name)
- end
- end
- end
-
- context 'when product discontinued_on does not change' do
- it do
- expect do
- product.update(width: 180)
- end.not_to emit_webhook_event(event_name)
- end
- end
- end
-
- context 'when changing status' do
- context 'to active' do
- let(:event_name) { 'product.activated' }
-
- before { product.update_column(:status, :draft) }
-
- it do
- expect { product.activate }.to emit_webhook_event(event_name, product)
- end
- end
-
- context 'to draft' do
- let(:event_name) { 'product.drafted' }
-
- it do
- expect { product.draft }.to emit_webhook_event(event_name, product)
- end
- end
-
- context 'to archived' do
- let(:event_name) { 'product.archived' }
-
- it do
- expect { product.archive }.to emit_webhook_event(event_name, product)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/shipment_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/shipment_webhooks_spec.rb
deleted file mode 100644
index a4ae93910d..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/shipment_webhooks_spec.rb
+++ /dev/null
@@ -1,108 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Shipment::Webhooks do
- let(:order) { create(:order) }
- let(:shipment) { create(:shipment) }
-
- describe '#after_ship' do
- before do
- # Avoid creating a new state change after transitioning as is defined in the model
- # because after_ship queues the HTTP request before finishing the transition, hence
- # the total state changes that are sent in the body is one less.
- allow(shipment).to receive_message_chain(:state_changes, :create!)
-
- # because state_changes is an instance of Double and can not be serialized
- allow_any_instance_of(Spree::Shipment).to receive(:included_relationships).and_return(Spree::Api::V2::Platform::ShipmentSerializer.relationships_to_serialize.keys - [:state_changes])
- end
-
- context 'emitting shipment.shipped' do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::ShipmentSerializer.new(
- shipment,
- include: Spree::Api::V2::Platform::ShipmentSerializer.relationships_to_serialize.keys - [:state_changes]
- ).serializable_hash
- end
- let(:event_name) { 'shipment.shipped' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'ready -> ship' do
- let(:shipment) { create(:shipment, order: order) }
-
- before do
- order.update(state: 'complete', completed_at: Time.current)
- shipment.reload
- shipment.ready
- end
-
- it { expect { shipment.ship }.to emit_webhook_event(event_name, shipment) }
- end
-
- context 'canceled -> ship' do
- before { shipment.cancel }
-
- it { expect { shipment.ship }.to emit_webhook_event(event_name, shipment) }
- end
- end
-
- context 'emitting order.shipped' do
- let(:webhook_payload_body) do
- webhook_payload_body = Spree::Api::V2::Platform::OrderSerializer.new(
- order.reload,
- include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
- ).serializable_hash
- webhook_payload_body[:included].each { |resource_hash| resource_hash[:relationships][:state_changes][:data] = [] if resource_hash[:type] == :shipment }
- webhook_payload_body
- end
- let(:event_name) { 'order.shipped' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let!(:shipments) do
- create_list(
- :shipment, 2,
- order: order,
- shipping_methods: [create(:shipping_method)],
- stock_location: build(:stock_location)
- )
- end
-
- context 'ready -> ship' do
- before do
- order.update(state: 'complete', completed_at: Time.current)
- shipments.each(&:reload) # must reload to make shipments order state see it's complete
- shipments[0].ready
- end
-
- context 'with all order shipments shipped' do
- before do
- shipments[0].ship
- shipments[1].ready
- shipments.each { |s| s.state_changes.delete_all }
- end
-
- it { expect { shipments[1].ship }.to emit_webhook_event(event_name, order) }
- end
-
- context 'without all order shipments shipped' do
- it { expect { shipments[0].ship }.not_to emit_webhook_event(event_name, shipments[0]) }
- end
- end
-
- context 'canceled -> ship' do
- before { shipments[0].cancel }
-
- context 'with all order shipments shipped' do
- before do
- shipments[0].ship
- shipments[1].cancel
- shipments.each { |s| s.state_changes.delete_all }
- end
-
- it { expect { shipments[1].ship }.to emit_webhook_event(event_name, order) }
- end
-
- context 'without all order shipments shipped' do
- it { expect { shipments[0].ship }.not_to emit_webhook_event(event_name, shipments[0]) }
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/stock_item_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/stock_item_webhooks_spec.rb
deleted file mode 100644
index 6d1dafb4bd..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/stock_item_webhooks_spec.rb
+++ /dev/null
@@ -1,133 +0,0 @@
-require 'spec_helper'
-
-describe Spree::StockItem::Webhooks do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::VariantSerializer.new(
- variant,
- include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:variant) { create(:variant) }
- let(:stock_item) { variant.stock_items.first }
- let(:stock_location) { variant.stock_locations.first }
-
- describe 'emitting product.backorderable' do
- subject { stock_item.update(backorderable: backorderable) }
-
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::ProductSerializer.new(
- product,
- include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:product) { variant.product }
- let(:event_name) { 'product.backorderable' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let!(:variant2) { create(:variant, product: product) }
-
- before do
- Spree::StockItem.update_all(backorderable: false)
- product.reload
- end
-
- context 'when product was out of stock' do
- context 'when none of the product variants is backorderable' do
- context 'when one of the variants is made backorderable' do
- let(:backorderable) { true }
-
- it { expect { subject }.to emit_webhook_event(event_name, product) }
- end
-
- context 'when none of the variants is made backorderable' do
- let(:backorderable) { false }
-
- it { expect { subject }.not_to emit_webhook_event(event_name, product) }
- end
- end
-
- context 'when other variant is already backorderable' do
- let(:another_stock_item) { variant2.stock_items.first }
-
- context 'when other variant is made backorderable' do
- let(:backorderable) { true }
-
- before do
- another_stock_item.update(backorderable: true)
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name, product) }
- end
- end
- end
-
- context 'when product was in stock' do
- let(:backorderable) { true }
-
- before do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 1
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name, product) }
- end
- end
-
- describe 'emitting variant.backorderable' do
- subject { stock_item.save }
-
- let(:event_name) { 'variant.backorderable' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'when variant was out of stock' do
- before do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 0
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- end
-
- context 'when variant was backorderable' do
- before { stock_item.backorderable = true }
-
- it { expect { subject }.not_to emit_webhook_event(event_name, variant) }
- end
-
- context 'when variant was not backorderable' do
- before do
- variant.stock_items.update_all(backorderable: false)
- variant.product.reload
- end
-
- context 'when variant is not set as backorderable' do
- before { stock_item.backorderable = false }
-
- it { expect { subject }.not_to emit_webhook_event(event_name, variant) }
- end
-
- context 'when variant is set as backorderable' do
- before { stock_item.backorderable = true }
-
- it { expect { subject }.to emit_webhook_event(event_name, variant) }
- end
- end
- end
-
- context 'when variant was not out of stock' do
- before do
- variant.stock_items.update_all(backorderable: false)
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 1
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- stock_item.backorderable = true
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name, variant) }
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/stock_movement_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/stock_movement_webhooks_spec.rb
deleted file mode 100644
index 2df63993db..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/stock_movement_webhooks_spec.rb
+++ /dev/null
@@ -1,249 +0,0 @@
-require 'spec_helper'
-
-describe Spree::StockMovement::Webhooks do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::VariantSerializer.new(
- variant.reload,
- include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:stock_item) { create(:stock_item) }
- let(:stock_location) { variant.stock_locations.first }
-
- describe 'emitting product events' do
- let!(:store) { @default_store }
- let!(:product) { create(:product, stores: [store]) }
- let!(:variant) { create(:variant, product: product) }
- let!(:variant2) { create(:variant, product: product) }
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::ProductSerializer.new(
- product.reload,
- include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
-
- describe 'emitting product.out_of_stock' do
- let(:event_name) { 'product.out_of_stock' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- before { Spree::StockItem.update_all(backorderable: false) }
-
- context 'when all product variants are in stock' do
- before do
- product.reload.variants.each do |variant|
- variant.stock_items.each do |stock_item|
- stock_item.set_count_on_hand(1)
- end
- end
- end
-
- context 'when one of the variants goes out of stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = -1
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- product.reload
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name) }
- end
-
- context 'when all the variants go out of stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = -1
- stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[1])
- stock_movement.save
- end
- product.reload
- Rails.cache.clear
- end
-
- before do
- # Set one of the two product variants as out of stock
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = -1
- stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[0])
- stock_movement.save!
- end
- Rails.cache.clear
- end
-
- it { expect { subject }.to emit_webhook_event(event_name, product) }
- end
- end
-
- context 'when only one product variant is in stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = -1
- stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[0])
- stock_movement.save!
- end
- product.reload
- Rails.cache.clear
- end
-
- before { product.reload.variants[0].stock_items[0].set_count_on_hand(1) }
-
- it { expect { subject }.to emit_webhook_event(event_name, product) }
- end
-
- context 'when no product variant is in stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 0
- stock_movement.stock_item = stock_location.set_up_stock_item(product.variants[0])
- stock_movement.save
- end
- product.reload
- Rails.cache.clear
- end
-
- before do
- product.reload.variants.each do |variant|
- variant.stock_items.each do |stock_item|
- stock_item.set_count_on_hand(0)
- end
- end
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name, product) }
- end
- end
-
- describe 'emitting product.back_in_stock' do
- let(:event_name) { 'product.back_in_stock' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- before { Spree::StockItem.update_all(backorderable: false) }
-
- context 'when all product variants are out of stock' do
- context 'when one of the variants is back in stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 1
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save!
- end
- product.reload
- Rails.cache.clear
- end
-
- it { expect { subject }.to emit_webhook_event(event_name, product) }
- end
-
- context 'when none of the variants is back in stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 0
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- product.reload
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name, product) }
- end
- end
-
- context 'when other variant is already in stock' do
- subject do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 100
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- end
-
- before do
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 1
- stock_movement.stock_item = stock_location.set_up_stock_item(variant2)
- stock_movement.save
- end
- end
-
- it { expect { subject }.not_to emit_webhook_event(event_name, product) }
- end
- end
- end
-
- describe 'emitting variant.back_in_stock' do
- let!(:variant) { create(:variant, track_inventory: true) }
- let(:event_name) { 'variant.back_in_stock' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'when stock item was out of stock' do
- context 'when stock item changes to be in stock' do
- # TODO: Fix this test
- xit do
- expect do
- variant.stock_items.update_all(backorderable: false)
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 100 # does make it to be in stock
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save!
- end
- end.to emit_webhook_event(event_name, variant)
- end
- end
-
- context 'when stock item does not change to be in stock' do
- it do
- expect do
- variant.stock_items.update_all(backorderable: false)
- stock_location.stock_movements.new.tap do |stock_movement|
- stock_movement.quantity = 0 # does not make it to be in stock
- stock_movement.stock_item = stock_location.set_up_stock_item(variant)
- stock_movement.save
- end
- end.not_to emit_webhook_event(event_name, variant)
- end
- end
- end
- end
-
- describe 'emitting variant.out_of_stock' do
- subject { stock_movement }
-
- let(:stock_movement) { create(:stock_movement, stock_item: stock_item, quantity: movement_quantity) }
- let(:event_name) { 'variant.out_of_stock' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
- let!(:variant) { stock_item.variant }
-
- before do
- Spree::StockItem.update_all(backorderable: false)
- end
-
- describe 'when the variant goes out of stock' do
- let(:movement_quantity) { -stock_item.count_on_hand }
-
- it 'emits the variant.out_of_stock event' do
- ActiveRecord::Base.no_touching do
- expect { subject }.to emit_webhook_event(event_name, variant)
- end
- end
- end
-
- describe 'when the variant does not go out of stock' do
- let(:movement_quantity) { -stock_item.count_on_hand + 1 }
-
- it 'does not emit the variant.out_of_stock event' do
- expect { subject }.not_to emit_webhook_event(event_name, variant)
- end
- end
-
- describe 'when the variant was out of stock before the update and after the update' do
- before { stock_item.set_count_on_hand(0) }
-
- let(:movement_quantity) { 0 }
-
- it 'does not emit the variant.out_of_stock event' do
- expect { subject }.not_to emit_webhook_event(event_name, variant)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/variant_webhooks_spec.rb b/packages/legacy_webhooks/spec/models/spree/variant_webhooks_spec.rb
deleted file mode 100644
index a24bc80086..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/variant_webhooks_spec.rb
+++ /dev/null
@@ -1,44 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Variant::Webhooks do
- let(:variant) { create(:variant) }
-
- context 'emitting variant.discontinued' do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::VariantSerializer.new(
- variant,
- include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:event_name) { 'variant.discontinued' }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
-
- context 'when variant discontinued_on changes' do
- context 'when the new value is "present"' do
- it do
- expect do
- variant.discontinue!
- end.to emit_webhook_event(event_name, variant)
- end
- end
-
- context 'when the new value is not "present"' do
- before { variant.update(discontinue_on: Date.yesterday) }
-
- it do
- expect do
- variant.update(discontinue_on: nil)
- end.not_to emit_webhook_event(event_name, variant)
- end
- end
- end
-
- context 'when variant discontinued_on does not change' do
- it do
- expect do
- variant.update(width: 180)
- end.not_to emit_webhook_event(event_name, variant)
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/webhooks/event_signature_spec.rb b/packages/legacy_webhooks/spec/models/spree/webhooks/event_signature_spec.rb
deleted file mode 100644
index 7b41f7d93f..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/webhooks/event_signature_spec.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::EventSignature do
- subject(:signature) { described_class.new(webhook_event, event_payload) }
-
- let(:event_payload) { { event_name: 'order.create', data: { id: 123 } } }
- let(:webhook_event) { create(:webhook_event) }
-
- describe '#computed_signature' do
- it 'computes a unique signature per payload' do
- order_finished_signature =
- described_class.new(webhook_event, { event_name: 'order.finished', data: { id: 123 } })
-
- expect(signature.computed_signature).
- not_to eq(order_finished_signature.computed_signature)
- end
-
- it 'computes a stable signature for the same payload' do
- order_created_signature = described_class.new(webhook_event, event_payload)
-
- expect(signature.computed_signature).
- to eq(order_created_signature.computed_signature)
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/webhooks/event_spec.rb b/packages/legacy_webhooks/spec/models/spree/webhooks/event_spec.rb
deleted file mode 100644
index 56392c1e11..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/webhooks/event_spec.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::Event do
- describe 'validations' do
- context 'name' do
- it 'is invalid with a blank name' do
- event = build(:event, :successful, name: '')
- expect(event.valid?).to be(false)
- expect(event.errors.messages).to eq(name: ["can't be blank"])
- end
-
- it 'is valid with a non-blank name' do
- event = build(:event, :successful, name: 'order.canceled')
- expect(event.valid?).to be(true)
- end
- end
-
- context 'subscriber' do
- it 'is invalid without a subscriber' do
- event = build(:event, :successful, subscriber_id: nil)
- expect(event.valid?).to be(false)
- expect(event.errors.messages).to eq(subscriber: ["can't be blank", 'must exist'])
- end
-
- it 'is valid with a subscriber' do
- event = build(:event, :successful, subscriber: create(:subscriber))
- expect(event.valid?).to be(true)
- end
- end
- end
-
- describe '#signature_for' do
- subject(:signature) { event.signature_for(payload) }
-
- let(:event) { create(:event) }
- let(:payload) { { id: 123 }.to_json }
-
- it 'computes a signature for the JSON payload' do
- expect(signature).to \
- eq(Spree::Webhooks::EventSignature.new(event, payload).computed_signature)
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/models/spree/webhooks/subscriber_spec.rb b/packages/legacy_webhooks/spec/models/spree/webhooks/subscriber_spec.rb
deleted file mode 100644
index bc17daa18c..0000000000
--- a/packages/legacy_webhooks/spec/models/spree/webhooks/subscriber_spec.rb
+++ /dev/null
@@ -1,102 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::Subscriber do
- describe 'before_save' do
- context 'with subscriptions' do
- let(:subscriptions) { ['order.placed', 'order.shipped'] }
-
- it 'json parses the subscriptions' do
- subscriber = create(:subscriber, subscriptions: subscriptions.to_json)
- expect(subscriber.subscriptions).to eq(subscriptions)
- end
- end
-
- context 'without subscriptions' do
- it 'json parses the subscriptions' do
- subscriber = create(:subscriber, subscriptions: nil)
- expect(subscriber.subscriptions).to eq(nil)
- end
- end
- end
-
- describe 'before_validation' do
- subject(:subscriber) { build(:subscriber, secret_key: nil) }
-
- context 'without a secret key' do
- it 'generates a new secret key' do
- expect { subscriber.save }.to change(subscriber, :secret_key).from(nil)
- end
- end
-
- context 'with secret key' do
- let(:assigned_secret_key) { 'some-secret-key-for-webhooks' }
-
- before do
- subscriber.secret_key = assigned_secret_key
- end
-
- it 'does not overwrite the assigned secret key' do
- expect { subscriber.save }.not_to change(subscriber, :secret_key)
- end
- end
- end
-
- describe 'validations' do
- context 'url format (UrlValidator)' do
- it 'is invalid with an invalid url' do
- subscriber = described_class.new(url: 'google.com')
- expect(subscriber.valid?).to be(false)
- end
-
- it 'is valid with a valid url' do
- subscriber = described_class.new(url: 'http://google.com/')
- expect(subscriber.valid?).to be(true)
- end
- end
-
- context 'url path' do
- it 'is invalid a url without path' do
- subscriber = described_class.new(url: 'http://google.com')
- expect(subscriber.valid?).to be(false)
- expect(subscriber.errors.messages).to eq(url: ['the URL must have a path'])
- end
- end
- end
-
- describe '.with_urls_for' do
- subject { described_class.with_urls_for(event) }
-
- let(:event) { 'order.placed' }
- let(:subscriptions) { ['order.placed'] }
- let!(:subscriber) { described_class.create(url: url, subscriptions: subscriptions, active: true) }
- let(:url) { 'https://url1.com/' }
-
- context 'with subscriptions for the given event' do
- it { expect(subject).to eq([subscriber]) }
- end
-
- context 'without subscriptions for the given event, but "*"' do
- let(:subscriptions) { ['*'] }
-
- it { expect(subject).to eq([subscriber]) }
- end
-
- context 'without subscriptions for the given event' do
- let(:subscriptions) { ['order.updated'] }
-
- it { expect(subject).to be_empty }
- end
- end
-
- describe '.supported_events' do
- subject { described_class.supported_events }
-
- xit 'includes the webhookable model in supported events list' do
- class WebhookableDummy < Spree::Base
- include Spree::Webhooks::HasWebhooks
- end
-
- expect(subject[:webhookable_dummy]).to contain_exactly('webhookable_dummy.create', 'webhookable_dummy.update', 'webhookable_dummy.delete')
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/requests/spree/api/v2/platform/webhooks/events_spec.rb b/packages/legacy_webhooks/spec/requests/spree/api/v2/platform/webhooks/events_spec.rb
deleted file mode 100644
index 55e2d78255..0000000000
--- a/packages/legacy_webhooks/spec/requests/spree/api/v2/platform/webhooks/events_spec.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-require 'spec_helper'
-
-describe 'Platform API v2 Webhooks Events spec', type: :request do
- include_context 'API v2 tokens'
- include_context 'Platform API v2'
-
- let(:bearer_token) { { 'Authorization' => valid_authorization } }
- let(:type) { 'event' }
-
- describe '#index' do
- context 'filtering' do
- let(:data_ids) { json_response['data'].pluck(:id) }
-
- context 'with no params' do
- before do
- create_list(:event, 2)
- get "/api/v2/platform/webhooks/events", headers: bearer_token
- end
-
- it 'returns all events' do
- expect(json_response['data'].count).to eq(2)
- expect(json_response['data'].first).to have_type(type)
- expect(json_response['data'].second).to have_type(type)
- end
- end
-
- context 'by name' do
- let!(:event_1) { create(:event, name: name_filter) }
- let!(:event_2) { create(:event, name: 'order.created') }
- let(:name_filter) { 'order.canceled' }
-
- before { get "/api/v2/platform/webhooks/events?filter[name_eq]=#{name_filter}", headers: bearer_token }
-
- it 'returns only the event matching the given name' do
- expect(json_response['data'].count).to eq(1)
- expect(data_ids).not_to include(event_2.id)
- expect(data_ids).to match_array([event_1.id.to_s])
- end
- end
-
- context 'by request_errors' do
- let!(:event_1) { create(:event, :failed, request_errors: "[SPREE WEBHOOKS] 'order.canceled' can not make a request to 'http://google.com/'") }
- let!(:event_2) { create(:event, :successful) }
-
- before { get "/api/v2/platform/webhooks/events?filter[request_errors_cont]=google", headers: bearer_token }
-
- it 'returns only the event matching the given name' do
- expect(json_response['data'].count).to eq(1)
- expect(data_ids).not_to include(event_2.id)
- expect(data_ids).to match_array([event_1.id.to_s])
- end
- end
-
- context 'by response_code' do
- let!(:event_1) { create(:event, response_code: response_code_filter) }
- let!(:event_2) { create(:event, response_code: '301') }
- let(:response_code_filter) { '200' }
-
- before { get "/api/v2/platform/webhooks/events?filter[response_code_eq]=#{response_code_filter}", headers: bearer_token }
-
- it 'returns only the event matching the given name' do
- expect(json_response['data'].count).to eq(1)
- expect(data_ids).not_to include(event_2.id)
- expect(data_ids).to match_array([event_1.id.to_s])
- end
- end
-
- context 'by success' do
- let!(:event_success) { create(:event, :successful) }
- let!(:event_fail) { create(:event, :failed) }
- let(:success_filter) { true }
-
- before { get "/api/v2/platform/webhooks/events?filter[success_eq]=#{success_filter}", headers: bearer_token }
-
- it 'returns only the event matching the given name' do
- expect(json_response['data'].count).to eq(1)
- expect(data_ids).not_to include(event_fail.id)
- expect(data_ids).to match_array([event_success.id.to_s])
- end
- end
-
- context 'by url' do
- let!(:event_1) { create(:event, url: 'https://mysite.com/spree_webhooks') }
- let!(:event_2) { create(:event, url: 'http://localhost.com/') }
- let(:url_filter) { 'mysite' }
-
- before { get "/api/v2/platform/webhooks/events?filter[url_cont]=#{url_filter}", headers: bearer_token }
-
- it 'returns only the event matching the given name' do
- expect(json_response['data'].count).to eq(1)
- expect(data_ids).not_to include(event_2.id)
- expect(data_ids).to match_array([event_1.id.to_s])
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/requests/spree/api/v2/platform/webhooks/subscribers_spec.rb b/packages/legacy_webhooks/spec/requests/spree/api/v2/platform/webhooks/subscribers_spec.rb
deleted file mode 100644
index c2f2973b94..0000000000
--- a/packages/legacy_webhooks/spec/requests/spree/api/v2/platform/webhooks/subscribers_spec.rb
+++ /dev/null
@@ -1,95 +0,0 @@
-require 'spec_helper'
-
-describe 'Platform API v2 Webhooks Subscribers spec', type: :request do
- include_context 'API v2 tokens'
- include_context 'Platform API v2'
-
- let(:bearer_token) { { 'Authorization' => valid_authorization } }
- let(:type) { 'subscriber' }
- let(:url) { 'https://www.mysite.com/spree_webhooks' }
-
- describe '#index' do
- context 'filtering' do
- let(:data_ids) { json_response['data'].pluck(:id) }
-
- context 'with no params' do
- before do
- create_list(:subscriber, 2)
- get "/api/v2/platform/webhooks/subscribers", headers: bearer_token
- end
-
- it 'returns all subscribers' do
- expect(json_response['data'].count).to eq(2)
- expect(json_response['data'].first).to have_type(type)
- expect(json_response['data'].second).to have_type(type)
- end
- end
-
- context 'by active' do
- let!(:subscriber_active) { create(:subscriber, :active) }
- let!(:subscriber_inactive) { create(:subscriber, :inactive) }
-
- before { get "/api/v2/platform/webhooks/subscribers?filter[active_eq]=#{active}", headers: bearer_token }
-
- context 'active' do
- let(:active) { true }
- let(:active_subscribers) { Spree::Webhooks::Subscriber.active }
-
- it 'returns only active subscribers' do
- expect(json_response['data'].count).to eq(active_subscribers.count)
- expect(data_ids).not_to include(subscriber_inactive.id)
- expect(data_ids).to match_array(active_subscribers.ids.map(&:to_s))
- end
- end
-
- context 'inactive' do
- let(:active) { false }
- let(:inactive_subscribers) { Spree::Webhooks::Subscriber.inactive }
-
- it 'returns only active subscribers' do
- expect(json_response['data'].count).to eq(inactive_subscribers.count)
- expect(data_ids).not_to include(subscriber_active.id)
- expect(data_ids).to match_array(inactive_subscribers.ids.map(&:to_s))
- end
- end
- end
-
- context 'by url' do
- let!(:subscriber) { create(:subscriber, url: url) }
- let!(:another_subscriber) { create(:subscriber, url: 'http://localhost/') }
-
- before { get "/api/v2/platform/webhooks/subscribers?filter[url_cont]=spree_webhooks", headers: bearer_token }
-
- context 'matching the given term' do
- it 'returns subscribers only for the given url' do
- expect(json_response['data'].count).to eq(1)
- expect(data_ids).not_to include(another_subscriber.id)
- expect(data_ids).to match_array([subscriber.id.to_s])
- end
- end
- end
- end
- end
-
- context '#create' do
- let(:active) { true }
- let(:events) { ['order.created', 'order.placed', 'product.updated'] }
- let(:params) { { subscriber: { active: true, url: url, subscriptions: events } } }
-
- it 'returns status created' do
- post '/api/v2/platform/webhooks/subscribers', headers: bearer_token, params: params
- expect(response).to have_http_status :created
- end
-
- it 'creates a subscriber' do
- expect {
- post '/api/v2/platform/webhooks/subscribers', headers: bearer_token, params: params
- }.to change {
- Spree::Webhooks::Subscriber.
- all.
- as_json(except: %i[created_at id preferences updated_at secret_key]).
- map(&:values)
- }.from([]).to([[url, active, events]])
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/serializers/spree/api/v2/platform/webhooks/event_serializer_spec.rb b/packages/legacy_webhooks/spec/serializers/spree/api/v2/platform/webhooks/event_serializer_spec.rb
deleted file mode 100644
index 199326881a..0000000000
--- a/packages/legacy_webhooks/spec/serializers/spree/api/v2/platform/webhooks/event_serializer_spec.rb
+++ /dev/null
@@ -1,38 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Api::V2::Platform::Webhooks::EventSerializer do
- include_context 'API v2 serializers params'
-
- subject { described_class.new(resource, params: serializer_params).serializable_hash }
-
- let(:resource) { create(type, subscriber: subscriber) }
- let(:subscriber) { create(:subscriber) }
- let(:type) { :event }
-
- it do
- expect(subject).to eq(
- data: {
- id: resource.id.to_s,
- type: type,
- attributes: {
- created_at: resource.created_at,
- execution_time: resource.execution_time,
- name: resource.name,
- request_errors: resource.request_errors,
- response_code: resource.response_code,
- success: resource.success,
- updated_at: resource.updated_at,
- url: resource.url
- },
- relationships: {
- subscriber: {
- data: {
- id: subscriber.id.to_s,
- type: :subscriber
- }
- }
- }
- }
- )
- end
-end
diff --git a/packages/legacy_webhooks/spec/serializers/spree/api/v2/platform/webhooks/subscriber_serializer_spec.rb b/packages/legacy_webhooks/spec/serializers/spree/api/v2/platform/webhooks/subscriber_serializer_spec.rb
deleted file mode 100644
index 08264fcabe..0000000000
--- a/packages/legacy_webhooks/spec/serializers/spree/api/v2/platform/webhooks/subscriber_serializer_spec.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Api::V2::Platform::Webhooks::SubscriberSerializer do
- include_context 'API v2 serializers params'
-
- subject { described_class.new(resource, params: serializer_params).serializable_hash }
-
- let(:resource) { create(type) }
- let(:type) { :subscriber }
-
- it do
- expect(subject).to eq(
- data: {
- id: resource.id.to_s,
- type: type,
- attributes: {
- active: resource.active,
- created_at: resource.created_at,
- secret_key: resource.secret_key,
- subscriptions: resource.subscriptions,
- updated_at: resource.updated_at,
- url: resource.url
- }
- }
- )
- end
-end
diff --git a/packages/legacy_webhooks/spec/services/spree/products/queue_status_changed_webhook_spec.rb b/packages/legacy_webhooks/spec/services/spree/products/queue_status_changed_webhook_spec.rb
deleted file mode 100644
index c1526331d8..0000000000
--- a/packages/legacy_webhooks/spec/services/spree/products/queue_status_changed_webhook_spec.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Products::QueueStatusChangedWebhook, :job do
- subject { described_class.call(ids: products.map(&:id), event: event) }
- let!(:product) { create(:product) }
- let!(:products) { [product] }
- let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: ['*']) }
-
- shared_examples 'triggers change status webhooks' do |event|
- it 'triggers product activated webhooks' do
- queue_requests = instance_double(Spree::Webhooks::Subscribers::QueueRequests)
-
- allow(Spree::Webhooks::Subscribers::QueueRequests).to receive(:new).and_return(queue_requests)
- allow(queue_requests).to receive(:call)
- expect(queue_requests).to receive(:call).with(event_name: "product.#{event}", webhook_payload_body: anything, record: product)
-
- perform_enqueued_jobs do
- with_webhooks_enabled { Timecop.freeze { subject } }
- end
- end
- end
-
- context 'on activate' do
- let(:event) { 'activated' }
-
- it_behaves_like 'triggers change status webhooks', 'activated'
- end
-
- context 'on draft' do
- let(:event) { 'drafted' }
-
- it_behaves_like 'triggers change status webhooks', 'drafted'
- end
-
- context 'on archive' do
- let(:event) { 'archived' }
-
- it_behaves_like 'triggers change status webhooks', 'archived'
- end
-end
diff --git a/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/handle_request_spec.rb b/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/handle_request_spec.rb
deleted file mode 100644
index 00e35c6cf0..0000000000
--- a/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/handle_request_spec.rb
+++ /dev/null
@@ -1,219 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::Subscribers::HandleRequest do
- describe '#call' do
- subject do
- described_class.new(
- event_name: event_name,
- subscriber: subscriber,
- webhook_payload_body: webhook_payload_body
- )
- end
-
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::AddressSerializer.new(
- resource,
- include: Spree::Api::V2::Platform::AddressSerializer.relationships_to_serialize.keys
- ).serializable_hash.to_json
- end
- let(:event_name) { 'order.canceled' }
- let(:event) { Spree::Webhooks::Event.find_by(name: event_name, subscriber_id: subscriber.id, url: url) }
- let(:make_request_double) { instance_double(Spree::Webhooks::Subscribers::MakeRequest) }
- let(:subscriber) { create(:subscriber, :active, subscriptions: [event_name], url: url) }
- let(:url) { 'http://google.com/' }
- let(:resource) { create(:address) }
-
- shared_examples 'logging and creating a webhooks event' do |with_log_level:|
- before { stub_request(:post, url) }
-
- context 'before making the request' do
- let(:body_with_event_metadata) do
- JSON.parse(webhook_payload_body).merge(
- event_created_at: event.created_at,
- event_id: event.id,
- event_type: event.name
- ).to_json
- end
-
- let(:event_signature) do
- Spree::Webhooks::EventSignature.new(event, body_with_event_metadata).computed_signature
- end
-
- it 'debug logs' do
- allow(Rails.logger).to receive(:debug)
- allow(Spree::Webhooks::Subscribers::MakeRequest).to receive(:new).and_call_original
- subject.call
- message_fst = "[SPREE WEBHOOKS] 'order.canceled' sending to 'http://google.com/'"
- message_snd = "[SPREE WEBHOOKS] 'order.canceled' webhook_payload_body: #{body_with_event_metadata}"
- expect(Rails.logger).to have_received(:debug).with(message_fst)
- expect(Rails.logger).to have_received(:debug).with(message_snd)
-
- expect(Spree::Webhooks::Subscribers::MakeRequest).to \
- have_received(:new).
- with(
- signature: event_signature,
- url: url,
- webhook_payload_body: body_with_event_metadata
- )
- end
-
- it "#{with_log_level} logs" do
- # in this case the content of body isn't necessary
- expect(Spree::Webhooks::Subscribers::MakeRequest).to(
- receive(:new).with(hash_including(url: url)).and_return(make_request_double)
- )
- expect(make_request_double).to(
- receive_messages(
- execution_time: execution_time,
- response_code: response_code,
- success?: success,
- unprocessable_uri?: unprocessable_uri
- )
- )
- allow(Rails.logger).to receive(with_log_level)
- subject.call
- expect(Rails.logger).to have_received(with_log_level).with(log_msg) if log_msg
- end
- end
-
- it 'updates the event record created previously with the missing data' do
- expect(Spree::Webhooks::Subscribers::MakeRequest).to(
- receive(:new).with(hash_including(url: url)).and_return(make_request_double)
- )
- expect(make_request_double).to(
- receive_messages(
- execution_time: execution_time,
- response_code: response_code,
- success?: success,
- unprocessable_uri?: unprocessable_uri
- )
- )
- expect { subject.call }.to change {
- Spree::Webhooks::Event.
- all.
- as_json(except: %i[id created_at id preferences updated_at]).
- map(&:values)
- }.from(
- []
- ).to(
- [[execution_time, event_name, log_msg, response_code.to_s, subscriber.id, success, url]]
- )
- end
-
- it { expect(subject.call).to eq(nil) }
- end
-
- context 'with an unprocessable uri' do
- let(:execution_time) { 0 }
- let(:log_msg) { "[SPREE WEBHOOKS] 'order.canceled' can not make a request to 'http://google.com/'" }
- let(:response_code) { 0 }
- let(:success) { false }
- let(:unprocessable_uri) { true }
-
- it_behaves_like 'logging and creating a webhooks event', with_log_level: :warn
- end
-
- context 'with a processable uri' do
- let(:unprocessable_uri) { false }
-
- before do
- allow(make_request_double).to(
- receive_messages(
- failed_request?: failed_request
- )
- )
- end
-
- context 'with a failed request' do
- let(:execution_time) { 0 }
- let(:failed_request) { true }
- let(:log_msg) { "[SPREE WEBHOOKS] 'order.canceled' failed for 'http://google.com/'" }
- let(:response_code) { 0 }
- let(:success) { false }
-
- it_behaves_like 'logging and creating a webhooks event', with_log_level: :warn
- end
-
- context 'without a failed request' do
- let(:execution_time) { rand(1..999999) }
- let(:failed_request) { false }
- let(:log_msg) { nil }
- let(:response_code) { 200 }
- let(:success) { true }
-
- it_behaves_like 'logging and creating a webhooks event', with_log_level: :debug
- end
- end
-
- context 'full flow' do
- let(:webhook_payload_body) do
- Spree::Api::V2::Platform::OrderSerializer.new(
- order.reload,
- include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
- ).serializable_hash
- end
- let(:event) { Spree::Webhooks::Event.find_by(name: event_name, subscriber_id: subscriber.id, url: url) }
- let(:event_name) { 'order.placed' }
- let(:order) { create(:order, email: '[email protected]') }
-
- before do
- stub_request(:post, url)
- subscriber
- end
-
- it 'queues a job without event data on the webhook body right after the event is executed', :job do
- with_webhooks_enabled do
- order.finalize!
- expect(Spree::Webhooks::Subscribers::MakeRequestJob).to(
- have_been_enqueued.
- on_queue('spree_webhooks').
- with(webhook_payload_body.to_json, event_name, subscriber).
- once
- )
- end
- end
-
- context 'after executing the job' do
- let(:body_with_event_metadata) do
- webhook_payload_body.merge(
- event_created_at: event.created_at, event_id: event.id, event_type: event.name
- ).to_json
- end
-
- let(:event_signature) do
- Spree::Webhooks::EventSignature.new(event, body_with_event_metadata).computed_signature
- end
-
- it 'adds the event data to the body' do
- perform_enqueued_jobs(except: Spree::Addresses::GeocodeAddressJob) do
- with_webhooks_enabled do
- allow(Spree::Webhooks::Subscribers::MakeRequest).to receive(:new).and_call_original
- order.finalize!
- expect(Spree::Webhooks::Subscribers::MakeRequest).to \
- have_received(:new).
- with(
- signature: event_signature,
- url: url,
- webhook_payload_body: body_with_event_metadata
- )
- end
- end
- end
- end
- end
-
- context 'when the event can not be created' do
- it 'raises ActiveRecord::RecordInvalid' do
- with_webhooks_enabled do
- expect do
- described_class.new(
- webhook_payload_body: webhook_payload_body,
- event_name: nil, # forces a validation error
- subscriber: subscriber
- ).call
- end.to raise_error(ActiveRecord::RecordInvalid)
- end
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/make_request_spec.rb b/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/make_request_spec.rb
deleted file mode 100644
index 7cc6a6dbcc..0000000000
--- a/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/make_request_spec.rb
+++ /dev/null
@@ -1,229 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::Subscribers::MakeRequest do
- let(:http_double) { instance_double(Net::HTTP) }
- let(:url) { 'http://google.com/' }
- let(:webhook_payload_body) { { data: [{}] }.to_json }
- let(:signature) { 'some-signature-of-data' }
- let(:uri) { URI(url) }
- let(:http) { Net::HTTP.new(uri.host, uri.port) }
-
- describe '#execution_time' do
- subject { described_class.new(webhook_payload_body: webhook_payload_body, url: url, signature: signature).execution_time }
-
- before do
- stub_request(:post, url)
- allow(Process).to receive(:clock_gettime).and_call_original
- allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC).and_return(start_time, end_time)
- end
-
- let(:end_time) { start_time + rand_variation } # add a custom rand amount to simulate elapsed time
- let(:execution_time_in_seconds) { ((end_time - start_time) * 1000).to_i }
- let(:rand_variation) { rand(1..10) }
- let(:start_time) { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
-
- it 'returns the POSIX time it took for the request to be finished' do
- expect(subject).to eq(execution_time_in_seconds)
- end
-
- context 'when request raises an exception' do
- before do
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to receive(:use_ssl=).with(false)
- allow(http_double).to receive(:request) { raise Errno::ECONNREFUSED }
- end
-
- it 'returns the @execution_time_in_seconds default value' do
- expect(subject).to eq(0)
- end
- end
- end
-
- describe '#failed_request?' do
- subject { described_class.new(webhook_payload_body: webhook_payload_body, url: url, signature: signature).failed_request? }
-
- before { stub_request(:post, url) }
-
- let(:headers) { { 'Content-Type' => 'application/json' } }
-
- describe 'rescuing from known exceptions' do
- shared_examples 'rescues from' do |exception|
- before do
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to receive(:use_ssl=).with(false)
- allow(http_double).to receive(:request) { raise exception }
- end
-
- it "rescues from #{exception} and returns it is a failed request" do
- expect(subject).to eq(true)
- end
- end
-
- include_examples 'rescues from', Errno::ECONNREFUSED
- include_examples 'rescues from', Net::ReadTimeout
- include_examples 'rescues from', SocketError
- end
-
- context 'when the request status code is not 2xx' do
- before do
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to receive(:request).and_return(request_double)
- allow(http_double).to receive(:use_ssl=).with(false)
- allow(request_double).to receive(:code).and_return('304')
- end
-
- let(:request_double) { double }
-
- it { expect(subject).to eq(true) }
- end
-
- context 'when request status code is 2xx' do
- it { expect(subject).to eq(false) }
- end
- end
-
- describe '#use_ssl?' do
- subject { described_class.new(webhook_payload_body: webhook_payload_body, url: url, signature: signature).send(:use_ssl?) }
-
- context 'with HTTP scheme' do
- it { expect(subject).to eq(false) }
- end
-
- context 'with HTTPS scheme' do
- let(:url) { 'https://google.com/' }
-
- it { expect(subject).to eq(true) }
- end
- end
-
- describe 'HTTP request headers' do
- subject do
- described_class.new(
- signature: signature,
- url: url,
- webhook_payload_body: webhook_payload_body
- )
- end
-
- before do
- stub_request(:post, url)
- end
-
- it 'marks the HTTP request as a JSON request' do
- expect(subject.send(:request)).to \
- have_requested(:post, url).
- with(headers: { 'Content-Type' => 'application/json' })
- end
-
- it "includes the event's signature in the HTTP headers" do
- expect(subject.send(:request)).to \
- have_requested(:post, url).
- with(headers: { 'X-Spree-Hmac-SHA256' => signature })
- end
- end
-
- describe '#response_code' do
- subject { described_class.new(webhook_payload_body: webhook_payload_body, url: url, signature: signature).response_code }
-
- context 'when request raises an Errno::ECONNREFUSED exception' do
- before do
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to receive(:use_ssl=).with(false)
- allow(http_double).to receive(:request) { raise Errno::ECONNREFUSED }
- end
-
- it { expect(subject).to eq(0) }
- end
-
- context 'when request raises a Net::ReadTimeout exception' do
- before do
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to receive(:use_ssl=).with(false)
- allow(http_double).to receive(:request) { raise Net::ReadTimeout }
- end
-
- it { expect(subject).to eq(0) }
- end
-
- context 'when request raises a SocketError exception' do
- before do
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to receive(:use_ssl=).with(false)
- allow(http_double).to receive(:request) { raise SocketError }
- end
-
- it { expect(subject).to eq(0) }
- end
-
- context 'when request succeeds' do
- before { stub_request(:post, url) }
-
- it { expect(subject).to eq(200) }
- end
- end
-
- describe '#success?' do
- subject { described_class.new(webhook_payload_body: webhook_payload_body, url: url, signature: signature) }
-
- context 'when unprocessable_uri? equals true' do
- before { allow(subject).to receive(:unprocessable_uri?).and_return(true) }
-
- it { expect(subject.success?).to eq(false) }
- end
-
- context 'when failed_request? equals true' do
- before do
- allow(subject).to receive(:unprocessable_uri?).and_return(false)
- allow(subject).to receive(:failed_request?).and_return(true)
- end
-
- it { expect(subject.success?).to eq(false) }
- end
-
- context 'when failed_request? equals false' do
- before do
- allow(subject).to receive(:unprocessable_uri?).and_return(false)
- allow(subject).to receive(:failed_request?).and_return(false)
- end
-
- it { expect(subject.success?).to eq(true) }
- end
- end
-
- describe '#unprocessable_uri?' do
- subject { described_class.new(webhook_payload_body: webhook_payload_body, url: url, signature: signature) }
-
- before { allow(subject).to receive(:URI).with(url).and_return(uri) }
-
- shared_examples 'detects an unprocessable uri' do
- it { expect(subject.unprocessable_uri?).to eq(true) }
- end
-
- let(:url) { 'google.com' }
-
- context 'uri with path ""' do
- before { uri.path = '' }
-
- include_examples 'detects an unprocessable uri'
- end
-
- context 'uri with path "" and without host' do
- before do
- uri.path = ''
- uri.host = nil
- end
-
- include_examples 'detects an unprocessable uri'
- end
-
- context 'uri with path "", without host and without port' do
- before do
- uri.path = ''
- uri.host = nil
- uri.port = nil
- end
-
- include_examples 'detects an unprocessable uri'
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/queue_requests_spec.rb b/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/queue_requests_spec.rb
deleted file mode 100644
index 0d7b41e5f2..0000000000
--- a/packages/legacy_webhooks/spec/services/spree/webhooks/subscribers/queue_requests_spec.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Webhooks::Subscribers::QueueRequests, :job, :spree_webhooks do
- describe '#call' do
- subject { described_class.call(event_name: event_name, webhook_payload_body: webhook_payload_body) }
-
- let(:event_name) { 'order.finalize' }
- let(:webhook_payload_body) { {} }
- let(:queue) { 'spree_webhooks' }
- let(:make_request_job) { Spree::Webhooks::Subscribers::MakeRequestJob }
-
- shared_examples 'queues a job to make a request' do |url|
- before { stub_request(:post, subscriber.url) }
-
- let(:subscriber) { create(:subscriber, :active, url: url, subscriptions: subscriptions) }
-
- it do
- expect { subject }.to(
- have_enqueued_job(make_request_job).with(webhook_payload_body, event_name, subscriber).on_queue(queue)
- )
- end
- end
-
- shared_examples 'does not queue a job to make a request' do
- it { expect { subject }.not_to have_enqueued_job(make_request_job).on_queue(queue) }
- end
-
- context 'without subscriptions for the given event' do
- before { Spree::Webhooks::Subscriber.delete_all }
-
- include_examples 'does not queue a job to make a request'
- end
-
- context 'with subscriptions for the given event' do
- context 'when subscriber subscriptions includes all events (*)' do
- let(:subscriptions) { ['*'] }
-
- include_examples 'queues a job to make a request', 'https://url1.com/'
- end
-
- context 'when subscriber subscriptions includes the specific event being used' do
- let(:subscriptions) { [event_name] }
-
- include_examples 'queues a job to make a request', 'https://url2.com/'
- end
-
- context 'when subscriber subscriptions are not active' do
- let!(:subscriber) do
- create(:subscriber, url: 'https://url3.com/', subscriptions: [event_name])
- end
-
- include_examples 'does not queue a job to make a request'
- end
-
- context 'when subscriber subscriptions do not include the event or "*"' do
- let!(:subscriber) do
- create(:subscriber, :active, url: 'https://url4.com/', subscriptions: ['order.resumed'])
- end
-
- include_examples 'does not queue a job to make a request'
- end
- end
- end
-end
diff --git a/packages/legacy_webhooks/spec/spec_helper.rb b/packages/legacy_webhooks/spec/spec_helper.rb
deleted file mode 100644
index b39de1c6d0..0000000000
--- a/packages/legacy_webhooks/spec/spec_helper.rb
+++ /dev/null
@@ -1,126 +0,0 @@
-if ENV['COVERAGE']
- # Run Coverage report
- require 'simplecov'
- SimpleCov.start 'rails' do
- add_group 'Serializers', 'app/serializers'
- add_group 'Libraries', 'lib/spree'
-
- add_filter '/bin/'
- add_filter '/db/'
- add_filter '/script/'
- add_filter '/spec/'
- add_filter '/lib/spree/api/testing_support/'
-
- coverage_dir "#{ENV['COVERAGE_DIR']}/api_"+ ENV.fetch('CIRCLE_NODE_INDEX', 0) if ENV['COVERAGE_DIR']
- command_name "test_" + ENV.fetch('CIRCLE_NODE_INDEX', 0)
-
- end
-end
-
-# This file is copied to spec/ when you run 'rails generate rspec:install'
-ENV['RAILS_ENV'] ||= 'test'
-
-begin
- require File.expand_path('../dummy/config/environment', __FILE__)
-rescue LoadError
- puts 'Could not load dummy application. Please ensure you have run `bundle exec rake test_app`'
- exit
-end
-
-require 'rspec/rails'
-require 'database_cleaner/active_record'
-require 'ffaker'
-require 'webmock/rspec'
-require 'i18n/tasks'
-
-# Requires supporting ruby files with custom matchers and macros, etc,
-# in spec/support/ and its subdirectories.
-Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f }
-
-require 'spree/testing_support/authorization_helpers'
-require 'spree/testing_support/factories'
-require 'spree/testing_support/jobs'
-require 'spree/testing_support/store'
-require 'spree/testing_support/preferences'
-require 'spree/testing_support/image_helpers'
-require 'spree/testing_support/next_instance_of'
-require 'spree/testing_support/rspec_retry_config'
-require 'spree/testing_support/controller_requests'
-require 'spree/testing_support/url_helpers'
-require 'spree/admin/testing_support/capybara_utils'
-require 'spree/admin/testing_support/tom_select'
-require 'spree/testing_support/capybara_config'
-require 'spree/testing_support/rspec_retry_config'
-
-require 'spree/api/testing_support/serializers'
-require 'spree/api/testing_support/v2/base'
-require 'spree/api/testing_support/v2/current_order'
-require 'spree/api/testing_support/v2/platform_contexts'
-require 'spree/api/testing_support/v2/serializers_params'
-require 'spree/api/testing_support/factories'
-
-require 'spree/legacy_webhooks/testing_support/matchers/webhooks'
-require 'spree/legacy_webhooks/testing_support/factories'
-
-require 'spree/core/controller_helpers/strong_parameters'
-
-def json_response
- case body = JSON.parse(response.body)
- when Hash
- body.with_indifferent_access
- when Array
- body
- end
-end
-
-RSpec.configure do |config|
- config.backtrace_exclusion_patterns = [/gems\/activesupport/, /gems\/actionpack/, /gems\/rspec/]
- config.color = true
- config.default_formatter = 'doc'
- config.fail_fast = ENV['FAIL_FAST'] || false
- config.infer_spec_type_from_file_location!
- config.raise_errors_for_deprecations!
- config.use_transactional_fixtures = true
-
- config.include JSONAPI::RSpec
- config.include FactoryBot::Syntax::Methods
- config.include Spree::TestingSupport::Preferences
- config.include Spree::TestingSupport::ImageHelpers
-
- config.include Spree::Admin::TestingSupport::CapybaraUtils
- config.include Spree::Admin::TestingSupport::TomSelect
- config.include Spree::TestingSupport::Preferences
- config.include Spree::TestingSupport::UrlHelpers
- config.include Spree::TestingSupport::ControllerRequests, type: :controller
-
- config.before(:each, :spree_webhooks) do
- Spree::LegacyWebhooks.disabled = false
- end
-
- config.after(:each, :spree_webhooks) do
- Spree::LegacyWebhooks.disabled = true
- end
-
- config.before(:suite) do
- # Clean out the database state before the tests run
- DatabaseCleaner.strategy = :transaction
- DatabaseCleaner.clean_with(:truncation)
- end
-
- config.before do
- Spree::LegacyWebhooks.disabled = true
- reset_spree_preferences
-
- # Request specs to paths with ?locale=xx don't reset the locale afterwards
- # Some tests assume that the current locale is :en, so we ensure it here
- I18n.locale = :en
- end
-
- config.around(:each) do |example|
- DatabaseCleaner.cleaning do
- example.run
- end
- end
-
- config.order = :random
-end
diff --git a/packages/legacy_webhooks/spec/zeitwerk_spec.rb b/packages/legacy_webhooks/spec/zeitwerk_spec.rb
deleted file mode 100644
index 59b6c709b0..0000000000
--- a/packages/legacy_webhooks/spec/zeitwerk_spec.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-require 'spec_helper'
-
-describe 'Zeitwerk' do
- it 'eager loads all files' do
- expect do
- Zeitwerk::Loader.eager_load_all
- end.not_to raise_error
- end
-end
diff --git a/packages/legacy_webhooks/spree_legacy_webhooks.gemspec b/packages/legacy_webhooks/spree_legacy_webhooks.gemspec
deleted file mode 100644
index a0da4f9702..0000000000
--- a/packages/legacy_webhooks/spree_legacy_webhooks.gemspec
+++ /dev/null
@@ -1,31 +0,0 @@
-# frozen_string_literal: true
-
-require_relative '../../core/lib/spree/core/version.rb'
-
-Gem::Specification.new do |s|
- s.platform = Gem::Platform::RUBY
- s.name = 'spree_legacy_webhooks'
- s.version = Spree.version
- s.authors = ['Spark Solutions Sp. z o.o.', 'Vendo Connect Inc.']
- s.email = '[email protected]'
- s.summary = 'Legacy webhooks system for Spree Commerce'
- s.description = 'HTTP webhooks for Spree Commerce using the legacy callback-based system. Consider migrating to the new event-based webhooks in spree_api.'
- s.homepage = 'https://spreecommerce.org'
- s.licenses = ['AGPL-3.0-or-later', 'BSD-3-Clause']
-
- s.metadata = {
- "bug_tracker_uri" => "https://github.com/spree/spree/issues",
- "changelog_uri" => "https://github.com/spree/spree/releases/tag/v#{s.version}",
- "documentation_uri" => "https://docs.spreecommerce.org/",
- "source_code_uri" => "https://github.com/spree/spree/tree/v#{s.version}",
- }
-
- s.required_ruby_version = '>= 3.2'
-
- s.files = Dir["{app,config,db,lib}/**/*", "LICENSE.md", "Rakefile", "README.md"].reject { |f| f.match(/^spec/) && !f.match(/^spec\/fixtures/) }
- s.require_path = 'lib'
-
- s.add_dependency 'spree_core', ">= #{s.version}"
- s.add_dependency 'spree_api', ">= #{s.version}"
- s.add_dependency 'spree_admin', ">= #{s.version}"
-end