From d37b7e2b50f470f033e70c6033304051aa05d534 Mon Sep 17 00:00:00 2001 From: Jingyi Gao Date: Mon, 16 Oct 2023 23:15:08 +1100 Subject: [PATCH] add some unittests --- .../clientSideTokenGenerate.test.ts | 78 +++++++++++++++++++ src/mocks.ts | 12 ++- src/unitTests/callCstgApi.test.ts | 0 src/unitTests/cstgSetIdentity.test.ts | 65 ++++++++++++++++ .../diiNormalization.test.ts | 0 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/integrationTests/clientSideTokenGenerate.test.ts create mode 100644 src/unitTests/callCstgApi.test.ts create mode 100644 src/unitTests/cstgSetIdentity.test.ts rename src/{integrationTests => unitTests}/diiNormalization.test.ts (100%) diff --git a/src/integrationTests/clientSideTokenGenerate.test.ts b/src/integrationTests/clientSideTokenGenerate.test.ts new file mode 100644 index 0000000..1a77944 --- /dev/null +++ b/src/integrationTests/clientSideTokenGenerate.test.ts @@ -0,0 +1,78 @@ +import * as mocks from "../mocks"; +import { ClientSideIdentityOptions } from "../uid2ClientSideIdentityOptions"; +import { sdkWindow, UID2 } from "../uid2Sdk"; + +let callback: any; +let uid2: UID2; +let xhrMock: any; +let _cryptoMock; + +const clientSideOpt: ClientSideIdentityOptions = { + serverPublicKey: + "UID2-X-L-24B8a/eLYBmRkXA9yPgRZt+ouKbXewG2OPs23+ov3JC8mtYJBCx6AxGwJ4MlwUcguebhdDp2CvzsCgS9ogwwGA==", + subscriptionId: "subscription-id", +}; + +mocks.setupFakeTime(); + +beforeEach(() => { + callback = jest.fn(); + uid2 = new UID2(); + xhrMock = new mocks.XhrMock(sdkWindow); + _cryptoMock = new mocks.CryptoMock(sdkWindow); + mocks.setCookieMock(sdkWindow.document); + removeUid2LocalStorage(); +}); + +afterEach(() => { + mocks.resetFakeTime(); +}); + +const getUid2LocalStorage = mocks.getUid2LocalStorage; +const removeUid2LocalStorage = mocks.removeUid2LocalStorage; + +describe.only("Using Client-side token generation", () => { + let scenarios = [ + { + validIdentity: "test@example.com", + invalidIdentity: "test.com", + setIdentityFn: "setIdentityFromEmail", + }, + { + identity: "lz3+Rj7IV4X1+Vr1ujkG7tstkxwk5pgkqJ6mXbpOgTs=", + invalidIdentity: "test@example.com", + setIdentity: "setIdentityFromEmailHash", + }, + { + identity: "+12345678910", + invalidIdentity: "12345678910", + setIdentity: "setIdentityFromPhone", + }, + { + identity: "kVJ+4ilhrqm3HZDDnCQy4niZknvCoM4MkoVzZrQSdJw=", + invalidIdentity: "+12345678910", + setIdentity: "setIdentityFromPhoneHash", + }, + ]; + + scenarios.forEach((scenario) => { + describe(scenario.setIdentity!, () => { + describe("When invalid identity is provided", () => { + test("should throw error and not generate UID2", () => {}); + }); + + describe("When valid identity is provided", () => { + describe("when call cstg API succeeds", () => { + test("should invoke the callback", () => {}); + test("should set identity to storage", () => {}); + test("should auto refresh token once token expired", () => {}); + test("should be in available state", () => {}); + }); + + describe("when call cstg API failed", () => { + test("should not set identity", () => {}); + }); + }); + }); + }); +}); diff --git a/src/mocks.ts b/src/mocks.ts index 46b788a..8cb3d2d 100644 --- a/src/mocks.ts +++ b/src/mocks.ts @@ -148,8 +148,7 @@ export function setUid2Cookie(value: any) { } export function removeUid2Cookie() { - document.cookie = - document.cookie + "=;expires=Tue, 1 Jan 1980 23:59:59 GMT"; + document.cookie = document.cookie + "=;expires=Tue, 1 Jan 1980 23:59:59 GMT"; } export async function flushPromises() { @@ -230,3 +229,12 @@ export function makeIdentityV2(overrides = {}) { ...(overrides || {}), }; } + +export function makeCstgOption(overrides?: any) { + return { + serverPublicKey: + "UID2-X-L-24B8a/eLYBmRkXA9yPgRZt+ouKbXewG2OPs23+ov3JC8mtYJBCx6AxGwJ4MlwUcguebhdDp2CvzsCgS9ogwwGA==", + subscriptionId: "subscription-id", + ...(overrides || {}), + }; +} diff --git a/src/unitTests/callCstgApi.test.ts b/src/unitTests/callCstgApi.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/unitTests/cstgSetIdentity.test.ts b/src/unitTests/cstgSetIdentity.test.ts new file mode 100644 index 0000000..aa835a3 --- /dev/null +++ b/src/unitTests/cstgSetIdentity.test.ts @@ -0,0 +1,65 @@ +import { makeCstgOption } from "../mocks"; +import { + ClientSideIdentityOptions, + isClientSideIdentityOptionsOrThrow, +} from "../uid2ClientSideIdentityOptions"; +import { UID2 } from "../uid2Sdk"; + +let uid2: UID2; + +const clientSideOpt: ClientSideIdentityOptions = { + serverPublicKey: + "UID2-X-L-24B8a/eLYBmRkXA9yPgRZt+ouKbXewG2OPs23+ov3JC8mtYJBCx6AxGwJ4MlwUcguebhdDp2CvzsCgS9ogwwGA==", + subscriptionId: "subscription-id", +}; + +describe("CSTG Set Identity Tests", () => { + beforeEach(() => { + uid2 = new UID2(); + }); + + describe("When setIdentity is called before init", () => { + test("should throw init not complete error", async () => { + try { + await uid2.setIdentityFromEmail("test@123.com", makeCstgOption()); + fail("Expected an error to be thrown"); + } catch (err: unknown) { + expect(err).toBeInstanceOf(Error); + } + }); + }); +}); + +describe("#isClientSideIdentityOptionsOrThrow", () => { + test("should throw opts must be an object error when config is not object", () => { + expect(() => isClientSideIdentityOptionsOrThrow("")).toThrow( + "opts must be an object" + ); + }); + test("should throw serverPublicKey must be a string error when serverPublicKey is not a string", () => { + expect(() => + isClientSideIdentityOptionsOrThrow( + makeCstgOption({ serverPublicKey: {} }) + ) + ).toThrow("opts.serverPublicKey must be a string"); + }); + test("should throw serverPublicKey prefix when serverPublicKey is invalid", () => { + expect(() => + isClientSideIdentityOptionsOrThrow( + makeCstgOption({ serverPublicKey: "test-server-public-key" }) + ) + ).toThrow( + "opts.serverPublicKey must match the regular expression /^UID2-X-[A-Z]-.+/" + ); + }); + test("should throw subscriptionId must be a string error when subscriptionId is not a string", () => { + expect(() => + isClientSideIdentityOptionsOrThrow(makeCstgOption({ subscriptionId: {} })) + ).toThrow("opts.subscriptionId must be a string"); + }); + test("should throw subscriptionId is empty error when subscriptionId is not given", () => { + expect(() => + isClientSideIdentityOptionsOrThrow(makeCstgOption({ subscriptionId: "" })) + ).toThrow("opts.subscriptionId is empty"); + }); +}); diff --git a/src/integrationTests/diiNormalization.test.ts b/src/unitTests/diiNormalization.test.ts similarity index 100% rename from src/integrationTests/diiNormalization.test.ts rename to src/unitTests/diiNormalization.test.ts