Public: anyone can read this; the crew can change it.
Removing an organization's last owner can retire the organization and its repositories with them, after a confirm page (destructive) #985
scout/forge:scout/859-removing-an-organization-s-last-owner-ca → main 1 commit fork
5 files
changed +425
−8
src/actions/accounts.ts +88 −5
| @@ -12,7 +12,8 @@ | ||
| 12 | 12 | |
| 13 | 13 | import type { Database } from "bun:sqlite"; |
| 14 | 14 | import type { App } from "../app.ts"; |
| 15 | import { soleOwnedOrgs } from "../core/orgs.ts"; | |
| 15 | import { listTeams, soleOwnedOrgs } from "../core/orgs.ts"; | |
| 16 | import { appsForRepos } from "../core/apps.ts"; | |
| 16 | 17 | import { appsRuntime } from "../apps/runtime.ts"; |
| 17 | 18 | import { |
| 18 | 19 | dropCredentials, |
| @@ -21,7 +22,7 @@ import { | ||
| 21 | 22 | setSuspended, |
| 22 | 23 | type AccountRow, |
| 23 | 24 | } from "../core/accounts.ts"; |
| 24 | import { deleteRepo as destroyRepo, listReposOwnedBy } from "../core/repos.ts"; | |
| 25 | import { deleteRepo as destroyRepo, listReposOwnedBy, type Repo } from "../core/repos.ts"; | |
| 25 | 26 | import { listActive } from "../core/runs.ts"; |
| 26 | 27 | import { getUserByName, type User } from "../core/users.ts"; |
| 27 | 28 | import { markVerified } from "../core/verify.ts"; |
| @@ -123,10 +124,83 @@ export function unsuspendAccount( | ||
| 123 | 124 | return ok(getUserByName(app.db, username)!); |
| 124 | 125 | } |
| 125 | 126 | |
| 127 | /** `destroyRepo`, with a thrown disk error told as the failure it is. */ | |
| 128 | async function removeRepo( | |
| 129 | app: App, | |
| 130 | repo: Repo, | |
| 131 | ): Promise<Result<void, "delete_failed">> { | |
| 132 | try { | |
| 133 | return await destroyRepo(app.db, app.reposRoot, repo); | |
| 134 | } catch { | |
| 135 | return err("delete_failed"); | |
| 136 | } | |
| 137 | } | |
| 138 | ||
| 139 | /** What retiring one organization takes with it: the confirm page lists exactly this. */ | |
| 140 | export interface OrgHoldings { | |
| 141 | readonly org: string; | |
| 142 | readonly repos: { name: string; app: string | null }[]; | |
| 143 | readonly teams: string[]; | |
| 144 | } | |
| 145 | ||
| 146 | /** An account's sole-owned organizations and all each holds, for the confirm page. */ | |
| 147 | export function orgHoldings(app: App, user: User): OrgHoldings[] { | |
| 148 | return soleOwnedOrgs(app.db, user.id).map((org) => { | |
| 149 | const repos = listReposOwnedBy(app.db, org.id); | |
| 150 | const apps = appsForRepos(app.db, repos.map((r) => r.id)); | |
| 151 | return { | |
| 152 | org: org.username, | |
| 153 | repos: repos.map((r) => ({ name: r.name, app: apps.get(r.id)?.slug ?? null })), | |
| 154 | teams: listTeams(app.db, org.id).map((t) => t.name), | |
| 155 | }; | |
| 156 | }); | |
| 157 | } | |
| 158 | ||
| 159 | /** Repositories first (apps down before each), then teams, then the organization itself. */ | |
| 160 | async function retireOrg( | |
| 161 | app: App, | |
| 162 | admin: User, | |
| 163 | org: User, | |
| 164 | done: string[], | |
| 165 | ): Promise<Result<void, "delete_failed">> { | |
| 166 | setSuspended(app.db, org.id, new Date().toISOString()); | |
| 167 | await quiet(app, admin, org); | |
| 168 | const runtime = appsRuntime(app); | |
| 169 | for (const repo of listReposOwnedBy(app.db, org.id)) { | |
| 170 | if (runtime !== undefined) await runtime.teardownAll(repo.id); | |
| 171 | const deleted = await removeRepo(app, repo); | |
| 172 | if (!deleted.ok) return deleted; | |
| 173 | app.bus.append({ | |
| 174 | kind: "repo.deleted", | |
| 175 | actorId: admin.id, | |
| 176 | payload: { name: `${org.username}/${repo.name}` }, | |
| 177 | }); | |
| 178 | done.push(`${org.username}/${repo.name}`); | |
| 179 | } | |
| 180 | app.db.query("DELETE FROM team WHERE org_id = ?1").run(org.id); | |
| 181 | scrubAccount(app.db, org.id, new Date().toISOString()); | |
| 182 | app.bus.append({ | |
| 183 | kind: "org.removed", | |
| 184 | actorId: admin.id, | |
| 185 | payload: { name: org.username }, | |
| 186 | }); | |
| 187 | done.push(org.username); | |
| 188 | return ok(); | |
| 189 | } | |
| 190 | ||
| 126 | 191 | export async function removeAccount( |
| 127 | 192 | app: App, |
| 128 | 193 | actor: User | undefined, |
| 129 | 194 | username: string, |
| 195 | options: { | |
| 196 | /** | |
| 197 | * The owner has seen (`orgHoldings`) that the organizations this account | |
| 198 | * alone owns go with it, and says so. Without it they refuse the removal. | |
| 199 | */ | |
| 200 | withOrgs?: boolean; | |
| 201 | /** Filled with what was removed, so a failure partway can say so. */ | |
| 202 | removed?: string[]; | |
| 203 | } = {}, | |
| 130 | 204 | ): Promise<Result<void, AccountActionError | "delete_failed" | "last_owner">> { |
| 131 | 205 | const admin = requireForgeAdmin(actor); |
| 132 | 206 | if (!admin.ok) return admin; |
| @@ -134,19 +208,27 @@ export async function removeAccount( | ||
| 134 | 208 | if (!found.ok) return found; |
| 135 | 209 | const user = found.value; |
| 136 | 210 | if (user.removedAt !== null) return ok(); |
| 211 | const orgs = soleOwnedOrgs(app.db, user.id); | |
| 137 | 212 | // An organization this account alone owns would be left with nobody to |
| 138 | 213 | // administer it: someone else is made an owner first (`soleOwnedOrgs` |
| 139 | // names them), and nothing here has changed yet. | |
| 140 | if (soleOwnedOrgs(app.db, user.id).length > 0) return err("last_owner"); | |
| 214 | // names them), or the owner retires it with the account; nothing here | |
| 215 | // has changed yet. | |
| 216 | if (orgs.length > 0 && options.withOrgs !== true) return err("last_owner"); | |
| 217 | const done = options.removed ?? []; | |
| 141 | 218 | // Stop it first, so nothing it owns is running while it is taken apart. |
| 142 | 219 | setSuspended(app.db, user.id, new Date().toISOString()); |
| 143 | 220 | dropCredentials(app.db, user.id); |
| 144 | 221 | await quiet(app, admin.value, user); |
| 222 | for (const org of orgs) { | |
| 223 | const retired = await retireOrg(app, admin.value, org, done); | |
| 224 | if (!retired.ok) return retired; | |
| 225 | } | |
| 145 | 226 | const runtime = appsRuntime(app); |
| 146 | 227 | for (const repo of listReposOwnedBy(app.db, user.id)) { |
| 147 | 228 | if (runtime !== undefined) await runtime.teardownAll(repo.id); |
| 148 | const deleted = await destroyRepo(app.db, app.reposRoot, repo); | |
| 229 | const deleted = await removeRepo(app, repo); | |
| 149 | 230 | if (!deleted.ok) return deleted; |
| 231 | done.push(`${user.username}/${repo.name}`); | |
| 150 | 232 | } |
| 151 | 233 | scrubAccount(app.db, user.id, new Date().toISOString()); |
| 152 | 234 | app.bus.append({ |
| @@ -154,6 +236,7 @@ export async function removeAccount( | ||
| 154 | 236 | actorId: admin.value.id, |
| 155 | 237 | payload: { username: user.username }, |
| 156 | 238 | }); |
| 239 | done.push(user.username); | |
| 157 | 240 | return ok(); |
| 158 | 241 | } |
| 159 | 242 |
src/http/api.ts +14 −3
| @@ -1274,7 +1274,18 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1274 | 1274 | router.add("DELETE", "/api/v1/admin/accounts/:username", async (ctx) => { |
| 1275 | 1275 | const denied = accountAdmin(ctx, "admin"); |
| 1276 | 1276 | if (denied) return denied; |
| 1277 | const done = await removeAccount(ctx.app, ctx.auth.user, ctx.params.username!); | |
| 1277 | const input = await body(ctx.request); | |
| 1278 | const removed: string[] = []; | |
| 1279 | const done = await removeAccount(ctx.app, ctx.auth.user, ctx.params.username!, { | |
| 1280 | withOrgs: input.confirm === true, | |
| 1281 | removed, | |
| 1282 | }); | |
| 1283 | if (!done.ok && done.error === "delete_failed") | |
| 1284 | return apiError( | |
| 1285 | "delete_failed", | |
| 1286 | 500, | |
| 1287 | `removed so far: ${removed.join(", ") || "nothing"}; send the request again to finish`, | |
| 1288 | ); | |
| 1278 | 1289 | if (!done.ok && done.error === "last_owner") { |
| 1279 | 1290 | const user = getUserByName(ctx.app.db, ctx.params.username!); |
| 1280 | 1291 | const orgs = |
| @@ -1284,10 +1295,10 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1284 | 1295 | return apiError( |
| 1285 | 1296 | "last_owner", |
| 1286 | 1297 | 409, |
| 1287 | `${ctx.params.username} is the only person owning ${orgs.join(", ")}: make someone else an owner first`, | |
| 1298 | `${ctx.params.username} is the only person owning ${orgs.join(", ")}: make someone else an owner first, or send {"confirm": true} to remove the organization and its repositories too`, | |
| 1288 | 1299 | ); |
| 1289 | 1300 | } |
| 1290 | return done.ok ? json({ removed: ctx.params.username }) : accountError(done.error); | |
| 1301 | return done.ok ? json({ removed: ctx.params.username, deleted: removed }) : accountError(done.error); | |
| 1291 | 1302 | }); |
| 1292 | 1303 | |
| 1293 | 1304 | router.add("GET", "/api/v1/user/orgs", (ctx) => { |
src/http/server.ts +4
| @@ -15,6 +15,8 @@ import { | ||
| 15 | 15 | } from "./caller.ts"; |
| 16 | 16 | import { |
| 17 | 17 | accountAct, |
| 18 | removeOrgsAct, | |
| 19 | removeOrgsPage, | |
| 18 | 20 | accountsPage, |
| 19 | 21 | personSignInsPage, |
| 20 | 22 | signInAct, |
| @@ -673,6 +675,8 @@ export function buildRouter(): Router<Handler> { | ||
| 673 | 675 | accountAct("unsuspend"), |
| 674 | 676 | ); |
| 675 | 677 | router.add("POST", "/settings/accounts/:username/remove", accountAct("remove")); |
| 678 | router.add("GET", "/settings/accounts/:username/remove-organizations", removeOrgsPage); | |
| 679 | router.add("POST", "/settings/accounts/:username/remove-organizations", removeOrgsAct); | |
| 676 | 680 | router.add("POST", "/settings/accounts/:username/verify", accountAct("verify")); |
| 677 | 681 | router.add("GET", "/settings/crew", crewPage); |
| 678 | 682 | router.add("POST", "/settings/crew", agentCreate); |
src/web/pages/accounts.ts +125
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | |
| 8 | 8 | import { |
| 9 | 9 | listAccounts, |
| 10 | orgHoldings, | |
| 10 | 11 | removeAccount, |
| 11 | 12 | suspendAccount, |
| 12 | 13 | unsuspendAccount, |
| @@ -225,6 +226,122 @@ function confirmSuspend(username: string, orgs: readonly string[]): Html { | ||
| 225 | 226 | ); |
| 226 | 227 | } |
| 227 | 228 | |
| 229 | /** The removal was refused: make an owner first, or retire the organization too (a page that lists it). */ | |
| 230 | function refuseRemove(username: string, orgs: readonly string[]): Html { | |
| 231 | const at = `/settings/accounts/${encodeURIComponent(username)}`; | |
| 232 | return card( | |
| 233 | html`<p> | |
| 234 | <strong>${username}</strong> is the only person owning | |
| 235 | ${orgs.join(", ")}. Nothing was changed. Make someone else an owner | |
| 236 | first, or remove the organization too. | |
| 237 | </p> | |
| 238 | <div class="form-actions"> | |
| 239 | ${btn("Remove the organization too", { | |
| 240 | href: `${at}/remove-organizations`, | |
| 241 | size: "sm", | |
| 242 | variant: "danger", | |
| 243 | })} | |
| 244 | ${orgs[0] === undefined | |
| 245 | ? null | |
| 246 | : btn("Add an owner first", { href: ownersTeam(orgs[0]), size: "sm" })} | |
| 247 | ${btn("Keep them", { href: "/settings/accounts", size: "sm", variant: "ghost" })} | |
| 248 | </div>`, | |
| 249 | { tone: "attn" }, | |
| 250 | ); | |
| 251 | } | |
| 252 | ||
| 253 | // GET /settings/accounts/:username/remove-organizations — what would go, before anything does | |
| 254 | export function removeOrgsPage(ctx: Ctx): Response { | |
| 255 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 256 | if (!ctx.auth.user.isAdmin) return notFoundPage(ctx); | |
| 257 | const username = ctx.params.username ?? ""; | |
| 258 | const user = getUserByName(ctx.app.db, username); | |
| 259 | const listed = listAccounts(ctx.app, ctx.auth.user); | |
| 260 | if ( | |
| 261 | !listed.ok || | |
| 262 | user === undefined || | |
| 263 | user.kind !== "user" || | |
| 264 | user.isAdmin || | |
| 265 | user.removedAt !== null | |
| 266 | ) | |
| 267 | return notFoundPage(ctx); | |
| 268 | const held = orgHoldings(ctx.app, user); | |
| 269 | const at = `/settings/accounts/${encodeURIComponent(username)}`; | |
| 270 | if (held.length === 0) return flashTo(ctx, "/settings/accounts", { | |
| 271 | message: `${username} is not the only owner of any organization.`, | |
| 272 | }); | |
| 273 | return shell( | |
| 274 | ctx, | |
| 275 | card( | |
| 276 | html`<p> | |
| 277 | Removing <strong>${username}</strong> also removes the organizations | |
| 278 | they alone own, and everything those hold. This cannot be undone. | |
| 279 | </p> | |
| 280 | ${held.map( | |
| 281 | (h) => html`<h3>${h.org}</h3> | |
| 282 | <ul> | |
| 283 | <li>Organization ${h.org}</li> | |
| 284 | ${h.repos.map( | |
| 285 | (r) => | |
| 286 | html`<li> | |
| 287 | Repository ${h.org}/${r.name}${r.app === null | |
| 288 | ? null | |
| 289 | : html` and its app ${r.app}`} | |
| 290 | </li>`, | |
| 291 | )} | |
| 292 | <li> | |
| 293 | ${h.teams.length === 1 ? "Team" : "Teams"}: | |
| 294 | ${h.teams.join(", ")} | |
| 295 | </li> | |
| 296 | </ul>`, | |
| 297 | )} | |
| 298 | <p> | |
| 299 | Then ${username}'s own repositories are deleted and the account is | |
| 300 | scrubbed. | |
| 301 | </p> | |
| 302 | <form method="post" action="${at}/remove-organizations" class="form-actions"> | |
| 303 | <input type="hidden" name="confirm" value="1" /> | |
| 304 | ${btn("Remove everything listed", { type: "submit", size: "sm", variant: "danger" })} | |
| 305 | ${btn("Cancel", { href: "/settings/accounts", size: "sm", variant: "ghost" })} | |
| 306 | </form>`, | |
| 307 | { head: html`<h2>Remove ${username} and their organizations</h2>` }, | |
| 308 | ), | |
| 309 | ); | |
| 310 | } | |
| 311 | ||
| 312 | // POST /settings/accounts/:username/remove-organizations | |
| 313 | export async function removeOrgsAct(ctx: Ctx): Promise<Response> { | |
| 314 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 315 | if (!ctx.auth.user.isAdmin) return notFoundPage(ctx); | |
| 316 | const form = await readForm(ctx, "admin"); | |
| 317 | if (form instanceof Response) return form; | |
| 318 | const username = ctx.params.username ?? ""; | |
| 319 | // Nothing is removed unless this page's own confirm field came with it. | |
| 320 | if (field(form, "confirm") !== "1") | |
| 321 | return flashTo(ctx, `/settings/accounts/${encodeURIComponent(username)}/remove-organizations`, { | |
| 322 | message: "Confirm to remove.", | |
| 323 | tone: "danger", | |
| 324 | }); | |
| 325 | const removed: string[] = []; | |
| 326 | const done = await removeAccount(ctx.app, ctx.auth.user, username, { | |
| 327 | withOrgs: true, | |
| 328 | removed, | |
| 329 | }); | |
| 330 | if (!done.ok) { | |
| 331 | if (done.error === "not_found") return notFoundPage(ctx); | |
| 332 | return flashTo(ctx, "/settings/accounts", { | |
| 333 | message: | |
| 334 | done.error === "not_allowed" | |
| 335 | ? "You cannot do that to yourself, another admin, or an agent." | |
| 336 | : `That stopped partway. Removed so far: ${removed.join(", ") || "nothing"}. What is left is intact; remove ${username} again to finish.`, | |
| 337 | tone: "danger", | |
| 338 | }); | |
| 339 | } | |
| 340 | return flashTo(ctx, "/settings/accounts", { | |
| 341 | message: `${username} removed, with ${removed.filter((n) => n !== username).join(", ") || "nothing else"}.`, | |
| 342 | }); | |
| 343 | } | |
| 344 | ||
| 228 | 345 | // GET /settings/accounts/:username — where one person is signed in |
| 229 | 346 | export function personSignInsPage(ctx: Ctx): Response { |
| 230 | 347 | if (!ctx.auth.user) return signInFirst(ctx); |
| @@ -324,6 +441,14 @@ export function accountAct(act: Act) { | ||
| 324 | 441 | : await removeAccount(ctx.app, ctx.auth.user, username); |
| 325 | 442 | if (!done.ok) { |
| 326 | 443 | if (done.error === "not_found") return notFoundPage(ctx); |
| 444 | if (act === "remove" && done.error === "last_owner") { | |
| 445 | const user = getUserByName(ctx.app.db, username); | |
| 446 | const orgs = | |
| 447 | user === undefined | |
| 448 | ? [] | |
| 449 | : soleOwnedOrgs(ctx.app.db, user.id).map((o) => o.username); | |
| 450 | return accountsView(ctx, refuseRemove(username, orgs), 409); | |
| 451 | } | |
| 327 | 452 | if (act === "suspend" && done.error === "last_owner") { |
| 328 | 453 | const user = getUserByName(ctx.app.db, username); |
| 329 | 454 | const orgs = |
test/org-retire.test.ts added +194
| @@ -0,0 +1,194 @@ | ||
| 1 | /** | |
| 2 | * Removing an account that alone owns an organization can retire the | |
| 3 | * organization with it (#859): the confirm page lists what goes, nothing | |
| 4 | * changes before the confirm, and then repositories, teams, organization and | |
| 5 | * account go, each recorded in the outbox. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { existsSync } from "node:fs"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { orgHoldings, removeAccount } from "../src/actions/accounts.ts"; | |
| 13 | import { createOrg } from "../src/actions/orgs.ts"; | |
| 14 | import { createRepo } from "../src/actions/repos.ts"; | |
| 15 | import { createApp, type App } from "../src/app.ts"; | |
| 16 | import { listTeams } from "../src/core/orgs.ts"; | |
| 17 | import { listReposOwnedBy } from "../src/core/repos.ts"; | |
| 18 | import { createToken } from "../src/core/tokens.ts"; | |
| 19 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 20 | import { repoDir } from "../src/git/repos.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | import { proveAll } from "./helpers/verified.ts"; | |
| 23 | ||
| 24 | describe("removing an account and the organization it alone owns", () => { | |
| 25 | let dataDir: string; | |
| 26 | let app: App; | |
| 27 | let servers: Servers; | |
| 28 | let base: string; | |
| 29 | let alice: User; | |
| 30 | let bob: User; | |
| 31 | let carol: User; | |
| 32 | let aliceToken = ""; | |
| 33 | let bobToken = ""; | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | dataDir = await mkdtemp(join(tmpdir(), "forge-org-retire-")); | |
| 37 | app = createApp({ dataDir, port: 0 }); | |
| 38 | servers = await serve(app); | |
| 39 | base = servers.url; | |
| 40 | const people: User[] = []; | |
| 41 | for (const [name, first] of [ | |
| 42 | ["alice", true], | |
| 43 | ["bob", false], | |
| 44 | ["carol", false], | |
| 45 | ] as const) { | |
| 46 | const made = await createUser(app.db, { | |
| 47 | trusted: true, | |
| 48 | username: name, | |
| 49 | email: `${name}@forge.local`, | |
| 50 | password: "a-good-passphrase", | |
| 51 | adminIfFirst: first, | |
| 52 | }); | |
| 53 | if (!made.ok) throw new Error(made.error); | |
| 54 | people.push(made.value); | |
| 55 | } | |
| 56 | [alice, bob, carol] = people as [User, User, User]; | |
| 57 | proveAll(app); | |
| 58 | aliceToken = createToken(app.db, alice.id, "t", "admin").token; | |
| 59 | bobToken = createToken(app.db, bob.id, "t", "admin").token; | |
| 60 | const org = createOrg(app, bob, "acme"); | |
| 61 | if (!org.ok) throw new Error(String(org.error)); | |
| 62 | for (const [owner, name] of [ | |
| 63 | ["acme", "site"], | |
| 64 | ["acme", "tools"], | |
| 65 | ["bob", "notes"], | |
| 66 | ] as const) { | |
| 67 | const made = await createRepo(app, bob, { name, owner }); | |
| 68 | if (!made.ok) throw new Error(String(made.error)); | |
| 69 | } | |
| 70 | }, 60_000); | |
| 71 | ||
| 72 | afterAll(async () => { | |
| 73 | servers.stop(); | |
| 74 | app.close(); | |
| 75 | await rm(dataDir, { recursive: true, force: true }); | |
| 76 | }); | |
| 77 | ||
| 78 | const form = (path: string, token: string, fields: Record<string, string>) => | |
| 79 | fetch(`${base}${path}`, { | |
| 80 | method: "POST", | |
| 81 | redirect: "manual", | |
| 82 | headers: { | |
| 83 | authorization: `Bearer ${token}`, | |
| 84 | origin: base, | |
| 85 | "content-type": "application/x-www-form-urlencoded", | |
| 86 | }, | |
| 87 | body: new URLSearchParams(fields).toString(), | |
| 88 | }); | |
| 89 | const kinds = () => | |
| 90 | ( | |
| 91 | app.db.query("SELECT kind, payload FROM event").all() as { | |
| 92 | kind: string; | |
| 93 | payload: string; | |
| 94 | }[] | |
| 95 | ).map((e) => `${e.kind} ${e.payload}`); | |
| 96 | ||
| 97 | test("a non-admin reaches neither page", async () => { | |
| 98 | const page = await fetch(`${base}/settings/accounts/bob/remove-organizations`, { | |
| 99 | headers: { authorization: `Bearer ${bobToken}` }, | |
| 100 | }); | |
| 101 | expect(page.status).toBe(404); | |
| 102 | const posted = await form("/settings/accounts/bob/remove-organizations", bobToken, { | |
| 103 | confirm: "1", | |
| 104 | }); | |
| 105 | expect(posted.status).toBe(404); | |
| 106 | expect(getUserByName(app.db, "acme")!.removedAt).toBeNull(); | |
| 107 | }); | |
| 108 | ||
| 109 | test("the plain removal is refused and the page lists exactly what would go", async () => { | |
| 110 | const refused = await removeAccount(app, alice, "bob"); | |
| 111 | expect(!refused.ok && refused.error).toBe("last_owner"); | |
| 112 | const held = orgHoldings(app, bob); | |
| 113 | expect(held).toHaveLength(1); | |
| 114 | expect(held[0]!.repos.map((r) => r.name).sort()).toEqual(["site", "tools"]); | |
| 115 | const page = await fetch(`${base}/settings/accounts/bob/remove-organizations`, { | |
| 116 | headers: { authorization: `Bearer ${aliceToken}` }, | |
| 117 | }); | |
| 118 | expect(page.status).toBe(200); | |
| 119 | const html = await page.text(); | |
| 120 | expect(html).toContain("acme/site"); | |
| 121 | expect(html).toContain("acme/tools"); | |
| 122 | expect(html).toContain("Owners"); | |
| 123 | expect(html).not.toContain("acme/notes"); | |
| 124 | expect(listReposOwnedBy(app.db, getUserByName(app.db, "acme")!.id)).toHaveLength(2); | |
| 125 | }); | |
| 126 | ||
| 127 | test("the refused removal on the accounts page offers the way", async () => { | |
| 128 | const res = await form("/settings/accounts/bob/remove", aliceToken, {}); | |
| 129 | expect(res.status).toBe(409); | |
| 130 | expect(await res.text()).toContain("remove-organizations"); | |
| 131 | }); | |
| 132 | ||
| 133 | test("without the confirm field nothing changes, by page or API", async () => { | |
| 134 | const posted = await form("/settings/accounts/bob/remove-organizations", aliceToken, {}); | |
| 135 | expect(posted.status).toBe(303); | |
| 136 | const api = await fetch(`${base}/api/v1/admin/accounts/bob`, { | |
| 137 | method: "DELETE", | |
| 138 | headers: { authorization: `Bearer ${aliceToken}` }, | |
| 139 | }); | |
| 140 | expect(api.status).toBe(409); | |
| 141 | expect(getUserByName(app.db, "bob")!.removedAt).toBeNull(); | |
| 142 | expect(getUserByName(app.db, "acme")!.removedAt).toBeNull(); | |
| 143 | expect(listTeams(app.db, getUserByName(app.db, "acme")!.id)).toHaveLength(1); | |
| 144 | }); | |
| 145 | ||
| 146 | test("a failure partway leaves the rest intact, says what went, and a second run finishes", async () => { | |
| 147 | // The disk copy of "tools" is already gone, so its delete cannot finish. | |
| 148 | await rm(repoDir(app.reposRoot, "acme", "tools"), { recursive: true }); | |
| 149 | const removed: string[] = []; | |
| 150 | const first = await removeAccount(app, alice, "bob", { withOrgs: true, removed }); | |
| 151 | expect(!first.ok && first.error).toBe("delete_failed"); | |
| 152 | expect(removed).toEqual(["acme/site"]); | |
| 153 | expect(getUserByName(app.db, "bob")!.removedAt).toBeNull(); | |
| 154 | expect(getUserByName(app.db, "acme")!.removedAt).toBeNull(); | |
| 155 | expect(getUserByName(app.db, "bob")!.suspendedAt).not.toBeNull(); | |
| 156 | }); | |
| 157 | ||
| 158 | test("confirmed, the organization, its repositories, teams and the account are gone and recorded", async () => { | |
| 159 | const res = await form("/settings/accounts/bob/remove-organizations", aliceToken, { | |
| 160 | confirm: "1", | |
| 161 | }); | |
| 162 | expect(res.status).toBe(303); | |
| 163 | const acme = getUserByName(app.db, "acme")!; | |
| 164 | expect(acme.removedAt).not.toBeNull(); | |
| 165 | expect(listReposOwnedBy(app.db, acme.id)).toHaveLength(0); | |
| 166 | expect(listTeams(app.db, acme.id)).toHaveLength(0); | |
| 167 | expect(existsSync(repoDir(app.reposRoot, "acme", "site"))).toBe(false); | |
| 168 | expect(existsSync(repoDir(app.reposRoot, "acme", "tools"))).toBe(false); | |
| 169 | expect(listReposOwnedBy(app.db, bob.id)).toHaveLength(0); | |
| 170 | expect(getUserByName(app.db, "bob")!.removedAt).not.toBeNull(); | |
| 171 | const events = kinds(); | |
| 172 | expect(events.some((e) => e.startsWith("repo.deleted") && e.includes("acme/site"))).toBe(true); | |
| 173 | expect(events.some((e) => e.startsWith("org.removed") && e.includes("acme"))).toBe(true); | |
| 174 | expect(events.some((e) => e.startsWith("account.removed") && e.includes("bob"))).toBe(true); | |
| 175 | expect(getUserByName(app.db, "carol")!.removedAt).toBeNull(); | |
| 176 | }); | |
| 177 | ||
| 178 | test("the API removes with an explicit confirm", async () => { | |
| 179 | const org = createOrg(app, carol, "zeta"); | |
| 180 | if (!org.ok) throw new Error(String(org.error)); | |
| 181 | const made = await createRepo(app, carol, { name: "z", owner: "zeta" }); | |
| 182 | if (!made.ok) throw new Error(String(made.error)); | |
| 183 | const res = await fetch(`${base}/api/v1/admin/accounts/carol`, { | |
| 184 | method: "DELETE", | |
| 185 | headers: { authorization: `Bearer ${aliceToken}`, "content-type": "application/json" }, | |
| 186 | body: JSON.stringify({ confirm: true }), | |
| 187 | }); | |
| 188 | expect(res.status).toBe(200); | |
| 189 | const out = (await res.json()) as { deleted: string[] }; | |
| 190 | expect(out.deleted).toContain("zeta"); | |
| 191 | expect(getUserByName(app.db, "zeta")!.removedAt).not.toBeNull(); | |
| 192 | expect(getUserByName(app.db, "carol")!.removedAt).not.toBeNull(); | |
| 193 | }); | |
| 194 | }); |
new above
offline