-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathinvites.node.test.ts
More file actions
91 lines (78 loc) · 2.96 KB
/
Copy pathinvites.node.test.ts
File metadata and controls
91 lines (78 loc) · 2.96 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
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterAll, expect, test } from "@effect/vitest";
import { mintInviteCode } from "../testing/mint-invite";
// Real Better Auth path: signup must be invite-gated.
process.env.EXECUTOR_DATA_DIR = mkdtempSync(join(tmpdir(), "eh-invite-"));
process.env.BETTER_AUTH_SECRET = "invite-test-secret-0123456789-abcdefghij-klmn";
process.env.EXECUTOR_BOOTSTRAP_ADMIN_EMAIL = "admin@invite.test";
process.env.EXECUTOR_BOOTSTRAP_ADMIN_PASSWORD = "admin-pass-123456";
const { makeSelfHostApiHandler } = await import("../app");
const { handler, dispose } = await makeSelfHostApiHandler();
afterAll(() => dispose());
const BASE = "http://localhost:4788";
const signUp = (body: Record<string, unknown>) =>
handler(
new Request(`${BASE}/api/auth/sign-up/email`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
}),
);
const inviteStatus = async (code: string): Promise<boolean> => {
const response = await handler(
new Request(`${BASE}/api/invite-status/${encodeURIComponent(code)}`),
);
expect(response.status).toBe(200);
const body = (await response.json()) as { valid?: boolean };
return body.valid === true;
};
test("open signup is closed: a signup without a valid invite code is rejected", async () => {
const res = await signUp({
email: "intruder@invite.test",
password: "password-12345678",
name: "Intruder",
});
expect(res.status).not.toBe(200);
const badCode = await signUp({
email: "intruder2@invite.test",
password: "password-12345678",
name: "Intruder",
inviteCode: "AAAA-BBBB-CCCC",
});
expect(badCode.status).not.toBe(200);
});
test("a code minted via the admin API redeems into a real org membership", async () => {
// Minted through the TYPED admin HttpApi client (see mint-invite.ts).
const inviteCode = await mintInviteCode(handler);
expect(await inviteStatus("AAAA-BBBB-CCCC")).toBe(false);
expect(await inviteStatus(inviteCode)).toBe(true);
const res = await signUp({
email: "member@invite.test",
password: "password-12345678",
name: "Member",
inviteCode,
});
expect(res.status).toBe(200);
const token = res.headers.get("set-auth-token") ?? "";
expect(token).not.toBe("");
// The new user resolves to a real org membership (the bound tenant).
const me = await handler(
new Request(`${BASE}/api/account/me`, {
headers: { authorization: `Bearer ${token}` },
}),
);
expect(me.status).toBe(200);
const meBody = (await me.json()) as { organization: { id: string } | null };
expect(meBody.organization?.id).toBeTruthy();
// The single-use code is now spent: reusing it is rejected.
const reuse = await signUp({
email: "second@invite.test",
password: "password-12345678",
name: "Second",
inviteCode,
});
expect(reuse.status).not.toBe(200);
expect(await inviteStatus(inviteCode)).toBe(false);
});