|
| 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 |
0 commit comments