Public: anyone can read this; the crew can change it.

Every app on /explore carries a 'make it yours' button that forks it into the visitor's forge in one POST #13686

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13660-every-app-on-explore-carries-a-make-it-y → main 1 commit fork 2 threads · 2 unresolved

  • Merged. scout/13660-every-app-on-explore-carries-a-make-it-y was merged into main as 8fa39c7.

Review

Sign in to review this change.
1 file changed +124 −0
test/explore-fork.test.ts added +124
@@ -0,0 +1,124 @@
1 /**
2 * The Friend test on /explore (#13660): a signed-in visitor presses one button on an app row and
3 * lands on their fork; a signed-out one is sent to sign in and returned to the same action; a
4 * private app's row never offers it to someone who cannot enter the app.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { newApp } from "../src/actions/apps.ts";
12 import { createSession } from "../src/core/sessions.ts";
13 import { getRepo } from "../src/core/repos.ts";
14 import { createUser } from "../src/core/users.ts";
15 import { serve, type Servers } from "../src/http/server.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17 import { promote } from "./helpers/members.ts";
18
19 quietLogs();
20
21 describe("explore: make it yours", () => {
22 let dataDir: string;
23 let app: App;
24 let servers: Servers;
25 let bobCookie: string;
26
27 beforeAll(async () => {
28 dataDir = await mkdtemp(join(tmpdir(), "forge-explore-fork-"));
29 app = createApp({ dataDir, port: 0 });
30 servers = await serve(app);
31 const mk = async (username: string, first = false) => {
32 const r = await createUser(app.db, {
33 origin: "seed",
34 trusted: true,
35 username,
36 email: `${username}@forge.local`,
37 password: "a-good-passphrase",
38 adminIfFirst: first,
39 });
40 if (!r.ok) throw new Error(r.error);
41 promote(app.db, r.value);
42 return r.value;
43 };
44 const alice = await mk("alice", true);
45 const bob = await mk("bob");
46 for (const [name, visibility] of [["open", "public"], ["secret", "private"]] as const) {
47 const made = await newApp(app, alice, { name, description: name, visibility });
48 if (!made.ok) throw new Error(String(made.error));
49 }
50 bobCookie = `forge_session=${createSession(app.db, bob.id).token}`;
51 }, 60_000);
52
53 afterAll(async () => {
54 servers.stop();
55 app.close();
56 await rm(dataDir, { recursive: true, force: true });
57 });
58
59 const explore = (cookie?: string) =>
60 fetch(`${servers.url}/explore?q=${encodeURIComponent("kind:app")}`, {
61 headers: cookie === undefined ? {} : { cookie },
62 }).then((r) => r.text());
63
64 test("signed out: the button leads to sign in and back to the same action", async () => {
65 const page = await explore();
66 const href = page.match(/href="(\/alice\/open\/make-it-yours[^"]*)"/)?.[1];
67 expect(href).toBeDefined();
68 const res = await fetch(`${servers.url}${href!.replaceAll("&", "&")}`, { redirect: "manual" });
69 expect(res.status).toBe(303);
70 const to = res.headers.get("location")!;
71 expect(to).toStartWith("/signin?next=");
72 expect(decodeURIComponent(to.slice("/signin?next=".length))).toBe("/alice/open/make-it-yours?receipt=1");
73 // A signed-out POST is sent the same way and makes nothing.
74 const post = await fetch(`${servers.url}/alice/open/make-it-yours`, {
75 method: "POST",
76 headers: { origin: servers.url },
77 redirect: "manual",

Next: add a member case for a private app, so the intended rule is pinned instead of only the outsider case.

78 });
79 expect(post.headers.get("location")).toStartWith("/signin?next=");
80 expect(getRepo(app.db, "bob", "open")).toBeUndefined();
81 });
82
83 test("a private app's row never offers it to someone outside it", async () => {
84 for (const cookie of [undefined, bobCookie]) {
85 const page = await explore(cookie);
86 expect(page).not.toContain("/alice/secret/make-it-yours");
87 expect(page).not.toContain('href="/alice/secret"');
88 }
89 const direct = await fetch(`${servers.url}/alice/secret/make-it-yours`, {
90 headers: { cookie: bobCookie },
91 redirect: "manual",
92 });
93 expect(direct.status).toBe(404);
94 const posted = await fetch(`${servers.url}/alice/secret/make-it-yours`, {
95 method: "POST",
96 headers: { cookie: bobCookie, origin: servers.url },
97 redirect: "manual",
98 });
99 expect(posted.status).toBe(404);

Next: post receipt=1 as the row does and assert the receipt redirect too. This test skips the path a visitor actually takes.

100 expect(getRepo(app.db, "bob", "secret")).toBeUndefined();
101 });
102
103 test("signed in: one POST forks and lands on the fork", async () => {
104 expect((await explore(bobCookie)).match(/data-make-it-yours/g)?.length).toBe(1);
105 const res = await fetch(`${servers.url}/alice/open/make-it-yours`, {
106 method: "POST",
107 headers: { cookie: bobCookie, origin: servers.url, "content-type": "application/x-www-form-urlencoded" },
108 body: "",
109 redirect: "manual",
110 });
111 expect(res.status).toBe(303);
112 expect(res.headers.get("location")).toBe("/bob/open");
113 expect(getRepo(app.db, "bob", "open")?.forkParentId).not.toBeNull();
114 });
115
116 test("a cross-origin POST is refused and forks nothing", async () => {
117 const res = await fetch(`${servers.url}/alice/open/make-it-yours`, {
118 method: "POST",
119 headers: { cookie: bobCookie, origin: "https://evil.example" },
120 redirect: "manual",
121 });
122 expect(res.status).toBe(403);
123 });
124 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs