Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created February 28, 2012 05:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agmcleod/1929816 to your computer and use it in GitHub Desktop.
Save agmcleod/1929816 to your computer and use it in GitHub Desktop.
shopify api integration testing
require 'spec_helper'
describe "home" do
before do
@domain = "myshop.myshopify.com"
@token = SecureRandom.hex(16)
@shopify_session = ShopifyAPI::Session.new(@domain, @token)
end
context "not logged in" do
it "should be at login" do
visit "/"
page.should have_content("Install this app in a shop to get access to its private admin data")
end
describe "sign in" do
before do
@shopify_session.should_receive(:valid?).and_return(true)
ShopifyAPI::Session.should_receive(:new).and_return(@shopify_session)
ShopifyAPI::Session.any_instance.should_receive(:create_permission_url).and_return("/login/finalize?shop=#{@domain}&t=#{@token}")
end
it "should contain content with 'Add new charity" do
visit "/"
fill_in "shop", with: @domain
click_button "Install"
page.should have_content("Add new charity")
end
end
end
context "logged in" do
before do
page.set_rack_session(:shopify => ShopifyAPI::Session.new(@domain, @token))
end
it "should contain content with 'Add new charity" do
visit "/"
page.should have_content("Add new charity")
end
end
end
class LoginController < ApplicationController
def index
# Ask user for their #{shop}.myshopify.com address
# If the #{shop}.myshopify.com address is already provided in the URL, just skip to #authenticate
if params[:shop].present?
redirect_to authenticate_path(:shop => params[:shop])
end
end
def authenticate
@shopify_session = ShopifyAPI::Session.new(params[:shop].to_s.strip)
if params[:shop].present?
redirect_to @shopify_session.create_permission_url
else
redirect_to return_address
end
end
# Shopify redirects the logged-in user back to this action along with
# the authorization token t.
#
# This token is later combined with the developer's shared secret to form
# the password used to call API methods.
def finalize
Rails.logger.debug "Session finalize"
@shopify_session = ShopifyAPI::Session.new(params[:shop], params[:t], params)
if @shopify_session.valid?
session[:shopify] = @shopify_session
flash[:notice] = "Logged in to shopify store."
shop = Shop.find_or_create_by_url_and_api_url(url: @shopify_session.url, api_url: @shopify_session.site)
Rails.logger.debug "shop set"
session[:return_to] = nil
redirect_to root_url
else
Rails.logger.debug "Session invalid"
flash[:error] = "Could not log in to Shopify store."
redirect_to :action => 'index'
end
end
def logout
session[:shopify] = nil
flash[:notice] = "Successfully logged out."
redirect_to :action => 'index'
end
protected
def return_address
session[:return_to] || root_url
end
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/dsl'
require "rack_session_access/capybara"
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Rails.application.config do
config.middleware.use RackSessionAccess::Middleware
end
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/factories"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# RSpec automatically cleans stuff out of backtraces;
# sometimes this is annoying when trying to debug something e.g. a gem
config.backtrace_clean_patterns = [
/\/lib\d*\/ruby\//,
/bin\//,
/gems/,
/spec\/spec_helper\.rb/,
/lib\/rspec\/(core|expectations|matchers|mocks)/
]
end
@aton1004
Copy link

aton1004 commented Jun 14, 2018

Hi @agmcleod

I tried the above code , not sure if I have implemented correctly, i just wanted to bypass the authentication .

So on these lines

@shopify_session.should_receive(:valid?).and_return(true)
        ShopifyAPI::Session.should_receive(:new).and_return(@shopify_session)
        ShopifyAPI::Session.any_instance.should_receive(:create_permission_url).and_return("/login/finalize?shop=#{@domain}&t=#{@token}")

there is an error on should_receive . I am trying on cucumber steps definition and while running cucumber on console it return error which says nosuch method "should_receive".

Is this method depricated?? Does above code still works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment