@@ -52,6 +52,24 @@ def test_expired_with_passed_expiry_date
5252 assert ( session . expired? )
5353 end
5454
55+ def test_refresh_token_expired_with_no_expiry_date
56+ session = ShopifyAPI ::Auth ::Session . new ( shop : "test-shop" , refresh_token_expires : nil )
57+
58+ assert_equal ( false , session . refresh_token_expired? )
59+ end
60+
61+ def test_refresh_token_expired_with_future_expiry_date
62+ session = ShopifyAPI ::Auth ::Session . new ( shop : "test-shop" , refresh_token_expires : Time . now + 1 * 60 * 60 )
63+
64+ assert_equal ( false , session . refresh_token_expired? )
65+ end
66+
67+ def test_refresh_token_expired_with_passed_expiry_date
68+ session = ShopifyAPI ::Auth ::Session . new ( shop : "test-shop" , refresh_token_expires : Time . now - 1 )
69+
70+ assert ( session . refresh_token_expired? )
71+ end
72+
5573 def test_temp
5674 session = ShopifyAPI ::Auth ::Session . new ( shop : "test-shop1" , access_token : "token1" )
5775
@@ -96,6 +114,8 @@ def test_from_with_offline_access_token_response_with_no_expires_in
96114 associated_user : nil ,
97115 expires : nil ,
98116 shopify_session_id : response . session ,
117+ refresh_token : nil ,
118+ refresh_token_expires : nil ,
99119 )
100120
101121 session = ShopifyAPI ::Auth ::Session . from ( shop : shop , access_token_response : response )
@@ -121,6 +141,36 @@ def test_from_with_offline_access_token_response_with_expires_in
121141 associated_user : nil ,
122142 expires : Time . now + response . expires_in ,
123143 shopify_session_id : response . session ,
144+ refresh_token : nil ,
145+ refresh_token_expires : nil ,
146+ )
147+
148+ session = ShopifyAPI ::Auth ::Session . from ( shop : shop , access_token_response : response )
149+ assert_equal ( expected_session , session )
150+ end
151+
152+ def test_from_with_expiring_offline_access_token_response
153+ shop = "test-shop"
154+ response = ShopifyAPI ::Auth ::Oauth ::AccessTokenResponse . new (
155+ access_token : "token" ,
156+ scope : "scope1, scope2" ,
157+ expires_in : 1000 ,
158+ refresh_token : "refresh_token" ,
159+ refresh_token_expires_in : 2000 ,
160+ )
161+
162+ expected_session = ShopifyAPI ::Auth ::Session . new (
163+ id : "offline_#{ shop } " ,
164+ shop : shop ,
165+ access_token : response . access_token ,
166+ scope : response . scope ,
167+ is_online : false ,
168+ associated_user_scope : nil ,
169+ associated_user : nil ,
170+ expires : Time . now + response . expires_in ,
171+ shopify_session_id : response . session ,
172+ refresh_token : response . refresh_token ,
173+ refresh_token_expires : Time . now + response . refresh_token_expires_in ,
124174 )
125175
126176 session = ShopifyAPI ::Auth ::Session . from ( shop : shop , access_token_response : response )
@@ -158,6 +208,8 @@ def test_from_with_online_access_token_response
158208 associated_user : associated_user ,
159209 expires : time_now + response . expires_in ,
160210 shopify_session_id : response . session ,
211+ refresh_token : nil ,
212+ refresh_token_expires : nil ,
161213 )
162214
163215 session = Time . stub ( :now , time_now ) do
@@ -179,6 +231,8 @@ def test_copy_attributes_from
179231 associated_user : build_user ,
180232 is_online : true ,
181233 shopify_session_id : "123" ,
234+ refresh_token : "to-refresh-token" ,
235+ refresh_token_expires : Time . now - 7200 ,
182236 )
183237
184238 session_from = ShopifyAPI ::Auth ::Session . new (
@@ -192,6 +246,8 @@ def test_copy_attributes_from
192246 associated_user : build_user ,
193247 is_online : true ,
194248 shopify_session_id : "456" ,
249+ refresh_token : "from-refresh-token" ,
250+ refresh_token_expires : Time . now + 7200 ,
195251 )
196252
197253 assert_equal ( session_to , session_to . copy_attributes_from ( session_from ) )
@@ -204,6 +260,211 @@ def test_copy_attributes_from
204260 assert_equal ( session_from . expires , session_to . expires )
205261 assert_equal ( session_from . associated_user , session_to . associated_user )
206262 assert_equal ( session_from . shopify_session_id , session_to . shopify_session_id )
263+ assert_equal ( session_from . refresh_token , session_to . refresh_token )
264+ assert_equal ( session_from . refresh_token_expires , session_to . refresh_token_expires )
265+ end
266+
267+ def test_equality_with_all_fields_matching
268+ id = "session-id"
269+ shop = "test-shop"
270+ state = "test-state"
271+ scope = "read_products,write_products"
272+ associated_user_scope = "read_products"
273+ expires = Time . now + 3600
274+ associated_user = build_user
275+ shopify_session_id = "shopify-session-123"
276+ refresh_token = "refresh-token-abc"
277+ refresh_token_expires = Time . now + 7200
278+
279+ session1 = ShopifyAPI ::Auth ::Session . new (
280+ id :,
281+ shop :,
282+ state :,
283+ scope :,
284+ associated_user_scope :,
285+ expires :,
286+ is_online : true ,
287+ associated_user :,
288+ shopify_session_id :,
289+ refresh_token :,
290+ refresh_token_expires :,
291+ )
292+
293+ session2 = ShopifyAPI ::Auth ::Session . new (
294+ id :,
295+ shop :,
296+ state :,
297+ scope :,
298+ associated_user_scope :,
299+ expires :,
300+ is_online : true ,
301+ associated_user :,
302+ shopify_session_id :,
303+ refresh_token :,
304+ refresh_token_expires :,
305+ )
306+
307+ assert_equal ( session1 , session2 )
308+ end
309+
310+ def test_inequality_with_different_id
311+ shop = "test-shop"
312+
313+ session1 = ShopifyAPI ::Auth ::Session . new ( id : "id-1" , shop :)
314+ session2 = ShopifyAPI ::Auth ::Session . new ( id : "id-2" , shop :)
315+
316+ refute_equal ( session1 , session2 )
317+ end
318+
319+ def test_inequality_with_different_shop
320+ id = "session-id"
321+
322+ session1 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : "shop-1" )
323+ session2 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : "shop-2" )
324+
325+ refute_equal ( session1 , session2 )
326+ end
327+
328+ def test_inequality_with_different_state
329+ id = "session-id"
330+ shop = "test-shop"
331+
332+ session1 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , state : "state-1" )
333+ session2 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , state : "state-2" )
334+
335+ refute_equal ( session1 , session2 )
336+ end
337+
338+ def test_inequality_with_different_scope
339+ id = "session-id"
340+ shop = "test-shop"
341+
342+ session1 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , scope : "read_products" )
343+ session2 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , scope : "write_products" )
344+
345+ refute_equal ( session1 , session2 )
346+ end
347+
348+ def test_inequality_with_different_associated_user_scope
349+ id = "session-id"
350+ shop = "test-shop"
351+
352+ session1 = ShopifyAPI ::Auth ::Session . new (
353+ id : id ,
354+ shop : shop ,
355+ associated_user_scope : "read_products" ,
356+ )
357+ session2 = ShopifyAPI ::Auth ::Session . new (
358+ id : id ,
359+ shop : shop ,
360+ associated_user_scope : "write_products" ,
361+ )
362+
363+ refute_equal ( session1 , session2 )
364+ end
365+
366+ def test_inequality_with_different_expires
367+ id = "session-id"
368+ shop = "test-shop"
369+
370+ session1 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , expires : Time . now + 3600 )
371+ session2 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , expires : Time . now + 7200 )
372+
373+ refute_equal ( session1 , session2 )
374+ end
375+
376+ def test_inequality_with_different_is_online
377+ id = "session-id"
378+ shop = "test-shop"
379+
380+ session1 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , is_online : true )
381+ session2 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , is_online : false )
382+
383+ refute_equal ( session1 , session2 )
384+ end
385+
386+ def test_inequality_with_different_associated_user
387+ id = "session-id"
388+ shop = "test-shop"
389+ user1 = ShopifyAPI ::Auth ::AssociatedUser . new (
390+ id : 1 ,
391+ first_name : "first" ,
392+ last_name : "last" ,
393+ 394+ email_verified : true ,
395+ account_owner : true ,
396+ locale : "en" ,
397+ collaborator : false ,
398+ )
399+ user2 = ShopifyAPI ::Auth ::AssociatedUser . new (
400+ id : 2 ,
401+ first_name : "other" ,
402+ last_name : "user" ,
403+ 404+ email_verified : true ,
405+ account_owner : false ,
406+ locale : "en" ,
407+ collaborator : true ,
408+ )
409+
410+ session1 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , associated_user : user1 )
411+ session2 = ShopifyAPI ::Auth ::Session . new ( id : id , shop : shop , associated_user : user2 )
412+
413+ refute_equal ( session1 , session2 )
414+ end
415+
416+ def test_inequality_with_different_shopify_session_id
417+ id = "session-id"
418+ shop = "test-shop"
419+
420+ session1 = ShopifyAPI ::Auth ::Session . new (
421+ id : id ,
422+ shop : shop ,
423+ shopify_session_id : "shopify-session-1" ,
424+ )
425+ session2 = ShopifyAPI ::Auth ::Session . new (
426+ id : id ,
427+ shop : shop ,
428+ shopify_session_id : "shopify-session-2" ,
429+ )
430+
431+ refute_equal ( session1 , session2 )
432+ end
433+
434+ def test_inequality_with_different_refresh_token
435+ id = "session-id"
436+ shop = "test-shop"
437+
438+ session1 = ShopifyAPI ::Auth ::Session . new (
439+ id : id ,
440+ shop : shop ,
441+ refresh_token : "refresh-token-1" ,
442+ )
443+ session2 = ShopifyAPI ::Auth ::Session . new (
444+ id : id ,
445+ shop : shop ,
446+ refresh_token : "refresh-token-2" ,
447+ )
448+
449+ refute_equal ( session1 , session2 )
450+ end
451+
452+ def test_inequality_with_different_refresh_token_expires
453+ id = "session-id"
454+ shop = "test-shop"
455+
456+ session1 = ShopifyAPI ::Auth ::Session . new (
457+ id : id ,
458+ shop : shop ,
459+ refresh_token_expires : Time . now + 3600 ,
460+ )
461+ session2 = ShopifyAPI ::Auth ::Session . new (
462+ id : id ,
463+ shop : shop ,
464+ refresh_token_expires : Time . now + 7200 ,
465+ )
466+
467+ refute_equal ( session1 , session2 )
207468 end
208469
209470 def teardown
0 commit comments