Public: anyone can read this; the crew can change it.
Full suite red: test/unreached.test.ts #9309
mason/forge:mason/9306-full-suite-red-test-unreached-test-ts → main 3 commits fork
8 files
changed +194
−0
src/contract/openapi/routes/deleteApiV1ReposOwnerNameBuildsLabelDemo.ts added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "delete", | |
| 5 | path: `${REPO}/builds/{label}/demo`, | |
| 6 | summary: "Clear a pin's demo (rw scope, a person who writes); answers { label, demo: null }", | |
| 7 | auth: true, | |
| 8 | refusals: { | |
| 9 | forbidden: "403: not a person who writes the repository (an agent is refused)", | |
| 10 | not_found: "404: no such repository or pin, or the caller cannot read it", | |
| 11 | mirrored: "409: the repository mirrors an origin", | |
| 12 | invalid_demo: "422: a name or version too long, more than one line, or a page that is not a plain path", | |
| 13 | }, | |
| 14 | }; |
src/contract/openapi/routes/index.ts +4
| @@ -14,6 +14,7 @@ export { route as deleteApiV1ReposOwnerNameAppDomainsHost } from "./deleteApiV1R | ||
| 14 | 14 | export { route as deleteApiV1ReposOwnerNameAppEnvVar } from "./deleteApiV1ReposOwnerNameAppEnvVar.ts"; |
| 15 | 15 | export { route as deleteApiV1ReposOwnerNameBranchesBranch } from "./deleteApiV1ReposOwnerNameBranchesBranch.ts"; |
| 16 | 16 | export { route as deleteApiV1ReposOwnerNameBuildsLabel } from "./deleteApiV1ReposOwnerNameBuildsLabel.ts"; |
| 17 | export { route as deleteApiV1ReposOwnerNameBuildsLabelDemo } from "./deleteApiV1ReposOwnerNameBuildsLabelDemo.ts"; | |
| 17 | 18 | export { route as deleteApiV1ReposOwnerNameBuildsLabelShotsFile } from "./deleteApiV1ReposOwnerNameBuildsLabelShotsFile.ts"; |
| 18 | 19 | export { route as deleteApiV1ReposOwnerNameCrewUsername } from "./deleteApiV1ReposOwnerNameCrewUsername.ts"; |
| 19 | 20 | export { route as deleteApiV1ReposOwnerNameEgressGrants } from "./deleteApiV1ReposOwnerNameEgressGrants.ts"; |
| @@ -258,10 +259,12 @@ export { route as postApiV1ReposOwnerNameAppNotesIdHide } from "./postApiV1Repos | ||
| 258 | 259 | export { route as postApiV1ReposOwnerNameAppNotesIdUnban } from "./postApiV1ReposOwnerNameAppNotesIdUnban.ts"; |
| 259 | 260 | export { route as postApiV1ReposOwnerNameBranches } from "./postApiV1ReposOwnerNameBranches.ts"; |
| 260 | 261 | export { route as postApiV1ReposOwnerNameBuilds } from "./postApiV1ReposOwnerNameBuilds.ts"; |
| 262 | export { route as postApiV1ReposOwnerNameBuildsLabelCapture } from "./postApiV1ReposOwnerNameBuildsLabelCapture.ts"; | |
| 261 | 263 | export { route as postApiV1ReposOwnerNameBuildsLabelCart } from "./postApiV1ReposOwnerNameBuildsLabelCart.ts"; |
| 262 | 264 | export { route as postApiV1ReposOwnerNameBuildsLabelShots } from "./postApiV1ReposOwnerNameBuildsLabelShots.ts"; |
| 263 | 265 | export { route as postApiV1ReposOwnerNameBuildsLabelShotsFile } from "./postApiV1ReposOwnerNameBuildsLabelShotsFile.ts"; |
| 264 | 266 | export { route as postApiV1ReposOwnerNameBuildsLabelShotsLooks } from "./postApiV1ReposOwnerNameBuildsLabelShotsLooks.ts"; |
| 267 | export { route as postApiV1ReposOwnerNameBuildsLabelShowcase } from "./postApiV1ReposOwnerNameBuildsLabelShowcase.ts"; | |
| 265 | 268 | export { route as postApiV1ReposOwnerNameBuildsPreview } from "./postApiV1ReposOwnerNameBuildsPreview.ts"; |
| 266 | 269 | export { route as postApiV1ReposOwnerNameChecksRecheck } from "./postApiV1ReposOwnerNameChecksRecheck.ts"; |
| 267 | 270 | export { route as postApiV1ReposOwnerNameContents } from "./postApiV1ReposOwnerNameContents.ts"; |
| @@ -334,6 +337,7 @@ export { route as putApiV1BoardsSlug } from "./putApiV1BoardsSlug.ts"; | ||
| 334 | 337 | export { route as putApiV1Front } from "./putApiV1Front.ts"; |
| 335 | 338 | export { route as putApiV1ReposOwnerNameAppEnvVar } from "./putApiV1ReposOwnerNameAppEnvVar.ts"; |
| 336 | 339 | export { route as putApiV1ReposOwnerNameAppMembers } from "./putApiV1ReposOwnerNameAppMembers.ts"; |
| 340 | export { route as putApiV1ReposOwnerNameBuildsLabelDemo } from "./putApiV1ReposOwnerNameBuildsLabelDemo.ts"; | |
| 337 | 341 | export { route as putApiV1ReposOwnerNameCrewUsername } from "./putApiV1ReposOwnerNameCrewUsername.ts"; |
| 338 | 342 | export { route as putApiV1ReposOwnerNameIssuesNumberAssignee } from "./putApiV1ReposOwnerNameIssuesNumberAssignee.ts"; |
| 339 | 343 | export { route as putApiV1ReposOwnerNameIssuesNumberLabels } from "./putApiV1ReposOwnerNameIssuesNumberLabels.ts"; |
src/contract/openapi/routes/postApiV1ReposOwnerNameBuildsLabelCapture.ts added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "post", | |
| 5 | path: `${REPO}/builds/{label}/capture`, | |
| 6 | summary: "Build the pin's page and keep it (rw scope, a person who writes); answers 202 { state } while the capture runs, 200 once done", | |
| 7 | auth: true, | |
| 8 | status: 202, | |
| 9 | refusals: { | |
| 10 | forbidden: "403: not a person who writes the repository (an agent is refused)", | |
| 11 | not_found: "404: no such repository or pin, or the caller cannot read it", | |
| 12 | mirrored: "409: the repository mirrors an origin", | |
| 13 | invalid_demo: "422: a name or version too long, more than one line, or a page that is not a plain path", | |
| 14 | }, | |
| 15 | }; |
src/contract/openapi/routes/postApiV1ReposOwnerNameBuildsLabelShowcase.ts added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "post", | |
| 5 | path: `${REPO}/builds/{label}/showcase`, | |
| 6 | summary: "Put a pin's demo in or take it out of the showcase (rw scope, a person who writes): { on }; answers { label, demo }", | |
| 7 | auth: true, | |
| 8 | body: { on: "boolean" }, | |
| 9 | refusals: { | |
| 10 | forbidden: "403: not a person who writes the repository (an agent is refused)", | |
| 11 | not_found: "404: no such repository or pin, or the caller cannot read it", | |
| 12 | mirrored: "409: the repository mirrors an origin", | |
| 13 | invalid_demo: "422: a name or version too long, more than one line, or a page that is not a plain path", | |
| 14 | }, | |
| 15 | }; |
src/contract/openapi/routes/putApiV1ReposOwnerNameBuildsLabelDemo.ts added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "put", | |
| 5 | path: `${REPO}/builds/{label}/demo`, | |
| 6 | summary: "Save a pin's demo (rw scope, a person who writes): { name, version, launch, page, showcase }; answers { label, demo }", | |
| 7 | auth: true, | |
| 8 | body: { name: "string", version: "string", launch: "string", page: "string", showcase: "boolean" }, | |
| 9 | refusals: { | |
| 10 | forbidden: "403: not a person who writes the repository (an agent is refused)", | |
| 11 | not_found: "404: no such repository or pin, or the caller cannot read it", | |
| 12 | mirrored: "409: the repository mirrors an origin", | |
| 13 | invalid_demo: "422: a name or version too long, more than one line, or a page that is not a plain path", | |
| 14 | }, | |
| 15 | }; |
src/http/api.ts +2
| @@ -356,6 +356,7 @@ import { appRoutes } from "./api/app.ts"; | ||
| 356 | 356 | import { buildRoutes } from "./api/builds.ts"; |
| 357 | 357 | import { buildShotRoutes } from "./api/build_shots.ts"; |
| 358 | 358 | import { buildCartRoutes } from "./api/build_cart.ts"; |
| 359 | import { pinDemoRoutes } from "./api/pin_demos.ts"; | |
| 359 | 360 | import { iterationRoutes } from "./api/iterations.ts"; |
| 360 | 361 | import { appNoteRoutes } from "./api/app_notes.ts"; |
| 361 | 362 | import { reportRoutes } from "./api/reports.ts"; |
| @@ -3639,6 +3640,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3639 | 3640 | buildRoutes(router); |
| 3640 | 3641 | buildShotRoutes(router); |
| 3641 | 3642 | buildCartRoutes(router); |
| 3643 | pinDemoRoutes(router); | |
| 3642 | 3644 | iterationRoutes(router); |
| 3643 | 3645 | |
| 3644 | 3646 | egressGrantRoutes(router); |
src/http/api/pin_demos.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** | |
| 2 | * A pin's demo over the API (`actions/pin_demos.ts`): save it, put it in or | |
| 3 | * take it out of the showcase, clear it, or build its page. Every door | |
| 4 | * needs the `rw` scope and a person who writes the repository. | |
| 5 | */ | |
| 6 | ||
| 7 | import { captureBuild, deleteDemo, saveDemo, setShowcase } from "../../actions/pin_demos.ts"; | |
| 8 | import { isLaunch } from "../../git/builds.ts"; | |
| 9 | import { loadRepo } from "../api.ts"; | |
| 10 | import type { Handler } from "../context.ts"; | |
| 11 | import { body, json, notFound, requireScope, respondAccess } from "../respond.ts"; | |
| 12 | import type { Router } from "../router.ts"; | |
| 13 | ||
| 14 | const STATUS: Record<string, number> = { mirrored: 409, invalid_demo: 422, invalid_cover: 422, not_a_file: 409, git_failed: 500 }; | |
| 15 | const text = (v: unknown, max: number): string => (typeof v === "string" ? v.slice(0, max) : ""); | |
| 16 | ||
| 17 | export function pinDemoRoutes(router: Router<Handler>): void { | |
| 18 | // Save the demo ({ name, version, launch?, page, showcase }); its page is captured when it opens as a file. | |
| 19 | router.add("PUT", "/api/v1/repos/:owner/:name/builds/:label/demo", async (ctx) => { | |
| 20 | const denied = requireScope(ctx, "rw"); | |
| 21 | if (denied) return denied; | |
| 22 | const repo = loadRepo(ctx); | |
| 23 | if (!repo) return notFound(); | |
| 24 | const b = await body(ctx.request); | |
| 25 | const done = await saveDemo(ctx.app, ctx.auth.user, repo, ctx.params.label ?? "", { | |
| 26 | name: text(b.name, 200), | |
| 27 | version: text(b.version, 200), | |
| 28 | ...(isLaunch(b.launch) ? { launch: b.launch } : {}), | |
| 29 | page: text(b.page, 400), | |
| 30 | showcase: b.showcase === true, | |
| 31 | }); | |
| 32 | return done.ok ? json({ label: done.value.label, demo: done.value.demo ?? null }) : respondAccess(done.error, STATUS[done.error] ?? 422); | |
| 33 | }); | |
| 34 | ||
| 35 | // { on: boolean }: the Showcased chip. | |
| 36 | router.add("POST", "/api/v1/repos/:owner/:name/builds/:label/showcase", async (ctx) => { | |
| 37 | const denied = requireScope(ctx, "rw"); | |
| 38 | if (denied) return denied; | |
| 39 | const repo = loadRepo(ctx); | |
| 40 | if (!repo) return notFound(); | |
| 41 | const b = await body(ctx.request); | |
| 42 | const done = await setShowcase(ctx.app, ctx.auth.user, repo, ctx.params.label ?? "", b.on === true); | |
| 43 | return done.ok ? json({ label: done.value.label, demo: done.value.demo ?? null }) : respondAccess(done.error, STATUS[done.error] ?? 422); | |
| 44 | }); | |
| 45 | ||
| 46 | router.add("DELETE", "/api/v1/repos/:owner/:name/builds/:label/demo", async (ctx) => { | |
| 47 | const denied = requireScope(ctx, "rw"); | |
| 48 | if (denied) return denied; | |
| 49 | const repo = loadRepo(ctx); | |
| 50 | if (!repo) return notFound(); | |
| 51 | const done = await deleteDemo(ctx.app, ctx.auth.user, repo, ctx.params.label ?? ""); | |
| 52 | return done.ok ? json({ label: done.value.label, demo: null }) : respondAccess(done.error, STATUS[done.error] ?? 422); | |
| 53 | }); | |
| 54 | ||
| 55 | // Build the pin's page and keep it: 202 while the capture runs. | |
| 56 | router.add("POST", "/api/v1/repos/:owner/:name/builds/:label/capture", async (ctx) => { | |
| 57 | const denied = requireScope(ctx, "rw"); | |
| 58 | if (denied) return denied; | |
| 59 | const repo = loadRepo(ctx); | |
| 60 | if (!repo) return notFound(); | |
| 61 | const done = await captureBuild(ctx.app, ctx.auth.user, repo, ctx.params.label ?? ""); | |
| 62 | return done.ok ? json({ state: done.value }, done.value === "done" ? 200 : 202) : respondAccess(done.error, STATUS[done.error] ?? 422); | |
| 63 | }); | |
| 64 | } |
test/pin-demos-api.test.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | /** A pin's demo through the API (src/http/api/pin_demos.ts). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { pinBuild } from "../src/actions/builds.ts"; | |
| 7 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { createToken } from "../src/core/tokens.ts"; | |
| 10 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | ||
| 13 | let dataDir: string; | |
| 14 | let app: App; | |
| 15 | let servers: Servers; | |
| 16 | const tokens: Record<string, string> = {}; | |
| 17 | ||
| 18 | const call = (method: string, path: string, who?: string, b?: object) => | |
| 19 | fetch(`${servers.url}/api/v1/repos/alice/karts/builds/v1/${path}`, { | |
| 20 | method, | |
| 21 | headers: { ...(who ? { authorization: `Bearer ${tokens[who]}` } : {}), "content-type": "application/json" }, | |
| 22 | ...(b ? { body: JSON.stringify(b) } : {}), | |
| 23 | }); | |
| 24 | ||
| 25 | beforeAll(async () => { | |
| 26 | dataDir = await mkdtemp(join(tmpdir(), "forge-pindemo-")); | |
| 27 | app = createApp({ dataDir, port: 0 }); | |
| 28 | servers = await serve(app); | |
| 29 | for (const name of ["alice", "bob"]) { | |
| 30 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: name, email: `${name}@forge.local`, password: "a-good-passphrase", adminIfFirst: true }); | |
| 31 | if (!made.ok) throw new Error(made.error); | |
| 32 | tokens[name] = createToken(app.db, getUserByName(app.db, name)!.id, "t", "rw").token; | |
| 33 | } | |
| 34 | const alice = getUserByName(app.db, "alice")!; | |
| 35 | const repo = await createRepo(app, alice, { name: "karts", visibility: "public" }); | |
| 36 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 37 | const c = await commitToBranch(app, alice, repo.value, { message: "one", files: [{ path: "a.txt", content: "a\n" }] }); | |
| 38 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 39 | const pinned = await pinBuild(app, alice, repo.value, { label: "v1", oid: c.value.commitOid }); | |
| 40 | if (!pinned.ok) throw new Error(pinned.error); | |
| 41 | }); | |
| 42 | ||
| 43 | afterAll(async () => { | |
| 44 | servers.stop(); | |
| 45 | app.close(); | |
| 46 | await rm(dataDir, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | test("a writer saves, showcases and clears a demo; others are refused", async () => { | |
| 50 | const demo = { name: "Karts", version: "1", launch: "env", page: "/", showcase: false }; | |
| 51 | expect((await call("PUT", "demo", undefined, demo)).status).toBe(401); | |
| 52 | expect((await call("PUT", "demo", "bob", demo)).status).toBeGreaterThanOrEqual(403); | |
| 53 | const saved = await call("PUT", "demo", "alice", demo); | |
| 54 | expect(saved.status).toBe(200); | |
| 55 | expect(await saved.json()).toMatchObject({ label: "v1", demo: { name: "Karts", showcase: false } }); | |
| 56 | const on = await call("POST", "showcase", "alice", { on: true }); | |
| 57 | expect(await on.json()).toMatchObject({ demo: { showcase: true } }); | |
| 58 | const off = await call("POST", "showcase", "alice", { on: false }); | |
| 59 | expect(await off.json()).toMatchObject({ demo: { showcase: false } }); | |
| 60 | expect((await call("PUT", "demo", "alice", { ...demo, name: "" })).status).toBe(422); | |
| 61 | expect((await call("POST", "capture", "alice")).status).toBe(409); | |
| 62 | const gone = await call("DELETE", "demo", "alice"); | |
| 63 | expect(await gone.json()).toEqual({ label: "v1", demo: null }); | |
| 64 | expect((await call("DELETE", "demo", "alice")).status).toBe(404); | |
| 65 | }); |
new above
offline