// SPDX-License-Identifier: AGPL-3.0-or-later package app import ( "html" "net/http" "net/url" "regexp" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "bnema.dev/ermite/internal/domain" "bnema.dev/ermite/internal/ports" ) // mrRepo is a public repository "proj" with README.md on main and a branch // feat/search that is one commit ahead of it (search.go, three lines). type mrRepo struct { *cliHarness wt, url string } func newMRRepo(t *testing.T, vis domain.Visibility) *mrRepo { t.Helper() c := newCLIHarness(t) _, err := c.forge.Repos.Create(c.ctx, domain.Root(), createParams(c.harness, "proj", vis)) require.NoError(t, err) r := &mrRepo{cliHarness: c, wt: c.workTree("main", "README.md"), url: c.urlAuth(adminToken, "/proj.git")} c.mustGit(r.wt, "push", "-q", r.url, "main") c.mustGit(r.wt, "checkout", "-q", "-b", "feat/search") c.writeFile(r.wt, "search.go", []byte("package search\n\nfunc Find() {}\n")) c.mustGit(r.wt, "push", "-q", r.url, "feat/search") return r } func (r *mrRepo) headOf(branch string) string { return r.bareGit("proj", "rev-parse", branch) } // form posts url-encoded values as the signed-in owner, from this site. func (b browser) form(path, token string, values url.Values) page { b.h.t.Helper() return b.do("POST", path, post(token), values.Encode()) } var reHeadField = regexp.MustCompile(`name="head_id" value="([0-9a-f]{40})"`) // formHead reads the head the merge form of a page carries. func formHead(t *testing.T, body string) string { t.Helper() m := reHeadField.FindStringSubmatch(body) require.Len(t, m, 2, "the page has a merge form") return m[1] } // AC7: the pages as an anonymous visitor sees them on a public repository: // read-only, no forms, public cache headers; the same pages signed in carry // the forms and the private headers. func TestWebMergeRequestPagesRender(t *testing.T) { r := newMRRepo(t, domain.VisibilityPublic) b := browser{r.harness} token := r.signIn(t) // The compare page: anonymous reads, the owner gets the form. anon := b.get("/proj/compare/feat/search", nil) require.Equal(t, 200, anon.status, anon.body) assert.Equal(t, "no-cache", anon.hdr.Get("Cache-Control")) assert.Equal(t, []string{"Accept-Encoding"}, anon.hdr.Values("Vary")) assert.Contains(t, anon.body, "feat/search") assert.Contains(t, anon.body, "package search", "the diff of the single commit") assert.NotContains(t, anon.body, "