fileediting_test.go
⎇
Raw
1package e2e
2
3import (
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.
12func 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
24func 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 t.Run("save after a push to another file lands on top of it", func(t *testing.T) {
120 base := admin.get("/edit-repo/edit/main/index.js").Value("input[name=base]")
121 if base != e.headCommit("edit-repo") {
122 t.Fatalf("base = %q", base)
123 }
124 e.seedRepo("edit-repo", map[string]string{"other.txt": "pushed\n"}, "Push other")
125 admin.post("/edit-repo/edit/main/index.js", url.Values{
126 "content": {"on top\n"}, "new_path": {"index.js"}, "message": {"On top"}, "base": {base},
127 }).mustRedirect("/edit-repo/commit/")
128 if got := gitRun(t, e.repoPath("edit-repo"), "show", "main:other.txt"); got != "pushed" {
129 t.Errorf("other.txt = %q", got)
130 }
131 })
132
133 t.Run("save after a push to the same file keeps the input and asks again", func(t *testing.T) {
134 base := admin.get("/edit-repo/edit/main/index.js").Value("input[name=base]")
135 tip := e.seedRepo("edit-repo", map[string]string{"index.js": "pushed\n"}, "Push index")
136 form := url.Values{"content": {"mine\n"}, "new_path": {"index.js"}, "message": {"Mine"}, "base": {base}}
137 r := admin.post("/edit-repo/edit/main/index.js", form).mustStatus(409)
138 if got := r.Value("textarea[name=content]"); got != "mine\n" {
139 t.Errorf("content = %q", got)
140 }
141 if got := r.Value("input[name=base]"); got != tip {
142 t.Errorf("base = %q, want %q", got, tip)
143 }
144 if !r.Has(`.form-error a[href="/edit-repo/blob/` + tip + `/index.js"]`) {
145 t.Error("link to the current version missing")
146 }
147 if e.headCommit("edit-repo") != tip {
148 t.Fatal("stale save moved the branch")
149 }
150 form.Set("base", tip)
151 admin.post("/edit-repo/edit/main/index.js", form).mustRedirect("/edit-repo/commit/")
152 if got := gitRun(t, e.repoPath("edit-repo"), "show", "main:index.js"); got != "mine" {
153 t.Errorf("index.js = %q", got)
154 }
155 })
156
157 t.Run("create over an existing file is rejected with the input kept", func(t *testing.T) {
158 r := admin.post("/edit-repo/new-file/main", url.Values{
159 "path": {"README.md"}, "content": {"new body"}, "message": {"Clobber"},
160 }).mustStatus(409)
161 if got := r.Value("textarea[name=content]"); got != "new body" {
162 t.Errorf("content = %q", got)
163 }
164 if got := r.Value("input[name=path]"); got != "README.md" {
165 t.Errorf("path = %q", got)
166 }
167 })
168
169 t.Run("delete after a push to the same file shows the current version", func(t *testing.T) {
170 base := admin.get("/edit-repo/blob/main/README.md").Value(`form[action*="/delete-file/"] input[name=base]`)
171 if base != e.headCommit("edit-repo") {
172 t.Fatalf("base = %q", base)
173 }
174 tip := e.seedRepo("edit-repo", map[string]string{"README.md": "pushed\n"}, "Push readme")
175 r := admin.post("/edit-repo/delete-file/main/README.md", url.Values{"base": {base}}).mustStatus(409)
176 if !r.Has(".form-error") || !r.Contains("pushed") {
177 t.Error("error or current content missing")
178 }
179 if got := r.Value(`form[action*="/delete-file/"] input[name=base]`); got != tip {
180 t.Errorf("base = %q, want %q", got, tip)
181 }
182 if e.headCommit("edit-repo") != tip {
183 t.Fatal("stale delete moved the branch")
184 }
185 admin.post("/edit-repo/delete-file/main/README.md", url.Values{"base": {tip}}).mustRedirect("/edit-repo/commit/")
186 })
187
188 t.Run("delete of a missing file makes no commit", func(t *testing.T) {
189 tip := e.headCommit("edit-repo")
190 admin.post("/edit-repo/delete-file/main/nope.txt", url.Values{}).mustStatus(404)
191 if e.headCommit("edit-repo") != tip {
192 t.Error("branch moved")
193 }
194 })
195}
196