@@ -332,7 +332,7 @@ def user
332
332
expect ( script_tag . intercom_settings [ :user_hash ] ) . to be_nil
333
333
end
334
334
335
- it 'generates a valid JWT with correct payload ' do
335
+ it 'generates a valid JWT with the correct user_id ' do
336
336
user_id = '1234'
337
337
script_tag = ScriptTag . new (
338
338
user_details : { user_id : user_id } ,
@@ -343,7 +343,6 @@ def user
343
343
decoded_payload = JWT . decode ( jwt , 'super-secret' , true , { algorithm : 'HS256' } ) [ 0 ]
344
344
345
345
expect ( decoded_payload [ 'user_id' ] ) . to eq ( user_id )
346
- expect ( decoded_payload [ 'exp' ] ) . to be_within ( 5 ) . of ( 24 . hours . from_now . to_i )
347
346
end
348
347
349
348
it 'does not generate JWT when user_id is missing' do
@@ -467,6 +466,48 @@ def user
467
466
expect ( script_tag . intercom_settings [ :name ] ) . to eq ( 'Test User' )
468
467
end
469
468
end
469
+
470
+ context 'JWT expiry' do
471
+ it 'includes expiry when configured' do
472
+ IntercomRails . config . jwt . expiry = 12 . hours
473
+ script_tag = ScriptTag . new (
474
+ user_details : { user_id : '1234' } ,
475
+ jwt_enabled : true
476
+ )
477
+
478
+ jwt = script_tag . intercom_settings [ :intercom_user_jwt ]
479
+ decoded_payload = JWT . decode ( jwt , 'super-secret' , true , { algorithm : 'HS256' } ) [ 0 ]
480
+
481
+ expect ( decoded_payload [ 'exp' ] ) . to be_within ( 5 ) . of ( 12 . hours . from_now . to_i )
482
+ end
483
+
484
+ it 'omits expiry when not configured' do
485
+ IntercomRails . config . jwt . expiry = nil
486
+ script_tag = ScriptTag . new (
487
+ user_details : { user_id : '1234' } ,
488
+ jwt_enabled : true
489
+ )
490
+
491
+ jwt = script_tag . intercom_settings [ :intercom_user_jwt ]
492
+ decoded_payload = JWT . decode ( jwt , 'super-secret' , true , { algorithm : 'HS256' } ) [ 0 ]
493
+
494
+ expect ( decoded_payload ) . not_to have_key ( 'exp' )
495
+ end
496
+
497
+ it 'allows overriding expiry via options' do
498
+ IntercomRails . config . jwt . expiry = 24 . hours
499
+ script_tag = ScriptTag . new (
500
+ user_details : { user_id : '1234' } ,
501
+ jwt_enabled : true ,
502
+ jwt_expiry : 1 . hour
503
+ )
504
+
505
+ jwt = script_tag . intercom_settings [ :intercom_user_jwt ]
506
+ decoded_payload = JWT . decode ( jwt , 'super-secret' , true , { algorithm : 'HS256' } ) [ 0 ]
507
+
508
+ expect ( decoded_payload [ 'exp' ] ) . to be_within ( 5 ) . of ( 1 . hour . from_now . to_i )
509
+ end
510
+ end
470
511
end
471
512
472
513
end
0 commit comments