fileediting_test.go
| 1 | package e2e |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/PuerkitoBio/goquery" |
| 9 | ) |
| 10 | |
| 11 | // fileeditCommitItem returns the commit log row with the given subject, or nil. |
| 12 | func fileeditCommitItem(r *response, subject string) *goquery.Selection { |
| 13 | var found *goquery.Selection |
| 14 | r.Find(".commit-item").EachWithBreak(func(_ int, s *goquery.Selection) bool { |
| 15 | if strings.TrimSpace(s.Find(".commit-subject").First().Text()) == subject { |
| 16 | found = s |
| 17 | return false |
| 18 | } |
| 19 | return true |
| 20 | }) |
| 21 | return found |
| 22 | } |
| 23 | |
| 24 | func TestFileEditing(t *testing.T) { |
| 25 | e := newEnv(t) |
| 26 | admin := e.admin() |
| 27 | |
| 28 | // A dedicated repo so edits do not interfere with other tests. |
| 29 | e.createRepo(admin, "edit-repo") |
| 30 | e.seedRepo("edit-repo", nil) |
| 31 | |
| 32 | t.Run("Edit button appears on text file blob when viewing a branch as admin", func(t *testing.T) { |
| 33 | r := admin.get("/edit-repo/blob/main/index.js").mustStatus(200) |
| 34 | sel := `a[href*="/edit/main/index.js"]` |
| 35 | if r.Count(sel) == 0 { |
| 36 | t.Fatal("Edit link missing") |
| 37 | } |
| 38 | if got := r.Text(sel); got != "Edit" { |
| 39 | t.Errorf("link text = %q", got) |
| 40 | } |
| 41 | }) |
| 42 | |
| 43 | t.Run("Edit button does not appear when viewing a commit SHA", func(t *testing.T) { |
| 44 | sha := e.headCommit("edit-repo") |
| 45 | if n := admin.get("/edit-repo/blob/" + sha + "/index.js").Count(`a[href*="/edit/"]`); n != 0 { |
| 46 | t.Errorf("Edit links = %d", n) |
| 47 | } |
| 48 | }) |
| 49 | |
| 50 | t.Run("Edit button does not appear for unauthenticated visitors", func(t *testing.T) { |
| 51 | if n := e.anon().get("/edit-repo/blob/main/index.js").Count(`a[href*="/edit/main/"]`); n != 0 { |
| 52 | t.Errorf("Edit links = %d", n) |
| 53 | } |
| 54 | }) |
| 55 | |
| 56 | t.Run("edit page loads with file content pre-filled", func(t *testing.T) { |
| 57 | r := admin.get("/edit-repo/edit/main/index.js").mustStatus(200) |
| 58 | if got := r.Text(".file-blob-name"); got != "index.js" { |
| 59 | t.Errorf("file name = %q", got) |
| 60 | } |
| 61 | if got := r.Value("textarea[name=content]"); !strings.Contains(got, "hello") { |
| 62 | t.Errorf("content = %q", got) |
| 63 | } |
| 64 | if got := r.Value("textarea[name=message]"); got != "Edited index.js" { |
| 65 | t.Errorf("message = %q", got) |
| 66 | } |
| 67 | }) |
| 68 | |
| 69 | t.Run("edit page shows which branch will be committed to", func(t *testing.T) { |
| 70 | if !admin.get("/edit-repo/edit/main/index.js").Contains("main") { |
| 71 | t.Error("branch name missing") |
| 72 | } |
| 73 | }) |
| 74 | |
| 75 | t.Run("edit page returns 404 for non-branch ref", func(t *testing.T) { |
| 76 | sha := e.headCommit("edit-repo") |
| 77 | admin.get("/edit-repo/edit/" + sha + "/index.js").mustStatus(404) |
| 78 | }) |
| 79 | |
| 80 | t.Run("submitting edit creates a new commit and redirects to blob view", func(t *testing.T) { |
| 81 | r := admin.post("/edit-repo/edit/main/index.js", url.Values{ |
| 82 | "content": {"console.log(\"edited\");\n"}, |
| 83 | "new_path": {"index.js"}, |
| 84 | "message": {"Update index.js via web editor"}, |
| 85 | }) |
| 86 | r.mustRedirect("/edit-repo/commit/") |
| 87 | if !admin.follow(r).Contains("Update index.js via web editor") { |
| 88 | t.Error("commit view does not show the message") |
| 89 | } |
| 90 | }) |
| 91 | |
| 92 | t.Run("edit commit has a gpgsig header (is signed)", func(t *testing.T) { |
| 93 | dir := e.repoPath("edit-repo") |
| 94 | hash := gitRun(t, dir, "log", "--format=%H", "--grep=Update index.js via web editor", "-1") |
| 95 | if hash == "" { |
| 96 | t.Fatal("edit commit not found") |
| 97 | } |
| 98 | if obj := gitRun(t, dir, "cat-file", "-p", hash); !strings.Contains(obj, "gpgsig") { |
| 99 | t.Errorf("commit object has no gpgsig:\n%s", obj) |
| 100 | } |
| 101 | }) |
| 102 | |
| 103 | t.Run("edit commit shows verified badge in commit log", func(t *testing.T) { |
| 104 | r := admin.get("/edit-repo/commits/main").mustStatus(200) |
| 105 | item := fileeditCommitItem(r, "Update index.js via web editor") |
| 106 | if item == nil { |
| 107 | t.Fatal("commit row missing") |
| 108 | } |
| 109 | if item.Find(".sig-badge.verified").Length() == 0 { |
| 110 | t.Error("verified badge missing") |
| 111 | } |
| 112 | }) |
| 113 | |
| 114 | t.Run("GET edit page returns 404 for non-branch ref", func(t *testing.T) { |
| 115 | sha := e.headCommit("edit-repo") |
| 116 | admin.get("/edit-repo/edit/" + sha + "/index.js").mustStatus(404) |
| 117 | }) |
| 118 | } |
| 119 |