@@ -34,7 +34,7 @@ def test_authorize_overrides_site_with_https_scheme
3434      env [ 'omniauth.strategy' ] . options [ :client_options ] [ :site ]  =  "http://#{ params [ 'shop' ] }  
3535    } 
3636
37-     response  =  authorize ( ' snowdevil.myshopify.com') 
37+     response  =  request . get ( 'https://app.example.com/auth/shopify?shop= snowdevil.myshopify.com') 
3838    assert_match  %r{\A #{ Regexp . quote ( shopify_authorize_url ) }  ,  response . location 
3939  end 
4040
@@ -48,6 +48,7 @@ def test_site_validation
4848      '[email protected] ' ,    # shop contains user  4949      'snowdevil.myshopify.com:22' ,      # shop contains port 
5050    ] . each  do  |shop ,  valid |
51+       @shop  =  shop 
5152      response  =  authorize ( shop ) 
5253      assert_auth_failure ( response ,  'invalid_site' ) 
5354
@@ -133,7 +134,10 @@ def test_callback_rejects_body_params
133134
134135    response  =  request . get ( "https://app.example.com/auth/shopify/callback?#{ Rack ::Utils . build_query ( params ) }  , 
135136                           input : body , 
136-                            "CONTENT_TYPE"  =>  'application/x-www-form-urlencoded' ) 
137+                            "CONTENT_TYPE"  =>  'application/x-www-form-urlencoded' , 
138+                            'rack.session'  =>  { 
139+                               'shopify.omniauth_params'  =>  {  shop : 'snowdevil.myshopify.com'  } 
140+                             } ) 
137141
138142    assert_auth_failure ( response ,  'invalid_signature' ) 
139143  end 
@@ -148,25 +152,33 @@ def test_provider_options
148152                env [ 'omniauth.strategy' ] . options [ :client_options ] [ :site ]  =  "https://#{ shop }  
149153              } 
150154
151-     response  =  authorize ( ' snowdevil' ) 
155+     response  =  request . get ( "https://app.example.com/auth/shopify?shop= snowdevil.myshopify.dev:3000" ) 
152156    assert_equal  302 ,  response . status 
153157    assert_match  %r{\A #{ Regexp . quote ( "https://snowdevil.myshopify.dev:3000/admin/oauth/authorize?" ) }  ,  response . location 
154158    redirect_params  =  Rack ::Utils . parse_query ( URI ( response . location ) . query ) 
155159    assert_equal  'read_products,read_orders,write_content' ,  redirect_params [ 'scope' ] 
156160    assert_equal  'https://app.example.com/admin/auth/legacy/callback' ,  redirect_params [ 'redirect_uri' ] 
157161  end 
158162
163+   def  test_default_setup_reads_shop_from_session 
164+     build_app 
165+     response  =  authorize ( 'snowdevil.myshopify.com' ) 
166+     assert_equal  302 ,  response . status 
167+     assert_match  %r{\A #{ Regexp . quote ( "https://snowdevil.myshopify.com/admin/oauth/authorize?" ) }  ,  response . location 
168+     redirect_params  =  Rack ::Utils . parse_query ( URI ( response . location ) . query ) 
169+     assert_equal  'https://app.example.com/auth/shopify/callback' ,  redirect_params [ 'redirect_uri' ] 
170+   end 
171+ 
159172  def  test_unnecessary_read_scopes_are_removed 
160173    build_app  scope : 'read_content,read_products,write_products' , 
161174              callback_path : '/admin/auth/legacy/callback' , 
162175              myshopify_domain : 'myshopify.dev:3000' , 
163176              setup : lambda  {  |env |
164177                shop  =  Rack ::Request . new ( env ) . GET [ 'shop' ] 
165-                 shop  += ".myshopify.dev:3000"  unless  shop . include? ( "." ) 
166178                env [ 'omniauth.strategy' ] . options [ :client_options ] [ :site ]  =  "https://#{ shop }  
167179              } 
168180
169-     response  =  authorize ( ' snowdevil' ) 
181+     response  =  request . get ( "https://app.example.com/auth/shopify?shop= snowdevil.myshopify.dev:3000" ) 
170182    assert_equal  302 ,  response . status 
171183    redirect_params  =  Rack ::Utils . parse_query ( URI ( response . location ) . query ) 
172184    assert_equal  'read_content,write_products' ,  redirect_params [ 'scope' ] 
@@ -345,11 +357,17 @@ def build_app(options={})
345357    @app  =  Rack ::Session ::Cookie . new ( app ,  secret : SecureRandom . hex ( 64 ) ) 
346358  end 
347359
360+   def  shop 
361+     @shop  ||= 'snowdevil.myshopify.com' 
362+   end 
363+ 
348364  def  authorize ( shop ) 
349-     request . get ( "https://app.example.com/auth/shopify?shop=#{ CGI . escape ( shop ) }  ,  opts ) 
365+     @opts [ 'rack.session' ] [ 'shopify.omniauth_params' ]  =  {  shop : shop  } 
366+     request . get ( 'https://app.example.com/auth/shopify' ,  opts ) 
350367  end 
351368
352369  def  callback ( params ) 
370+     @opts [ 'rack.session' ] [ 'shopify.omniauth_params' ]  =  {  shop : shop  } 
353371    request . get ( "https://app.example.com/auth/shopify/callback?#{ Rack ::Utils . build_query ( params ) }  ,  opts ) 
354372  end 
355373
0 commit comments