vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
| 1 | package server_test |
| 2 | |
| 3 | // Browser parity for release files: the same attach the CLI performs, the same |
| 4 | // pointer it writes, and the same rule — a release a channel names does not |
| 5 | // change. |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "io" |
| 10 | "mime/multipart" |
| 11 | "net/http" |
| 12 | "net/url" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "rodent.software/vole/metadata" |
| 18 | ) |
| 19 | |
| 20 | // uploadRelease posts the attach form the way a browser does: one multipart body |
| 21 | // carrying the CSRF field, an optional path prefix, and one part per file. |
| 22 | func uploadRelease(t *testing.T, f *fixture, cookie, tag, csrf, prefix string, files map[string]string) (*http.Response, string) { |
| 23 | t.Helper() |
| 24 | var buf bytes.Buffer |
| 25 | mw := multipart.NewWriter(&buf) |
| 26 | if err := mw.WriteField("csrf", csrf); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if prefix != "" { |
| 30 | if err := mw.WriteField("path", prefix); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | } |
| 34 | for name, body := range files { |
| 35 | w, err := mw.CreateFormFile("files", name) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if _, err := io.WriteString(w, body); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | } |
| 43 | if err := mw.Close(); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | req, err := http.NewRequest("POST", f.http.URL+"/"+f.repo+"/releases/"+tag+"/attach", &buf) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | req.Header.Set("Content-Type", mw.FormDataContentType()) |
| 51 | if cookie != "" { |
| 52 | req.Header.Set("Cookie", cookie) |
| 53 | } |
| 54 | resp, err := noRedirectAuth.Do(req) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | body, _ := io.ReadAll(resp.Body) |
| 59 | resp.Body.Close() |
| 60 | return resp, string(body) |
| 61 | } |
| 62 | |
| 63 | // waitForPage polls a page until it shows what a write just published. A browser |
| 64 | // write wakes the metadata loop rather than waiting for it, so the read model is |
| 65 | // allowed to lag by that one tick. |
| 66 | func waitForPage(t *testing.T, f *fixture, cookie, path, want string) string { |
| 67 | t.Helper() |
| 68 | deadline := time.Now().Add(3 * time.Second) |
| 69 | var body string |
| 70 | for { |
| 71 | resp, got := f.browserGet(t, path, cookie) |
| 72 | body = got |
| 73 | if resp.StatusCode == http.StatusOK && strings.Contains(got, want) { |
| 74 | return got |
| 75 | } |
| 76 | if time.Now().After(deadline) { |
| 77 | t.Fatalf("%s does not show %q:\n%s", path, want, body) |
| 78 | } |
| 79 | time.Sleep(25 * time.Millisecond) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestAttachFromTheBrowser(t *testing.T) { |
| 84 | t.Parallel() |
| 85 | f := newFixture(t) |
| 86 | f.publish(f.repo) |
| 87 | work := workRepo(t, map[string]string{"a.txt": "one\n"}) |
| 88 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { |
| 89 | t.Fatalf("push: %s", errs) |
| 90 | } |
| 91 | if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "notes\n"); err != nil { |
| 92 | t.Fatalf("tag: %s", errs) |
| 93 | } |
| 94 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil { |
| 95 | t.Fatalf("push tag: %s", errs) |
| 96 | } |
| 97 | sess, cookie := f.browserSession(t, f.ownerID) |
| 98 | |
| 99 | // The release page links to the manage-files page to a maintainer, and to |
| 100 | // nobody else; the multipart form itself lives on its own page. |
| 101 | _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie) |
| 102 | if strings.Contains(page, `enctype="multipart/form-data"`) { |
| 103 | t.Fatalf("release page still embeds the attach form:\n%s", page) |
| 104 | } |
| 105 | if !strings.Contains(page, "/releases/v1.0.0/attach") || !strings.Contains(page, "Manage files") { |
| 106 | t.Fatalf("release page has no manage-files link:\n%s", page) |
| 107 | } |
| 108 | resp, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0/attach", cookie) |
| 109 | if resp.StatusCode != http.StatusOK || !strings.Contains(page, `enctype="multipart/form-data"`) || !strings.Contains(page, "Attach files") { |
| 110 | t.Fatalf("manage files page = %d:\n%s", resp.StatusCode, page) |
| 111 | } |
| 112 | |
| 113 | // A flat file keeps its base name. |
| 114 | resp, body := uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "", map[string]string{"tool": "the released bytes"}) |
| 115 | if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/"+f.repo+"/releases/v1.0.0" { |
| 116 | t.Fatalf("attach = %d %q:\n%s", resp.StatusCode, resp.Header.Get("Location"), body) |
| 117 | } |
| 118 | // A directory upload keeps its paths under the named prefix. |
| 119 | resp, body = uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "apt", map[string]string{"Packages": "Package: vole\n"}) |
| 120 | if resp.StatusCode != http.StatusSeeOther { |
| 121 | t.Fatalf("attach under a path = %d:\n%s", resp.StatusCode, body) |
| 122 | } |
| 123 | |
| 124 | // The pointer is the whole record, exactly as `vole release attach` writes it. |
| 125 | m := mustRepo(t, f) |
| 126 | arts, err := metadata.ListReleaseArtifacts(m.Git(), "v1.0.0") |
| 127 | if err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | if len(arts) != 2 || arts[0].Name != "apt/Packages" || arts[1].Name != "tool" { |
| 131 | t.Fatalf("attached artifacts = %+v", arts) |
| 132 | } |
| 133 | if arts[1].Size != int64(len("the released bytes")) { |
| 134 | t.Fatalf("tool size = %d", arts[1].Size) |
| 135 | } |
| 136 | if !f.srv.LFS.Has(f.repo, arts[1].OID) { |
| 137 | t.Fatal("the bytes were not stored") |
| 138 | } |
| 139 | |
| 140 | page = waitForPage(t, f, cookie, "/"+f.repo+"/releases/v1.0.0", "/artifacts/tool") |
| 141 | for _, want := range []string{"/artifacts/apt/Packages", "Manage files", `class="release-files"`, |
| 142 | `action="/` + f.repo + `/releases/v1.0.0/files/delete"`} { |
| 143 | if !strings.Contains(page, want) { |
| 144 | t.Fatalf("release page does not show %q:\n%s", want, page) |
| 145 | } |
| 146 | } |
| 147 | resp, body = f.browserGet(t, "/"+f.repo+"/releases/v1.0.0/artifacts/tool", cookie) |
| 148 | if resp.StatusCode != http.StatusOK || body != "the released bytes" { |
| 149 | t.Fatalf("download = %d %q", resp.StatusCode, body) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestDeleteFilesFromTheBrowser(t *testing.T) { |
| 154 | t.Parallel() |
| 155 | f := releaseFixture(t) |
| 156 | attachArtifact(t, f, "v1.0.0", "tool", []byte("the released bytes")) |
| 157 | attachArtifact(t, f, "v1.0.0", "apt/Packages", []byte("Package: vole\n")) |
| 158 | sess, cookie := f.browserSession(t, f.ownerID) |
| 159 | |
| 160 | // The release page lists each file with a remove action. |
| 161 | _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie) |
| 162 | for _, want := range []string{"/artifacts/tool", "/artifacts/apt/Packages", |
| 163 | `action="/` + f.repo + `/releases/v1.0.0/files/delete"`} { |
| 164 | if !strings.Contains(page, want) { |
| 165 | t.Fatalf("release page has no remove action %q:\n%s", want, page) |
| 166 | } |
| 167 | } |
| 168 | if strings.Contains(page, "/files/edit") { |
| 169 | t.Fatalf("release page still offers a rename/move form:\n%s", page) |
| 170 | } |
| 171 | |
| 172 | // Delete removes the pointer, not the other file. |
| 173 | form := url.Values{"csrf": {sess.CSRF}, "path": {"tool"}} |
| 174 | resp, body := f.browserPost(t, "/"+f.repo+"/releases/v1.0.0/files/delete", cookie, form) |
| 175 | if resp.StatusCode != http.StatusSeeOther { |
| 176 | t.Fatalf("delete file = %d:\n%s", resp.StatusCode, body) |
| 177 | } |
| 178 | m := mustRepo(t, f) |
| 179 | if arts, err := metadata.ListReleaseArtifacts(m.Git(), "v1.0.0"); err != nil || len(arts) != 1 || arts[0].Name != "apt/Packages" { |
| 180 | t.Fatalf("artifacts after delete = %+v %v", arts, err) |
| 181 | } |
| 182 | |
| 183 | // The file is gone from the page and the download, and a second delete is |
| 184 | // refused rather than silently doing nothing. |
| 185 | page = waitForPage(t, f, cookie, "/"+f.repo+"/releases/v1.0.0", "apt/Packages") |
| 186 | if strings.Contains(page, "/artifacts/tool") { |
| 187 | t.Fatalf("release page still names the deleted file:\n%s", page) |
| 188 | } |
| 189 | if resp, _ := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0/artifacts/tool", cookie); resp.StatusCode != http.StatusNotFound { |
| 190 | t.Fatalf("deleted download = %d, want 404", resp.StatusCode) |
| 191 | } |
| 192 | if resp, body := f.browserPost(t, "/"+f.repo+"/releases/v1.0.0/files/delete", cookie, form); resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "no longer attached") { |
| 193 | t.Fatalf("second delete = %d:\n%s", resp.StatusCode, body) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestAttachRefusalsFromTheBrowser(t *testing.T) { |
| 198 | t.Parallel() |
| 199 | f := newFixture(t) |
| 200 | f.publish(f.repo) |
| 201 | work := workRepo(t, map[string]string{"a.txt": "one\n"}) |
| 202 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { |
| 203 | t.Fatalf("push: %s", errs) |
| 204 | } |
| 205 | if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "notes\n"); err != nil { |
| 206 | t.Fatalf("tag: %s", errs) |
| 207 | } |
| 208 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil { |
| 209 | t.Fatalf("push tag: %s", errs) |
| 210 | } |
| 211 | m := mustRepo(t, f) |
| 212 | commit, _ := m.Git().RevParse("main") |
| 213 | if err := m.Git().UpdateRef("refs/tags/light", commit); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | sess, cookie := f.browserSession(t, f.ownerID) |
| 217 | |
| 218 | // A token in the body still has to be this session's token. |
| 219 | if resp, body := uploadRelease(t, f, cookie, "v1.0.0", "not-a-token", "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusForbidden { |
| 220 | t.Fatalf("bad csrf = %d:\n%s", resp.StatusCode, body) |
| 221 | } |
| 222 | // A path that climbs out of the release is refused before anything is stored. |
| 223 | if resp, body := uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "../env", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusBadRequest { |
| 224 | t.Fatalf("path traversal = %d:\n%s", resp.StatusCode, body) |
| 225 | } |
| 226 | // A lightweight tag has no release to attach to. |
| 227 | if resp, body := uploadRelease(t, f, cookie, "light", sess.CSRF, "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "lightweight") { |
| 228 | t.Fatalf("lightweight attach = %d:\n%s", resp.StatusCode, body) |
| 229 | } |
| 230 | if resp, body := f.browserGet(t, "/"+f.repo+"/releases/light/attach", cookie); resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "Lightweight") { |
| 231 | t.Fatalf("lightweight attach page = %d:\n%s", resp.StatusCode, body) |
| 232 | } |
| 233 | // An anonymous visitor gets the login page, not the form. |
| 234 | if resp, _ := uploadRelease(t, f, "", "v1.0.0", sess.CSRF, "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusSeeOther || !strings.HasPrefix(resp.Header.Get("Location"), "/-/login") { |
| 235 | t.Fatalf("anonymous attach = %d %q", resp.StatusCode, resp.Header.Get("Location")) |
| 236 | } |
| 237 | if resp, _ := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0/attach", ""); resp.StatusCode != http.StatusSeeOther || !strings.HasPrefix(resp.Header.Get("Location"), "/-/login") { |
| 238 | t.Fatalf("anonymous attach page = %d %q", resp.StatusCode, resp.Header.Get("Location")) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // TestEditNotesAfterAttachingFiles is the change this rule set exists for: an |
| 243 | // attached file no longer freezes the notes, but a channel still does. |
| 244 | func TestEditNotesAfterAttachingFiles(t *testing.T) { |
| 245 | t.Parallel() |
| 246 | f := newFixture(t) |
| 247 | f.publish(f.repo) |
| 248 | work := workRepo(t, map[string]string{"a.txt": "one\n"}) |
| 249 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil { |
| 250 | t.Fatalf("push: %s", errs) |
| 251 | } |
| 252 | if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "First release.\n"); err != nil { |
| 253 | t.Fatalf("tag: %s", errs) |
| 254 | } |
| 255 | if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil { |
| 256 | t.Fatalf("push tag: %s", errs) |
| 257 | } |
| 258 | attachArtifact(t, f, "v1.0.0", "tool", []byte("bytes")) |
| 259 | sess, cookie := f.browserSession(t, f.ownerID) |
| 260 | |
| 261 | resp, page := f.browserGet(t, "/"+f.repo+"/releases/edit?tag=v1.0.0", cookie) |
| 262 | if resp.StatusCode != http.StatusOK || !strings.Contains(page, "Save notes") { |
| 263 | t.Fatalf("edit page locks an attached release = %d:\n%s", resp.StatusCode, page) |
| 264 | } |
| 265 | form := url.Values{"csrf": {sess.CSRF}, "tag": {"v1.0.0"}, "notes": {"Corrected notes.\n"}} |
| 266 | resp, body := f.browserPost(t, "/"+f.repo+"/releases/edit", cookie, form) |
| 267 | if resp.StatusCode != http.StatusSeeOther { |
| 268 | t.Fatalf("editing after an attach = %d:\n%s", resp.StatusCode, body) |
| 269 | } |
| 270 | m := mustRepo(t, f) |
| 271 | if tag, err := m.Git().Tag("v1.0.0"); err != nil || !strings.Contains(tag.Message, "Corrected notes.") { |
| 272 | t.Fatalf("edited notes = %+v %v", tag, err) |
| 273 | } |
| 274 | // The attachment survives the re-tag: the pointers name paths, not the tag object. |
| 275 | if arts, err := metadata.ListReleaseArtifacts(m.Git(), "v1.0.0"); err != nil || len(arts) != 1 || arts[0].Name != "tool" { |
| 276 | t.Fatalf("artifacts after a notes edit = %+v %v", arts, err) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestChanneledReleaseIsLockedInTheBrowser(t *testing.T) { |
| 281 | t.Parallel() |
| 282 | f := releaseFixture(t) |
| 283 | attachArtifact(t, f, "v1.0.0", "tool", []byte("bytes")) |
| 284 | setChannel(t, f, "stable", "v1.0.0") |
| 285 | sess, cookie := f.browserSession(t, f.ownerID) |
| 286 | |
| 287 | _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie) |
| 288 | if !strings.Contains(page, "A channel targets this release") { |
| 289 | t.Fatalf("release page does not explain the lock:\n%s", page) |
| 290 | } |
| 291 | for _, unwanted := range []string{"Edit release", `enctype="multipart/form-data"`, |
| 292 | "/releases/v1.0.0/attach", "/files/delete"} { |
| 293 | if strings.Contains(page, unwanted) { |
| 294 | t.Fatalf("release page still offers %q:\n%s", unwanted, page) |
| 295 | } |
| 296 | } |
| 297 | resp, body := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0/attach", cookie) |
| 298 | if resp.StatusCode != http.StatusConflict || !strings.Contains(body, "Channel stable targets this release") { |
| 299 | t.Fatalf("channeled attach page = %d:\n%s", resp.StatusCode, body) |
| 300 | } |
| 301 | remove := url.Values{"csrf": {sess.CSRF}, "path": {"tool"}} |
| 302 | if resp, body := f.browserPost(t, "/"+f.repo+"/releases/v1.0.0/files/delete", cookie, remove); resp.StatusCode != http.StatusConflict || !strings.Contains(body, "Channel stable") { |
| 303 | t.Fatalf("removing a file of a channeled release = %d:\n%s", resp.StatusCode, body) |
| 304 | } |
| 305 | |
| 306 | resp, page = f.browserGet(t, "/"+f.repo+"/releases/edit?tag=v1.0.0", cookie) |
| 307 | if resp.StatusCode != http.StatusOK || !strings.Contains(page, "Channel stable targets this release") { |
| 308 | t.Fatalf("edit page = %d:\n%s", resp.StatusCode, page) |
| 309 | } |
| 310 | if strings.Contains(page, "Save notes") { |
| 311 | t.Fatalf("edit page offers a submit button it would refuse:\n%s", page) |
| 312 | } |
| 313 | form := url.Values{"csrf": {sess.CSRF}, "tag": {"v1.0.0"}, "notes": {"sneaky\n"}} |
| 314 | if resp, body := f.browserPost(t, "/"+f.repo+"/releases/edit", cookie, form); resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "Channel stable") { |
| 315 | t.Fatalf("editing a channeled release = %d:\n%s", resp.StatusCode, body) |
| 316 | } |
| 317 | if resp, body := uploadRelease(t, f, cookie, "v1.0.0", sess.CSRF, "", map[string]string{"tool": "x"}); resp.StatusCode != http.StatusConflict || !strings.Contains(body, "Channel stable") { |
| 318 | t.Fatalf("attaching to a channeled release = %d:\n%s", resp.StatusCode, body) |
| 319 | } |
| 320 | |
| 321 | // The list does not link to a form that cannot save. |
| 322 | _, list := f.browserGet(t, "/"+f.repo+"/releases", cookie) |
| 323 | if strings.Contains(list, "/releases/edit?tag=v1.0.0") { |
| 324 | t.Fatalf("the release list still offers to edit a channeled release:\n%s", list) |
| 325 | } |
| 326 | // Moving the channel re-opens the form. |
| 327 | setChannel(t, f, "stable", "v0.9.0") |
| 328 | if _, page := f.browserGet(t, "/"+f.repo+"/releases/v1.0.0", cookie); !strings.Contains(page, "Edit release") { |
| 329 | t.Fatalf("release page did not re-open after the channel moved:\n%s", page) |
| 330 | } |
| 331 | } |
| 332 |