package e2e import ( "net/url" "strings" "testing" "github.com/PuerkitoBio/goquery" ) // fileeditCommitItem returns the commit log row with the given subject, or nil. func fileeditCommitItem(r *response, subject string) *goquery.Selection { var found *goquery.Selection r.Find(".commit-item").EachWithBreak(func(_ int, s *goquery.Selection) bool { if strings.TrimSpace(s.Find(".commit-subject").First().Text()) == subject { found = s return false } return true }) return found } func TestFileEditing(t *testing.T) { e := newEnv(t) admin := e.admin() // A dedicated repo so edits do not interfere with other tests. e.createRepo(admin, "edit-repo") e.seedRepo("edit-repo", nil) t.Run("Edit button appears on text file blob when viewing a branch as admin", func(t *testing.T) { r := admin.get("/edit-repo/blob/main/index.js").mustStatus(200) sel := `a[href*="/edit/main/index.js"]` if r.Count(sel) == 0 { t.Fatal("Edit link missing") } if got := r.Text(sel); got != "Edit" { t.Errorf("link text = %q", got) } }) t.Run("Edit button does not appear when viewing a commit SHA", func(t *testing.T) { sha := e.headCommit("edit-repo") if n := admin.get("/edit-repo/blob/" + sha + "/index.js").Count(`a[href*="/edit/"]`); n != 0 { t.Errorf("Edit links = %d", n) } }) t.Run("Edit button does not appear for unauthenticated visitors", func(t *testing.T) { if n := e.anon().get("/edit-repo/blob/main/index.js").Count(`a[href*="/edit/main/"]`); n != 0 { t.Errorf("Edit links = %d", n) } }) t.Run("edit page loads with file content pre-filled", func(t *testing.T) { r := admin.get("/edit-repo/edit/main/index.js").mustStatus(200) if got := r.Text(".file-blob-name"); got != "index.js" { t.Errorf("file name = %q", got) } if got := r.Value("textarea[name=content]"); !strings.Contains(got, "hello") { t.Errorf("content = %q", got) } if got := r.Value("textarea[name=message]"); got != "Edited index.js" { t.Errorf("message = %q", got) } }) t.Run("edit page shows which branch will be committed to", func(t *testing.T) { if !admin.get("/edit-repo/edit/main/index.js").Contains("main") { t.Error("branch name missing") } }) t.Run("edit page returns 404 for non-branch ref", func(t *testing.T) { sha := e.headCommit("edit-repo") admin.get("/edit-repo/edit/" + sha + "/index.js").mustStatus(404) }) t.Run("submitting edit creates a new commit and redirects to blob view", func(t *testing.T) { r := admin.post("/edit-repo/edit/main/index.js", url.Values{ "content": {"console.log(\"edited\");\n"}, "new_path": {"index.js"}, "message": {"Update index.js via web editor"}, }) r.mustRedirect("/edit-repo/commit/") if !admin.follow(r).Contains("Update index.js via web editor") { t.Error("commit view does not show the message") } }) t.Run("edit commit has a gpgsig header (is signed)", func(t *testing.T) { dir := e.repoPath("edit-repo") hash := gitRun(t, dir, "log", "--format=%H", "--grep=Update index.js via web editor", "-1") if hash == "" { t.Fatal("edit commit not found") } if obj := gitRun(t, dir, "cat-file", "-p", hash); !strings.Contains(obj, "gpgsig") { t.Errorf("commit object has no gpgsig:\n%s", obj) } }) t.Run("edit commit shows verified badge in commit log", func(t *testing.T) { r := admin.get("/edit-repo/commits/main").mustStatus(200) item := fileeditCommitItem(r, "Update index.js via web editor") if item == nil { t.Fatal("commit row missing") } if item.Find(".sig-badge.verified").Length() == 0 { t.Error("verified badge missing") } }) t.Run("GET edit page returns 404 for non-branch ref", func(t *testing.T) { sha := e.headCommit("edit-repo") admin.get("/edit-repo/edit/" + sha + "/index.js").mustStatus(404) }) }