Skip to content

Commit 8ec3f7b

Browse files
committed
test: improvement to tests
1 parent 0368e77 commit 8ec3f7b

File tree

4 files changed

+43
-80
lines changed

4 files changed

+43
-80
lines changed

lib/utils/checkAuth.test.ts

+16-47
Original file line numberDiff line numberDiff line change
@@ -61,51 +61,20 @@ describe("checkAuth", () => {
6161
});
6262
});
6363

64-
// it.only('should use cookie refresh type when using custom domain and no _kbrte cookie', async () => {
65-
// vi.spyOn(refreshToken, "refreshToken").mockResolvedValue({
66-
// success: true,
67-
// });
68-
69-
// const result = await checkAuth({ domain: "test.kinde.com", clientId });
70-
71-
// expect(refreshToken.refreshToken).toHaveBeenCalledWith({
72-
// domain: "test.kinde.com",
73-
// clientId,
74-
// refreshType: refreshToken.RefreshType.refreshToken,
75-
// });
76-
// expect(result).toEqual({});
77-
// });
78-
79-
// it('should use refresh token type when not using custom domain', async () => {
80-
// (refreshToken as vi.Mock).mockResolvedValue({} as RefreshTokenResult);
81-
82-
// const result = await checkAuth({ domain: 'test.kinde.com', clientId });
83-
84-
// expect(refreshToken).toHaveBeenCalledWith({
85-
// domain: 'not-custom.com',
86-
// clientId,
87-
// refreshType: RefreshType.refreshToken,
88-
// });
89-
// expect(result).toEqual({});
90-
// });
91-
92-
// it('should use refresh token type when forceLocalStorage is true', async () => {
93-
// (refreshToken as vi.Mock).mockResolvedValue({} as RefreshTokenResult);
94-
95-
// // Mock storageSettings to force local storage
96-
// const originalStorageSettings = storageSettings;
97-
// storageSettings.useInsecureForRefreshToken = true;
98-
99-
// const result = await checkAuth({ domain, clientId });
100-
101-
// expect(refreshToken).toHaveBeenCalledWith({
102-
// domain,
103-
// clientId,
104-
// refreshType: RefreshType.refreshToken,
105-
// });
106-
// expect(result).toEqual({});
107-
108-
// // Restore original storageSettings
109-
// storageSettings.useInsecureForRefreshToken = originalStorageSettings.useInsecureForRefreshToken;
110-
// });
64+
it("error when domain supplied", async () => {
65+
const result = await CheckAuth.checkAuth({ domain: null, clientId });
66+
expect (result).toEqual({
67+
success: false,
68+
error: "Domain is required for authentication check",
69+
});
70+
});
71+
72+
it("error when clientId supplied", async () => {
73+
const result = await CheckAuth.checkAuth({ domain, clientId: null });
74+
expect (result).toEqual({
75+
success: false,
76+
error: "Client ID is required for authentication check",
77+
});
78+
});
79+
11180
});

lib/utils/exchangeAuthCode.test.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,10 @@ describe("exchangeAuthCode", () => {
409409
});
410410

411411
it("should return error if code verifier is missing", async () => {
412-
const state = new MemoryStorage();
413-
await state.setSessionItem(StorageKeys.state, "test");
414-
setActiveStorage(state);
412+
const store = new MemoryStorage();
413+
await store.setSessionItem(StorageKeys.state, "test");
414+
setActiveStorage(store);
415+
415416
const urlParams = new URLSearchParams();
416417
urlParams.append("state", "test");
417418
urlParams.append("code", "test");
@@ -444,16 +445,14 @@ describe("exchangeAuthCode", () => {
444445
});
445446
fetchMock.mockRejectOnce(new Error("Fetch failed"));
446447

447-
try {
448-
await exchangeAuthCode({
448+
await expect(
449+
exchangeAuthCode({
449450
urlParams,
450451
domain: "test.com",
451452
clientId: "test",
452453
redirectURL: "test.com",
453-
});
454-
} catch (error) {
455-
expect((error as Error).message).toBe("Fetch failed");
456-
}
454+
})
455+
).rejects.toThrow("Fetch failed");
457456
});
458457

459458
it("should return error if token response is invalid", async () => {

lib/utils/getCookie.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,10 @@ describe("getCookie", () => {
8484
const result = getCookie("_kbrte");
8585
expect(result).toBeNull();
8686
});
87+
88+
it("should return null if parts.pop() returns undefined", () => {
89+
document.cookie = "_kbrte=%E0%A4%A;path=/";
90+
const result = getCookie("_kbrte");
91+
expect(result).toBeNull();
92+
});
8793
});

lib/utils/getCookie.ts

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
export function getCookie(name: string): string | null {
2-
const cookies = document.cookie.split(";");
3-
for (let i = 0; i < cookies.length; i++) {
4-
const cookie = cookies[i].trim();
5-
if (cookie.startsWith(name + "=")) {
6-
const cookieValue = cookie.substring(name.length + 1);
7-
const parts = cookieValue.split(";");
8-
let hasExpiration = false;
9-
for (let j = 0; j < parts.length; j++) {
10-
const part = parts[j].trim();
11-
if (part.startsWith("expires=")) {
12-
hasExpiration = true;
13-
const expirationDate = new Date(part.substring(8));
14-
if (expirationDate > new Date()) {
15-
return cookieValue || null; // Cookie is valid
16-
} else {
17-
return null; // Cookie is expired
18-
}
19-
}
20-
}
21-
if (!hasExpiration) {
22-
return cookieValue || null; // Cookie has no expiration, so it's valid
23-
}
24-
}
2+
const cookies = document.cookie.split('; ');
3+
console.log(cookies);
4+
const cookieMatch = cookies.find(c => c.startsWith(`${name}=`));
5+
6+
if (!cookieMatch) return null;
7+
8+
try {
9+
console.log('cookieMatch:', cookieMatch);
10+
const value = cookieMatch.split('=')[1];
11+
return value ? decodeURIComponent(value) : null;
12+
} catch (e) {
13+
console.error(`Error parsing cookie ${name}:`, e);
14+
return null;
2515
}
26-
return null; // Cookie not found
2716
}

0 commit comments

Comments
 (0)