WIP 2.0.0: v1 removed; Argon2id passwords; node-fetch gone
- /api (v1) answers 410 API_V1_REMOVED, except provider webhooks (/api/webhooks/*, /api/whatsapp/webhook) and links in sent messages (308s, one-click POST). 43 v1 route files and deprecation.ts removed; /docs redirects to /docs/v2; API_DOCUMENTATION.md points there. - Read-only and impersonation lists name v2 paths only. The campus scope route test now runs over v2 routes (v2CampusScope.test.ts). - Passwords hashed with Argon2id (@node-rs/argon2); bcrypt hashes still sign in and are re-hashed at the next sign-in. - e2e on /api/v2 only (v2() helper); Playwright waits on /api/v2. - MIGRATING-TO-2.md with the full v1 -> v2 table. - Two documented response shapes corrected. Backend 690 tests and e2e 102/102 (PayFast sandbox skipped) pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Page } from "@playwright/test";
|
||||
import { test, expect, seed, loginAs, api, API } from "./fixtures";
|
||||
import { test, expect, seed, loginAs, api, API, v2 } from "./fixtures";
|
||||
|
||||
type Account = { id: string; token: string; email: string };
|
||||
type PrivacyInfo = { news: { email: boolean; whatsapp: boolean }; history: { source: string }[]; requests: { type: string; status: string }[] };
|
||||
@@ -9,12 +9,12 @@ type Requests = { requests: { id: string; type: string; userId: string; blockers
|
||||
/** A brand-new account made through sign-up, so tests can change it freely. */
|
||||
async function signUp(page: Page, newsOptIn: boolean): Promise<Account> {
|
||||
const email = `privacy-${Date.now()}-${Math.floor(Math.random() * 1e6)}@e2e.test`;
|
||||
const res = await page.request.post(`${API}/api/users`, {
|
||||
data: { name: "Pat Privacy", email, password: "E2e-password-123", newsOptIn },
|
||||
const res = await page.request.post(`${API}/api/v2/auth/signup`, {
|
||||
data: { name: "Pat Privacy", email, password: "E2e-password-123", newsOptIn, acceptTerms: true },
|
||||
});
|
||||
expect(res.ok()).toBe(true);
|
||||
const body = await res.json();
|
||||
return { id: body.id, token: body.token, email };
|
||||
const { data } = await res.json();
|
||||
return { id: data.user.id, token: data.token, email };
|
||||
}
|
||||
|
||||
async function asAccount<T>(page: Page, account: Account, method: string, url: string, body?: unknown) {
|
||||
@@ -23,13 +23,15 @@ async function asAccount<T>(page: Page, account: Account, method: string, url: s
|
||||
headers: { Authorization: `Bearer ${account.token}`, "Content-Type": "application/json" },
|
||||
data: body === undefined ? undefined : JSON.stringify(body),
|
||||
});
|
||||
return { status: res.status(), body: (await res.json().catch(() => null)) as T };
|
||||
// v2 answers { data }; a download (my data) is the file itself.
|
||||
const json = await res.json().catch(() => null);
|
||||
return { status: res.status(), body: (json && typeof json === "object" && "data" in json ? json.data : json) as T };
|
||||
}
|
||||
|
||||
test.describe("privacy (POPIA)", () => {
|
||||
test("sign-up records the news choice; the privacy page changes it and downloads my data", async ({ page }) => {
|
||||
const me = await signUp(page, false);
|
||||
const before = await asAccount<PrivacyInfo>(page, me, "GET", "/api/privacy/me");
|
||||
const before = await asAccount<PrivacyInfo>(page, me, "GET", "/api/v2/me/privacy");
|
||||
expect(before.body.news.email).toBe(false);
|
||||
expect(before.body.history.map((h) => h.source)).toContain("signup");
|
||||
|
||||
@@ -41,7 +43,7 @@ test.describe("privacy (POPIA)", () => {
|
||||
await expect(page.getByText("You're subscribed.")).toBeVisible();
|
||||
await expect(byEmail).toBeChecked();
|
||||
|
||||
const exported = await asAccount<{ account: { email: string }; communication: { news: { email: boolean } } }>(page, me, "GET", "/api/privacy/me/export");
|
||||
const exported = await asAccount<{ account: { email: string }; communication: { news: { email: boolean } } }>(page, me, "GET", "/api/v2/me/privacy/export");
|
||||
expect(exported.status).toBe(200);
|
||||
expect(exported.body.account.email).toBe(me.email);
|
||||
expect(exported.body.communication.news.email).toBe(true);
|
||||
@@ -49,10 +51,10 @@ test.describe("privacy (POPIA)", () => {
|
||||
|
||||
test("news broadcasts skip people who aren't opted in; event updates don't", async ({ page }) => {
|
||||
const out = await signUp(page, false);
|
||||
const news = await api<Preview>(page, "supervisor", "POST", "/api/broadcasts/preview", { userIds: [out.id, seed().users.admin.id], emails: "", kind: "news" });
|
||||
const news = await v2<Preview>(page, "supervisor", "POST", "/api/v2/broadcasts/email/preview", { userIds: [out.id, seed().users.admin.id], emails: "", kind: "news" });
|
||||
expect(news.matched).toBe(1);
|
||||
expect(news.skipped).toBe(1);
|
||||
const update = await api<Preview>(page, "supervisor", "POST", "/api/broadcasts/preview", { userIds: [out.id, seed().users.admin.id], emails: "", kind: "update" });
|
||||
const update = await v2<Preview>(page, "supervisor", "POST", "/api/v2/broadcasts/email/preview", { userIds: [out.id, seed().users.admin.id], emails: "", kind: "update" });
|
||||
expect(update.matched).toBe(2);
|
||||
expect(update.skipped).toBe(0);
|
||||
});
|
||||
@@ -61,11 +63,11 @@ test.describe("privacy (POPIA)", () => {
|
||||
await page.goto(`/unsubscribe?t=${encodeURIComponent(seed().unsubscribeToken)}`);
|
||||
await page.getByRole("button", { name: "Unsubscribe from all news" }).click();
|
||||
await expect(page.getByText(/You're unsubscribed from news and announcements/)).toBeVisible();
|
||||
const after = await api<PrivacyInfo>(page, "user", "GET", "/api/privacy/me");
|
||||
const after = await v2<PrivacyInfo>(page, "user", "GET", "/api/v2/me/privacy");
|
||||
expect(after.news).toEqual({ email: false, whatsapp: false });
|
||||
expect(after.history.some((h) => h.source === "unsubscribe_link")).toBe(true);
|
||||
|
||||
const bad = await page.request.get(`${API}/api/privacy/unsubscribe/not-a-real-token`);
|
||||
const bad = await page.request.get(`${API}/api/v2/unsubscribe/not-a-real-token`);
|
||||
expect(bad.status()).toBe(404);
|
||||
});
|
||||
|
||||
@@ -84,17 +86,17 @@ test.describe("privacy (POPIA)", () => {
|
||||
|
||||
test("delete my account: closed at once, anonymised when an admin completes the request", async ({ page }) => {
|
||||
const me = await signUp(page, true);
|
||||
const wrong = await asAccount(page, me, "POST", "/api/privacy/me/requests", { type: "deletion", password: "nope" });
|
||||
const wrong = await asAccount(page, me, "POST", "/api/v2/me/privacy/requests", { type: "deletion", password: "nope" });
|
||||
expect(wrong.status).toBe(400);
|
||||
const made = await asAccount(page, me, "POST", "/api/privacy/me/requests", { type: "deletion", password: "E2e-password-123" });
|
||||
const made = await asAccount(page, me, "POST", "/api/v2/me/privacy/requests", { type: "deletion", password: "E2e-password-123" });
|
||||
expect(made.status).toBe(201);
|
||||
|
||||
// The account is closed: the old session no longer works, and logging in is refused.
|
||||
expect((await asAccount(page, me, "GET", "/api/privacy/me")).status).toBe(401);
|
||||
const login = await page.request.post(`${API}/api/users/login`, { data: { email: me.email, password: "E2e-password-123" } });
|
||||
expect((await asAccount(page, me, "GET", "/api/v2/me/privacy")).status).toBe(401);
|
||||
const login = await page.request.post(`${API}/api/v2/auth/login`, { data: { identifier: me.email, password: "E2e-password-123" } });
|
||||
expect(login.ok()).toBe(false);
|
||||
|
||||
const inbox = await api<Requests>(page, "admin", "GET", "/api/privacy/requests?status=open");
|
||||
const inbox = await v2<Requests>(page, "admin", "GET", "/api/v2/privacy/requests?status=open");
|
||||
const request = inbox.requests.find((r) => r.userId === me.id);
|
||||
expect(request?.type).toBe("deletion");
|
||||
expect(request?.blockers).toEqual([]);
|
||||
@@ -103,8 +105,8 @@ test.describe("privacy (POPIA)", () => {
|
||||
await loginAs(page, "admin");
|
||||
await page.goto("/dashboard/admin/data-requests");
|
||||
await expect(page.getByText(me.email)).toBeVisible();
|
||||
await api(page, "admin", "POST", `/api/privacy/requests/${request!.id}/complete`, {});
|
||||
const user = await api<{ name: string; email: string }>(page, "admin", "GET", `/api/users/${me.id}`);
|
||||
await v2(page, "admin", "POST", `/api/v2/privacy/requests/${request!.id}/complete`, {});
|
||||
const user = await v2<{ name: string; email: string }>(page, "admin", "GET", `/api/v2/users/${me.id}`);
|
||||
expect(user.name).toBe("Deleted User");
|
||||
expect(user.email).toMatch(/@deleted\.invalid$/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user