e2e/autolink_test.go
93 lines · 3525 bytes
1package e2e
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestWebAutolinks(t *testing.T) {
11 t.Parallel()
12 inst := startInstance(t)
13 aliceKey := inst.newKey(t, "alice")
14 bobKey := inst.newKey(t, "bob")
15 inst.admin(t, "admin", "user", "create", "alice",
16 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
17 inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
18
19 // Public repo with an issue, an MR, and a private repo with an issue.
20 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
21 t.Fatalf("repo create: %s", errOut)
22 }
23 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/secret", "--private"); code != 0 {
24 t.Fatal("private repo create failed")
25 }
26 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/secret", "--title", "'hidden'"); code != 0 {
27 t.Fatal("private issue create failed")
28 }
29 work := t.TempDir()
30 env := inst.gitEnv(aliceKey)
31 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
32 dir := filepath.Join(work, "w")
33 os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
34 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
35 mustGit(t, dir, env, "add", ".")
36 mustGit(t, dir, env, "commit", "-q", "-m", "base")
37 mustGit(t, dir, env, "push", "-q", "origin", "main")
38 mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
39 os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644)
40 mustGit(t, dir, env, "add", ".")
41 mustGit(t, dir, env, "commit", "-q", "-m", "feat")
42 mustGit(t, dir, env, "push", "-q", "origin", "feat")
43 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
44 "--source", "feat", "--target", "main", "--title", "'feat'"); code != 0 {
45 t.Fatalf("mr create: %s", errOut)
46 }
47 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'first'"); code != 0 {
48 t.Fatal("issue 1 create failed")
49 }
50 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app",
51 "--title", "'refs'", "--body",
52 "'see #1 and !1 and alice/app#1 and @bob, not #99, not alice/secret#1, not `#1` in code'"); code != 0 {
53 t.Fatal("issue 2 create failed")
54 }
55
56 status, body := inst.get(t, "/alice/app/issues/2")
57 if status != 200 {
58 t.Fatalf("issue page: %d", status)
59 }
60 for _, want := range []string{
61 `<a href="/alice/app/issues/1" class="xref">#1</a>`,
62 `<a href="/alice/app/mrs/1" class="xref">!1</a>`,
63 `<a href="/alice/app/issues/1" class="xref">alice/app#1</a>`,
64 `<a href="/bob" class="xref">@bob</a>`,
65 } {
66 if !strings.Contains(body, want) {
67 t.Errorf("missing %q on issue page", want)
68 }
69 }
70 if strings.Contains(body, "issues/99") {
71 t.Error("nonexistent issue got linked")
72 }
73 // The private repo must stay plain text for anonymous viewers: a link
74 // would confirm it exists.
75 if strings.Contains(body, `href="/alice/secret`) {
76 t.Error("private repo reference leaked as a link")
77 }
78 if !strings.Contains(body, "alice/secret#1") {
79 t.Error("private repo reference text missing entirely")
80 }
81 if !strings.Contains(body, "<code>#1</code>") {
82 t.Error("code-span reference was rewritten")
83 }
84
85 // Comments on MR pages get the same treatment.
86 if _, _, code := inst.ssh(t, bobKey, "", "mr", "comment", "alice/app", "1", "--message", "'closes #2'"); code != 0 {
87 t.Fatal("mr comment failed")
88 }
89 _, body = inst.get(t, "/alice/app/mrs/1")
90 if !strings.Contains(body, `<a href="/alice/app/issues/2" class="xref">#2</a>`) {
91 t.Error("MR comment reference not linked")
92 }
93}