-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathgoogle-authorization.test.ts
More file actions
174 lines (156 loc) · 5.7 KB
/
google-authorization.test.ts
File metadata and controls
174 lines (156 loc) · 5.7 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Status } from "@core/errors/status.codes";
import { completeGoogleAuthorization } from "./complete-google-authorization";
import { GOOGLE_AUTH_SCOPES_REQUIRED } from "./google-authorization.constants";
import {
readGoogleAuthorizationIntent,
writeGoogleAuthorizationIntent,
} from "./google-authorization.storage";
import { beforeEach, describe, expect, it, mock } from "bun:test";
const makeDeps = () => ({
authApi: {
loginOrSignup: mock(async () => ({
createdNewRecipeUser: false,
status: "OK" as const,
user: { emails: ["user@example.com"] },
})),
connectGoogle: mock(async () => ({ status: "OK" as const })),
},
completeAuthentication: mock(async () => undefined),
refreshUserMetadata: mock(async () => undefined),
requestEventFetch: mock(() => undefined),
});
const callbackSearch = (
state: string,
scope = GOOGLE_AUTH_SCOPES_REQUIRED.join(" "),
) =>
`?state=${encodeURIComponent(
state,
)}&code=auth-code&scope=${encodeURIComponent(scope)}`;
describe("completeGoogleAuthorization", () => {
beforeEach(() => {
sessionStorage.clear();
});
it("completes a saved Google sign-in intent", async () => {
const deps = makeDeps();
writeGoogleAuthorizationIntent("state-1", {
intent: "signIn",
returnPath: "/week",
createdAt: Date.now(),
});
await expect(
completeGoogleAuthorization({
...deps,
search: callbackSearch("state-1"),
}),
).resolves.toEqual({ status: "completed", returnPath: "/week" });
expect(deps.authApi.loginOrSignup).toHaveBeenCalledWith(
expect.objectContaining({
redirectURIInfo: expect.objectContaining({
redirectURIQueryParams: expect.objectContaining({
code: "auth-code",
state: "state-1",
}),
}),
}),
);
expect(deps.completeAuthentication).toHaveBeenCalledWith({
email: "user@example.com",
});
expect(deps.authApi.connectGoogle).not.toHaveBeenCalled();
expect(readGoogleAuthorizationIntent("state-1")).toBeNull();
});
it("completes a saved Google Calendar connect intent", async () => {
const deps = makeDeps();
writeGoogleAuthorizationIntent("state-2", {
intent: "connectCalendar",
returnPath: "/day",
createdAt: Date.now(),
});
await expect(
completeGoogleAuthorization({
...deps,
search: callbackSearch("state-2"),
}),
).resolves.toEqual({ status: "completed", returnPath: "/day" });
expect(deps.authApi.connectGoogle).toHaveBeenCalledTimes(1);
expect(deps.authApi.connectGoogle.mock.calls[0]).toHaveLength(1);
expect(deps.refreshUserMetadata).toHaveBeenCalledTimes(1);
expect(deps.requestEventFetch).toHaveBeenCalledTimes(1);
expect(deps.completeAuthentication).not.toHaveBeenCalled();
});
it("uses Google sign-in recovery for a saved connect intent when the Compass session is missing", async () => {
const deps = makeDeps();
writeGoogleAuthorizationIntent("state-session-missing", {
intent: "connectCalendar",
returnPath: "/day",
createdAt: Date.now(),
});
await expect(
completeGoogleAuthorization({
...deps,
doesSessionExist: mock(async () => false),
search: callbackSearch("state-session-missing"),
}),
).resolves.toEqual({ status: "completed", returnPath: "/day" });
expect(deps.authApi.connectGoogle).not.toHaveBeenCalled();
expect(deps.authApi.loginOrSignup).toHaveBeenCalledTimes(1);
expect(deps.completeAuthentication).toHaveBeenCalledWith({
email: "user@example.com",
});
expect(deps.refreshUserMetadata).not.toHaveBeenCalled();
expect(deps.requestEventFetch).not.toHaveBeenCalled();
});
it("uses Google sign-in recovery when the connect endpoint rejects an expired Compass session", async () => {
const deps = makeDeps();
deps.authApi.connectGoogle.mockRejectedValue(
Object.assign(new Error("Request failed with status 401"), {
response: {
data: { message: "unauthorised" },
status: Status.UNAUTHORIZED,
},
}),
);
writeGoogleAuthorizationIntent("state-session-expired", {
intent: "connectCalendar",
returnPath: "/day",
createdAt: Date.now(),
});
await expect(
completeGoogleAuthorization({
...deps,
search: callbackSearch("state-session-expired"),
}),
).resolves.toEqual({ status: "completed", returnPath: "/day" });
expect(deps.authApi.connectGoogle).toHaveBeenCalledTimes(1);
expect(deps.authApi.connectGoogle.mock.calls[0]).toHaveLength(1);
expect(deps.authApi.loginOrSignup).toHaveBeenCalledTimes(1);
expect(deps.completeAuthentication).toHaveBeenCalledWith({
email: "user@example.com",
});
expect(deps.refreshUserMetadata).not.toHaveBeenCalled();
expect(deps.requestEventFetch).not.toHaveBeenCalled();
});
it("rejects callbacks that are missing required Google Calendar scopes", async () => {
const deps = makeDeps();
writeGoogleAuthorizationIntent("state-3", {
intent: "signIn",
returnPath: "/week",
createdAt: Date.now(),
});
await expect(
completeGoogleAuthorization({
...deps,
search: callbackSearch("state-3", GOOGLE_AUTH_SCOPES_REQUIRED[0]),
}),
).resolves.toEqual({
status: "failed",
message:
"Missing Google Calendar permissions. Please grant all requested permissions.",
returnPath: "/week",
});
expect(deps.authApi.loginOrSignup).not.toHaveBeenCalled();
expect(deps.authApi.connectGoogle).not.toHaveBeenCalled();
expect(deps.completeAuthentication).not.toHaveBeenCalled();
expect(readGoogleAuthorizationIntent("state-3")).toBeNull();
});
});