Skip to content

Commit

Permalink
Merge pull request #1151 from Shopify/fix-failing-post-perform-jobs
Browse files Browse the repository at this point in the history
Ensure offline and online tokens are fetched via available auth hash
  • Loading branch information
rezaansyed committed Jan 20, 2021
2 parents 80c9f91 + d779e0a commit 88f42f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 10 additions & 2 deletions app/controllers/shopify_app/callback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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?

Expand Down Expand Up @@ -132,7 +140,7 @@ def install_webhooks

WebhooksManager.queue(
shop_name,
shop_session&.token || user_session.token,
offline_access_token || online_access_token,
ShopifyApp.configuration.webhooks
)
end
Expand All @@ -142,7 +150,7 @@ def install_scripttags

ScripttagsManager.queue(
shop_name,
shop_session&.token || user_session.token,
offline_access_token || online_access_token,
ShopifyApp.configuration.scripttags
)
end
Expand Down
11 changes: 8 additions & 3 deletions test/controllers/callback_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' }]
Expand All @@ -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
Expand Down

0 comments on commit 88f42f0

Please sign in to comment.