@@ -40,12 +40,24 @@ def setup
4040 client_id : ShopifyAPI ::Context . api_key ,
4141 client_secret : ShopifyAPI ::Context . api_secret_key ,
4242 code : @callback_code ,
43+ expiring : 0 ,
4344 }
4445
46+ @expiring_access_token_request = @access_token_request . merge ( { expiring : 1 } )
47+
4548 @offline_token_response = {
4649 access_token : SecureRandom . alphanumeric ( 10 ) ,
4750 scope : "scope1,scope2" ,
4851 }
52+
53+ @expiring_offline_token_response = @offline_token_response . merge (
54+ {
55+ expires_in : 1000 ,
56+ refresh_token : SecureRandom . alphanumeric ( 10 ) ,
57+ refresh_token_expires_in : 2000 ,
58+ } ,
59+ )
60+
4961 @online_token_response = {
5062 access_token : SecureRandom . alphanumeric ( 10 ) ,
5163 scope : "scope1,scope2" ,
@@ -123,7 +135,7 @@ def test_begin_auth_private_app
123135 end
124136
125137 def test_validate_auth_callback_offline
126- modify_context ( is_embedded : false )
138+ modify_context ( is_embedded : false , expiring_offline_access_tokens : false )
127139
128140 stub_request ( :post , "https://#{ @shop } /admin/oauth/access_token" )
129141 . with ( body : @access_token_request )
@@ -155,7 +167,7 @@ def test_validate_auth_callback_offline_embedded
155167 )
156168 expected_cookie = ShopifyAPI ::Auth ::Oauth ::SessionCookie . new ( value : "" , expires : @stubbed_time_now )
157169
158- modify_context ( is_embedded : true )
170+ modify_context ( is_embedded : true , expiring_offline_access_tokens : false )
159171
160172 got = Time . stub ( :now , @stubbed_time_now ) do
161173 ShopifyAPI ::Auth ::Oauth . validate_auth_callback ( cookies : @cookies , auth_query : @auth_query )
@@ -164,6 +176,34 @@ def test_validate_auth_callback_offline_embedded
164176 verify_oauth_complete ( got : got , expected_session : expected_session , expected_cookie : expected_cookie )
165177 end
166178
179+ def test_validate_auth_callback_offline_token_with_expiring_token_enabled
180+ modify_context ( expiring_offline_access_tokens : true )
181+
182+ stub_request ( :post , "https://#{ @shop } /admin/oauth/access_token" )
183+ . with ( body : @expiring_access_token_request )
184+ . to_return ( body : @expiring_offline_token_response . to_json , headers : { content_type : "application/json" } )
185+
186+ expected_session = ShopifyAPI ::Auth ::Session . new (
187+ id : "offline_#{ @shop } " ,
188+ shop : @shop ,
189+ access_token : @offline_token_response [ :access_token ] ,
190+ scope : @offline_token_response [ :scope ] ,
191+ expires : @stubbed_time_now + @online_token_response [ :expires_in ] . to_i ,
192+ refresh_token : @expiring_offline_token_response [ :refresh_token ] ,
193+ refresh_token_expires : @stubbed_time_now + @expiring_offline_token_response [ :refresh_token_expires_in ] . to_i ,
194+ )
195+ expected_cookie = ShopifyAPI ::Auth ::Oauth ::SessionCookie . new (
196+ value : "offline_#{ @shop } " ,
197+ expires : expected_session . expires ,
198+ )
199+
200+ got = Time . stub ( :now , @stubbed_time_now ) do
201+ ShopifyAPI ::Auth ::Oauth . validate_auth_callback ( cookies : @cookies , auth_query : @auth_query )
202+ end
203+
204+ verify_oauth_complete ( got :, expected_session :, expected_cookie :)
205+ end
206+
167207 def test_validate_auth_callback_online
168208 stub_request ( :post , "https://#{ @shop } /admin/oauth/access_token" )
169209 . with ( body : @access_token_request )
0 commit comments