/** * The switch (src/account_delete.ts `switchedOn`): deleting an account answers only where the Worker's var * ACCOUNT_DELETE is `on` (dev); off, the default and live's, every delete-account route and the /delete page answer * a plain not-available and nothing reaches an object. */ import { env } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { ANY_DELETE_PATH, switchedOn } from "../src/account_delete.ts"; import worker from "../src/index.ts"; import type { Env } from "../src/env.ts"; import { bearer, open, OPEN, signUp } from "./calling.ts"; const off = (): Env => ({ ...(env as unknown as Env), ACCOUNT_DELETE: undefined }); const ask = (path: string, method = "POST", headers: Record = {}): Request => new Request(`${OPEN}${path}`, { method, headers: { "content-type": "application/json", "cf-connecting-ip": "192.0.2.77", ...headers }, body: method === "GET" || method === "HEAD" ? undefined : "{}" }); describe("the account deletion switch", () => { it("is on only for the word on: unset, off or anything else is off", () => { expect(switchedOn({ ACCOUNT_DELETE: "on" })).toBe(true); for (const value of [undefined, "", "off", "ON", "true", "1"]) expect(switchedOn({ ACCOUNT_DELETE: value })).toBe(false); }); it("names every route that asks for or starts a deletion, on any prefix, and nothing else", () => { for (const path of ["/delete", "/delete/", "/v1/account/delete", "/v1/account/delete/code", "/v1/account/delete/web", "/v1/account/delete/web/code", "/b/p-x/v1/account/delete", "/b/p-x/v1/account/delete/code"]) expect(ANY_DELETE_PATH.test(path), path).toBe(true); for (const path of ["/v1/sync", "/v1/act", "/privacy", "/b/p-x/delete", "/v1/rows/delete-later", "/deleted"]) expect(ANY_DELETE_PATH.test(path), path).toBe(false); }); it("off: the page, the web routes and the app's routes answer not available, and the account stays", async () => { const phone = "+919000004601"; const credential = await signUp(phone, "phone-switch-1"); const slug = `p-${credential.split("_")[1]}`; // With no site bound, the page answers not available; with the site bound, see the next test. const page = await worker.fetch(ask("/delete", "GET"), { ...off(), SITE: undefined }); expect(page.status).toBe(404); expect(await page.text()).toContain("not available"); const pagePost = await worker.fetch(new Request(`${OPEN}/delete`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: `phone=${encodeURIComponent(phone)}` }), off()); expect(pagePost.status).toBe(404); for (const path of ["/v1/account/delete/web/code", "/v1/account/delete/web", "/v1/account/delete/code", "/v1/account/delete", `/b/${slug}/v1/account/delete/code`, `/b/${slug}/v1/account/delete`]) { const answer = await worker.fetch(ask(path, "POST", bearer(credential)), off()); expect(answer.status, path).toBe(404); expect(((await answer.json()) as { code: string }).code, path).toBe("not_available"); } // Nothing started: the account answers as before. expect((await open("GET", "/v1/me", undefined, bearer(credential))).status).toBe(200); }); it("off: reading /delete gets the site's page (how to ask by email), never a 404; asking still answers not available", async () => { const served: string[] = []; const site = { fetch: async (request: Request) => (served.push(`${request.method} ${new URL(request.url).pathname}`), new Response("

Delete your Cotton account

", { status: 200, headers: { "content-type": "text/html" } })) } as unknown as Fetcher; for (const [path, method] of [["/delete", "GET"], ["/delete/", "GET"], ["/delete", "HEAD"]] as const) { const page = await worker.fetch(ask(path, method), { ...off(), SITE: site }); expect(page.status, `${method} ${path}`).toBe(200); } expect(served).toEqual(["GET /delete", "GET /delete/", "HEAD /delete"]); const post = await worker.fetch(new Request(`${OPEN}/delete`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: "phone=%2B919000004603" }), { ...off(), SITE: site }); expect(post.status).toBe(404); expect(served.length).toBe(3); }); it("on (dev): the routes answer as built", async () => { const credential = await signUp("+919000004602", "phone-switch-2"); const code = await open<{ challenge_id: string }>("POST", "/v1/account/delete/code", {}, bearer(credential)); expect(code.status, JSON.stringify(code.body)).toBe(200); expect(code.body.challenge_id).toBeTruthy(); const page = await worker.fetch(ask("/delete", "GET"), { ...(env as unknown as Env), ACCOUNT_DELETE: "on" }); expect(page.status).toBe(200); }); });