Skip to content

Commit

Permalink
add some unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyi-gao-ttd committed Oct 16, 2023
1 parent 15bb318 commit d37b7e2
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
78 changes: 78 additions & 0 deletions src/integrationTests/clientSideTokenGenerate.test.ts
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
invalidIdentity: "test.com",
setIdentityFn: "setIdentityFromEmail",
},
{
identity: "lz3+Rj7IV4X1+Vr1ujkG7tstkxwk5pgkqJ6mXbpOgTs=",
invalidIdentity: "[email protected]",
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", () => {});
});
});
});
});
});
12 changes: 10 additions & 2 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 || {}),
};
}
Empty file.
65 changes: 65 additions & 0 deletions src/unitTests/cstgSetIdentity.test.ts
Original file line number Diff line number Diff line change
@@ -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("[email protected]", 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");
});
});
File renamed without changes.

0 comments on commit d37b7e2

Please sign in to comment.