Skip to content

Commit ea6f0e4

Browse files
added tests for cstg and refresh timer
1 parent 75b4952 commit ea6f0e4

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/integrationTests/options.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,46 @@ describe('calls the NoIdentityAvailable event', () => {
638638
identity: null,
639639
});
640640
});
641+
test('when cstg does not succeed', () => {
642+
uid2.init({});
643+
expect(uid2.setIdentityFromEmail('a', mocks.makeUid2CstgOption())).rejects.toThrow(
644+
'Invalid email address'
645+
);
646+
647+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
648+
identity: null,
649+
});
650+
});
651+
test('when an identity was valid but has since expired', () => {
652+
uid2.init({});
653+
expect(uid2.setIdentityFromEmail('a', mocks.makeUid2CstgOption())).rejects.toThrow(
654+
'Invalid email address'
655+
);
656+
657+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
658+
identity: null,
659+
});
660+
});
661+
test('when identity was valid on init but has since expired', () => {
662+
const refreshFrom = Date.now() + 100;
663+
const originalIdentity = makeIdentity({
664+
advertising_token: 'original_advertising_token',
665+
identity_expires: refreshFrom,
666+
//refresh_from: refreshFrom,
667+
});
668+
669+
uid2.init({ identity: originalIdentity });
670+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
671+
672+
// set time to an expired date for this identity
673+
jest.setSystemTime(originalIdentity.refresh_expires * 1000 + 1);
674+
675+
uid2.isIdentityAvailable();
676+
677+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
678+
identity: originalIdentity,
679+
});
680+
});
641681
});
642682

643683
describe('does not call NoIdentityAvailable event', () => {
@@ -674,4 +714,15 @@ describe('does not call NoIdentityAvailable event', () => {
674714
identity: null,
675715
});
676716
});
717+
test('when cstg is successful', async () => {
718+
uid2.init({});
719+
handler = jest.fn();
720+
expect(async () => {
721+
await uid2.setIdentityFromEmail('[email protected]', mocks.makeUid2CstgOption());
722+
}).not.toThrow();
723+
724+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
725+
identity: null,
726+
});
727+
});
677728
});

src/sdkBase.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export abstract class SdkBase {
157157
} else {
158158
this.triggerRefreshOrSetTimer(validatedIdentity);
159159
}
160+
this._callbackManager.runCallbacks(EventType.IdentityUpdated, {});
160161
}
161162
this.isIdentityAvailable();
162163
}
@@ -193,6 +194,11 @@ export abstract class SdkBase {
193194
}
194195

195196
public isLoginRequired() {
197+
const identity = this._identity ?? this.getIdentityNoInit();
198+
// if identity temporarily unavailable, login is not required
199+
if (this.temporarilyUnavailable(identity)) {
200+
return false;
201+
}
196202
return !this.isIdentityAvailable();
197203
}
198204

@@ -526,6 +532,7 @@ export abstract class SdkBase {
526532
} else {
527533
const errorText = 'Unexpected status received from CSTG endpoint.';
528534
this._logger.warn(errorText);
535+
this.isIdentityAvailable();
529536
throw new Error(errorText);
530537
}
531538
}

0 commit comments

Comments
 (0)