-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathgoogle-auth-callback.api.test.ts
More file actions
97 lines (85 loc) · 2.56 KB
/
google-auth-callback.api.test.ts
File metadata and controls
97 lines (85 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Status } from "@core/errors/status.codes";
import { type GoogleAuthCodeRequest } from "@core/types/auth.types";
import { BaseApi } from "@web/common/apis/base/base.api";
import { session } from "@web/common/classes/Session";
import { GoogleAuthCallbackApi } from "./google-auth-callback.api";
import {
afterEach,
beforeEach,
describe,
expect,
it,
mock,
spyOn,
} from "bun:test";
const originalFetch = globalThis.fetch;
const payload: GoogleAuthCodeRequest = {
clientType: "web",
redirectURIInfo: {
redirectURIOnProviderDashboard:
"http://localhost:9080/auth/google/callback",
redirectURIQueryParams: {
code: "auth-code",
scope: "https://www.googleapis.com/auth/calendar",
state: "state-1",
},
},
thirdPartyId: "google",
};
describe("GoogleAuthCallbackApi", () => {
beforeEach(() => {
BaseApi.defaults.adapter = undefined;
});
afterEach(() => {
globalThis.fetch = originalFetch;
BaseApi.defaults.adapter = undefined;
});
it("lets the callback flow handle an expired connect session locally", async () => {
const signOutSpy = spyOn(session, "signOut").mockResolvedValue(undefined);
globalThis.fetch = mock(async () =>
Promise.resolve(
new Response(JSON.stringify({ message: "unauthorised" }), {
status: Status.UNAUTHORIZED,
}),
),
) as unknown as typeof fetch;
await expect(
GoogleAuthCallbackApi.connectGoogle(payload),
).rejects.toMatchObject({
name: "ApiError",
response: {
status: Status.UNAUTHORIZED,
},
});
expect(signOutSpy).not.toHaveBeenCalled();
expect(globalThis.fetch).toHaveBeenCalledWith(
expect.stringContaining("/auth/google/connect"),
expect.objectContaining({
credentials: "include",
method: "POST",
}),
);
signOutSpy.mockRestore();
});
it("uses shared session handling for non-recoverable connect session errors", async () => {
window.history.pushState({}, "", "/day");
const signOutSpy = spyOn(session, "signOut").mockResolvedValue(undefined);
globalThis.fetch = mock(async () =>
Promise.resolve(
new Response(JSON.stringify({ message: "not found" }), {
status: Status.NOT_FOUND,
}),
),
) as unknown as typeof fetch;
await expect(
GoogleAuthCallbackApi.connectGoogle(payload),
).rejects.toMatchObject({
name: "ApiError",
response: {
status: Status.NOT_FOUND,
},
});
expect(signOutSpy).toHaveBeenCalledTimes(1);
signOutSpy.mockRestore();
});
});