From 3b0647041e88baedf51af370786b866ac3300968 Mon Sep 17 00:00:00 2001 From: rezaansyed <60748788+rezaansyed@users.noreply.github.com> Date: Wed, 20 Jan 2021 09:54:51 -0500 Subject: [PATCH] Merge pull request #1151 from Shopify/fix-failing-post-perform-jobs Ensure offline and online tokens are fetched via available auth hash --- app/controllers/shopify_app/callback_controller.rb | 12 ++++++++++-- test/controllers/callback_controller_test.rb | 11 ++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/controllers/shopify_app/callback_controller.rb b/app/controllers/shopify_app/callback_controller.rb index 17cb39886..924046a66 100644 --- a/app/controllers/shopify_app/callback_controller.rb +++ b/app/controllers/shopify_app/callback_controller.rb @@ -95,6 +95,14 @@ def shop_name auth_hash.uid end + def offline_access_token + ShopifyApp::SessionRepository.retrieve_shop_session_by_shopify_domain(shop_name)&.token + end + + def online_access_token + ShopifyApp::SessionRepository.retrieve_user_session_by_shopify_user_id(associated_user_id)&.token + end + def associated_user return unless auth_hash.dig('extra', 'associated_user').present? @@ -138,7 +146,7 @@ def install_webhooks WebhooksManager.queue( shop_name, - shop_session&.token || user_session.token, + offline_access_token || online_access_token, ShopifyApp.configuration.webhooks ) end @@ -148,7 +156,7 @@ def install_scripttags ScripttagsManager.queue( shop_name, - shop_session&.token || user_session.token, + offline_access_token || online_access_token, ShopifyApp.configuration.scripttags ) end diff --git a/test/controllers/callback_controller_test.rb b/test/controllers/callback_controller_test.rb index ff51edeca..99c28a0d7 100644 --- a/test/controllers/callback_controller_test.rb +++ b/test/controllers/callback_controller_test.rb @@ -199,7 +199,9 @@ class CallbackControllerTest < ActionController::TestCase test '#install_webhooks uses the shop token for shop strategy' do shop_session = ShopifyAPI::Session.new(domain: 'shop', token: '1234', api_version: '2019-1') - ShopifyApp::SessionRepository.expects(:retrieve_shop_session).returns(shop_session) + ShopifyApp::SessionRepository + .expects(:retrieve_shop_session_by_shopify_domain) + .returns(shop_session) ShopifyApp.configure do |config| config.webhooks = [{ topic: 'carts/update', address: 'example-app.com/webhooks' }] end @@ -212,7 +214,9 @@ class CallbackControllerTest < ActionController::TestCase test '#install_webhooks still uses the shop token for user strategy' do shop_session = ShopifyAPI::Session.new(domain: 'shop', token: '4321', api_version: '2019-1') - ShopifyApp::SessionRepository.stubs(:retrieve_shop_session).with('135').returns(shop_session) + ShopifyApp::SessionRepository.stubs(:retrieve_shop_session_by_shopify_domain) + .with(TEST_SHOPIFY_DOMAIN) + .returns(shop_session) ShopifyApp.configure do |config| config.webhooks = [{ topic: 'carts/update', address: 'example-app.com/webhooks' }] @@ -227,7 +231,8 @@ class CallbackControllerTest < ActionController::TestCase test '#install_webhooks falls back to user token for user strategy if shop is not in session' do user_session = ShopifyAPI::Session.new(domain: 'shop', token: '4321', api_version: '2019-1') - ShopifyApp::SessionRepository.expects(:retrieve_user_session).returns(user_session).times(2) + ShopifyApp::SessionRepository.expects(:retrieve_shop_session_by_shopify_domain).returns(nil) + ShopifyApp::SessionRepository.expects(:retrieve_user_session_by_shopify_user_id).returns(user_session) ShopifyApp.configure do |config| config.webhooks = [{ topic: 'carts/update', address: 'example-app.com/webhooks' }] end