My project is a Rails 5.2 app, running Ruby 2.6, and uses the shopify_gem
and factory_bot_rails
.
I have a controller that inherits from ShopifyController
. My unit tests for controllers are stuck at a 302. I’m unable to figure out how to get past authentication…
I’ve tried these tutorials and other links, but no luck:
- http://www.codeshopify.com/blog_posts/testing-shopify-authenticated-controllers-with-rspec-rails
- https://community.shopify.com/c/Shopify-APIs-SDKs/Testing-a-Rails-app-created-through-shopify-app-gem/td-p/337337
- https://github.com/Shopify/shopify_app/issues/445
- https://github.com/Shopify/shopify_app/issues/731
My controller test is below
require 'rails_helper'
describe OnboardingController, type: :controller do
before do
shop = FactoryBot.create(:shop)
request.env['rack.url_scheme'] = 'https'
@request.session[:shopify] = shop.id
@request.session[:shopify_domain] = shop.shopify_domain
end
it 'onboards correctly', :focus do
get :onboard_completed
expect(response).to have_http_status(:success)
end
end
I was also playing with this code, but it failed (errors in comments):
module ShopifyHelper
def login(shop)
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:shopify,
provider: 'shopify',
uid: shop.shopify_domain,
credentials: { token: shop.shopify_token })
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:shopify]
get "/auth/shopify" # this leads to a UrlGenerationError
follow_redirect! # this is an undefined method. Seems to be a MiniTest thing
end
end
3
Answers
This ended up being the working solution
Something like this works for me, your getting the errors in your function probably because you do not have get and follow_redirect! functions defined in your ShopifyHelper module context.
Reference: http://www.codeshopify.com/blog_posts/testing-shopify-authenticated-controllers-with-rspec-rails
This worked for me:
Btw, testing controllers are deprecated in favour of requests.
Remember to setup "admin { true }" in your shop’s FactoryBot if you are using the ‘shopify_app’ gem.