krz/gitbay
A CLI-first git forge.
clone: git clone https://gitbay.org/krz/gitbay.git
1package e2e
2
3import (
4 "compress/gzip"
5 "fmt"
6 "io"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "golang.org/x/crypto/ssh"
14
15 "gitbay.org/gitbay/internal/sig"
16)
17
18func (i *instance) get(t *testing.T, path string) (int, string) {
19 t.Helper()
20 resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d%s", i.httpPort, path))
21 if err != nil {
22 t.Fatal(err)
23 }
24 defer resp.Body.Close()
25 body, _ := io.ReadAll(resp.Body)
26 return resp.StatusCode, string(body)
27}
28
29func TestWebUI(t *testing.T) {
30 inst := startInstance(t)
31
32 aliceKey := inst.newKey(t, "alice")
33 inst.admin(t, "admin", "user", "create", "alice",
34 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
35
36 // Public repo with real content: a README, a source file, a tag, and
37 // one SSHSIG-signed commit for the badge check.
38 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/site"); code != 0 {
39 t.Fatalf("repo create: %s", errOut)
40 }
41 work := t.TempDir()
42 env := inst.gitEnv(aliceKey)
43 mustGit(t, work, env, "clone", inst.sshURL("alice/site"), "w")
44 dir := filepath.Join(work, "w")
45 os.WriteFile(filepath.Join(dir, "README.md"), []byte("# hello site\n\nsome *markdown*\n"), 0o644)
46 os.MkdirAll(filepath.Join(dir, "src"), 0o755)
47 os.WriteFile(filepath.Join(dir, "src", "main.go"), []byte("package main\n\nfunc main() {}\n"), 0o644)
48 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
49 mustGit(t, dir, env, "add", ".")
50 mustGit(t, dir, env, "commit", "-q", "-m", "first commit")
51 mustGit(t, dir, env, "tag", "v1.0")
52 mustGit(t, dir, env, "push", "-q", "origin", "main", "v1.0")
53
54 // A signed commit on top, built with the M4 fixture helpers.
55 sshRaw, _ := os.ReadFile(aliceKey)
56 signer, err := ssh.ParsePrivateKey(sshRaw)
57 if err != nil {
58 t.Fatal(err)
59 }
60 head := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
61 buildSignedCommitOn(t, dir, env, head, "signed tip", "alice@example.test", signer)
62 mustGit(t, dir, env, "push", "-q", "origin", "main")
63
64 // Private repo must be invisible everywhere.
65 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/secret", "--private"); code != 0 {
66 t.Fatal("create private failed")
67 }
68
69 // Index lists the public repo, not the private one.
70 status, body := inst.get(t, "/")
71 if status != 200 || !strings.Contains(body, "alice/site") {
72 t.Fatalf("index: %d\n%s", status, body)
73 }
74 if strings.Contains(body, "secret") {
75 t.Fatal("index leaks private repo")
76 }
77
78 // Repo home: tree entries plus rendered README.
79 status, body = inst.get(t, "/alice/site")
80 if status != 200 || !strings.Contains(body, "src/") || !strings.Contains(body, "README.md") {
81 t.Fatalf("repo home: %d\n%s", status, body)
82 }
83 if !strings.Contains(body, "<h1>hello site</h1>") || !strings.Contains(body, "<em>markdown</em>") {
84 t.Fatalf("README not rendered:\n%s", body)
85 }
86 for _, tab := range []string{">issues<", ">merge requests<"} {
87 if !strings.Contains(body, tab) {
88 t.Fatalf("repo header missing %s tab", tab)
89 }
90 }
91
92 // Subdirectory tree and blob with highlighting.
93 status, body = inst.get(t, "/alice/site/tree/main/src")
94 if status != 200 || !strings.Contains(body, "main.go") {
95 t.Fatalf("tree src: %d", status)
96 }
97 status, body = inst.get(t, "/alice/site/blob/main/src/main.go")
98 if status != 200 || !strings.Contains(body, "package") {
99 t.Fatalf("blob: %d", status)
100 }
101
102 // Raw serves exact bytes with nosniff.
103 resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/raw/main/src/main.go", inst.httpPort))
104 if err != nil {
105 t.Fatal(err)
106 }
107 raw, _ := io.ReadAll(resp.Body)
108 resp.Body.Close()
109 if string(raw) != "package main\n\nfunc main() {}\n" {
110 t.Fatalf("raw bytes: %q", raw)
111 }
112 if resp.Header.Get("X-Content-Type-Options") != "nosniff" {
113 t.Fatal("raw missing nosniff")
114 }
115
116 // Log: both commits, with badges matching the M4 states exactly.
117 status, body = inst.get(t, "/alice/site/log")
118 if status != 200 {
119 t.Fatalf("log: %d", status)
120 }
121 if !strings.Contains(body, "badge-verified") || !strings.Contains(body, "signed tip") {
122 t.Fatalf("log missing verified badge:\n%s", body)
123 }
124 if !strings.Contains(body, "badge-unsigned") || !strings.Contains(body, "first commit") {
125 t.Fatalf("log missing unsigned badge:\n%s", body)
126 }
127
128 // Commit page for the signed tip.
129 tip := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
130 status, body = inst.get(t, "/alice/site/commit/"+tip)
131 if status != 200 || !strings.Contains(body, "badge-verified") || !strings.Contains(body, "alice") {
132 t.Fatalf("commit page: %d\n%s", status, body)
133 }
134
135 // Refs page shows branch and tag.
136 status, body = inst.get(t, "/alice/site/refs")
137 if status != 200 || !strings.Contains(body, "main") || !strings.Contains(body, "v1.0") {
138 t.Fatalf("refs: %d", status)
139 }
140
141 // Archive downloads a valid gzip.
142 resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:%d/alice/site/archive/main.tar.gz", inst.httpPort))
143 if err != nil {
144 t.Fatal(err)
145 }
146 gz, err := gzip.NewReader(resp.Body)
147 if err != nil {
148 t.Fatalf("archive not gzip: %v", err)
149 }
150 tarBytes, _ := io.ReadAll(gz)
151 resp.Body.Close()
152 if !strings.Contains(string(tarBytes), "README.md") {
153 t.Fatal("archive missing content")
154 }
155
156 // README formats: org-mode renders, HTML renders sanitized, unknown
157 // extensions fall back to plaintext, and richer formats win conflicts.
158 readmeRepo := func(name, file, content string) {
159 t.Helper()
160 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/"+name); code != 0 {
161 t.Fatalf("repo create %s failed", name)
162 }
163 w := t.TempDir()
164 mustGit(t, w, env, "clone", inst.sshURL("alice/"+name), "r")
165 d := filepath.Join(w, "r")
166 os.WriteFile(filepath.Join(d, file), []byte(content), 0o644)
167 mustGit(t, d, env, "checkout", "-q", "-b", "main")
168 mustGit(t, d, env, "add", ".")
169 mustGit(t, d, env, "commit", "-q", "-m", "readme")
170 mustGit(t, d, env, "push", "-q", "origin", "main")
171 }
172
173 readmeRepo("orgdoc", "README.org", "* Heading\n\nSome /emphasis/ here.\n")
174 status, body = inst.get(t, "/alice/orgdoc")
175 if status != 200 || !strings.Contains(body, "headline-1") || !strings.Contains(body, "<em>emphasis</em>") {
176 t.Fatalf("org README not rendered:\n%s", body)
177 }
178
179 readmeRepo("htmldoc", "README.html", "<p id=\"ok\">fine</p><script>alert(1)</script>")
180 status, body = inst.get(t, "/alice/htmldoc")
181 if status != 200 || !strings.Contains(body, "fine</p>") {
182 t.Fatalf("html README not rendered:\n%s", body)
183 }
184 if strings.Contains(body, "<script>alert") {
185 t.Fatal("repo HTML script survived sanitization")
186 }
187
188 readmeRepo("txtdoc", "README.txt", "plain <text> & stuff\n")
189 status, body = inst.get(t, "/alice/txtdoc")
190 if status != 200 || !strings.Contains(body, "plain <text> & stuff") {
191 t.Fatalf("txt README not escaped-plaintext:\n%s", body)
192 }
193
194 // Owner page: lists visible repos only; unknown owners 404.
195 status, body = inst.get(t, "/alice")
196 if status != 200 || !strings.Contains(body, ">site<") || !strings.Contains(body, "user") {
197 t.Fatalf("owner page: %d", status)
198 }
199 if strings.Contains(body, "secret") {
200 t.Fatal("owner page leaks private repo")
201 }
202 if status, _ := inst.get(t, "/nobody"); status != 404 {
203 t.Fatalf("unknown owner: %d, want 404", status)
204 }
205
206 // Private repo pages: 404, indistinguishable from nonexistent.
207 for _, p := range []string{"/alice/secret", "/alice/secret/log", "/alice/nothere"} {
208 if status, _ := inst.get(t, p); status != 404 {
209 t.Errorf("GET %s = %d, want 404", p, status)
210 }
211 }
212}
213
214// buildSignedCommitOn adds one SSHSIG-signed commit on top of parent,
215// reusing the M4 fixture machinery.
216func buildSignedCommitOn(t *testing.T, dir string, env []string, parent, subject, email string, signer ssh.Signer) {
217 t.Helper()
218 tree := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", parent+"^{tree}"))
219 specs := []commitSpec{{
220 authorEmail: email,
221 subject: subject,
222 sign: func(p []byte) string {
223 s, err := sig.MarshalSSHSig(signer, p)
224 if err != nil {
225 t.Fatal(err)
226 }
227 return string(s)
228 },
229 }}
230 buildChain(t, dir, env, tree, parent, specs)
231}