Skip to content

Commit 35eecaa

Browse files
committed
Merge pull request Shopify#24 from Shopify/integration-tests
Replace rspec mocked out unit tests with minitest integration tests.
2 parents 7b74c0c + ab45f7d commit 35eecaa

File tree

6 files changed

+134
-47
lines changed

6 files changed

+134
-47
lines changed

Diff for: Rakefile

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
require 'bundler/gem_tasks'
2-
require 'rspec/core/rake_task'
1+
require 'rake/testtask'
32

4-
RSpec::Core::RakeTask.new(:spec)
3+
task :default => :test
54

6-
task :default => :spec
5+
Rake::TestTask.new(:test) do |t|
6+
t.pattern = 'test/**/*_test.rb'
7+
t.verbose = true
8+
end

Diff for: omniauth-shopify-oauth2.gemspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
1818

1919
s.add_runtime_dependency 'omniauth-oauth2', '~> 1.2'
2020

21-
s.add_development_dependency 'rspec', '~> 2.7.0'
21+
s.add_development_dependency 'minitest', '~> 5.6'
22+
s.add_development_dependency 'fakeweb', '~> 1.3'
2223
s.add_development_dependency 'rake'
2324
end

Diff for: spec/spec_helper.rb

-6
This file was deleted.

Diff for: spec/support/shared_examples.rb

-36
This file was deleted.

Diff for: test/integration_test.rb

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
require_relative 'test_helper'
2+
3+
class IntegrationTest < Minitest::Test
4+
def setup
5+
build_app
6+
end
7+
8+
def teardown
9+
FakeWeb.clean_registry
10+
FakeWeb.last_request = nil
11+
end
12+
13+
def test_authorize
14+
response = authorize('snowdevil.myshopify.com')
15+
assert_equal 302, response.status
16+
assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.com/admin/oauth/authorize?")}/, response.location
17+
redirect_params = Rack::Utils.parse_query(URI(response.location).query)
18+
assert_equal "123", redirect_params['client_id']
19+
assert_equal "https://app.example.com/auth/shopify/callback", redirect_params['redirect_uri']
20+
assert_equal "read_products", redirect_params['scope']
21+
end
22+
23+
def test_authorize_overrides_site_with_https_scheme
24+
build_app setup: lambda { |env|
25+
params = Rack::Utils.parse_query(env['QUERY_STRING'])
26+
env['omniauth.strategy'].options[:client_options][:site] = "http://#{params['shop']}"
27+
}
28+
29+
response = authorize('snowdevil.myshopify.com')
30+
assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.com/admin/oauth/authorize?")}/, response.location
31+
end
32+
33+
def test_site_validation
34+
code = SecureRandom.hex(16)
35+
36+
[
37+
'foo.example.com', # shop doesn't end with .myshopify.com
38+
'http://snowdevil.myshopify.com', # shop contains protocol
39+
'snowdevil.myshopify.com/path', # shop contains path
40+
'[email protected]', # shop contains user
41+
'snowdevil.myshopify.com:22', # shop contains port
42+
].each do |shop, valid|
43+
response = authorize(shop)
44+
assert_equal 302, response.status
45+
assert_match /\A#{Regexp.quote("/auth/failure?message=invalid_site")}/, response.location
46+
47+
response = callback(shop: shop, code: code)
48+
assert_nil FakeWeb.last_request
49+
assert_equal 302, response.status
50+
assert_match /\A#{Regexp.quote("/auth/failure?message=invalid_site")}/, response.location
51+
end
52+
end
53+
54+
def test_callback
55+
access_token = SecureRandom.hex(16)
56+
code = SecureRandom.hex(16)
57+
FakeWeb.register_uri(:post, "https://snowdevil.myshopify.com/admin/oauth/access_token",
58+
body: JSON.dump(access_token: access_token),
59+
content_type: 'application/json')
60+
61+
response = callback(shop: 'snowdevil.myshopify.com', code: code)
62+
63+
token_request_params = Rack::Utils.parse_query(FakeWeb.last_request.body)
64+
assert_equal token_request_params['client_id'], '123'
65+
assert_equal token_request_params['client_secret'], '53cr3tz'
66+
assert_equal token_request_params['code'], code
67+
68+
assert_equal 'snowdevil.myshopify.com', @omniauth_result.uid
69+
assert_equal access_token, @omniauth_result.credentials.token
70+
assert_equal false, @omniauth_result.credentials.expires
71+
72+
assert_equal 200, response.status
73+
assert_equal "OK", response.body
74+
end
75+
76+
def test_provider_options
77+
build_app scope: 'read_products,read_orders,write_content', callback_path: '/admin/auth/legacy/callback', myshopify_domain: 'myshopify.dev:3000'
78+
response = authorize('snowdevil.myshopify.dev:3000')
79+
assert_equal 302, response.status
80+
assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.dev:3000/admin/oauth/authorize?")}/, response.location
81+
redirect_params = Rack::Utils.parse_query(URI(response.location).query)
82+
assert_equal 'read_products,read_orders,write_content', redirect_params['scope']
83+
assert_equal 'https://app.example.com/admin/auth/legacy/callback', redirect_params['redirect_uri']
84+
end
85+
86+
private
87+
88+
def build_app(options={})
89+
app = proc { |env|
90+
@omniauth_result = env['omniauth.auth']
91+
[200, {Rack::CONTENT_TYPE => "text/plain"}, "OK"]
92+
}
93+
94+
app = OmniAuth::Builder.new(app) do
95+
options[:setup] ||= lambda { |env|
96+
params = Rack::Utils.parse_query(env['QUERY_STRING'])
97+
env['omniauth.strategy'].options[:client_options][:site] = "https://#{params['shop']}"
98+
}
99+
100+
provider :shopify, '123', '53cr3tz', options
101+
end
102+
@app = Rack::Session::Cookie.new(app, secret: SecureRandom.hex(64))
103+
end
104+
105+
def authorize(shop)
106+
request.get("https://app.example.com/auth/shopify?shop=#{CGI.escape(shop)}")
107+
end
108+
109+
def callback(params)
110+
request.get("https://app.example.com/auth/shopify/callback?#{Rack::Utils.build_query(params)}")
111+
end
112+
113+
def request
114+
Rack::MockRequest.new(@app)
115+
end
116+
end

Diff for: test/test_helper.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$: << File.expand_path("../../lib", __FILE__)
2+
require 'bundler/setup'
3+
require 'omniauth-shopify-oauth2'
4+
5+
require 'minitest/autorun'
6+
require 'fakeweb'
7+
require 'json'
8+
9+
OmniAuth.config.logger = Logger.new(nil)
10+
FakeWeb.allow_net_connect = false

0 commit comments

Comments
 (0)