@@ -737,4 +737,278 @@ describe('PubNub Shared Worker Integration Tests', () => {
737737 subscription2 . subscribe ( ) ;
738738 } ) . timeout ( 15000 ) ;
739739 } ) ;
740+
741+ describe ( 'Authentication Token Management' , ( ) => {
742+ it ( 'should properly handle token changes in shared worker environment' , ( done ) => {
743+ const channel = testChannels [ 0 ] ;
744+ const testToken = 'test-auth-token-verification-123' ;
745+ let subscriptionEstablished = false ;
746+ let tokenUpdateCompleted = false ;
747+
748+ pubnubWithWorker . addListener ( {
749+ status : ( statusEvent ) => {
750+ if ( statusEvent . category === PubNub . CATEGORIES . PNConnectedCategory && ! subscriptionEstablished ) {
751+ subscriptionEstablished = true ;
752+
753+ // Set the token after initial subscription
754+ setTimeout ( ( ) => {
755+ pubnubWithWorker . setToken ( testToken ) ;
756+
757+ // Wait for the shared worker to process the token update
758+ setTimeout ( ( ) => {
759+ try {
760+ // Verify the token was set correctly
761+ const currentToken = pubnubWithWorker . getToken ( ) ;
762+ expect ( currentToken ) . to . equal ( testToken ) ;
763+
764+ tokenUpdateCompleted = true ;
765+
766+ // Test that subscription still works after token update
767+ // by triggering a subscription change which should use the new token
768+ const tempChannel = `temp-${ Date . now ( ) } ` ;
769+ const tempSubscription = pubnubWithWorker . channel ( tempChannel ) . subscription ( ) ;
770+ tempSubscription . subscribe ( ) ;
771+
772+ // Verify the subscription list includes both channels
773+ setTimeout ( ( ) => {
774+ const subscribedChannels = pubnubWithWorker . getSubscribedChannels ( ) ;
775+ expect ( subscribedChannels ) . to . include ( channel ) ;
776+ expect ( subscribedChannels ) . to . include ( tempChannel ) ;
777+
778+ // If we reach here, the token update was processed successfully
779+ // and the shared worker is using the new token for subscriptions
780+ done ( ) ;
781+ } , 1000 ) ;
782+ } catch ( error ) {
783+ done ( error ) ;
784+ }
785+ } , 1000 ) ;
786+ } , 500 ) ;
787+ } else if ( statusEvent . error && ! tokenUpdateCompleted ) {
788+ done ( new Error ( `Status error: ${ statusEvent . error } ` ) ) ;
789+ }
790+ } ,
791+ } ) ;
792+
793+ const subscription = pubnubWithWorker . channel ( channel ) . subscription ( ) ;
794+ subscription . subscribe ( ) ;
795+ } ) . timeout ( 15000 ) ;
796+
797+ it ( 'should verify token is passed to shared worker and subscription continues' , ( done ) => {
798+ const channel = testChannels [ 0 ] ;
799+ const initialToken = 'initial-token-123' ;
800+ const updatedToken = 'updated-token-456' ;
801+ let initialSubscriptionDone = false ;
802+ let tokenUpdateDone = false ;
803+
804+ pubnubWithWorker . addListener ( {
805+ status : ( statusEvent ) => {
806+ if ( statusEvent . category === PubNub . CATEGORIES . PNConnectedCategory && ! initialSubscriptionDone ) {
807+ initialSubscriptionDone = true ;
808+
809+ // Set initial token
810+ pubnubWithWorker . setToken ( initialToken ) ;
811+
812+ setTimeout ( ( ) => {
813+ try {
814+ expect ( pubnubWithWorker . getToken ( ) ) . to . equal ( initialToken ) ;
815+
816+ // Update to a new token
817+ pubnubWithWorker . setToken ( updatedToken ) ;
818+ tokenUpdateDone = true ;
819+
820+ setTimeout ( ( ) => {
821+ // Verify the updated token is set
822+ expect ( pubnubWithWorker . getToken ( ) ) . to . equal ( updatedToken ) ;
823+
824+ // Verify subscription is still active
825+ const subscribedChannels = pubnubWithWorker . getSubscribedChannels ( ) ;
826+ expect ( subscribedChannels ) . to . include ( channel ) ;
827+
828+ // Test that we can add another channel with the new token
829+ const newChannel = `new-${ Date . now ( ) } ` ;
830+ const newSubscription = pubnubWithWorker . channel ( newChannel ) . subscription ( ) ;
831+ newSubscription . subscribe ( ) ;
832+
833+ setTimeout ( ( ) => {
834+ const updatedChannels = pubnubWithWorker . getSubscribedChannels ( ) ;
835+ expect ( updatedChannels ) . to . include ( channel ) ;
836+ expect ( updatedChannels ) . to . include ( newChannel ) ;
837+ done ( ) ;
838+ } , 500 ) ;
839+ } , 500 ) ;
840+ } catch ( error ) {
841+ done ( error ) ;
842+ }
843+ } , 500 ) ;
844+ } else if ( statusEvent . error && ! tokenUpdateDone ) {
845+ done ( new Error ( `Status error: ${ statusEvent . error } ` ) ) ;
846+ }
847+ } ,
848+ } ) ;
849+
850+ const subscription = pubnubWithWorker . channel ( channel ) . subscription ( ) ;
851+ subscription . subscribe ( ) ;
852+ } ) . timeout ( 15000 ) ;
853+
854+ it ( 'should update subscription with new token when setToken() is called' , ( done ) => {
855+ const channel = testChannels [ 0 ] ;
856+ const testToken = 'test-dummy-token-12345' ;
857+ let subscriptionEstablished = false ;
858+ let tokenUpdateProcessed = false ;
859+
860+ pubnubWithWorker . addListener ( {
861+ status : ( statusEvent ) => {
862+ if ( statusEvent . category === PubNub . CATEGORIES . PNConnectedCategory && ! subscriptionEstablished ) {
863+ subscriptionEstablished = true ;
864+
865+ // Wait for subscription to be established, then update the token
866+ setTimeout ( ( ) => {
867+ // Set the new token - this should trigger the shared worker to update the subscription
868+ pubnubWithWorker . setToken ( testToken ) ;
869+
870+ // Verify the token was set correctly
871+ const currentToken = pubnubWithWorker . getToken ( ) ;
872+
873+ try {
874+ expect ( currentToken ) . to . equal ( testToken ) ;
875+ tokenUpdateProcessed = true ;
876+
877+ // Wait a bit to ensure the shared worker has processed the token update
878+ setTimeout ( ( ) => {
879+ // Check that the subscription is still active after token update
880+ const subscribedChannels = pubnubWithWorker . getSubscribedChannels ( ) ;
881+ expect ( subscribedChannels ) . to . include ( channel ) ;
882+
883+ // Try to publish a message to verify the subscription still works with the new token
884+ pubnubWithWorker
885+ . publish ( {
886+ channel,
887+ message : { text : 'Test message after token update' , timestamp : Date . now ( ) } ,
888+ } )
889+ . then ( ( ) => {
890+ // If publish succeeds, the token update was successful
891+ done ( ) ;
892+ } )
893+ . catch ( ( error ) => {
894+ // Even if publish fails due to demo keys, the token update mechanism worked
895+ // The important thing is that the subscription didn't break
896+ done ( ) ;
897+ } ) ;
898+ } , 1000 ) ;
899+ } catch ( error ) {
900+ done ( error ) ;
901+ }
902+ } , 1000 ) ;
903+ } else if ( statusEvent . error && ! tokenUpdateProcessed ) {
904+ done ( new Error ( `Status error before token update: ${ statusEvent . error } ` ) ) ;
905+ }
906+ } ,
907+ message : ( messageEvent ) => {
908+ // If we receive a message after token update, the subscription is working correctly
909+ if ( messageEvent . channel === channel && tokenUpdateProcessed ) {
910+ try {
911+ expect ( messageEvent . channel ) . to . equal ( channel ) ;
912+ done ( ) ;
913+ } catch ( error ) {
914+ done ( error ) ;
915+ }
916+ }
917+ } ,
918+ } ) ;
919+
920+ const subscription = pubnubWithWorker . channel ( channel ) . subscription ( ) ;
921+ subscription . subscribe ( ) ;
922+ } ) . timeout ( 15000 ) ;
923+
924+ it ( 'should handle token updates with multiple subscription changes' , ( done ) => {
925+ const channel1 = testChannels [ 0 ] ;
926+ const channel2 = testChannels [ 1 ] ;
927+ const testToken = 'test-multi-token-67890' ;
928+ let initialSubscriptionReady = false ;
929+ let tokenUpdated = false ;
930+
931+ pubnubWithWorker . addListener ( {
932+ status : ( statusEvent ) => {
933+ if ( statusEvent . category === PubNub . CATEGORIES . PNConnectedCategory && ! initialSubscriptionReady ) {
934+ initialSubscriptionReady = true ;
935+
936+ // Update token and add another subscription
937+ setTimeout ( ( ) => {
938+ pubnubWithWorker . setToken ( testToken ) ;
939+ tokenUpdated = true ;
940+
941+ // Add subscription to second channel after token update
942+ const subscription2 = pubnubWithWorker . channel ( channel2 ) . subscription ( ) ;
943+ subscription2 . subscribe ( ) ;
944+
945+ // Verify both channels are subscribed with the new token
946+ setTimeout ( ( ) => {
947+ const subscribedChannels = pubnubWithWorker . getSubscribedChannels ( ) ;
948+ try {
949+ expect ( subscribedChannels ) . to . include ( channel1 ) ;
950+ expect ( subscribedChannels ) . to . include ( channel2 ) ;
951+
952+ // Verify token was set
953+ const currentToken = pubnubWithWorker . getToken ( ) ;
954+ expect ( currentToken ) . to . equal ( testToken ) ;
955+
956+ done ( ) ;
957+ } catch ( error ) {
958+ done ( error ) ;
959+ }
960+ } , 1000 ) ;
961+ } , 500 ) ;
962+ }
963+ } ,
964+ } ) ;
965+
966+ const subscription1 = pubnubWithWorker . channel ( channel1 ) . subscription ( ) ;
967+ subscription1 . subscribe ( ) ;
968+ } ) . timeout ( 15000 ) ;
969+
970+ it ( 'should handle token removal (undefined token)' , ( done ) => {
971+ const channel = testChannels [ 0 ] ;
972+ const testToken = 'test-token-to-remove' ;
973+ let subscriptionEstablished = false ;
974+ let tokenSetAndRemoved = false ;
975+
976+ pubnubWithWorker . addListener ( {
977+ status : ( statusEvent ) => {
978+ if ( statusEvent . category === PubNub . CATEGORIES . PNConnectedCategory && ! subscriptionEstablished ) {
979+ subscriptionEstablished = true ;
980+
981+ setTimeout ( ( ) => {
982+ // First set a token
983+ pubnubWithWorker . setToken ( testToken ) ;
984+
985+ // Then remove it by setting undefined
986+ setTimeout ( ( ) => {
987+ pubnubWithWorker . setToken ( undefined ) ;
988+ tokenSetAndRemoved = true ;
989+
990+ // Verify token was removed
991+ const currentToken = pubnubWithWorker . getToken ( ) ;
992+
993+ try {
994+ expect ( currentToken ) . to . be . undefined ;
995+
996+ // Verify subscription is still active
997+ const subscribedChannels = pubnubWithWorker . getSubscribedChannels ( ) ;
998+ expect ( subscribedChannels ) . to . include ( channel ) ;
999+
1000+ done ( ) ;
1001+ } catch ( error ) {
1002+ done ( error ) ;
1003+ }
1004+ } , 500 ) ;
1005+ } , 500 ) ;
1006+ }
1007+ } ,
1008+ } ) ;
1009+
1010+ const subscription = pubnubWithWorker . channel ( channel ) . subscription ( ) ;
1011+ subscription . subscribe ( ) ;
1012+ } ) . timeout ( 15000 ) ;
1013+ } ) ;
7401014} ) ;
0 commit comments