Skip to content

Commit

Permalink
Extract post authenticate tasks into a class to be reused
Browse files Browse the repository at this point in the history
  • Loading branch information
zzooeeyy committed Mar 27, 2024
1 parent 5c51437 commit 31f7477
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 27 deletions.
28 changes: 1 addition & 27 deletions app/controllers/shopify_app/callback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,33 +138,7 @@ def user_access_scopes_strategy
end

def perform_post_authenticate_jobs(session)
# Ensure we use the shop session to install webhooks
session_for_shop = session.online? ? shop_session : session

install_webhooks(session_for_shop)

perform_after_authenticate_job(session)
end

def install_webhooks(session)
return unless ShopifyApp.configuration.has_webhooks?

WebhooksManager.queue(session.shop, session.access_token)
end

def perform_after_authenticate_job(session)
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
ShopifyApp::Auth::PostAuthenticateTasks.perform(session)
end
end
end
3 changes: 3 additions & 0 deletions lib/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def self.use_webpacker?
require "shopify_app/controller_concerns/webhook_verification"
require "shopify_app/controller_concerns/token_exchange"

# Auth helpers
require "shopify_app/auth/post_authenticate_tasks"

# jobs
require "shopify_app/jobs/webhooks_manager_job"

Expand Down
48 changes: 48 additions & 0 deletions lib/shopify_app/auth/post_authenticate_tasks.rb
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
67 changes: 67 additions & 0 deletions test/shopify_app/auth/post_authenticate_tasks_test.rb
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

0 comments on commit 31f7477

Please sign in to comment.