Skip to content

Commit

Permalink
Refactor recreate webhooks logic (#1686)
Browse files Browse the repository at this point in the history
* add session to block

* Use ShopifyApp::WebhooksManager methods to recreate_webhooks

* properly setup test

* changelog
  • Loading branch information
nelsonwittwer committed Apr 27, 2023
1 parent 5aa51c4 commit dd47eb8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Unreleased
* Set `access_scopes` column to string by default [#1636](https://github.com/Shopify/shopify_app/pull/1636)
* Fixes a bug with `EnsureBilling` causing infinite redirect in embedded apps [#1578](https://github.com/Shopify/shopify_app/pull/1578)
* Modifies SessionStorage#with_shopify_session to call a block with a supplied session instance [#1488](https://github.com/Shopify/shopify_app/pull/1488)
* Refactors `ShopifyApp::WebhhooksManager#recreate_webhooks!` to have a uniform webhook inventory that doesn't clash with the API library. Updates webhook generator to use supplied session. [#1686](https://github.com/Shopify/shopify_app/pull/1686)

21.4.1 (Feb 21, 2023)
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class <%= @job_class_name %> < ActiveJob::Base

if shop.nil?
logger.error("#{self.class} failed: cannot find shop with domain '#{shop_domain}'")

raise ActiveRecord::RecordNotFound, "Shop Not Found"
end

shop.with_shopify_session do
shop.with_shopify_session do |session|
end
end
end
4 changes: 1 addition & 3 deletions lib/shopify_app/managers/webhooks_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ def recreate_webhooks!(session:)
destroy_webhooks(session: session)
return unless ShopifyApp.configuration.has_webhooks?

add_registrations

ShopifyApp::Logger.debug("Recreating webhooks")
ShopifyAPI::Webhooks::Registry.register_all(session: session)
add_registrations
end

def destroy_webhooks(session:)
Expand Down
24 changes: 3 additions & 21 deletions test/shopify_app/managers/webhooks_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,15 @@ class ShopifyApp::WebhooksManagerTest < ActiveSupport::TestCase
end

test "#recreate_webhooks! destroys all webhooks and recreates" do
ShopifyApp.configuration.expects(:has_webhooks?).returns(true)
session = ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com")

ShopifyAPI::Webhooks::Registry.expects(:register_all)
ShopifyAPI::Webhooks::Registry.expects(:unregister).with(topic: "orders/updated", session: session)
ShopifyApp::WebhooksManager.expects(:add_registrations).twice
ShopifyApp::WebhooksManager.expects(:destroy_webhooks)
ShopifyApp::WebhooksManager.expects(:add_registrations)

ShopifyApp.configure do |config|
config.webhooks = [
{ topic: "orders/updated", path: "webhooks" },
]
end
ShopifyApp::WebhooksManager.add_registrations
ShopifyApp::WebhooksManager.recreate_webhooks!(session: session)
end

test "#recreate_webhooks! does not call unregister if there is no webhook" do
ShopifyAPI::Webhooks::Registry.expects(:register_all).never
ShopifyAPI::Webhooks::Registry.expects(:unregister).never
ShopifyAPI::Webhooks::Registry.expects(:add_registration).never

ShopifyApp.configure do |config|
config.webhooks = []
end
ShopifyApp::WebhooksManager.add_registrations
ShopifyApp::WebhooksManager.recreate_webhooks!(session: ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com"))
end

test "#destroy_webhooks destroy all webhooks" do
session = ShopifyAPI::Auth::Session.new(shop: "shop.myshopify.com")
ShopifyAPI::Webhooks::Registry.expects(:unregister).with(topic: "orders/updated", session: session)
Expand Down

0 comments on commit dd47eb8

Please sign in to comment.