Skip to content

Instantly share code, notes, and snippets.

@aarkerio
Last active May 3, 2021 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarkerio/c1d085918b064df94a4228d59ef6c4bb to your computer and use it in GitHub Desktop.
Save aarkerio/c1d085918b064df94a4228d59ef6c4bb to your computer and use it in GitHub Desktop.
Rails rspec shopify login
def shopify_login(shop)
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:shopify, provider: 'shopify', uid: shop.myshopify_domain,
credentials: { token: shop.api_token })
Rails.application.env_config['omniauth.auth'] = OmniAuth.config.mock_auth[:shopify]
get "/auth/shopify/callback?shop=#{shop.myshopify_domain}"
follow_redirect!
@request.session[:shopify] = shop.shopify_id
@request.session[:shop_id] = shop.id
@request.session[:shopify_domain] = shop.myshopify_domain
end
# FactoryBot File:
# frozen_string_literal: true
FactoryBot.define do
factory :shop do
initialize_with {
Shop.where(myshopify_domain: 'my-dev-store.myshopify.com')
.first_or_create(attributes)
}
myshopify_domain { 'my-dev-store.myshopify.com' }
name { 'my-dev-store' }
shopify_id { 43_462_361_243 }
email { 'foo@foigital.com' }
api_token { 'shpat_7db7e3fXXXXXXXb' }
admin { true }
installed_at { Time.now.utc }
app { 'incartupsell' }
trait :with_products do
after(:create) do |shop, _evaluator|
FactoryBot.create_list(:product, 5, shop: shop)
end
end
end
end
# Finally Shop Request test:
RSpec.describe 'ShopsController', type: :request do
let(:shop) { FactoryBot.build :shop }
let(:plan) { FactoryBot.build :enterprise_plan }
let(:subscription) { FactoryBot.create :subscription, shop: shop, plan: plan }
describe 'GET#product_search' do
it 'returns a successful 200 response for listing action do' do
VCR.use_cassette('shop-search-product', record: :new_episodes) do
new_subscrip = subscription
shopify_login(new_subscrip.shop)
get product_search_path, { params: { query: 'bike' } }
json = JSON.parse(response.body)
expect(response).to be_successful
expect(json.length).to eq(7)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment