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) }) t.Run("save after a push to another file lands on top of it", func(t *testing.T) { base := admin.get("/edit-repo/edit/main/index.js").Value("input[name=base]") if base != e.headCommit("edit-repo") { t.Fatalf("base = %q", base) } e.seedRepo("edit-repo", map[string]string{"other.txt": "pushed\n"}, "Push other") admin.post("/edit-repo/edit/main/index.js", url.Values{ "content": {"on top\n"}, "new_path": {"index.js"}, "message": {"On top"}, "base": {base}, }).mustRedirect("/edit-repo/commit/") if got := gitRun(t, e.repoPath("edit-repo"), "show", "main:other.txt"); got != "pushed" { t.Errorf("other.txt = %q", got) } }) t.Run("save after a push to the same file keeps the input and asks again", func(t *testing.T) { base := admin.get("/edit-repo/edit/main/index.js").Value("input[name=base]") tip := e.seedRepo("edit-repo", map[string]string{"index.js": "pushed\n"}, "Push index") form := url.Values{"content": {"mine\n"}, "new_path": {"index.js"}, "message": {"Mine"}, "base": {base}} r := admin.post("/edit-repo/edit/main/index.js", form).mustStatus(409) if got := r.Value("textarea[name=content]"); got != "mine\n" { t.Errorf("content = %q", got) } if got := r.Value("input[name=base]"); got != tip { t.Errorf("base = %q, want %q", got, tip) } if !r.Has(`.form-error a[href="/edit-repo/blob/` + tip + `/index.js"]`) { t.Error("link to the current version missing") } if e.headCommit("edit-repo") != tip { t.Fatal("stale save moved the branch") } form.Set("base", tip) admin.post("/edit-repo/edit/main/index.js", form).mustRedirect("/edit-repo/commit/") if got := gitRun(t, e.repoPath("edit-repo"), "show", "main:index.js"); got != "mine" { t.Errorf("index.js = %q", got) } }) t.Run("create over an existing file is rejected with the input kept", func(t *testing.T) { r := admin.post("/edit-repo/new-file/main", url.Values{ "path": {"README.md"}, "content": {"new body"}, "message": {"Clobber"}, }).mustStatus(409) if got := r.Value("textarea[name=content]"); got != "new body" { t.Errorf("content = %q", got) } if got := r.Value("input[name=path]"); got != "README.md" { t.Errorf("path = %q", got) } }) t.Run("delete after a push to the same file shows the current version", func(t *testing.T) { base := admin.get("/edit-repo/blob/main/README.md").Value(`form[action*="/delete-file/"] input[name=base]`) if base != e.headCommit("edit-repo") { t.Fatalf("base = %q", base) } tip := e.seedRepo("edit-repo", map[string]string{"README.md": "pushed\n"}, "Push readme") r := admin.post("/edit-repo/delete-file/main/README.md", url.Values{"base": {base}}).mustStatus(409) if !r.Has(".form-error") || !r.Contains("pushed") { t.Error("error or current content missing") } if got := r.Value(`form[action*="/delete-file/"] input[name=base]`); got != tip { t.Errorf("base = %q, want %q", got, tip) } if e.headCommit("edit-repo") != tip { t.Fatal("stale delete moved the branch") } admin.post("/edit-repo/delete-file/main/README.md", url.Values{"base": {tip}}).mustRedirect("/edit-repo/commit/") }) t.Run("delete of a missing file makes no commit", func(t *testing.T) { tip := e.headCommit("edit-repo") admin.post("/edit-repo/delete-file/main/nope.txt", url.Values{}).mustStatus(404) if e.headCommit("edit-repo") != tip { t.Error("branch moved") } }) }