krz/gitbay

A CLI-first git forge.

clone: git clone https://gitbay.org/krz/gitbay.git

repo-descriptions: e2e/issue_test.go · raw

  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	wantShow := `{"data":{"assignees":["bob"],"author":"alice","body":"it is broken",` +
 74		`"comments":[{"author":"bob","body":"me too","created_at":"TS"}],` +
 75		`"created_at":"TS","labels":["bug","urgent"],"number":1,"state":"open",` +
 76		`"title":"first bug"},"protocol_version":1}`
 77	if g := golden(t, out); g != wantShow {
 78		t.Fatalf("issue show golden mismatch:\ngot  %s\nwant %s", g, wantShow)
 79	}
 80
 81	// Permission edges: eve (read-only public) cannot label or close
 82	// someone else's issue; bob can close his own.
 83	_, errOut, code = inst.ssh(t, eveKey, "", "issue", "label", "alice/proj", "1", "--add", "spam")
 84	if code != 4 {
 85		t.Fatalf("eve label: exit %d (want 4), %s", code, errOut)
 86	}
 87	_, errOut, code = inst.ssh(t, eveKey, "", "issue", "close", "alice/proj", "1")
 88	if code != 4 {
 89		t.Fatalf("eve close: exit %d (want 4), %s", code, errOut)
 90	}
 91	if _, errOut, code = inst.ssh(t, bobKey, "", "issue", "close", "alice/proj", "2"); code != 0 {
 92		t.Fatalf("bob closing own issue: %s", errOut)
 93	}
 94
 95	// Close, list filters, reopen.
 96	if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "close", "alice/proj", "1"); code != 0 {
 97		t.Fatalf("close: %s", errOut)
 98	}
 99	out, _, _ = inst.ssh(t, aliceKey, "", "issue", "list", "alice/proj", "--json")
100	if g := golden(t, out); g != `{"data":[],"protocol_version":1}` { // empty list is [], never null
101		t.Fatalf("open list after close: %s", g)
102	}
103	out, _, _ = inst.ssh(t, aliceKey, "", "issue", "list", "alice/proj", "--state", "closed", "--json")
104	if !strings.Contains(out, `"number":2`) || !strings.Contains(out, `"number":1`) {
105		t.Fatalf("closed list: %s", out)
106	}
107	if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "reopen", "alice/proj", "1"); code != 0 {
108		t.Fatalf("reopen: %s", errOut)
109	}
110	// Double-reopen is a usage error.
111	if _, _, code = inst.ssh(t, aliceKey, "", "issue", "reopen", "alice/proj", "1"); code != 2 {
112		t.Fatalf("double reopen: exit %d, want 2", code)
113	}
114
115	// Label removal and assignee removal.
116	if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "label", "alice/proj", "1", "--remove", "urgent"); code != 0 {
117		t.Fatalf("label remove: %s", errOut)
118	}
119	if _, errOut, code = inst.ssh(t, aliceKey, "", "issue", "assign", "alice/proj", "1", "--remove", "bob"); code != 0 {
120		t.Fatalf("assign remove: %s", errOut)
121	}
122	out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/proj", "1", "--json")
123	if strings.Contains(out, "urgent") || strings.Contains(out, "assignees") {
124		t.Fatalf("removal not reflected: %s", out)
125	}
126
127	// Missing issue and missing repo produce not-found exit codes.
128	if _, _, code = inst.ssh(t, aliceKey, "", "issue", "show", "alice/proj", "99"); code != 3 {
129		t.Fatalf("missing issue: exit %d, want 3", code)
130	}
131	if _, _, code = inst.ssh(t, aliceKey, "", "issue", "list", "alice/nope"); code != 3 {
132		t.Fatalf("missing repo: exit %d, want 3", code)
133	}
134
135	// Web read views: list shows the issue, detail shows the comment, and
136	// bodies render as markdown (goldmark drops raw HTML).
137	status, body := inst.get(t, "/alice/proj/issues")
138	if status != 200 || !strings.Contains(body, "first bug") {
139		t.Fatalf("issues page: %d", status)
140	}
141	status, body = inst.get(t, "/alice/proj/issues/1")
142	if status != 200 || !strings.Contains(body, "me too") || !strings.Contains(body, "bug") {
143		t.Fatalf("issue detail: %d\n%s", status, body)
144	}
145	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/proj",
146		"--title", "'md body'", "--body", "'has **bold** and <script>x</script>'"); code != 0 {
147		t.Fatal("md issue create failed")
148	}
149	status, body = inst.get(t, "/alice/proj/issues/3")
150	if status != 200 || !strings.Contains(body, "<strong>bold</strong>") {
151		t.Fatalf("markdown body not rendered: %d\n%s", status, body)
152	}
153	if strings.Contains(body, "<script>x</script>") {
154		t.Fatal("raw HTML survived in issue body")
155	}
156
157	// Private repos hide their issues from non-readers, as not-found.
158	if _, _, code = inst.ssh(t, aliceKey, "", "repo", "create", "alice/secret", "--private"); code != 0 {
159		t.Fatal("create private failed")
160	}
161	if _, _, code = inst.ssh(t, aliceKey, "", "issue", "create", "alice/secret", "--title", "hidden"); code != 0 {
162		t.Fatal("issue in private repo failed")
163	}
164	if _, _, code = inst.ssh(t, eveKey, "", "issue", "show", "alice/secret", "1"); code != 3 {
165		t.Fatalf("eve sees private issue: exit %d, want 3", code)
166	}
167	if status, _ := inst.get(t, "/alice/secret/issues"); status != 404 {
168		t.Fatalf("anonymous issues page on private repo: %d, want 404", status)
169	}
170}