How to authenticate requests for RSpec tests in shopify_app gem v19+

Viewed 21

I am currently trying to update the shopify_app gem to v19+ which includes a forced upgrade of the shopify_api gem to version 10+.

Unfortunately, with this update, all RSpec tests that involve authentication break, usually with a 302 redirect error.

The following is the login method that could be used successfully in version 18x and below.

module AuthHelper
  def login(shop)
    OmniAuth.config.test_mode = true
    # recommended to enable /auth/shopify and bypass devise override
    OmniAuth.config.allowed_request_methods = [:get]

    # silences omniauth noise telling me that the above line opens us up to vulnerabilities
    # OmniAuth.config.silence_get_warning = true

    OmniAuth.config.logger = Rails.logger
    OmniAuth.config.add_mock(
      :shopify,
      provider: 'shopify',
      uid: shop.shopify_domain,
      credentials: { token: shop.shopify_token },
      extra: { scope: ShopifyApp.configuration.scope }
    )

    # the next 3 lines are technically setting `request.env['key']` on a request that
    # has not yet been made.
    Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:shopify]
    Rails.application.env_config['omniauth.params'] = { shop: shop.shopify_domain }
    Rails.application.env_config['jwt.shopify_domain'] = shop.shopify_domain

    get '/app/auth/shopify/callback', params: { shop: shop.shopify_domain }

    mock_shopify_session(shop)
  end

  def mock_shopify_session(shop)
    session = ShopifyAPI::Session.new(
      domain: shop.shopify_domain,
      token: shop.shopify_token,
      api_version: ShopifyApp.configuration.api_version,
      access_scopes: shop.access_scopes
    )
    ShopifyAPI::Base.activate_session(session)
  end
end

Does anybody have any idea how to get RSpec request tests to pass in version 19+?

I've posted this question in the Shopify Partners Slack community and in the Shopify discussion board (https://community.shopify.com/c/shopify-apis-and-sdks/how-to-authenticate-shopify-app-v19-for-spec-tests/td-p/1680400)

I've also posted on this related issue in the shopify_app gem: https://github.com/Shopify/shopify_app/issues/1266

So far I haven't had any replies in any of those threads so I hope I have more luck here.

0 Answers
Related