Testing Rails Shopify Authenticated Controller

Highlighted
Shopify Partner
7 0 1

I've been stuck on this for a while - and any help would be great!

 

This is my Rspec test - which is supposed to login the Shop - and after login the home page should give a status code of 200. But for some reason, I am not able to login .. Instead when I visit the home page (even after logging in) it is always redirecting to the login page. I am doing soemthing wrong- and wondering if anyone can help with this. 

 

require 'spec_helper'

RSpec.describe "Onboarding", type: :request do

    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"
      follow_redirect!
    end

    describe "GET /" do
      it "Products return with a HTTP status 200" do
        shop = Fabricate(:shop)
        login(shop)
       

        @request.session[:shopify] = shop.id
        @request.session[:shopify_domain] = shop.shopify_domain
        
        
        get '/'
        expect(response).to have_http_status(200)
      end
    end
  end

 

I took this code from http://www.codeshopify.com/blog_posts/testing-shopify-authenticated-controllers-with-rspec-rails but am still unable to login. Please help. Thanks!

0 Likes
Highlighted
Shopify Partner
7 0 1

Got it working! Made a stupid mistake. While fabricating the shop - the token was wrong. Works now. 


@arjunrajkumar wrote:

I've been stuck on this for a while - and any help would be great!

 

This is my Rspec test - which is supposed to login the Shop - and after login the home page should give a status code of 200. But for some reason, I am not able to login .. Instead when I visit the home page (even after logging in) it is always redirecting to the login page. I am doing soemthing wrong- and wondering if anyone can help with this. 

 

require 'spec_helper'

RSpec.describe "Onboarding", type: :request do

    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"
      follow_redirect!
    end

    describe "GET /" do
      it "Products return with a HTTP status 200" do
        shop = Fabricate(:shop)
        login(shop)
       

        @request.session[:shopify] = shop.id
        @request.session[:shopify_domain] = shop.shopify_domain
        
        
        get '/'
        expect(response).to have_http_status(200)
      end
    end
  end

 

I took this code from http://www.codeshopify.com/blog_posts/testing-shopify-authenticated-controllers-with-rspec-rails but am still unable to login. Please help. Thanks!


 

1 Like
Highlighted
New Member
1 0 0
RSpec.describe ShopSettingsController, type: :controller do

shop_settings_attributes = {
global_inventory_limit: 5,
alert_frequency: 10,
shop_id: 1,
multiple_emails: true,
admin_email: 'abc@gmail.com'
}

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"
follow_redirect!
end

describe 'GET #index' do
it 'returns success response' do
shop = Fabricate(:shop)
login(shop)
@request.session[:shopify] = shop.id
@request.session[:shopify_domain] = shop.shopify_domain
get :index
expect(response.status).to eq(200)
end
end
Above is my code
I am following your code but still it is throwing a error 

ActionController::UrlGenerationError:

       No route matches {:action=>"/auth/shopify", :controller=>"shop_settings"}

Can you help me to figure it out @arjunrajkumar sir

0 Likes
Highlighted
New Member
5 0 0
require 'rails_helper'

RSpec.describe "Products", type: :request do
  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"
    follow_redirect!
  end

  it "return with a HTTP status 200" do
    shop = FactoryBot.create(:shop)
    login(shop)
    @request.session[:shopify] = shop.id
    @request.session[:shopify_domain] = shop.shopify_domain
    get '/'

    # byebug
    expect(response).to have_http_status(200)
  end
end

Hi,

 

I'm new to shopify development, currently also facing this issue. I have the following codes for my spec. However when I byebug and log response.

$ response.status => 302

$ response.body => "<html><body>You are being <a href=\"https://www.example.com/login\">redirected</a>.</body></html>"

I follow from this and also from this. http://www.codeshopify.com/blog_posts/testing-shopify-authenticated-controllers-with-rspec-rails. Am I missing some setup else where?

 

Thanks for assistance.

0 Likes