~james/core

core/web/src/lib/api/signup.test.ts -rw-r--r-- 10.6 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { describe, expect, it, vi, type Mock } from "vitest";
import {
	beginSignup,
	completeSignup,
	getSignup,
	resendSignup,
	SignupError
} from "#lib/api/signup.ts";
import type { Fetch } from "#lib/fetch.ts";

const calledInit = (mock: Mock<Fetch>) => mock.mock.calls[0][1] as RequestInit;

describe("beginSignup", () => {
	it("posts email and turnstile token to the beginSignup nsid without auth", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(new Response("", { status: 200 }));
		await beginSignup("https://deliberi.test", "alice@example.com", "turnstile-123", fetchMock);

		expect(fetchMock).toHaveBeenCalledTimes(1);
		const url = new URL(String(fetchMock.mock.calls[0][0]));
		expect(url.origin).toBe("https://deliberi.test");
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.account.beginSignup");
		expect(calledInit(fetchMock).method).toBe("POST");
		expect(JSON.parse(String(calledInit(fetchMock).body))).toEqual({
			email: "alice@example.com",
			turnstileToken: "turnstile-123"
		});
		expect(calledInit(fetchMock).headers).not.toHaveProperty("authorization");
	});

	it("surfaces the server's error message", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					error: "EmailAlreadyRegistered",
					message: "an account already exists for this email"
				}),
				{
					status: 409,
					headers: { "content-type": "application/json" }
				}
			)
		);
		await expect(
			beginSignup("https://deliberi.test", "a@b.co", "t", fetchMock)
		).rejects.toThrow("An account already exists for this email.");
	});

	it("falls back to the status when the body is not an xrpc error", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(new Response("nope", { status: 409 }));
		await expect(
			beginSignup("https://deliberi.test", "a@b.co", "t", fetchMock)
		).rejects.toThrow("Unable to send the link (HTTP 409). Try again in a moment.");
	});
});

describe("resendSignup", () => {
	it("posts the email to the resendSignup nsid without auth", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(new Response("", { status: 200 }));
		await resendSignup("https://deliberi.test", "alice@example.com", fetchMock);

		expect(fetchMock).toHaveBeenCalledTimes(1);
		const url = new URL(String(fetchMock.mock.calls[0][0]));
		expect(url.origin).toBe("https://deliberi.test");
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.account.resendSignup");
		expect(calledInit(fetchMock).method).toBe("POST");
		expect(JSON.parse(String(calledInit(fetchMock).body))).toEqual({
			email: "alice@example.com"
		});
		expect(calledInit(fetchMock).headers).not.toHaveProperty("authorization");
	});

	it("surfaces the server's message when nothing is pending", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					error: "NoPendingSignup",
					message: "no link is waiting for this email; sign up again"
				}),
				{
					status: 404,
					headers: { "content-type": "application/json" }
				}
			)
		);
		await expect(resendSignup("https://deliberi.test", "a@b.co", fetchMock)).rejects.toThrow(
			"No link is waiting for this email; sign up again."
		);
	});
});

describe("getSignup", () => {
	it("queries the token and returns the signup's email", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(JSON.stringify({ email: "alice@example.com" }), {
				status: 200,
				headers: { "content-type": "application/json" }
			})
		);
		await expect(getSignup("https://deliberi.test", "tok-123", fetchMock)).resolves.toEqual({
			email: "alice@example.com"
		});
		const url = new URL(String(fetchMock.mock.calls[0][0]));
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.account.getSignup");
		expect(url.searchParams.get("token")).toBe("tok-123");
	});

	it("returns the pds the account will live on", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(JSON.stringify({ email: "alice@example.com", pds: "https://pds.test" }), {
				status: 200,
				headers: { "content-type": "application/json" }
			})
		);
		await expect(getSignup("https://deliberi.test", "tok-123", fetchMock)).resolves.toEqual({
			email: "alice@example.com",
			pds: "https://pds.test"
		});
	});

	it("reports a dead link with its code", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					error: "InvalidCode",
					message: "invalid or expired verification link"
				}),
				{ status: 400, headers: { "content-type": "application/json" } }
			)
		);
		await expect(getSignup("https://deliberi.test", "bad", fetchMock)).rejects.toMatchObject({
			code: "InvalidCode"
		});
	});

	it("rejects a response without an email", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(new Response("{}", { status: 200 }));
		await expect(getSignup("https://deliberi.test", "tok", fetchMock)).rejects.toBeInstanceOf(
			SignupError
		);
	});
});

describe("completeSignup", () => {
	it("posts token, username, and password and parses did/handle", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(JSON.stringify({ did: "did:plc:x", handle: "alice.tngl.sh" }), {
				status: 200,
				headers: { "content-type": "application/json" }
			})
		);
		const out = await completeSignup(
			"https://deliberi.test",
			{ token: "tok-123", username: "alice", password: "password1234" },
			fetchMock
		);

		expect(out).toEqual({ did: "did:plc:x", handle: "alice.tngl.sh" });
		const url = new URL(String(fetchMock.mock.calls[0][0]));
		expect(url.pathname).toBe("/xrpc/org.tangled.temp.account.completeSignup");
		expect(calledInit(fetchMock).method).toBe("POST");
		expect(JSON.parse(String(calledInit(fetchMock).body))).toEqual({
			token: "tok-123",
			username: "alice",
			password: "password1234"
		});
		expect(calledInit(fetchMock).headers).not.toHaveProperty("authorization");
	});

	it("sends the oauth request and returns the pds's authorization", async () => {
		const authorization = { code: "cod-1", state: "sid-1", iss: "https://pds.test" };
		const fetchMock = vi
			.fn<Fetch>()
			.mockResolvedValue(
				new Response(
					JSON.stringify({ did: "did:plc:x", handle: "alice.tngl.sh", authorization }),
					{ status: 200, headers: { "content-type": "application/json" } }
				)
			);
		const out = await completeSignup(
			"https://deliberi.test",
			{ token: "tok", username: "alice", password: "password1234", requestUri: "urn:req" },
			fetchMock
		);

		expect(out).toEqual({ did: "did:plc:x", handle: "alice.tngl.sh", authorization });
		expect(JSON.parse(String(calledInit(fetchMock).body))).toMatchObject({
			requestUri: "urn:req"
		});
	});

	it("drops a malformed authorization so the page signs in through the pds", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					did: "did:plc:x",
					handle: "alice.tngl.sh",
					authorization: { code: "c" }
				}),
				{ status: 200, headers: { "content-type": "application/json" } }
			)
		);
		await expect(
			completeSignup(
				"https://deliberi.test",
				{
					token: "tok",
					username: "alice",
					password: "password1234",
					requestUri: "urn:req"
				},
				fetchMock
			)
		).resolves.toEqual({ did: "did:plc:x", handle: "alice.tngl.sh" });
	});

	it("surfaces the server's error message", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					error: "InvalidCode",
					message: "invalid or expired verification link"
				}),
				{
					status: 400,
					headers: { "content-type": "application/json" }
				}
			)
		);
		await expect(
			completeSignup(
				"https://deliberi.test",
				{ token: "bad", username: "alice", password: "password1234" },
				fetchMock
			)
		).rejects.toThrow("Invalid or expired verification link.");
	});

	it("keeps the service's error code", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					error: "UsernameNotAvailable",
					message: "this username is already taken"
				}),
				{
					status: 400,
					headers: { "content-type": "application/json" }
				}
			)
		);
		const failure = completeSignup(
			"https://deliberi.test",
			{ token: "tok", username: "alice", password: "password1234" },
			fetchMock
		);
		await expect(failure).rejects.toBeInstanceOf(SignupError);
		await expect(failure).rejects.toMatchObject({ code: "UsernameNotAvailable" });
	});

	it("falls back to the status when the body is not an xrpc error", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(new Response("bad", { status: 400 }));
		await expect(
			completeSignup(
				"https://deliberi.test",
				{ token: "bad", username: "alice", password: "password1234" },
				fetchMock
			)
		).rejects.toThrow("Unable to create the account (HTTP 400). Try again in a moment.");
	});

	it("replaces server failures with a readable sentence", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(
				JSON.stringify({
					error: "InternalServerError",
					message: "internal server error"
				}),
				{ status: 500, headers: { "content-type": "application/json" } }
			)
		);
		await expect(
			beginSignup("https://deliberi.test", "a@b.co", "token", fetchMock)
		).rejects.toThrow("Unable to reach Tangled. Try again in a moment.");
	});

	it("surfaces the error code when the payload omits the message", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(JSON.stringify({ error: "InvalidCode" }), {
				status: 400,
				headers: { "content-type": "application/json" }
			})
		);
		await expect(
			completeSignup(
				"https://deliberi.test",
				{ token: "bad", username: "alice", password: "password1234" },
				fetchMock
			)
		).rejects.toThrow("InvalidCode");
	});

	it("rejects a 2xx payload missing the handle", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(
			new Response(JSON.stringify({ did: "did:plc:x" }), {
				status: 200,
				headers: { "content-type": "application/json" }
			})
		);
		await expect(
			completeSignup(
				"https://deliberi.test",
				{ token: "tok", username: "alice", password: "password1234" },
				fetchMock
			)
		).rejects.toThrow("Unable to create the account. Try again in a moment.");
	});

	it("rejects an empty 2xx body instead of leaking a parse error", async () => {
		const fetchMock = vi.fn<Fetch>().mockResolvedValue(new Response("", { status: 200 }));
		await expect(
			completeSignup(
				"https://deliberi.test",
				{ token: "tok", username: "alice", password: "password1234" },
				fetchMock
			)
		).rejects.toThrow("Unable to create the account. Try again in a moment.");
	});
});