@@ -77,10 +77,14 @@ describe('Yahoo ConnectID Submodule', () => {
7777 removeLocalStorageDataStub . restore ( ) ;
7878 } ) ;
7979
80- function invokeGetIdAPI ( configParams , consentData ) {
81- const result = connectIdSubmodule . getId ( {
80+ function invokeGetIdAPI ( configParams , consentData , storageConfig ) {
81+ const config = {
8282 params : configParams
83- } , consentData ) ;
83+ } ;
84+ if ( storageConfig ) {
85+ config . storage = storageConfig ;
86+ }
87+ const result = connectIdSubmodule . getId ( config , consentData ) ;
8488 if ( typeof result === 'object' && result . callback ) {
8589 result . callback ( sinon . stub ( ) ) ;
8690 }
@@ -803,6 +807,130 @@ describe('Yahoo ConnectID Submodule', () => {
803807 expect ( setLocalStorageStub . firstCall . args [ 0 ] ) . to . equal ( STORAGE_KEY ) ;
804808 expect ( setLocalStorageStub . firstCall . args [ 1 ] ) . to . deep . equal ( JSON . stringify ( expectedStoredData ) ) ;
805809 } ) ;
810+
811+ it ( 'stores the result in localStorage only when storage type is html5' , ( ) => {
812+ getAjaxFnStub . restore ( ) ;
813+ const dateNowStub = sinon . stub ( Date , 'now' ) ;
814+ dateNowStub . returns ( 0 ) ;
815+ const upsResponse = { connectid : 'html5only' } ;
816+ const expectedStoredData = {
817+ connectid : 'html5only' ,
818+ puid : PUBLISHER_USER_ID ,
819+ lastSynced : 0 ,
820+ lastUsed : 0
821+ } ;
822+ invokeGetIdAPI ( {
823+ puid : PUBLISHER_USER_ID ,
824+ pixelId : PIXEL_ID
825+ } , consentData , { type : 'html5' } ) ;
826+ const request = server . requests [ 0 ] ;
827+ request . respond (
828+ 200 ,
829+ { 'Content-Type' : 'application/json' } ,
830+ JSON . stringify ( upsResponse )
831+ ) ;
832+ dateNowStub . restore ( ) ;
833+
834+ expect ( setCookieStub . called ) . to . be . false ;
835+ expect ( setLocalStorageStub . calledOnce ) . to . be . true ;
836+ expect ( setLocalStorageStub . firstCall . args [ 0 ] ) . to . equal ( STORAGE_KEY ) ;
837+ expect ( setLocalStorageStub . firstCall . args [ 1 ] ) . to . deep . equal ( JSON . stringify ( expectedStoredData ) ) ;
838+ } ) ;
839+
840+ it ( 'stores the result in cookie only when storage type is cookie' , ( ) => {
841+ getAjaxFnStub . restore ( ) ;
842+ const dateNowStub = sinon . stub ( Date , 'now' ) ;
843+ dateNowStub . returns ( 0 ) ;
844+ const upsResponse = { connectid : 'cookieonly' } ;
845+ const expectedStoredData = {
846+ connectid : 'cookieonly' ,
847+ puid : PUBLISHER_USER_ID ,
848+ lastSynced : 0 ,
849+ lastUsed : 0
850+ } ;
851+ const expiryDelta = new Date ( 60 * 60 * 24 * 365 * 1000 ) ;
852+ invokeGetIdAPI ( {
853+ puid : PUBLISHER_USER_ID ,
854+ pixelId : PIXEL_ID
855+ } , consentData , { type : 'cookie' } ) ;
856+ const request = server . requests [ 0 ] ;
857+ request . respond (
858+ 200 ,
859+ { 'Content-Type' : 'application/json' } ,
860+ JSON . stringify ( upsResponse )
861+ ) ;
862+ dateNowStub . restore ( ) ;
863+
864+ expect ( setCookieStub . calledOnce ) . to . be . true ;
865+ expect ( setCookieStub . firstCall . args [ 0 ] ) . to . equal ( STORAGE_KEY ) ;
866+ expect ( setCookieStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedStoredData ) ) ;
867+ expect ( setCookieStub . firstCall . args [ 2 ] ) . to . equal ( expiryDelta . toUTCString ( ) ) ;
868+ expect ( setLocalStorageStub . called ) . to . be . false ;
869+ } ) ;
870+
871+ it ( 'does not sync localStorage to cookie when storage type is html5' , ( ) => {
872+ const localStorageData = { connectId : 'foobarbaz' } ;
873+ getLocalStorageStub . withArgs ( STORAGE_KEY ) . returns ( localStorageData ) ;
874+ invokeGetIdAPI ( {
875+ he : HASHED_EMAIL ,
876+ pixelId : PIXEL_ID
877+ } , consentData , { type : 'html5' } ) ;
878+
879+ expect ( setCookieStub . called ) . to . be . false ;
880+ } ) ;
881+
882+ it ( 'updates existing ID with html5 storage type without writing cookie' , ( ) => {
883+ const last13Days = Date . now ( ) - ( 60 * 60 * 24 * 1000 * 13 ) ;
884+ const cookieData = { connectId : 'foobar' , he : HASHED_EMAIL , lastSynced : last13Days } ;
885+ getCookieStub . withArgs ( STORAGE_KEY ) . returns ( JSON . stringify ( cookieData ) ) ;
886+ const dateNowStub = sinon . stub ( Date , 'now' ) ;
887+ dateNowStub . returns ( 20 ) ;
888+ const newCookieData = Object . assign ( { } , cookieData , { lastUsed : 20 } )
889+ const result = invokeGetIdAPI ( {
890+ he : HASHED_EMAIL ,
891+ pixelId : PIXEL_ID
892+ } , consentData , { type : 'html5' } ) ;
893+ dateNowStub . restore ( ) ;
894+
895+ expect ( result ) . to . be . an ( 'object' ) . that . has . all . keys ( 'id' ) ;
896+ expect ( setCookieStub . called ) . to . be . false ;
897+ expect ( setLocalStorageStub . calledOnce ) . to . be . true ;
898+ expect ( setLocalStorageStub . firstCall . args [ 0 ] ) . to . equal ( STORAGE_KEY ) ;
899+ expect ( setLocalStorageStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( newCookieData ) ) ;
900+ } ) ;
901+
902+ it ( 'stores the result in both storages when storage type is cookie&html5' , ( ) => {
903+ getAjaxFnStub . restore ( ) ;
904+ const dateNowStub = sinon . stub ( Date , 'now' ) ;
905+ dateNowStub . returns ( 0 ) ;
906+ const upsResponse = { connectid : 'both' } ;
907+ const expectedStoredData = {
908+ connectid : 'both' ,
909+ puid : PUBLISHER_USER_ID ,
910+ lastSynced : 0 ,
911+ lastUsed : 0
912+ } ;
913+ const expiryDelta = new Date ( 60 * 60 * 24 * 365 * 1000 ) ;
914+ invokeGetIdAPI ( {
915+ puid : PUBLISHER_USER_ID ,
916+ pixelId : PIXEL_ID
917+ } , consentData , { type : 'cookie&html5' } ) ;
918+ const request = server . requests [ 0 ] ;
919+ request . respond (
920+ 200 ,
921+ { 'Content-Type' : 'application/json' } ,
922+ JSON . stringify ( upsResponse )
923+ ) ;
924+ dateNowStub . restore ( ) ;
925+
926+ expect ( setCookieStub . calledOnce ) . to . be . true ;
927+ expect ( setCookieStub . firstCall . args [ 0 ] ) . to . equal ( STORAGE_KEY ) ;
928+ expect ( setCookieStub . firstCall . args [ 1 ] ) . to . equal ( JSON . stringify ( expectedStoredData ) ) ;
929+ expect ( setCookieStub . firstCall . args [ 2 ] ) . to . equal ( expiryDelta . toUTCString ( ) ) ;
930+ expect ( setLocalStorageStub . calledOnce ) . to . be . true ;
931+ expect ( setLocalStorageStub . firstCall . args [ 0 ] ) . to . equal ( STORAGE_KEY ) ;
932+ expect ( setLocalStorageStub . firstCall . args [ 1 ] ) . to . deep . equal ( JSON . stringify ( expectedStoredData ) ) ;
933+ } ) ;
806934 } ) ;
807935 } ) ;
808936 describe ( 'userHasOptedOut()' , ( ) => {
0 commit comments