Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Blame · server/channels_test.go

Line attribution on main · 348 line(s)

24019cb3
Keenan Nemetz
19d ago
1 package server_test
2
262e516e
Keenan Nemetz
19d ago
3 // Channels are a mutable server-side alias to an immutable release: the page
4 // redirects, the tree serves the target's files, and moving the pointer is a
de72893e
Keenan Nemetz
17d ago
5 // maintainer action with an audit line. The CLI and the releases page's form are
6 // two transports for the same writes and the same checks.
24019cb3
Keenan Nemetz
19d ago
7
8 import (
9 "io"
10 "net/http"
de72893e
Keenan Nemetz
17d ago
11 "net/url"
24019cb3
Keenan Nemetz
19d ago
12 "os"
13 "path/filepath"
14 "strings"
15 "testing"
16 "time"
262e516e
Keenan Nemetz
19d ago
17
18 "rodent.software/vole/forge/channel"
24019cb3
Keenan Nemetz
19d ago
19 )
20
262e516e
Keenan Nemetz
19d ago
21 // channelWork is a clone with an origin remote, which is what the CLI resolves
22 // before it asks the instance over SSH.
23 func channelWork(t *testing.T, f *fixture) string {
24019cb3
Keenan Nemetz
19d ago
24 t.Helper()
262e516e
Keenan Nemetz
19d ago
25 work := workRepo(t, map[string]string{"readme.md": "channels\n"})
26 if _, errs, err := runGit(t, work, "remote", "add", "origin", f.sshURL(f.repo)); err != nil {
27 t.Fatalf("remote: %v: %s", err, errs)
28 }
29 return work
30 }
31
32 func setChannelCLI(t *testing.T, f *fixture, key, work, name, tag, description string) {
33 t.Helper()
34 out, errOut, err := f.cliWithKey(key, work, "release", "channel", "set", name, tag, "-m", description)
35 if err != nil {
36 t.Fatalf("channel set: %v\n%s%s", err, out, errOut)
24019cb3
Keenan Nemetz
19d ago
37 }
38 }
39
40 func TestChannelPageAndTree(t *testing.T) {
41 t.Parallel()
42 f := releaseFixture(t)
43 data := []byte("Package: vole\n")
44 attachArtifact(t, f, "v1.0.0", "apt/Packages", data)
262e516e
Keenan Nemetz
19d ago
45 setChannelCLI(t, f, f.keyPath, channelWork(t, f), "stable", "v1.0.0", "Recommended for production.")
24019cb3
Keenan Nemetz
19d ago
46
de72893e
Keenan Nemetz
17d ago
47 code, page := f.get("/" + f.repo + "/releases")
24019cb3
Keenan Nemetz
19d ago
48 if code != 200 {
de72893e
Keenan Nemetz
17d ago
49 t.Fatalf("releases page = %d", code)
24019cb3
Keenan Nemetz
19d ago
50 }
de72893e
Keenan Nemetz
17d ago
51 for _, want := range []string{"stable", "v1.0.0", "Recommended for production.", "/channels/stable"} {
24019cb3
Keenan Nemetz
19d ago
52 if !strings.Contains(page, want) {
de72893e
Keenan Nemetz
17d ago
53 t.Fatalf("releases page does not show %q:\n%s", want, page)
24019cb3
Keenan Nemetz
19d ago
54 }
55 }
ea6ce9fe
Keenan Nemetz
17d ago
56 if strings.Contains(page, "/releases/channels/new") {
57 t.Fatalf("anonymous releases page exposes the channel form link:\n%s", page)
de72893e
Keenan Nemetz
17d ago
58 }
24019cb3
Keenan Nemetz
19d ago
59
60 if code, release := f.get("/" + f.repo + "/releases/v1.0.0"); code != 200 ||
61 !strings.Contains(release, "/channels/stable") {
62 t.Fatalf("release page = %d, badge missing:\n%s", code, release)
63 }
64
65 req, _ := http.NewRequest(http.MethodGet, f.http.URL+"/"+f.repo+"/channels/stable", nil)
66 resp, err := noRedirect.Do(req)
67 if err != nil {
68 t.Fatal(err)
69 }
70 resp.Body.Close()
71 if resp.StatusCode != http.StatusFound || resp.Header.Get("Location") != "/"+f.repo+"/releases/v1.0.0" {
72 t.Fatalf("channel redirect = %d %q", resp.StatusCode, resp.Header.Get("Location"))
73 }
74
75 resp, err = http.Get(f.http.URL + "/" + f.repo + "/channels/stable/apt/Packages")
76 if err != nil {
77 t.Fatal(err)
78 }
79 body, _ := io.ReadAll(resp.Body)
80 resp.Body.Close()
81 if resp.StatusCode != 200 || string(body) != string(data) {
82 t.Fatalf("channel file = %d %q", resp.StatusCode, body)
83 }
84 if ct := resp.Header.Get("Content-Type"); ct != "application/octet-stream" {
85 t.Fatalf("channel Content-Type = %q, want octet-stream", ct)
86 }
87 if cd := resp.Header.Get("Content-Disposition"); !strings.Contains(cd, `filename="Packages"`) {
88 t.Fatalf("channel Content-Disposition = %q", cd)
89 }
90 if cc := resp.Header.Get("Cache-Control"); !strings.Contains(cc, "must-revalidate") {
91 t.Fatalf("channel Cache-Control = %q", cc)
92 }
93 if resp.Header.Get("X-Content-Type-Options") != "nosniff" {
94 t.Fatal("channel file is served without nosniff")
95 }
96
97 if code, _ := f.get("/" + f.repo + "/channels/stable/missing"); code != 404 {
98 t.Fatalf("missing channel file = %d, want 404", code)
99 }
100 if code, _ := f.get("/" + f.repo + "/channels/missing"); code != 404 {
101 t.Fatalf("missing channel = %d, want 404", code)
102 }
103 }
104
de72893e
Keenan Nemetz
17d ago
105 func TestChannelWriteFromTheBrowser(t *testing.T) {
106 t.Parallel()
107 f := releaseFixture(t)
108 sess, cookie := f.browserSession(t, f.ownerID)
109
ea6ce9fe
Keenan Nemetz
17d ago
110 // The maintainer sees the link to the form; the list itself stays readable to
111 // everyone.
de72893e
Keenan Nemetz
17d ago
112 _, page := f.browserGet(t, "/"+f.repo+"/releases", cookie)
ea6ce9fe
Keenan Nemetz
17d ago
113 for _, want := range []string{"New channel", "/releases/channels/new"} {
de72893e
Keenan Nemetz
17d ago
114 if !strings.Contains(page, want) {
115 t.Fatalf("maintainer releases page does not show %q:\n%s", want, page)
116 }
117 }
118
ea6ce9fe
Keenan Nemetz
17d ago
119 // The form selects an annotated release; a channel cannot name a tag that is
120 // not a release, so there is no free-text target to get wrong.
121 _, page = f.browserGet(t, "/"+f.repo+"/releases/channels/new", cookie)
122 for _, want := range []string{`<select name="tag"`, "New channel", `value="v1.0.0"`, `value="v0.9.0"`} {
123 if !strings.Contains(page, want) {
124 t.Fatalf("new channel form does not show %q:\n%s", want, page)
125 }
126 }
127 if strings.Contains(page, `value="scratch-tag"`) {
128 t.Fatalf("channel form offers a lightweight tag:\n%s", page)
129 }
130
de72893e
Keenan Nemetz
17d ago
131 resp, body := f.browserPost(t, "/"+f.repo+"/channels", cookie, url.Values{
132 "csrf": {sess.CSRF}, "channel": {"stable"}, "tag": {"v1.0.0"},
133 "description": {"Recommended for production."},
134 })
135 if resp.StatusCode != http.StatusSeeOther {
136 t.Fatalf("create channel = %d, want 303:\n%s", resp.StatusCode, body)
137 }
138 if got := resp.Header.Get("Location"); got != "/"+f.repo+"/releases#channels" {
139 t.Fatalf("create channel Location = %q", got)
140 }
ea6ce9fe
Keenan Nemetz
17d ago
141 _, page = f.browserGet(t, "/"+f.repo+"/releases", cookie)
142 for _, want := range []string{"Recommended for production.", "v1.0.0", "/channels/stable", "/releases/channels/edit?channel=stable"} {
de72893e
Keenan Nemetz
17d ago
143 if !strings.Contains(page, want) {
144 t.Fatalf("releases page after create does not show %q:\n%s", want, page)
145 }
146 }
147
ea6ce9fe
Keenan Nemetz
17d ago
148 // The edit page selects the current target and keeps the description.
149 _, page = f.browserGet(t, "/"+f.repo+"/releases/channels/edit?channel=stable", cookie)
150 for _, want := range []string{"Edit channel stable", `value="v1.0.0" selected`, "Recommended for production."} {
de72893e
Keenan Nemetz
17d ago
151 if !strings.Contains(page, want) {
152 t.Fatalf("edit channel form does not show %q:\n%s", want, page)
153 }
154 }
155
156 // A second set moves the same name; it does not create a second channel.
157 resp, body = f.browserPost(t, "/"+f.repo+"/channels", cookie, url.Values{
158 "csrf": {sess.CSRF}, "channel": {"stable"}, "tag": {"v0.9.0"},
159 "description": {"Older but stable."}, "editing": {"1"},
160 })
161 if resp.StatusCode != http.StatusSeeOther {
162 t.Fatalf("move channel = %d, want 303:\n%s", resp.StatusCode, body)
163 }
164 _, page = f.get("/" + f.repo + "/releases")
165 if !strings.Contains(page, "Older but stable.") || strings.Contains(page, "Recommended for production.") {
166 t.Fatalf("releases page after move did not replace the description:\n%s", page)
167 }
168
169 // A lightweight tag is refused by the form as it is by the CLI.
170 resp, body = f.browserPost(t, "/"+f.repo+"/channels", cookie, url.Values{
171 "csrf": {sess.CSRF}, "channel": {"beta"}, "tag": {"scratch-tag"},
172 })
173 if resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "lightweight") {
174 t.Fatalf("lightweight channel target = %d, want 400:\n%s", resp.StatusCode, body)
175 }
176
177 resp, body = f.browserPost(t, "/"+f.repo+"/channels/stable/clear", cookie, url.Values{"csrf": {sess.CSRF}})
178 if resp.StatusCode != http.StatusSeeOther {
179 t.Fatalf("clear channel = %d, want 303:\n%s", resp.StatusCode, body)
180 }
181 _, page = f.get("/" + f.repo + "/releases")
182 if strings.Contains(page, "Older but stable.") || strings.Contains(page, "/channels/stable") {
183 t.Fatalf("releases page still shows the cleared channel:\n%s", page)
184 }
185
186 audit, err := os.ReadFile(f.cfg.AuditLog())
187 if err != nil {
188 t.Fatal(err)
189 }
190 for _, want := range []string{"web.channel.set", "web.channel.clear", "stable -> v1.0.0", "stable -> v0.9.0"} {
191 if !strings.Contains(string(audit), want) {
192 t.Fatalf("audit log does not contain %q:\n%s", want, audit)
193 }
194 }
195 }
196
24019cb3
Keenan Nemetz
19d ago
197 func TestChannelDanglingTarget(t *testing.T) {
198 t.Parallel()
199 f := releaseFixture(t)
262e516e
Keenan Nemetz
19d ago
200 m := mustRepo(t, f)
201 if err := f.srv.Channels.Set(m.ID, channel.Channel{
202 Name: "stable",
203 Release: "v9.9.9",
204 ReleaseObject: strings.Repeat("a", 40),
205 UpdatedBy: "keenan",
206 UpdatedAt: time.Now().UTC(),
207 }); err != nil {
24019cb3
Keenan Nemetz
19d ago
208 t.Fatal(err)
209 }
210
de72893e
Keenan Nemetz
17d ago
211 code, page := f.get("/" + f.repo + "/releases")
24019cb3
Keenan Nemetz
19d ago
212 if code != 200 || !strings.Contains(page, "dangling") || !strings.Contains(page, "v9.9.9") {
de72893e
Keenan Nemetz
17d ago
213 t.Fatalf("releases page = %d, want a dangling marker:\n%s", code, page)
24019cb3
Keenan Nemetz
19d ago
214 }
ea6ce9fe
Keenan Nemetz
17d ago
215 _, cookie := f.browserSession(t, f.ownerID)
216 if code, form := f.browserGet(t, "/"+f.repo+"/releases/channels/edit?channel=stable", cookie); code.StatusCode != 200 ||
217 !strings.Contains(form, "no longer a valid annotated release") || !strings.Contains(form, "v9.9.9") {
218 t.Fatalf("dangling channel form = %d, want the missing target:\n%s", code.StatusCode, form)
219 }
24019cb3
Keenan Nemetz
19d ago
220 if code, _ := f.get("/" + f.repo + "/channels/stable"); code != 404 {
221 t.Fatalf("dangling channel = %d, want 404", code)
222 }
223 if code, _ := f.get("/" + f.repo + "/channels/stable/apt/Packages"); code != 404 {
224 t.Fatalf("dangling channel file = %d, want 404", code)
225 }
226 }
227
228 func TestChannelMoveRequiresMaintain(t *testing.T) {
229 t.Parallel()
230 f := releaseFixture(t)
262e516e
Keenan Nemetz
19d ago
231 work := channelWork(t, f)
24019cb3
Keenan Nemetz
19d ago
232
233 pub, priv := keypair(t, filepath.Join(f.cfg.DataDir, "id_contrib"))
234 contrib, err := f.srv.Identity.Create("carol", pub, "test")
235 if err != nil {
236 t.Fatal(err)
237 }
238 m := mustRepo(t, f)
239 a, err := m.ACL()
240 if err != nil {
241 t.Fatal(err)
242 }
243 if err := a.SetEntry(contrib.Name, "contribute"); err != nil {
244 t.Fatal(err)
245 }
246 if err := m.SetACL(a, "test"); err != nil {
247 t.Fatal(err)
248 }
249
262e516e
Keenan Nemetz
19d ago
250 _, errOut, err := f.cliWithKey(priv, work, "release", "channel", "set", "stable", "v1.0.0", "-m", "move")
251 if err == nil || !strings.Contains(errOut, "maintain") {
252 t.Fatalf("contributor channel move = %v: %s, want a maintain refusal", err, errOut)
24019cb3
Keenan Nemetz
19d ago
253 }
254
de72893e
Keenan Nemetz
17d ago
255 contribSess, contribCookie := f.browserSession(t, contrib.Name)
256 if resp, _ := f.browserPost(t, "/"+f.repo+"/channels", contribCookie, url.Values{
257 "csrf": {contribSess.CSRF}, "channel": {"beta"}, "tag": {"v1.0.0"},
258 }); resp.StatusCode != http.StatusNotFound {
259 t.Fatalf("contributor channel write = %d, want 404", resp.StatusCode)
260 }
ea6ce9fe
Keenan Nemetz
17d ago
261 if resp, _ := f.browserGet(t, "/"+f.repo+"/releases/channels/new", contribCookie); resp.StatusCode != http.StatusNotFound {
262 t.Fatalf("contributor channel form = %d, want 404", resp.StatusCode)
263 }
de72893e
Keenan Nemetz
17d ago
264
262e516e
Keenan Nemetz
19d ago
265 setChannelCLI(t, f, f.addWriter(t), work, "stable", "v1.0.0", "moved")
24019cb3
Keenan Nemetz
19d ago
266 audit, err := os.ReadFile(f.cfg.AuditLog())
267 if err != nil {
268 t.Fatal(err)
269 }
270 if !strings.Contains(string(audit), "channel.set") || !strings.Contains(string(audit), "stable -> v1.0.0") {
271 t.Fatalf("audit log has no channel move:\n%s", audit)
272 }
de72893e
Keenan Nemetz
17d ago
273 if code, page := f.get("/" + f.repo + "/releases"); code != 200 || !strings.Contains(page, "v1.0.0") {
274 t.Fatalf("releases page after move = %d:\n%s", code, page)
24019cb3
Keenan Nemetz
19d ago
275 }
276 }
277
278 func TestChannelFileIsNotPublicOnAPrivateRepo(t *testing.T) {
279 t.Parallel()
280 f := newFixture(t)
281 work := workRepo(t, map[string]string{"readme.md": "secret\n"})
282 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
283 t.Fatalf("push: %s", errs)
284 }
285 if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "notes\n"); err != nil {
262e516e
Keenan Nemetz
19d ago
286 t.Fatalf("tag: %v: %s", err, errs)
24019cb3
Keenan Nemetz
19d ago
287 }
288 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil {
289 t.Fatalf("push tag: %s", errs)
290 }
291 attachArtifact(t, f, "v1.0.0", "apt/Packages", []byte("Package: vole\n"))
262e516e
Keenan Nemetz
19d ago
292 m := mustRepo(t, f)
293 ttag, err := m.Git().Tag("v1.0.0")
294 if err != nil {
295 t.Fatal(err)
296 }
297 if err := f.srv.Channels.Set(m.ID, channel.Channel{
298 Name: "stable",
299 Release: "v1.0.0",
300 ReleaseObject: ttag.OID,
301 UpdatedBy: "keenan",
302 UpdatedAt: time.Now().UTC(),
303 }); err != nil {
24019cb3
Keenan Nemetz
19d ago
304 t.Fatal(err)
305 }
306 if code, _ := f.get("/" + f.repo + "/channels/stable/apt/Packages"); code != 404 {
307 t.Fatalf("private channel file = %d, want 404", code)
308 }
309 _, cookie := f.browserSession(t, f.ownerID)
310 resp, body := f.browserGet(t, "/"+f.repo+"/channels/stable/apt/Packages", cookie)
311 if resp.StatusCode != 200 || body != "Package: vole\n" {
312 t.Fatalf("owner channel file = %d %q", resp.StatusCode, body)
313 }
314 if cc := resp.Header.Get("Cache-Control"); strings.Contains(cc, "public") {
315 t.Fatalf("private channel Cache-Control = %q", cc)
316 }
317 }
318
319 func TestChannelCLI(t *testing.T) {
320 t.Parallel()
321 f := releaseFixture(t)
262e516e
Keenan Nemetz
19d ago
322 work := channelWork(t, f)
24019cb3
Keenan Nemetz
19d ago
323
324 out, errOut, err := f.cli(work, "release", "channel", "set", "stable", "v1.0.0", "-m", "CLI channel")
325 if err != nil {
326 t.Fatalf("channel set: %v\n%s%s", err, out, errOut)
327 }
328 if !strings.Contains(out, "stable -> v1.0.0") {
329 t.Fatalf("channel set output = %q", out)
330 }
de72893e
Keenan Nemetz
17d ago
331 if code, page := f.get("/" + f.repo + "/releases"); code != 200 || !strings.Contains(page, "CLI channel") {
332 t.Fatalf("releases page after CLI set = %d:\n%s", code, page)
24019cb3
Keenan Nemetz
19d ago
333 }
334
335 out, errOut, err = f.cli(work, "release", "channel", "list")
336 if err != nil || !strings.Contains(out, "stable") || !strings.Contains(out, "v1.0.0") {
337 t.Fatalf("channel list = %v %q %q", err, out, errOut)
338 }
339
340 out, errOut, err = f.cli(work, "release", "channel", "clear", "stable")
341 if err != nil {
342 t.Fatalf("channel clear: %v\n%s%s", err, out, errOut)
343 }
344 out, _, err = f.cli(work, "release", "channel", "list")
345 if err != nil || !strings.Contains(out, "no channels") {
346 t.Fatalf("channel list after clear = %v %q", err, out)
347 }
348 }