e2e/design_test.go

185f434bf23123c61a922129270c2db4cfebd493
gitbay/e2e/design_test.go history · blame · raw

371 lines · 16667 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"net/http"
  7	"net/url"
  8	"os"
  9	"path/filepath"
 10	"strings"
 11	"testing"
 12)
 13
 14func TestReadmeRelativeLinks(t *testing.T) {
 15	inst := startInstance(t)
 16	aliceKey := inst.newKey(t, "alice")
 17	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
 18	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/site"); code != 0 {
 19		t.Fatal("repo create failed")
 20	}
 21	work := t.TempDir()
 22	env := inst.gitEnv(aliceKey)
 23	mustGit(t, work, env, "clone", inst.sshURL("alice/site"), "w")
 24	dir := filepath.Join(work, "w")
 25	os.MkdirAll(filepath.Join(dir, "docs"), 0o755)
 26	os.MkdirAll(filepath.Join(dir, "img"), 0o755)
 27	os.WriteFile(filepath.Join(dir, "README.md"), []byte(
 28		"# site\n\n[guide](docs/guide.md) and [export](docs/paper.html) and "+
 29			"[abs](https://example.org/x) here\n\n![logo](img/logo.png)\n"+
 30			"![ext](https://example.org/pic.png)\n\n"+
 31			"| flag | effect |\n|------|--------|\n| `-v` | verbose |\n\n"+
 32			"```go\nfunc main() {}\n```\n"), 0o644)
 33	os.WriteFile(filepath.Join(dir, "docs", "guide.md"), []byte("# guide\n"), 0o644)
 34	os.WriteFile(filepath.Join(dir, "docs", "paper.org"), []byte("* paper\n"), 0o644)
 35	os.WriteFile(filepath.Join(dir, "img", "logo.png"), []byte{0x89, 0x50}, 0o644)
 36	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 37	mustGit(t, dir, env, "add", ".")
 38	mustGit(t, dir, env, "commit", "-q", "-m", "base")
 39	mustGit(t, dir, env, "push", "-q", "origin", "main")
 40
 41	status, body := inst.get(t, "/alice/site")
 42	if status != 200 {
 43		t.Fatalf("tree: %d", status)
 44	}
 45	for _, want := range []string{
 46		`href="/alice/site/blob/main/docs/guide.md"`,  // relative link
 47		`href="/alice/site/blob/main/docs/paper.org"`, // .html mapped to .org source
 48		`src="/alice/site/raw/main/img/logo.png"`,     // relative image via raw
 49		`src="https://example.org/pic.png"`,           // remote image untouched
 50		`href="https://example.org/x"`,                // absolute untouched
 51		"<table>", "<td>verbose</td>",                 // GFM table renders
 52		`<span class="kd">func</span>`,                         // fenced code highlighted via classes
 53		`href="/alice/site/blob/main/README.md">README.md</a>`, // clickable card header
 54		`<th scope="col">name</th>`,                            // file table column headers
 55	} {
 56		if !strings.Contains(body, want) {
 57			t.Errorf("missing %q", want)
 58		}
 59	}
 60	// Directories sort ahead of files, whatever git's own tree order was:
 61	// docs/ and img/ precede LICENSE and README.md despite sorting after
 62	// them byte-wise.
 63	for _, dir := range []string{"docs", "img"} {
 64		d := strings.Index(body, `/tree/main/`+dir+`">`)
 65		f := strings.Index(body, `/blob/main/README.md">`)
 66		if d < 0 || f < 0 {
 67			t.Fatalf("listing missing %s/ or README.md", dir)
 68		}
 69		if d > f {
 70			t.Errorf("%s/ listed after README.md; directories should come first", dir)
 71		}
 72	}
 73	// Branch dropdown lists branches.
 74	if !strings.Contains(body, `class="refmenu"`) || !strings.Contains(body, ">All refs") {
 75		t.Error("branch dropdown missing")
 76	}
 77	// Per-file history: ?path= filters the log; blob pages link to it.
 78	os.WriteFile(filepath.Join(dir, "docs", "notes.txt"), []byte("n\n"), 0o644)
 79	mustGit(t, dir, env, "add", ".")
 80	mustGit(t, dir, env, "commit", "-q", "-m", "touch only the notes")
 81	mustGit(t, dir, env, "push", "-q", "origin", "main")
 82	if _, body := inst.get(t, "/alice/site/log?path=docs/notes.txt"); !strings.Contains(body, "touch only the notes") ||
 83		strings.Contains(body, ">base<") || !strings.Contains(body, "history of") {
 84		t.Fatalf("per-file log wrong:\n%s", body)
 85	}
 86	if _, body := inst.get(t, "/alice/site/log?path=no/such/file"); !strings.Contains(body, "nothing touches") {
 87		t.Fatalf("empty per-file log:\n%s", body)
 88	}
 89	if _, body := inst.get(t, "/alice/site/blob/main/docs/guide.md"); !strings.Contains(body, `log/main?path=docs%2fguide.md">history</a>`) {
 90		t.Fatalf("blob history link missing:\n%s", body)
 91	}
 92	// CLI parity: repo log --path.
 93	logOut, _, _ := inst.ssh(t, aliceKey, "", "repo", "log", "alice/site", "--path", "docs/notes.txt", "--json")
 94	if !strings.Contains(logOut, "touch only the notes") || strings.Contains(logOut, `"subject":"base"`) {
 95		t.Fatalf("repo log --path wrong:\n%s", logOut)
 96	}
 97	// Raw serves images with their real type (nosniff otherwise blocks
 98	// <img>); everything else stays inert text/plain.
 99	resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/img/logo.png", inst.httpPort))
100	if err != nil {
101		t.Fatal(err)
102	}
103	resp.Body.Close()
104	if ct := resp.Header.Get("Content-Type"); ct != "image/png" {
105		t.Errorf("raw png content-type = %q", ct)
106	}
107	resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/README.md", inst.httpPort))
108	if err != nil {
109		t.Fatal(err)
110	}
111	resp.Body.Close()
112	if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
113		t.Errorf("raw md content-type = %q", ct)
114	}
115	// Blob pages preview images inline.
116	if _, body := inst.get(t, "/alice/site/blob/main/img/logo.png"); !strings.Contains(body, `<img src="/alice/site/raw/main/img/logo.png"`) {
117		t.Errorf("blob image preview missing:\n%s", body)
118	}
119	// Highlighting is class-based so the palette follows the color scheme:
120	// no inline colors on code, and the stylesheet carries both palettes.
121	if _, body := inst.get(t, "/alice/site/blob/main/README.md"); !strings.Contains(body, `class="chroma"`) ||
122		strings.Contains(body, "style=\"color") {
123		t.Errorf("highlighting not class-based:\n%.2000s", body)
124	}
125	if _, css := inst.get(t, "/static/style.css"); strings.Count(css, "/* Background */") < 2 ||
126		!strings.Contains(css, ".chroma, .bg { background: transparent") {
127		t.Error("stylesheet missing dual syntax palettes")
128	}
129	// Explore rows carry topics, license, and updated date.
130	inst.ssh(t, aliceKey, "", "repo", "topics", "add", "alice/site", "web")
131	// Bare 0BSD grant (no notice-retention clause), wrapped mid-sentence.
132	os.WriteFile(filepath.Join(dir, "LICENSE"), []byte("Permission to use, copy, modify,\nand/or distribute this software for any\npurpose with or without fee is hereby granted.\n"), 0o644)
133	mustGit(t, dir, env, "add", ".")
134	mustGit(t, dir, env, "commit", "-q", "-m", "license")
135	mustGit(t, dir, env, "push", "-q", "origin", "main")
136	_, body = inst.get(t, "/explore")
137	for _, want := range []string{`href="/explore?q=web"`, "0BSD", "updated 20"} {
138		if !strings.Contains(body, want) {
139			t.Errorf("explore row missing %q", want)
140		}
141	}
142	// The description and topics belong to the code tab; task tabs render
143	// only the identity row and the tab bar.
144	for _, p := range []string{"/alice/site/issues", "/alice/site/mrs", "/alice/site/releases"} {
145		if _, body := inst.get(t, p); strings.Contains(body, `class="chip topic"`) {
146			t.Errorf("%s: header still carries topics on a task tab", p)
147		}
148	}
149	if _, body := inst.get(t, "/alice/site"); !strings.Contains(body, `class="chip topic"`) {
150		t.Error("repo home lost its topics")
151	}
152}
153
154// TestLandingRoutes checks the landing page's copy and the two routes.
155func TestLandingRoutes(t *testing.T) {
156	smtp := startFakeSMTP(t)
157	inst := startInstanceWith(t, fmt.Sprintf(
158		"[web]\nmode = \"accounts\"\n[registration]\nmode = \"open\"\n[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n",
159		smtp.addr))
160	_, body := inst.get(t, "/")
161	for _, want := range []string{
162		"A git forge you drive from the terminal.",
163		`class="button primary" href="/explore">Explore repositories</a>`,
164		`class="button btn" href="/register">Create an account</a>`,
165		"web login</code>",
166	} {
167		if !strings.Contains(body, want) {
168			t.Errorf("landing lacks %q", want)
169		}
170	}
171	if strings.Contains(body, "is the whole onboarding") {
172		t.Error("landing still calls repo create the whole onboarding")
173	}
174	_, reg := inst.get(t, "/register")
175	if !strings.Contains(reg, "Paste the contents of your public key file") || !strings.Contains(reg, "/krz/gitbay/wiki/SSH-keys") {
176		t.Error("register page lacks the key hint or the wiki link")
177	}
178}
179
180func TestWebInteractions(t *testing.T) {
181	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
182	aliceKey := inst.newKey(t, "alice")
183	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
184	if _, _, code := inst.ssh(t, aliceKey, "", "org", "create", "theorg"); code != 0 {
185		t.Fatal("org create failed")
186	}
187
188	out, _, code := inst.ssh(t, aliceKey, "", "web", "login", "--json")
189	if code != 0 {
190		t.Fatal("web login failed")
191	}
192	var env2 struct {
193		Data struct {
194			URL string `json:"url"`
195		} `json:"data"`
196	}
197	json.Unmarshal([]byte(out), &env2)
198	browser := newBrowser(t)
199	browserGet(t, browser, inst.base()+env2.Data.URL[strings.Index(env2.Data.URL, "/login"):])
200
201	// Create a repo under the org through the web form.
202	if status, _ := browserPost(t, browser, inst.base()+"/new",
203		url.Values{"owner": {"theorg"}, "name": {"webborn"}, "visibility": {"public"}}); status != 200 {
204		t.Fatalf("org repo via web: %d", status)
205	}
206	if out, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "theorg/webborn"); code != 0 {
207		t.Fatalf("org repo missing: %s", out)
208	}
209
210	// Pin from the web; the repo header reflects it and the dashboard
211	// lists it; a second toggle unpins.
212	if status, _ := browserPost(t, browser, inst.base()+"/theorg/webborn/pin", url.Values{}); status != 200 {
213		t.Fatal("pin toggle failed")
214	}
215	_, body := browserGet(t, browser, inst.base()+"/theorg/webborn")
216	if !strings.Contains(body, "</span> Pinned</button>") {
217		t.Fatal("repo header not pinned")
218	}
219	if _, body = browserGet(t, browser, inst.base()+"/"); !strings.Contains(body, ">theorg/</span>webborn") {
220		t.Fatal("dashboard missing pinned repo")
221	}
222	browserPost(t, browser, inst.base()+"/theorg/webborn/pin", url.Values{})
223	if _, body = browserGet(t, browser, inst.base()+"/theorg/webborn"); !strings.Contains(body, "</span> Pin</button>") {
224		t.Fatal("unpin failed")
225	}
226
227	// New issue with labels through the form; label chips filter.
228	if status, _ := browserPost(t, browser, inst.base()+"/theorg/webborn/issues/new",
229		url.Values{"title": {"styled"}, "body": {"b"}, "labels": {"bug ui"}}); status != 200 {
230		t.Fatal("issue via web failed")
231	}
232	if status, _ := browserPost(t, browser, inst.base()+"/theorg/webborn/issues/new",
233		url.Values{"title": {"plain"}}); status != 200 {
234		t.Fatal("second issue failed")
235	}
236	_, body = browserGet(t, browser, inst.base()+"/theorg/webborn/issues?label=bug")
237	if !strings.Contains(body, "styled") || strings.Contains(body, ">plain<") {
238		t.Fatalf("label filter wrong:\n%s", body)
239	}
240	_, body = browserGet(t, browser, inst.base()+"/theorg/webborn/issues/1")
241	if !strings.Contains(body, `href="/theorg/webborn/issues?label=bug"`) ||
242		!strings.Contains(body, `href="/alice">alice</a>`) {
243		t.Fatal("issue page chips/author not linked")
244	}
245}
246
247func TestCommitParentLinks(t *testing.T) {
248	inst := startInstance(t)
249	aliceKey := inst.newKey(t, "alice")
250	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
251	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
252		t.Fatal("repo create failed")
253	}
254	work := t.TempDir()
255	env := inst.gitEnv(aliceKey)
256	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
257	dir := filepath.Join(work, "w")
258	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
259	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
260	mustGit(t, dir, env, "add", ".")
261	mustGit(t, dir, env, "commit", "-q", "-m", "first")
262	first := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
263	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("b\n"), 0o644)
264	mustGit(t, dir, env, "commit", "-qam", "second")
265	head := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
266	mustGit(t, dir, env, "push", "-q", "origin", "main")
267
268	_, body := inst.get(t, "/alice/app/commit/"+head)
269	if !strings.Contains(body, `href="/alice/app/commit/`+first+`"`) {
270		t.Fatal("parent commit not linked")
271	}
272	_, body = inst.get(t, "/alice/app/commit/"+first)
273	if strings.Contains(body, ">parent") {
274		t.Fatal("root commit shows a parent")
275	}
276}
277
278// TestAuthorNamesResolve checks that a commit whose author address is
279// verified here displays the account's name rather than whatever git
280// config carried, and that an unknown address keeps its own name.
281func TestAuthorNamesResolve(t *testing.T) {
282	inst := startInstance(t)
283	aliceKey := inst.newKey(t, "alice")
284	inst.admin(t, "admin", "user", "create", "alice",
285		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
286	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
287		t.Fatalf("repo create: %s", errOut)
288	}
289	work := t.TempDir()
290	env := inst.gitEnv(aliceKey)
291	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
292	dir := filepath.Join(work, "w")
293
294	// One commit from the account's verified address under a different
295	// display name, one from an address nobody has proven.
296	known := append(append([]string{}, env...),
297		"GIT_AUTHOR_NAME=Alice Q. Longname", "GIT_AUTHOR_EMAIL=alice@example.test",
298		"GIT_COMMITTER_NAME=Alice Q. Longname", "GIT_COMMITTER_EMAIL=alice@example.test")
299	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
300	mustGit(t, dir, known, "checkout", "-q", "-b", "main")
301	mustGit(t, dir, known, "add", ".")
302	mustGit(t, dir, known, "commit", "-q", "-m", "from the account")
303	stranger := append(append([]string{}, env...),
304		"GIT_AUTHOR_NAME=Outside Person", "GIT_AUTHOR_EMAIL=outside@nowhere.test",
305		"GIT_COMMITTER_NAME=Outside Person", "GIT_COMMITTER_EMAIL=outside@nowhere.test")
306	os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644)
307	mustGit(t, dir, stranger, "add", ".")
308	mustGit(t, dir, stranger, "commit", "-q", "-m", "from a stranger")
309	// The tip is the account's, so the bar above the listing shows a link.
310	os.WriteFile(filepath.Join(dir, "c.txt"), []byte("c\n"), 0o644)
311	mustGit(t, dir, known, "add", ".")
312	mustGit(t, dir, known, "commit", "-q", "-m", "back to the account")
313	mustGit(t, dir, known, "push", "-q", "origin", "main")
314
315	// The log shows the account name for the verified address only.
316	_, body := inst.get(t, "/alice/app/log")
317	if strings.Contains(body, "Alice Q. Longname") {
318		t.Fatalf("log showed the git config name for a known address:\n%s", body)
319	}
320	if !strings.Contains(body, "Outside Person") {
321		t.Fatalf("log lost an unknown author's name:\n%s", body)
322	}
323	if !strings.Contains(body, "alice") {
324		t.Fatalf("log missing the account name:\n%s", body)
325	}
326	// A resolved name links to the profile; an unknown one stays text.
327	if !strings.Contains(body, `class="authorlink" href="/alice"`) {
328		t.Fatalf("account name is not a link:\n%s", body)
329	}
330	if strings.Contains(body, `href="/Outside Person"`) {
331		t.Fatalf("unknown author was linked:\n%s", body)
332	}
333	// The tipbar resolves and links the same way when the tip is an account's.
334	if _, tree := inst.get(t, "/alice/app"); !strings.Contains(tree, `class="authorlink" href="/alice"`) {
335		t.Fatalf("tipbar name is not a link:\n%s", tree)
336	}
337}
338
339// TestTreeSearchCodeAndClone: the overview links "Search code", not
340// "Find file", and shows two labelled clone blocks after the file table.
341func TestTreeSearchCodeAndClone(t *testing.T) {
342	inst := startInstance(t)
343	key := inst.newKey(t, "alice")
344	inst.admin(t, "admin", "user", "create", "alice", "--key", key+".pub")
345	inst.ssh(t, key, "", "repo", "create", "alice/app")
346	work := t.TempDir()
347	env := inst.gitEnv(key)
348	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
349	dir := filepath.Join(work, "w")
350	os.WriteFile(filepath.Join(dir, "README.md"), []byte("# app\n"), 0o644)
351	os.MkdirAll(filepath.Join(dir, "docs"), 0o755)
352	os.WriteFile(filepath.Join(dir, "docs", "README.md"), []byte("# docs\n"), 0o644)
353	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
354	mustGit(t, dir, env, "add", ".")
355	mustGit(t, dir, env, "commit", "-q", "-m", "init")
356	mustGit(t, dir, env, "push", "-q", "origin", "main")
357	_, body := inst.get(t, "/alice/app")
358	if strings.Contains(body, ">Find file<") || !strings.Contains(body, ">Search code<") {
359		t.Error("overview still says Find file")
360	}
361	if i, j := strings.Index(body, `<table class="tree">`), strings.Index(body, `<div class="clone">`); i < 0 || j < i {
362		t.Error("clone block does not follow the file table")
363	}
364	if !strings.Contains(body, `<label>SSH</label>`) || !strings.Contains(body, `<label>HTTPS</label>`) {
365		t.Error("clone blocks are not labelled")
366	}
367	// Clone and about belong to the repository root, not a subdirectory.
368	if _, sub := inst.get(t, "/alice/app/tree/main/docs"); strings.Contains(sub, `<div class="facts">`) {
369		t.Error("subdirectory listing still shows the clone/about facts block")
370	}
371}