@podosoft/podokit 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/deploy-compose-render.js +2 -1
  2. package/dist/deploy-render.js +14 -8
  3. package/dist/lockfile.d.ts +8 -1
  4. package/dist/lockfile.js +20 -3
  5. package/dist/templates/fullstack/AGENTS.md +12 -0
  6. package/dist/templates/fullstack/README.md +1 -1
  7. package/dist/templates/fullstack/apps/api/scripts/run-migrations.mjs +4 -0
  8. package/dist/templates/fullstack/apps/api/src/app.extensions.ts +3 -1
  9. package/dist/templates/fullstack/apps/api/src/database/database.spec.ts +66 -0
  10. package/dist/templates/fullstack/apps/api/src/database/database.ts +34 -11
  11. package/dist/templates/fullstack/apps/api/src/database/run-migrations.spec.ts +54 -6
  12. package/dist/templates/fullstack/apps/web/Dockerfile +1 -1
  13. package/dist/templates/fullstack/apps/web/src/lib/server/api.test.ts +116 -0
  14. package/dist/templates/fullstack/apps/web/src/lib/server/api.ts +34 -4
  15. package/dist/templates/fullstack/apps/web/src/lib/server/backend-proxy.test.ts +175 -0
  16. package/dist/templates/fullstack/apps/web/src/lib/server/backend-proxy.ts +170 -11
  17. package/dist/templates/fullstack/apps/web/src/routes/healthz/+server.ts +13 -0
  18. package/dist/templates/fullstack/dot-env.docker.example +4 -0
  19. package/dist/templates/fullstack/dot-env.example +7 -0
  20. package/dist/templates/fullstack/infra/k3s/web-deployment.yaml +6 -3
  21. package/dist/templates/fullstack/tests/api/health.api.spec.ts +8 -0
  22. package/dist/templates/modules/admin-dashboard/files/apps/web/src/hooks.server.test.ts +24 -1
  23. package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/server/runtime-context.test.ts +6 -0
  24. package/dist/templates/modules/admin-dashboard/files/apps/web/src/lib/server/runtime-context.ts +5 -0
  25. package/dist/templates/modules/admin-dashboard/files/tests/api/auth-config.api.spec.ts +167 -154
  26. package/dist/templates/modules/admin-dashboard/files/tests/api/auth.api.spec.ts +94 -57
  27. package/dist/templates/modules/admin-dashboard/files/tests/api/exclusive.api.spec.ts +75 -0
  28. package/dist/templates/modules/admin-dashboard/files/tests/api/site-settings.api.spec.ts +49 -39
  29. package/dist/templates/modules/admin-dashboard/files/tests/helpers/exclusive.ts +80 -0
  30. package/dist/templates/modules/admin-dashboard/files/tests/ui/account.ui.spec.ts +47 -43
  31. package/dist/templates/modules/admin-dashboard/files/tests/ui/appearance.ui.spec.ts +15 -11
  32. package/dist/templates/modules/admin-dashboard/files/tests/ui/auth.ui.spec.ts +35 -31
  33. package/dist/templates/modules/admin-dashboard/files/tests/ui/general-effects.ui.spec.ts +90 -76
  34. package/dist/templates/modules/admin-dashboard/files/tests/ui/i18n.ui.spec.ts +19 -15
  35. package/dist/templates/modules/admin-dashboard/files/tests/ui/oidc.ui.spec.ts +17 -3
  36. package/dist/templates/modules/admin-dashboard/files/tests/ui/request-budget.ui.spec.ts +15 -6
  37. package/dist/templates/modules/admin-dashboard/files/tests/ui/settings-dialogs.ui.spec.ts +71 -64
  38. package/dist/templates/modules/admin-dashboard/files/tests/ui/settings.ui.spec.ts +89 -79
  39. package/dist/templates/modules/admin-dashboard/files/tests/ui/two-factor.ui.spec.ts +54 -50
  40. package/dist/templates/modules/auth/files/apps/api/src/auth/auth.ts +11 -5
  41. package/dist/templates/modules/auth/files/apps/api/src/auth/feature-gate.spec.ts +83 -0
  42. package/dist/templates/modules/auth/files/apps/api/src/auth/feature-gate.ts +51 -7
  43. package/dist/templates/todo/README.md +1 -1
  44. package/package.json +1 -1
@@ -4,6 +4,7 @@ import {
4
4
  isUserCreationAllowed,
5
5
  } from "../../apps/api/src/auth/feature-gate";
6
6
  import { ADMIN } from "../helpers/accounts";
7
+ import { exclusive } from "../helpers/exclusive";
7
8
 
8
9
  const base = process.env.E2E_BASE_URL ?? "http://localhost:5001";
9
10
  const origin = { origin: base };
@@ -17,62 +18,68 @@ async function adminCtx(playwright: import("@playwright/test").PlaywrightWorkerA
17
18
  }
18
19
 
19
20
  test("admin can enable Google OAuth from the DB and it applies without a restart", async ({ playwright }) => {
20
- const admin = await adminCtx(playwright);
21
- await admin.put("/api/account/auth-config", { data: { social: { google: { enabled: false } } } }); // clean start
22
- const anon = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
23
- const social = () => anon.post("/api/auth/sign-in/social", { data: { provider: "google", callbackURL: `${base}/admin` } });
24
-
25
- // Before: provider not configured.
26
- await expect.poll(async () => (await social()).status(), { timeout: 8000 }).toBe(404);
27
-
28
- // Paste credentials via the admin API.
29
- const put = await admin.put("/api/account/auth-config", {
30
- data: { social: { google: { enabled: true, clientId: "dummy-google-client-id", clientSecret: "dummy-google-client-secret" } } },
21
+ test.slow();
22
+ await exclusive("social-providers", async () => {
23
+ const admin = await adminCtx(playwright);
24
+ await admin.put("/api/account/auth-config", { data: { social: { google: { enabled: false } } } }); // clean start
25
+ const anon = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
26
+ const social = () => anon.post("/api/auth/sign-in/social", { data: { provider: "google", callbackURL: `${base}/admin` } });
27
+
28
+ // Before: provider not configured.
29
+ await expect.poll(async () => (await social()).status(), { timeout: 8000 }).toBe(404);
30
+
31
+ // Paste credentials via the admin API.
32
+ const put = await admin.put("/api/account/auth-config", {
33
+ data: { social: { google: { enabled: true, clientId: "dummy-google-client-id", clientSecret: "dummy-google-client-secret" } } },
34
+ });
35
+ expect(put.ok()).toBeTruthy();
36
+ const view = await put.json();
37
+ expect(view.social.google.enabled).toBe(true);
38
+ expect(view.social.google.hasSecret).toBe(true);
39
+ expect(view.social.google.clientId).toBe("dummy-google-client-id");
40
+ expect("clientSecret" in view.social.google).toBe(false); // secret is never returned
41
+
42
+ // Applied live (no restart): social sign-in now redirects to Google.
43
+ await expect
44
+ .poll(async () => {
45
+ const r = await social();
46
+ return r.ok() ? (((await r.json())?.url as string) ?? "") : "";
47
+ }, { timeout: 8000 })
48
+ .toContain("accounts.google.com");
49
+ const caps = await (await anon.get("/api/account/capabilities")).json();
50
+ expect(caps.providers).toContain("google");
51
+
52
+ await admin.put("/api/account/auth-config", { data: { social: { google: { enabled: false } } } }); // restore
53
+ await anon.dispose();
54
+ await admin.dispose();
31
55
  });
32
- expect(put.ok()).toBeTruthy();
33
- const view = await put.json();
34
- expect(view.social.google.enabled).toBe(true);
35
- expect(view.social.google.hasSecret).toBe(true);
36
- expect(view.social.google.clientId).toBe("dummy-google-client-id");
37
- expect("clientSecret" in view.social.google).toBe(false); // secret is never returned
38
-
39
- // Applied live (no restart): social sign-in now redirects to Google.
40
- await expect
41
- .poll(async () => {
42
- const r = await social();
43
- return r.ok() ? (((await r.json())?.url as string) ?? "") : "";
44
- }, { timeout: 8000 })
45
- .toContain("accounts.google.com");
46
- const caps = await (await anon.get("/api/account/capabilities")).json();
47
- expect(caps.providers).toContain("google");
48
-
49
- await admin.put("/api/account/auth-config", { data: { social: { google: { enabled: false } } } }); // restore
50
- await anon.dispose();
51
- await admin.dispose();
52
56
  });
53
57
 
54
58
  test("admin can add and remove a social provider dynamically", async ({ playwright }) => {
55
- const admin = await adminCtx(playwright);
56
- // The catalog offers providers beyond the two legacy env-backed ones.
57
- const catalog = (await (await admin.get("/api/account/auth-config")).json()).catalog as Array<{ id: string }>;
58
- expect(catalog.map((c) => c.id)).toEqual(expect.arrayContaining(["google", "github", "apple"]));
59
+ test.slow();
60
+ await exclusive("social-providers", async () => {
61
+ const admin = await adminCtx(playwright);
62
+ // The catalog offers providers beyond the two legacy env-backed ones.
63
+ const catalog = (await (await admin.get("/api/account/auth-config")).json()).catalog as Array<{ id: string }>;
64
+ expect(catalog.map((c) => c.id)).toEqual(expect.arrayContaining(["google", "github", "apple"]));
65
+
66
+ const anon = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
67
+ const github = () => anon.post("/api/auth/sign-in/social", { data: { provider: "github", callbackURL: `${base}/admin` } });
68
+
69
+ // Add GitHub (a provider with no env fallback) → applies live, no restart.
70
+ await admin.put("/api/account/auth-config", { data: { social: { github: { enabled: true, clientId: "dummy-github-client-id", clientSecret: "dummy-github-client-secret" } } } });
71
+ await expect
72
+ .poll(async () => { const r = await github(); return r.ok() ? (((await r.json())?.url as string) ?? "") : ""; }, { timeout: 8000 })
73
+ .toContain("github.com");
74
+
75
+ // Remove it → the row is deleted and the provider stops working (again live).
76
+ await admin.put("/api/account/auth-config", { data: { social: { github: { delete: true } } } });
77
+ expect("github" in (await (await admin.get("/api/account/auth-config")).json()).social).toBe(false);
78
+ await expect.poll(async () => (await github()).status(), { timeout: 8000 }).toBe(404);
59
79
 
60
- const anon = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
61
- const github = () => anon.post("/api/auth/sign-in/social", { data: { provider: "github", callbackURL: `${base}/admin` } });
62
-
63
- // Add GitHub (a provider with no env fallback) → applies live, no restart.
64
- await admin.put("/api/account/auth-config", { data: { social: { github: { enabled: true, clientId: "dummy-github-client-id", clientSecret: "dummy-github-client-secret" } } } });
65
- await expect
66
- .poll(async () => { const r = await github(); return r.ok() ? (((await r.json())?.url as string) ?? "") : ""; }, { timeout: 8000 })
67
- .toContain("github.com");
68
-
69
- // Remove it → the row is deleted and the provider stops working (again live).
70
- await admin.put("/api/account/auth-config", { data: { social: { github: { delete: true } } } });
71
- expect("github" in (await (await admin.get("/api/account/auth-config")).json()).social).toBe(false);
72
- await expect.poll(async () => (await github()).status(), { timeout: 8000 }).toBe(404);
73
-
74
- await anon.dispose();
75
- await admin.dispose();
80
+ await anon.dispose();
81
+ await admin.dispose();
82
+ });
76
83
  });
77
84
 
78
85
  test("auth-config is admin-only and never leaks secrets @smoke", async ({ playwright }) => {
@@ -85,43 +92,46 @@ test("auth-config is admin-only and never leaks secrets @smoke", async ({ playwr
85
92
  });
86
93
 
87
94
  test("automatic logout validates and applies a sliding session lifetime", async ({ playwright }) => {
88
- const admin = await adminCtx(playwright);
89
- const freshAdmin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
90
-
91
- try {
92
- expect(
93
- (await admin.put("/api/account/auth-config", {
94
- data: { server: { sessionIdleTimeoutMinutes: 4 } },
95
- })).status(),
96
- ).toBe(400);
97
-
98
- const enabled = await admin.put("/api/account/auth-config", {
99
- data: { server: { sessionIdleTimeoutMinutes: 30 } },
100
- });
101
- expect(enabled.ok()).toBeTruthy();
102
- expect((await enabled.json()).server.sessionIdleTimeoutMinutes).toBe(30);
103
-
104
- const caps = await (await admin.get("/api/account/capabilities")).json();
105
- expect(caps.sessionIdleTimeoutMinutes).toBe(30);
106
-
107
- const signIn = await freshAdmin.post("/api/auth/sign-in/email", {
108
- data: { email: ADMIN.email, password: ADMIN.password },
109
- });
110
- expect(signIn.ok()).toBeTruthy();
111
- expect(signIn.headers()["set-cookie"]).toMatch(/Max-Age=1800(?:;|,)/i);
112
-
113
- const sessions = (await (await admin.get("/api/auth/list-sessions")).json()) as Array<{
114
- expiresAt: string;
115
- }>;
116
- const latestExpiry = Math.max(...sessions.map((session) => new Date(session.expiresAt).getTime()));
117
- expect(latestExpiry).toBeLessThanOrEqual(Date.now() + 30 * 60_000 + 10_000);
118
- } finally {
119
- await admin.put("/api/account/auth-config", {
120
- data: { server: { sessionIdleTimeoutMinutes: null } },
121
- });
122
- await freshAdmin.dispose();
123
- await admin.dispose();
124
- }
95
+ test.slow();
96
+ await exclusive("session-idle-timeout", async () => {
97
+ const admin = await adminCtx(playwright);
98
+ const freshAdmin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
99
+
100
+ try {
101
+ expect(
102
+ (await admin.put("/api/account/auth-config", {
103
+ data: { server: { sessionIdleTimeoutMinutes: 4 } },
104
+ })).status(),
105
+ ).toBe(400);
106
+
107
+ const enabled = await admin.put("/api/account/auth-config", {
108
+ data: { server: { sessionIdleTimeoutMinutes: 30 } },
109
+ });
110
+ expect(enabled.ok()).toBeTruthy();
111
+ expect((await enabled.json()).server.sessionIdleTimeoutMinutes).toBe(30);
112
+
113
+ const caps = await (await admin.get("/api/account/capabilities")).json();
114
+ expect(caps.sessionIdleTimeoutMinutes).toBe(30);
115
+
116
+ const signIn = await freshAdmin.post("/api/auth/sign-in/email", {
117
+ data: { email: ADMIN.email, password: ADMIN.password },
118
+ });
119
+ expect(signIn.ok()).toBeTruthy();
120
+ expect(signIn.headers()["set-cookie"]).toMatch(/Max-Age=1800(?:;|,)/i);
121
+
122
+ const sessions = (await (await admin.get("/api/auth/list-sessions")).json()) as Array<{
123
+ expiresAt: string;
124
+ }>;
125
+ const latestExpiry = Math.max(...sessions.map((session) => new Date(session.expiresAt).getTime()));
126
+ expect(latestExpiry).toBeLessThanOrEqual(Date.now() + 30 * 60_000 + 10_000);
127
+ } finally {
128
+ await admin.put("/api/account/auth-config", {
129
+ data: { server: { sessionIdleTimeoutMinutes: null } },
130
+ });
131
+ await freshAdmin.dispose();
132
+ await admin.dispose();
133
+ }
134
+ });
125
135
  });
126
136
 
127
137
  test("new registrations require approval when the policy is enabled", async ({ playwright }) => {
@@ -204,75 +214,78 @@ test("new registrations require approval when the policy is enabled", async ({ p
204
214
  });
205
215
 
206
216
  test("closed public sign-up blocks new OAuth users but preserves existing sign-in", async ({ playwright }) => {
207
- const admin = await adminCtx(playwright);
208
- const anon = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
209
- const password = "Podokit3e-Str0ng!pw";
210
- const adminCreatedEmail = `closed-admin-${Date.now()}@example.com`;
211
- const initialSite = (await (await anon.get("/api/site/settings")).json()) as {
212
- allowSignup?: string | null;
213
- };
214
- const initialAllowSignup = initialSite.allowSignup === "false" ? "false" : "true";
215
- let adminCreatedId = "";
216
-
217
- try {
218
- expect(
219
- (await admin.put("/api/account/auth-config", {
220
- data: {
221
- social: {
222
- google: {
223
- enabled: true,
224
- clientId: "dummy-google-client-id",
225
- clientSecret: "dummy-google-client-secret",
217
+ test.slow();
218
+ await exclusive(["site-general", "social-providers"], async () => {
219
+ const admin = await adminCtx(playwright);
220
+ const anon = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
221
+ const password = "Podokit3e-Str0ng!pw";
222
+ const adminCreatedEmail = `closed-admin-${Date.now()}@example.com`;
223
+ const initialSite = (await (await anon.get("/api/site/settings")).json()) as {
224
+ allowSignup?: string | null;
225
+ };
226
+ const initialAllowSignup = initialSite.allowSignup === "false" ? "false" : "true";
227
+ let adminCreatedId = "";
228
+
229
+ try {
230
+ expect(
231
+ (await admin.put("/api/account/auth-config", {
232
+ data: {
233
+ social: {
234
+ google: {
235
+ enabled: true,
236
+ clientId: "dummy-google-client-id",
237
+ clientSecret: "dummy-google-client-secret",
238
+ },
226
239
  },
227
240
  },
228
- },
229
- })).ok(),
230
- ).toBeTruthy();
231
- expect((await admin.put("/api/site/settings", { data: { allowSignup: "false" } })).ok()).toBeTruthy();
241
+ })).ok(),
242
+ ).toBeTruthy();
243
+ expect((await admin.put("/api/site/settings", { data: { allowSignup: "false" } })).ok()).toBeTruthy();
232
244
 
233
- // The site policy uses a short cache shared by sign-up requests and user
234
- // creation hooks, so wait for the setting to become live.
235
- await new Promise((resolve) => setTimeout(resolve, 3_200));
236
-
237
- const signup = await anon.post("/api/auth/sign-up/email", {
238
- data: {
239
- email: `closed-${Date.now()}@example.com`,
240
- password,
241
- name: "Closed Registration",
242
- },
243
- });
244
- expect(signup.status()).toBe(403);
245
- expect((await signup.json()).code).toBe(PUBLIC_SIGNUP_DISABLED);
245
+ // The site policy uses a short cache shared by sign-up requests and user
246
+ // creation hooks, so wait for the setting to become live.
247
+ await new Promise((resolve) => setTimeout(resolve, 3_200));
246
248
 
247
- // OAuth initiation must stay available for existing social users. Only a
248
- // callback that attempts to create a new user is rejected by the DB hook.
249
- const social = await anon.post("/api/auth/sign-in/social", {
250
- data: { provider: "google", callbackURL: `${base}/` },
251
- });
252
- expect(social.ok()).toBeTruthy();
253
- expect(((await social.json()).url as string)).toContain("accounts.google.com");
254
- expect(isUserCreationAllowed(false, "/callback/google")).toBe(false);
255
- expect(isUserCreationAllowed(false, undefined)).toBe(false);
256
-
257
- // Deliberate administrator provisioning remains available while the public
258
- // site is closed.
259
- expect(isUserCreationAllowed(false, "/admin/create-user")).toBe(true);
260
- const adminCreated = await admin.post("/api/auth/admin/create-user", {
261
- data: { email: adminCreatedEmail, password, name: "Admin Created", role: "user" },
262
- });
263
- expect(adminCreated.ok()).toBeTruthy();
264
- adminCreatedId = ((await adminCreated.json()).user as { id: string }).id;
265
- } finally {
266
- if (adminCreatedId) {
267
- await admin.post("/api/auth/admin/remove-user", { data: { userId: adminCreatedId } });
249
+ const signup = await anon.post("/api/auth/sign-up/email", {
250
+ data: {
251
+ email: `closed-${Date.now()}@example.com`,
252
+ password,
253
+ name: "Closed Registration",
254
+ },
255
+ });
256
+ expect(signup.status()).toBe(403);
257
+ expect((await signup.json()).code).toBe(PUBLIC_SIGNUP_DISABLED);
258
+
259
+ // OAuth initiation must stay available for existing social users. Only a
260
+ // callback that attempts to create a new user is rejected by the DB hook.
261
+ const social = await anon.post("/api/auth/sign-in/social", {
262
+ data: { provider: "google", callbackURL: `${base}/` },
263
+ });
264
+ expect(social.ok()).toBeTruthy();
265
+ expect(((await social.json()).url as string)).toContain("accounts.google.com");
266
+ expect(isUserCreationAllowed(false, "/callback/google")).toBe(false);
267
+ expect(isUserCreationAllowed(false, undefined)).toBe(false);
268
+
269
+ // Deliberate administrator provisioning remains available while the public
270
+ // site is closed.
271
+ expect(isUserCreationAllowed(false, "/admin/create-user")).toBe(true);
272
+ const adminCreated = await admin.post("/api/auth/admin/create-user", {
273
+ data: { email: adminCreatedEmail, password, name: "Admin Created", role: "user" },
274
+ });
275
+ expect(adminCreated.ok()).toBeTruthy();
276
+ adminCreatedId = ((await adminCreated.json()).user as { id: string }).id;
277
+ } finally {
278
+ if (adminCreatedId) {
279
+ await admin.post("/api/auth/admin/remove-user", { data: { userId: adminCreatedId } });
280
+ }
281
+ await admin.put("/api/site/settings", { data: { allowSignup: initialAllowSignup } });
282
+ await admin.put("/api/account/auth-config", {
283
+ data: { social: { google: { enabled: false } } },
284
+ });
285
+ await new Promise((resolve) => setTimeout(resolve, 3_200));
286
+ await anon.get("/api/auth/get-session");
287
+ await anon.dispose();
288
+ await admin.dispose();
268
289
  }
269
- await admin.put("/api/site/settings", { data: { allowSignup: initialAllowSignup } });
270
- await admin.put("/api/account/auth-config", {
271
- data: { social: { google: { enabled: false } } },
272
- });
273
- await new Promise((resolve) => setTimeout(resolve, 3_200));
274
- await anon.get("/api/auth/get-session");
275
- await anon.dispose();
276
- await admin.dispose();
277
- }
290
+ });
278
291
  });
@@ -1,6 +1,7 @@
1
1
  import { expect, test } from "@playwright/test";
2
2
  import { createPublicKey, verify, type JsonWebKey } from "node:crypto";
3
3
  import { ADMIN, USER } from "../helpers/accounts";
4
+ import { exclusive } from "../helpers/exclusive";
4
5
  import { clearSms, smsSinkReachable, waitForSmsOtp } from "../helpers/sms";
5
6
  import { totpCode } from "../helpers/totp";
6
7
 
@@ -144,46 +145,49 @@ test("two-factor: regenerating backup codes invalidates the old set", async ({ p
144
145
  });
145
146
 
146
147
  test("require-2fa: gates an un-enrolled user until they enrol", async ({ playwright }) => {
147
- const admin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
148
- const caps = await (await admin.get("/api/account/capabilities")).json();
149
- test.skip(!caps?.twoFactor, "two-factor not enabled");
150
- await admin.post("/api/auth/sign-in/email", { data: { email: ADMIN.email, password: ADMIN.password } });
151
-
152
- const user = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
153
- const email = `rq-${Date.now()}@example.com`;
154
- const pw = "Podokit3e-Str0ng!pw";
155
- await user.post("/api/auth/sign-up/email", { data: { email, password: pw, name: "RQ" } });
156
-
157
- try {
158
- // Turn the policy on (admin; the toggle endpoint is allow-listed by the guard).
159
- await admin.put("/api/account/settings", { data: { require2fa: true } });
160
- // Capabilities carry the same policy as the dedicated endpoint.
161
- await expect(async () => {
162
- expect((await (await admin.get("/api/account/capabilities")).json()).require2fa).toBe(true);
163
- expect(await (await admin.get("/api/account/require-2fa")).json()).toEqual({ require2fa: true });
164
- }).toPass({ timeout: 8000 });
165
- // The guard caches for a few seconds — poll until the un-enrolled user is gated.
166
- await expect(async () => {
167
- expect((await user.get("/api/account/me")).status()).toBe(403);
168
- }).toPass({ timeout: 8000 });
169
-
170
- // Enrolling restores access.
171
- const enable = await (await user.post("/api/auth/two-factor/enable", { data: { password: pw } })).json();
172
- await user.post("/api/auth/two-factor/verify-totp", { data: { code: totpCode(enable.totpURI) } });
173
- expect((await user.get("/api/account/me")).status()).toBe(200);
174
- } finally {
175
- // Always turn it back off so the shared (un-enrolled) sessions aren't gated.
176
- await admin.put("/api/account/settings", { data: { require2fa: false } });
177
- await expect(async () => {
178
- const probe = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
179
- await probe.post("/api/auth/sign-in/email", { data: { email: USER.email, password: USER.password } });
180
- const status = (await probe.get("/api/account/me")).status();
181
- await probe.dispose();
182
- expect(status).toBe(200);
183
- }).toPass({ timeout: 8000 });
184
- await admin.dispose();
185
- await user.dispose();
186
- }
148
+ test.slow();
149
+ await exclusive("require2fa", async () => {
150
+ const admin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
151
+ const caps = await (await admin.get("/api/account/capabilities")).json();
152
+ test.skip(!caps?.twoFactor, "two-factor not enabled");
153
+ await admin.post("/api/auth/sign-in/email", { data: { email: ADMIN.email, password: ADMIN.password } });
154
+
155
+ const user = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
156
+ const email = `rq-${Date.now()}@example.com`;
157
+ const pw = "Podokit3e-Str0ng!pw";
158
+ await user.post("/api/auth/sign-up/email", { data: { email, password: pw, name: "RQ" } });
159
+
160
+ try {
161
+ // Turn the policy on (admin; the toggle endpoint is allow-listed by the guard).
162
+ await admin.put("/api/account/settings", { data: { require2fa: true } });
163
+ // Capabilities carry the same policy as the dedicated endpoint.
164
+ await expect(async () => {
165
+ expect((await (await admin.get("/api/account/capabilities")).json()).require2fa).toBe(true);
166
+ expect(await (await admin.get("/api/account/require-2fa")).json()).toEqual({ require2fa: true });
167
+ }).toPass({ timeout: 8000 });
168
+ // The guard caches for a few seconds — poll until the un-enrolled user is gated.
169
+ await expect(async () => {
170
+ expect((await user.get("/api/account/me")).status()).toBe(403);
171
+ }).toPass({ timeout: 8000 });
172
+
173
+ // Enrolling restores access.
174
+ const enable = await (await user.post("/api/auth/two-factor/enable", { data: { password: pw } })).json();
175
+ await user.post("/api/auth/two-factor/verify-totp", { data: { code: totpCode(enable.totpURI) } });
176
+ expect((await user.get("/api/account/me")).status()).toBe(200);
177
+ } finally {
178
+ // Always turn it back off so the shared (un-enrolled) sessions aren't gated.
179
+ await admin.put("/api/account/settings", { data: { require2fa: false } });
180
+ await expect(async () => {
181
+ const probe = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
182
+ await probe.post("/api/auth/sign-in/email", { data: { email: USER.email, password: USER.password } });
183
+ const status = (await probe.get("/api/account/me")).status();
184
+ await probe.dispose();
185
+ expect(status).toBe(200);
186
+ }).toPass({ timeout: 8000 });
187
+ await admin.dispose();
188
+ await user.dispose();
189
+ }
190
+ });
187
191
  });
188
192
 
189
193
  test("multi-session holds several accounts and switches between them", async ({ playwright }) => {
@@ -380,9 +384,39 @@ test("an organization can have a parent and manager members", async ({ playwrigh
380
384
  await ctx.dispose();
381
385
  });
382
386
 
383
- // OIDC discovery + the enabled/disabled behaviour is covered by the front-channel
384
- // test in tests/ui/oidc.ui.spec.ts (which owns toggling the oidcProvider flag), and
385
- // the server-side gate is covered generically by the feature-toggle test above.
387
+ test("the OIDC provider's discovery and endpoints follow its setting", async ({ playwright }) => {
388
+ // The provider's metadata is answered before endpoint hooks run, so it needs its
389
+ // own check that "off" hides it. The lock keeps tests/ui/oidc.ui.spec.ts, which
390
+ // also toggles the provider, from running at the same time.
391
+ test.slow();
392
+ const admin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
393
+ await admin.post("/api/auth/sign-in/email", { data: { email: ADMIN.email, password: ADMIN.password } });
394
+ const status = (path: string): Promise<number> => admin.get(path).then((r) => r.status());
395
+ const discovery = [
396
+ "/api/auth/.well-known/openid-configuration",
397
+ "/api/auth/.well-known/oauth-authorization-server",
398
+ ];
399
+ const allStatuses = (): Promise<number[]> =>
400
+ Promise.all([...discovery, "/api/auth/oauth2/authorize?client_id=probe"].map(status));
401
+
402
+ await exclusive("oidc-provider", async () => {
403
+ try {
404
+ expect((await admin.put("/api/account/settings", { data: { oidcProvider: false } })).ok()).toBeTruthy();
405
+ await expect.poll(allStatuses, { timeout: 8000 }).toEqual([404, 404, 404]);
406
+ // The jwt plugin's keys and session tokens are not part of the provider.
407
+ expect(await status("/api/auth/jwks")).toBe(200);
408
+
409
+ expect((await admin.put("/api/account/settings", { data: { oidcProvider: true } })).ok()).toBeTruthy();
410
+ await expect.poll(() => Promise.all(discovery.map(status)), { timeout: 8000 }).toEqual([200, 200]);
411
+ const metadata = await (await admin.get(discovery[0]!)).json();
412
+ expect(metadata.authorization_endpoint).toContain("/api/auth/oauth2/authorize");
413
+ } finally {
414
+ expect((await admin.put("/api/account/settings", { data: { oidcProvider: false } })).ok()).toBeTruthy();
415
+ }
416
+ await expect.poll(allStatuses, { timeout: 8000 }).toEqual([404, 404, 404]);
417
+ });
418
+ await admin.dispose();
419
+ });
386
420
 
387
421
  test("only admins can change settings @smoke", async ({ playwright }) => {
388
422
  const userCtx = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
@@ -393,24 +427,27 @@ test("only admins can change settings @smoke", async ({ playwright }) => {
393
427
  });
394
428
 
395
429
  test("disabling a feature blocks its endpoint server-side and re-enabling restores it @smoke", async ({ playwright }) => {
396
- // Uses a fresh admin context (not the shared storageState) so it never rotates
397
- // the seeded session. Toggles a flag and confirms the feature gate enforces it.
398
- const admin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
399
- await admin.post("/api/auth/sign-in/email", { data: { email: ADMIN.email, password: ADMIN.password } });
400
- const caps = await (await admin.get("/api/account/capabilities")).json();
401
- test.skip(!caps?.magicLink, "magic link not enabled");
430
+ test.slow();
431
+ await exclusive("magic-link", async () => {
432
+ // Uses a fresh admin context (not the shared storageState) so it never rotates
433
+ // the seeded session. Toggles a flag and confirms the feature gate enforces it.
434
+ const admin = await playwright.request.newContext({ baseURL: base, extraHTTPHeaders: origin });
435
+ await admin.post("/api/auth/sign-in/email", { data: { email: ADMIN.email, password: ADMIN.password } });
436
+ const caps = await (await admin.get("/api/account/capabilities")).json();
437
+ test.skip(!caps?.magicLink, "magic link not enabled");
402
438
 
403
- // A disabled feature's endpoint 404s; the gate reads the flag through a short cache.
404
- const status = (): Promise<number> =>
405
- admin.post("/api/auth/sign-in/magic-link", { data: { email: "probe@example.com" } }).then((r) => r.status());
439
+ // A disabled feature's endpoint 404s; the gate reads the flag through a short cache.
440
+ const status = (): Promise<number> =>
441
+ admin.post("/api/auth/sign-in/magic-link", { data: { email: "probe@example.com" } }).then((r) => r.status());
406
442
 
407
- expect((await admin.put("/api/account/settings", { data: { magicLink: false } })).ok()).toBeTruthy();
408
- await expect.poll(status, { timeout: 8000 }).toBe(404);
443
+ expect((await admin.put("/api/account/settings", { data: { magicLink: false } })).ok()).toBeTruthy();
444
+ await expect.poll(status, { timeout: 8000 }).toBe(404);
409
445
 
410
- expect((await admin.put("/api/account/settings", { data: { magicLink: true } })).ok()).toBeTruthy();
411
- await expect.poll(status, { timeout: 8000 }).not.toBe(404);
446
+ expect((await admin.put("/api/account/settings", { data: { magicLink: true } })).ok()).toBeTruthy();
447
+ await expect.poll(status, { timeout: 8000 }).not.toBe(404);
412
448
 
413
- await admin.dispose();
449
+ await admin.dispose();
450
+ });
414
451
  });
415
452
 
416
453
  test("sign-in with a wrong password is rejected", async ({ request }) => {
@@ -0,0 +1,75 @@
1
+ import { expect, test } from "@playwright/test";
2
+ import { spawnSync } from "node:child_process";
3
+ import { existsSync, mkdirSync, utimesSync, writeFileSync } from "node:fs";
4
+ import { rm } from "node:fs/promises";
5
+ import { join } from "node:path";
6
+ import { exclusive, lockPath } from "../helpers/exclusive";
7
+
8
+ // Checks the lock that specs changing shared settings hold. Names are unique per
9
+ // run so these never wait on, or release, a lock a real spec holds.
10
+ const unique = (label: string): string => `self-test-${label}-${process.pid}-${Date.now()}`;
11
+
12
+ test("exclusive releases its locks when the body fails", async () => {
13
+ const first = unique("first");
14
+ const second = unique("second");
15
+ await expect(
16
+ exclusive([second, first], async () => {
17
+ expect(existsSync(lockPath(first))).toBe(true);
18
+ expect(existsSync(lockPath(second))).toBe(true);
19
+ throw new Error("body failed");
20
+ }),
21
+ ).rejects.toThrow("body failed");
22
+ expect(existsSync(lockPath(first))).toBe(false);
23
+ expect(existsSync(lockPath(second))).toBe(false);
24
+
25
+ // The next holder gets the lock without waiting.
26
+ const started = Date.now();
27
+ await exclusive(first, async () => undefined);
28
+ expect(Date.now() - started).toBeLessThan(1_000);
29
+ });
30
+
31
+ test("exclusive runs holders of one lock one at a time", async () => {
32
+ const name = unique("serial");
33
+ const events: string[] = [];
34
+ const hold = (id: string): Promise<void> =>
35
+ exclusive(name, async () => {
36
+ events.push(`${id} start`);
37
+ await new Promise((resolve) => setTimeout(resolve, 100));
38
+ events.push(`${id} end`);
39
+ });
40
+ await Promise.all([hold("a"), hold("b")]);
41
+ expect(events.map((event) => event.split(" ")[1])).toEqual(["start", "end", "start", "end"]);
42
+ expect(existsSync(lockPath(name))).toBe(false);
43
+ });
44
+
45
+ test("exclusive takes over a lock left by a run that was killed", async () => {
46
+ const name = unique("stale");
47
+ mkdirSync(lockPath(name));
48
+ const longAgo = new Date(Date.now() - 10 * 60_000);
49
+ utimesSync(lockPath(name), longAgo, longAgo);
50
+ await expect(exclusive(name, async () => "acquired")).resolves.toBe("acquired");
51
+ expect(existsSync(lockPath(name))).toBe(false);
52
+ });
53
+
54
+ test("exclusive takes over a lock whose holder exited, such as after Ctrl-C", async () => {
55
+ const name = unique("exited");
56
+ const exited = spawnSync(process.execPath, ["-e", ""]).pid;
57
+ mkdirSync(lockPath(name));
58
+ writeFileSync(join(lockPath(name), "owner"), String(exited));
59
+ const started = Date.now();
60
+ await expect(exclusive(name, async () => "acquired")).resolves.toBe("acquired");
61
+ expect(Date.now() - started).toBeLessThan(1_000);
62
+ expect(existsSync(lockPath(name))).toBe(false);
63
+ });
64
+
65
+ test("exclusive waits for a lock whose holder is still running", async () => {
66
+ const name = unique("alive");
67
+ mkdirSync(lockPath(name));
68
+ writeFileSync(join(lockPath(name), "owner"), String(process.pid));
69
+ const waiting = exclusive(name, async () => "acquired");
70
+ await new Promise((resolve) => setTimeout(resolve, 500));
71
+ expect(existsSync(lockPath(name))).toBe(true);
72
+ // The holder finishes; the waiter then gets the lock.
73
+ await rm(lockPath(name), { recursive: true, force: true });
74
+ await expect(waiting).resolves.toBe("acquired");
75
+ });