~bigbes/sr-ht-compare

ref: 3e79c6dea6e380e00d7a7ad17ea8674dd62c8ca2 sr-ht-compare/web/web_test.go -rw-r--r-- 15.5 KiB
3e79c6de — bigbes web: draw the chrome from sr-ht-ecore 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package web

import (
	"context"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"errors"
	"net/http"
	"net/http/httptest"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"github.com/fernet/fernet-go"
	"github.com/go-chi/chi/v5"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vaughan0/go-ini"
	"sourcecraft.dev/bigbes/sr-ht-core/config"
	"sourcecraft.dev/bigbes/sr-ht-core/crypto"

	"sourcecraft.dev/bigbes/sr-ht-compare/authz"
	"sourcecraft.dev/bigbes/sr-ht-compare/core"
	"sourcecraft.dev/bigbes/sr-ht-compare/gitx"
)

// testConf carries the crypto keys established in TestMain so tests can seal
// unified-login cookies.
var testConf ini.File

func TestMain(m *testing.M) {
	var fk fernet.Key
	if err := fk.Generate(); err != nil {
		panic("generate fernet key: " + err.Error())
	}
	seed := make([]byte, 32)
	if _, err := rand.Read(seed); err != nil {
		panic("generate webhook seed: " + err.Error())
	}
	testConf = ini.File{
		"sr.ht":    ini.Section{"network-key": fk.Encode()},
		"webhooks": ini.Section{"private-key": base64.StdEncoding.EncodeToString(seed)},
	}
	crypto.InitCrypto(testConf)
	os.Exit(m.Run())
}

// ---- fixtures -------------------------------------------------------------

// stubAuthorizer is a fixed-map Authorizer with optional error injection.
type stubAuthorizer struct {
	repos map[string]authz.RepoInfo // key "owner/name"
	my    []authz.RepoInfo
	err   error // when set, every call fails with this (transport-style) error
}

func (s *stubAuthorizer) Repo(_ context.Context, _, owner, name string) (*authz.RepoInfo, error) {
	if s.err != nil {
		return nil, s.err
	}
	owner = strings.TrimPrefix(owner, "~")
	if info, ok := s.repos[owner+"/"+name]; ok {
		return &info, nil
	}
	return nil, core.ErrNotFound
}

func (s *stubAuthorizer) MyRepos(_ context.Context, _ string) ([]authz.RepoInfo, error) {
	if s.err != nil {
		return nil, s.err
	}
	return s.my, nil
}

// gitFixture drives the git CLI to build a bare repo at <root>/~alice/demo:
//
//	c1 (main): add a.txt
//	c2 (main): add b.txt, edit a.txt          <- main HEAD
//	feature off c1: add feature.txt           <- branch "feature"
//
// It returns the repos root and the full SHA of main's HEAD.
func gitFixture(t *testing.T) (root, mainSHA string) {
	t.Helper()
	if _, err := exec.LookPath("git"); err != nil {
		t.Skipf("git not available: %v", err)
	}
	root = t.TempDir()
	work := t.TempDir()

	git := func(date string, args ...string) string {
		cmd := exec.Command("git", args...)
		cmd.Dir = work
		cmd.Env = append(os.Environ(),
			"GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null",
			"GIT_TERMINAL_PROMPT=0", "LC_ALL=C",
			"GIT_AUTHOR_NAME=Alice", "GIT_AUTHOR_EMAIL=alice@example.com",
			"GIT_COMMITTER_NAME=Alice", "GIT_COMMITTER_EMAIL=alice@example.com",
			"GIT_AUTHOR_DATE="+date, "GIT_COMMITTER_DATE="+date,
		)
		out, err := cmd.CombinedOutput()
		require.NoErrorf(t, err, "git %s:\n%s", strings.Join(args, " "), out)
		return string(out)
	}
	write := func(name, data string) {
		require.NoError(t, os.WriteFile(filepath.Join(work, name), []byte(data), 0o644))
	}

	d1, d2, d3 := "2024-01-01T00:00:00Z", "2024-01-02T00:00:00Z", "2024-01-03T00:00:00Z"
	git(d1, "init", "-b", "main")
	write("a.txt", "hello\nworld\n")
	git(d1, "add", "a.txt")
	git(d1, "commit", "-m", "add a.txt")
	git(d2, "branch", "feature")
	write("a.txt", "hello\nworld\nmore\n")
	write("b.txt", "bee\n")
	git(d2, "add", "a.txt", "b.txt")
	git(d2, "commit", "-m", "add b, edit a")
	git(d3, "checkout", "feature")
	write("feature.txt", "feature\n")
	git(d3, "add", "feature.txt")
	git(d3, "commit", "-m", "add feature.txt")
	git(d3, "checkout", "main")

	require.NoError(t, os.MkdirAll(filepath.Join(root, "~alice"), 0o755))
	bare := filepath.Join(root, "~alice", "demo")
	git(d3, "clone", "--bare", work, bare)

	mainSHA = strings.TrimSpace(runGit(t, bare, "rev-parse", "main"))
	return root, mainSHA
}

func runGit(t *testing.T, dir string, args ...string) string {
	t.Helper()
	cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
	out, err := cmd.CombinedOutput()
	require.NoErrorf(t, err, "git %s:\n%s", strings.Join(args, " "), out)
	return string(out)
}

// testServer wires a Server (fixture repo + given authorizer) behind the same
// middleware the cmd layer installs, and returns the handler.
func testServer(t *testing.T, root string, az authz.Authorizer) http.Handler {
	t.Helper()
	conf := ini.File{
		"sr.ht": ini.Section{
			"network-key": testConf.Section("sr.ht")["network-key"],
			"site-name":   "sourcehut",
			"environment": "development",
		},
		"webhooks":      ini.Section{"private-key": testConf.Section("webhooks")["private-key"]},
		"compare.sr.ht": ini.Section{"origin": "https://compare.example"},
		"meta.sr.ht":    ini.Section{"origin": "https://meta.example"},
		"git.sr.ht":     ini.Section{"origin": "https://git.example", "repos": root},
		"todo.sr.ht":    ini.Section{"origin": "https://todo.example"},
		"hub.sr.ht":     ini.Section{"origin": "https://hub.example"},
	}
	srv, err := New(conf, az)
	require.NoError(t, err, "New")

	r := chi.NewRouter()
	r.Use(config.Middleware(conf, "compare.sr.ht"))
	r.Use(authz.Middleware())
	srv.Register(r)
	return r
}

// login seals a unified-login cookie for the given user onto a request.
func login(req *http.Request, user string) {
	payload, _ := json.Marshal(map[string]string{"name": user})
	req.AddCookie(&http.Cookie{Name: authz.CookieName, Value: string(crypto.Encrypt(payload))})
}

func demoAuthorizer() *stubAuthorizer {
	return &stubAuthorizer{
		repos: map[string]authz.RepoInfo{
			"alice/demo": {ID: 1, Name: "demo", Description: "the demo repo", Visibility: "PUBLIC"},
		},
		my: []authz.RepoInfo{
			{ID: 1, Name: "demo", Description: "the demo repo", Visibility: "PUBLIC"},
			{ID: 2, Name: "secret", Description: "", Visibility: "PRIVATE"},
		},
	}
}

func get(t *testing.T, h http.Handler, target string, user string) *httptest.ResponseRecorder {
	t.Helper()
	req := httptest.NewRequest(http.MethodGet, target, nil)
	if user != "" {
		login(req, user)
	}
	rec := httptest.NewRecorder()
	h.ServeHTTP(rec, req)
	return rec
}

// ---- tests ----------------------------------------------------------------

func TestComparePage(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/~alice/demo/compare/main...feature", "")
	require.Equalf(t, http.StatusOK, rec.Code, "body:\n%s", rec.Body.String())

	body := rec.Body.String()
	assert.Contains(t, body, `id="compare-data"`, "missing compare-data script")
	assert.Contains(t, body, `data-diff-wrap`, "missing long-line wrapping control")
	assert.Contains(t, body, `src="/static/`+bundleName(t)+`"`, "missing hashed bundle script tag")

	cd := extractCompareData(t, body)
	assert.Equal(t, "compare", cd.Mode)
	assert.Equal(t, jsonSpec{Base: "main", Head: "feature", ThreeDot: true}, cd.Spec)

	// feature adds feature.txt relative to the merge base (c1), and paths carry
	// no a/ or b/ diff prefix.
	found := false
	for _, f := range cd.Files {
		if f.Path == "feature.txt" {
			found = true
		}
		assert.NotRegexp(t, `^[ab]/`, f.Path, "file path has a diff prefix")
	}
	assert.Truef(t, found, "feature.txt not in files: %+v", cd.Files)
}

func TestTwoDotVsThreeDot(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	two := extractCompareData(t, get(t, h, "/~alice/demo/compare/main..feature", "").Body.String())
	assert.False(t, two.Spec.ThreeDot, "main..feature parsed as three-dot")

	three := extractCompareData(t, get(t, h, "/~alice/demo/compare/main...feature", "").Body.String())
	assert.True(t, three.Spec.ThreeDot, "main...feature parsed as two-dot")
}

func TestComparePatchRoute(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/~alice/demo/compare/main...feature.patch", "")
	require.Equal(t, http.StatusOK, rec.Code)
	assert.True(t, strings.HasPrefix(rec.Header().Get("Content-Type"), "text/plain"),
		"content-type = %q, want text/plain", rec.Header().Get("Content-Type"))
	assert.Contains(t, rec.Body.String(), "diff --git", "patch body missing diff header")
}

func TestCommitPage(t *testing.T) {
	root, mainSHA := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/~alice/demo/commit/"+mainSHA, "")
	require.Equalf(t, http.StatusOK, rec.Code, "body:\n%s", rec.Body.String())
	assert.Contains(t, rec.Body.String(), `data-diff-wrap`, "missing long-line wrapping control")

	cd := extractCompareData(t, rec.Body.String())
	assert.Equal(t, "commit", cd.Mode)
	// c2 modifies a.txt and adds b.txt.
	assert.NotEmpty(t, cd.Files, "commit page has no files")
}

func TestCommitPatchRoute(t *testing.T) {
	root, mainSHA := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/~alice/demo/commit/"+mainSHA+".patch", "")
	require.Equal(t, http.StatusOK, rec.Code)
	assert.Contains(t, rec.Body.String(), "diff --git", "commit patch missing diff header")
}

func TestUnknownRepoIs404(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/~alice/nope/compare/main...feature", "")
	assert.Equal(t, http.StatusNotFound, rec.Code)
}

func TestPrivateRepoInvisibleIs404(t *testing.T) {
	// Authorizer reports the repo as not-found (visibility hidden) even though
	// the bare repo exists on disk.
	root, _ := gitFixture(t)
	h := testServer(t, root, &stubAuthorizer{repos: map[string]authz.RepoInfo{}})

	rec := get(t, h, "/~alice/demo", "")
	assert.Equal(t, http.StatusNotFound, rec.Code)
}

func TestAuthorizerTransportErrorIs500(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, &stubAuthorizer{err: errors.New("graphql unreachable")})

	rec := get(t, h, "/~alice/demo", "")
	assert.Equal(t, http.StatusInternalServerError, rec.Code,
		"a transport error must not be reported as 404")
}

func TestBadRefIs400(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/~alice/demo/compare/..bad", "")
	assert.Equal(t, http.StatusBadRequest, rec.Code)
}

func TestIndexAnonymous(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/", "")
	require.Equal(t, http.StatusOK, rec.Code)

	body := rec.Body.String()
	assert.Contains(t, body, `action="/jump"`, "anonymous index missing jump form")
	assert.Contains(t, body, "return_to=", "login URL missing return_to")
}

func TestIndexLoggedIn(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/", "bigbes")
	require.Equal(t, http.StatusOK, rec.Code)

	body := rec.Body.String()
	assert.Contains(t, body, "/~bigbes/demo", "logged-in index missing repo link from MyRepos")
	assert.Contains(t, body, `<small class="pull-right">private</small>`,
		"logged-in index missing the visibility label on the event card")
}

// TestChromeIsRendered checks that the layout really draws the shared partials
// of sr-ht-ecore — the brand's red service label and the login block. What the
// switcher contains and how it is ordered is ecore's business and ecore's test;
// this one only asserts that this service's layout invokes the chrome at all,
// which is the wiring a bad merge here would break.
func TestChromeIsRendered(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	anon := get(t, h, "/", "").Body.String()
	assert.Contains(t, anon, `<span class="text-danger">compare</span>`,
		"the brand's service label is missing")
	assert.Contains(t, anon, "Log in", "an anonymous viewer must be offered the login")
	// The environment is "development" in the test config, so the banner shows.
	assert.Contains(t, anon, "DEVELOPMENT ENVIRONMENT", "missing the non-production banner")

	viewer := get(t, h, "/", "bigbes").Body.String()
	assert.Contains(t, viewer, "Logged in as", "the login block does not name the viewer")
	assert.Contains(t, viewer, "https://hub.example/~bigbes",
		"the profile link should prefer hub's ~username page")
	assert.Contains(t, viewer, "https://todo.example", "the switcher is missing a sibling service")
}

// TestDiffPagesAreFullBleed pins the one chrome decision compare makes for
// itself: the diff views ask for the full window, because a side-by-side diff in
// the centered container is a column of code half the page wide.
func TestDiffPagesAreFullBleed(t *testing.T) {
	root, mainSHA := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	for _, tc := range []struct {
		name, target, want string
	}{
		{"index", "/", `<div class="container">`},
		{"repo", "/~alice/demo", `<div class="container">`},
		{"compare", "/~alice/demo/compare/main...feature", `<div class="container-fluid">`},
		{"commit", "/~alice/demo/commit/" + mainSHA, `<div class="container-fluid">`},
	} {
		t.Run(tc.name, func(t *testing.T) {
			rec := get(t, h, tc.target, "")
			require.Equal(t, http.StatusOK, rec.Code)
			assert.Contains(t, rec.Body.String(), tc.want)
		})
	}
}

func TestHealthz(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	rec := get(t, h, "/healthz", "")
	require.Equal(t, http.StatusOK, rec.Code)
	assert.Contains(t, rec.Body.String(), "ok")
}

func TestStaticBundleAndCSS(t *testing.T) {
	root, _ := gitFixture(t)
	h := testServer(t, root, demoAuthorizer())

	bundle := bundleName(t)
	rec := get(t, h, "/static/"+bundle, "")
	require.Equalf(t, http.StatusOK, rec.Code, "%s", bundle)
	assert.Contains(t, rec.Header().Get("Content-Type"), "javascript")
	assert.Contains(t, rec.Header().Get("Cache-Control"), "immutable",
		"a hashed bundle must be cacheable forever")

	css := cssName(t)
	rec = get(t, h, "/static/"+css, "")
	require.Equalf(t, http.StatusOK, rec.Code, "%s", css)
	assert.Contains(t, rec.Header().Get("Cache-Control"), "immutable",
		"a hashed stylesheet must be cacheable forever")
}

// TestCompareJSONNoScriptBreakout verifies a file path containing "</script>"
// cannot break out of the embedded <script> element.
func TestCompareJSONNoScriptBreakout(t *testing.T) {
	patch := &gitx.Patch{Text: "diff --git a/x b/x\n"}
	files := []gitx.FileChange{{Path: "evil</script><script>alert(1)</script>.txt", Status: "A", Additions: 1}}

	html, err := buildCompareJSON("compare", patch, files, jsonSpec{Base: "a", Head: "b"})
	require.NoError(t, err)

	s := string(html)
	// The '<' of "</script>" must be escaped, so no literal "</script" tag can
	// appear to close the embedding element...
	assert.NotContainsf(t, s, "</script", "raw </script in the JSON (breakout possible): %s", s)
	// ...and it must appear in its escaped form instead, proving the marshaler
	// HTML-escaped the '<'.
	assert.Containsf(t, s, "\\u003c/script", "expected an escaped \\u003c/script, got: %s", s)
}

func cssName(t *testing.T) string {
	t.Helper()
	href, err := resolveCSSHref()
	require.NoError(t, err)
	return strings.TrimPrefix(href, "/static/")
}

// bundleName resolves the content-hashed frontend bundle filename (bundle.<hash>.js).
func bundleName(t *testing.T) string {
	t.Helper()
	href, err := resolveBundleHref()
	require.NoError(t, err)
	return strings.TrimPrefix(href, "/static/")
}

// extractCompareData pulls and decodes the embedded JSON payload from a page.
func extractCompareData(t *testing.T, body string) compareData {
	t.Helper()
	const open = `id="compare-data" type="application/json">`
	i := strings.Index(body, open)
	require.GreaterOrEqualf(t, i, 0, "no compare-data script in body:\n%s", body)

	rest := body[i+len(open):]
	j := strings.Index(rest, "</script>")
	require.GreaterOrEqual(t, j, 0, "compare-data script not closed")

	var cd compareData
	require.NoErrorf(t, json.Unmarshal([]byte(rest[:j]), &cd), "raw: %s", rest[:j])
	return cd
}