e2e/issue_test.go
173 lines · 7178 bytes
1package e2e
2
3import (
4 "encoding/json"
5 "regexp"
6 "strings"
7 "testing"
8)
9
10// normalizeJSON parses JSON and blanks volatile timestamp fields so the
11// remainder can be compared as a golden value.
12var tsPat = regexp.MustCompile(`"\d{4}-\d{2}-\d{2}T[0-9:.]+Z?"`)
13
14func golden(t *testing.T, raw string) string {
15 t.Helper()
16 norm := tsPat.ReplaceAllString(strings.TrimSpace(raw), `"TS"`)
17 // Re-encode compactly for stable comparison.
18 var v any
19 if err := json.Unmarshal([]byte(norm), &v); err != nil {
20 t.Fatalf("not JSON: %v\n%s", err, raw)
21 }
22 out, _ := json.Marshal(v)
23 return string(out)
24}
25
26func TestIssueLifecycleOverBareSSH(t *testing.T) {
27 inst := startInstance(t)
28
29 aliceKey := inst.newKey(t, "alice")
30 bobKey := inst.newKey(t, "bob")
31 eveKey := inst.newKey(t, "eve")
32 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
33 inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
34 inst.admin(t, "admin", "user", "create", "eve", "--key", eveKey+".pub")
35
36 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/proj"); code != 0 {
37 t.Fatalf("repo create: %s", errOut)
38 }
39
40 // Create with inline body; numbering starts at 1.
41 out, errOut, code := inst.ssh(t, aliceKey, "",
42 "issue", "create", "alice/proj", "--title", "'first bug'", "--body", "'it is broken'", "--json")
43 if code != 0 {
44 t.Fatalf("issue create: %s", errOut)
45 }
46 if g := golden(t, out); g != `{"data":{"number":1},"protocol_version":1}` {
47 t.Fatalf("create output: %s", g)
48 }
49
50 // Bob (no grant, public repo) can file an issue too — body over stdin.
51 _, errOut, code = inst.ssh(t, bobKey, "long body\nfrom stdin\n",
52 "issue", "create", "alice/proj", "--title", "'from bob'", "--file", "-")
53 if code != 0 {
54 t.Fatalf("bob issue create: %s", errOut)
55 }
56
57 // Comment, label, assign, close.
58 if _, errOut, code = inst.ssh(t, bobKey, "", "issue", "comment", "alice/proj", "1", "--message", "'me too'"); code != 0 {
59 t.Fatalf("comment: %s", errOut)
60 }
61 if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "label", "alice/proj", "1", "--add", "bug", "--add", "urgent"); code != 0 {
62 t.Fatalf("label: %s", errOut)
63 }
64 if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "assign", "alice/proj", "1", "--add", "bob"); code != 0 {
65 t.Fatalf("assign: %s", errOut)
66 }
67
68 // Golden check on issue show --json.
69 out, _, code = inst.ssh(t, aliceKey, "", "issue", "show", "alice/proj", "1", "--json")
70 if code != 0 {
71 t.Fatal("issue show failed")
72 }
73 // A body reports the markup it was written in; "md" is what a body with no
74 // --format carries, and what everything written before formats existed has.
75 wantShow := `{"data":{"assignees":["bob"],"author":"alice","body":"it is broken",` +
76 `"body_format":"md",` +
77 `"comments":[{"author":"bob","body":"me too","body_format":"md","created_at":"TS"}],` +
78 `"created_at":"TS","labels":["bug","urgent"],"number":1,"state":"open",` +
79 `"title":"first bug"},"protocol_version":1}`
80 if g := golden(t, out); g != wantShow {
81 t.Fatalf("issue show golden mismatch:\ngot %s\nwant %s", g, wantShow)
82 }
83
84 // Permission edges: eve (read-only public) cannot label or close
85 // someone else's issue; bob can close his own.
86 _, errOut, code = inst.ssh(t, eveKey, "", "issue", "label", "alice/proj", "1", "--add", "spam")
87 if code != 4 {
88 t.Fatalf("eve label: exit %d (want 4), %s", code, errOut)
89 }
90 _, errOut, code = inst.ssh(t, eveKey, "", "issue", "close", "alice/proj", "1")
91 if code != 4 {
92 t.Fatalf("eve close: exit %d (want 4), %s", code, errOut)
93 }
94 if _, errOut, code = inst.ssh(t, bobKey, "", "issue", "close", "alice/proj", "2"); code != 0 {
95 t.Fatalf("bob closing own issue: %s", errOut)
96 }
97
98 // Close, list filters, reopen.
99 if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "close", "alice/proj", "1"); code != 0 {
100 t.Fatalf("close: %s", errOut)
101 }
102 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "list", "alice/proj", "--json")
103 if g := golden(t, out); g != `{"data":[],"protocol_version":1}` { // empty list is [], never null
104 t.Fatalf("open list after close: %s", g)
105 }
106 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "list", "alice/proj", "--state", "closed", "--json")
107 if !strings.Contains(out, `"number":2`) || !strings.Contains(out, `"number":1`) {
108 t.Fatalf("closed list: %s", out)
109 }
110 if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "reopen", "alice/proj", "1"); code != 0 {
111 t.Fatalf("reopen: %s", errOut)
112 }
113 // Double-reopen is a usage error.
114 if _, _, code = inst.ssh(t, aliceKey, "", "issue", "reopen", "alice/proj", "1"); code != 2 {
115 t.Fatalf("double reopen: exit %d, want 2", code)
116 }
117
118 // Label removal and assignee removal.
119 if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "label", "alice/proj", "1", "--remove", "urgent"); code != 0 {
120 t.Fatalf("label remove: %s", errOut)
121 }
122 if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "assign", "alice/proj", "1", "--remove", "bob"); code != 0 {
123 t.Fatalf("assign remove: %s", errOut)
124 }
125 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/proj", "1", "--json")
126 if strings.Contains(out, "urgent") || strings.Contains(out, "assignees") {
127 t.Fatalf("removal not reflected: %s", out)
128 }
129
130 // Missing issue and missing repo produce not-found exit codes.
131 if _, _, code = inst.ssh(t, aliceKey, "", "issue", "show", "alice/proj", "99"); code != 3 {
132 t.Fatalf("missing issue: exit %d, want 3", code)
133 }
134 if _, _, code = inst.ssh(t, aliceKey, "", "issue", "list", "alice/nope"); code != 3 {
135 t.Fatalf("missing repo: exit %d, want 3", code)
136 }
137
138 // Web read views: list shows the issue, detail shows the comment, and
139 // bodies render as markdown (goldmark drops raw HTML).
140 status, body := inst.get(t, "/alice/proj/issues")
141 if status != 200 || !strings.Contains(body, "first bug") {
142 t.Fatalf("issues page: %d", status)
143 }
144 status, body = inst.get(t, "/alice/proj/issues/1")
145 if status != 200 || !strings.Contains(body, "me too") || !strings.Contains(body, "bug") {
146 t.Fatalf("issue detail: %d\n%s", status, body)
147 }
148 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/proj",
149 "--title", "'md body'", "--body", "'has **bold** and <script>x</script>'"); code != 0 {
150 t.Fatal("md issue create failed")
151 }
152 status, body = inst.get(t, "/alice/proj/issues/3")
153 if status != 200 || !strings.Contains(body, "<strong>bold</strong>") {
154 t.Fatalf("markdown body not rendered: %d\n%s", status, body)
155 }
156 if strings.Contains(body, "<script>x</script>") {
157 t.Fatal("raw HTML survived in issue body")
158 }
159
160 // Private repos hide their issues from non-readers, as not-found.
161 if _, _, code = inst.ssh(t, aliceKey, "", "repo", "create", "alice/secret", "--private"); code != 0 {
162 t.Fatal("create private failed")
163 }
164 if _, _, code = inst.ssh(t, aliceKey, "", "issue", "create", "alice/secret", "--title", "hidden"); code != 0 {
165 t.Fatal("issue in private repo failed")
166 }
167 if _, _, code = inst.ssh(t, eveKey, "", "issue", "show", "alice/secret", "1"); code != 3 {
168 t.Fatalf("eve sees private issue: exit %d, want 3", code)
169 }
170 if status, _ := inst.get(t, "/alice/secret/issues"); status != 404 {
171 t.Fatalf("anonymous issues page on private repo: %d, want 404", status)
172 }
173}