-
Notifications
You must be signed in to change notification settings - Fork 684
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract post authenticate tasks into a class to be reused
- Loading branch information
Showing
4 changed files
with
119 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module ShopifyApp | ||
module Auth | ||
class PostAuthenticateTasks | ||
class << self | ||
def perform(session) | ||
ShopifyApp::Logger.debug("Performing post authenticate tasks") | ||
# Ensure we use the shop session to install webhooks | ||
session_for_shop = session.online? ? shop_session(session) : session | ||
|
||
install_webhooks(session_for_shop) | ||
|
||
perform_after_authenticate_job(session) | ||
end | ||
|
||
private | ||
|
||
def shop_session(session) | ||
ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(session.shop) | ||
end | ||
|
||
def install_webhooks(session) | ||
ShopifyApp::Logger.debug("PostAuthenticateTasks: Installing webhooks") | ||
return unless ShopifyApp.configuration.has_webhooks? | ||
|
||
WebhooksManager.queue(session.shop, session.access_token) | ||
end | ||
|
||
def perform_after_authenticate_job(session) | ||
ShopifyApp::Logger.debug("PostAuthenticateTasks: Performing after_authenticate_job") | ||
config = ShopifyApp.configuration.after_authenticate_job | ||
|
||
return unless config && config[:job].present? | ||
|
||
job = config[:job] | ||
job = job.constantize if job.is_a?(String) | ||
|
||
if config[:inline] == true | ||
job.perform_now(shop_domain: session.shop) | ||
else | ||
job.perform_later(shop_domain: session.shop) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
module Shopify | ||
class AfterAuthenticateJob < ActiveJob::Base | ||
def perform; end | ||
end | ||
end | ||
|
||
class CartsUpdateJob < ActiveJob::Base | ||
extend ShopifyAPI::Webhooks::Handler | ||
|
||
class << self | ||
def handle(topic:, shop:, body:) | ||
perform_later(topic: topic, shop_domain: shop, webhook: body) | ||
end | ||
end | ||
|
||
def perform; end | ||
end | ||
|
||
class PostAuthenticateTasksTest < ActiveSupport::TestCase | ||
SHOP_DOMAIN = "shop.myshopify.io" | ||
|
||
setup do | ||
ShopifyApp::SessionRepository.shop_storage = ShopifyApp::InMemoryShopSessionStore | ||
ShopifyApp::SessionRepository.user_storage = ShopifyApp::InMemoryShopSessionStore | ||
ShopifyAppConfigurer.setup_context | ||
|
||
@offline_session = ShopifyAPI::Auth::Session.new(shop: SHOP_DOMAIN, access_token: "offline_token") | ||
@online_session = ShopifyAPI::Auth::Session.new(shop: SHOP_DOMAIN, access_token: "online_token", is_online: true) | ||
|
||
ShopifyApp::SessionRepository.store_shop_session(@offline_session) | ||
end | ||
|
||
test "#perform triggers install_webhook job after authentication" do | ||
ShopifyApp.configure do |config| | ||
config.webhooks = [{ topic: "carts/update", address: "example-app.com/webhooks" }] | ||
end | ||
|
||
ShopifyApp::WebhooksManager.expects(:queue).with(SHOP_DOMAIN, "offline_token") | ||
|
||
ShopifyApp::Auth::PostAuthenticateTasks.perform(@offline_session) | ||
end | ||
|
||
test "#perform triggers install_webhook job with an offline session after an online session OAuth" do | ||
ShopifyApp.configure do |config| | ||
config.webhooks = [{ topic: "carts/update", address: "example-app.com/webhooks" }] | ||
end | ||
ShopifyApp::WebhooksManager.expects(:queue).with(SHOP_DOMAIN, "offline_token") | ||
|
||
ShopifyApp::Auth::PostAuthenticateTasks.perform(@online_session) | ||
ensure | ||
ShopifyApp::SessionRepository.shop_storage.clear | ||
end | ||
|
||
test "#perform triggers after_authenticate job after authentication" do | ||
ShopifyApp.configure do |config| | ||
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true } | ||
end | ||
|
||
Shopify::AfterAuthenticateJob.expects(:perform_now).with(shop_domain: SHOP_DOMAIN) | ||
|
||
ShopifyApp::Auth::PostAuthenticateTasks.perform(@offline_session) | ||
end | ||
end |