e2e/mrweb_test.go

0a69a8facd64e67e42ec04fa11dd4a0304e40d79
gitbay/e2e/mrweb_test.go history · blame · raw

348 lines · 14198 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"net/http"
  6	"net/url"
  7	"os"
  8	"path/filepath"
  9	"regexp"
 10	"strconv"
 11	"strings"
 12	"testing"
 13)
 14
 15// login returns a browser holding a session for the given key's account.
 16func (i *instance) login(t *testing.T, key string) *http.Client {
 17	t.Helper()
 18	out, errOut, code := i.ssh(t, key, "", "web", "login", "--json")
 19	if code != 0 {
 20		t.Fatalf("web login: %s", errOut)
 21	}
 22	var env struct {
 23		Data struct {
 24			URL string `json:"url"`
 25		} `json:"data"`
 26	}
 27	json.Unmarshal([]byte(out), &env)
 28	c := newBrowser(t)
 29	path := env.Data.URL[strings.Index(env.Data.URL, "/login"):]
 30	if status, _ := browserGet(t, c, i.base()+path); status != 200 {
 31		t.Fatalf("login landed: %d", status)
 32	}
 33	return c
 34}
 35
 36// TestMRWebReviewLoop drives review, thread resolution, and merge from the
 37// browser. Every action runs the same control command the CLI runs, so the
 38// test also proves the merge gates apply to web merges.
 39func TestMRWebReviewLoop(t *testing.T) {
 40	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
 41	aliceKey := inst.newKey(t, "alice")
 42	bobKey := inst.newKey(t, "bob")
 43	inst.admin(t, "admin", "user", "create", "alice",
 44		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
 45	inst.admin(t, "admin", "user", "create", "bob",
 46		"--key", bobKey+".pub", "--email", "bob@example.test", "--verified")
 47
 48	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/lib"); code != 0 {
 49		t.Fatalf("repo create: %s", errOut)
 50	}
 51	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/lib", "bob", "write"); code != 0 {
 52		t.Fatalf("grant: %s", errOut)
 53	}
 54	// Unresolved review threads block merges, so the gate is observable.
 55	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "settings", "require-resolved", "alice/lib", "on"); code != 0 {
 56		t.Fatalf("require-resolved: %s", errOut)
 57	}
 58
 59	env := inst.gitEnv(aliceKey)
 60	work := t.TempDir()
 61	mustGit(t, work, env, "clone", inst.sshURL("alice/lib"), "w")
 62	dir := filepath.Join(work, "w")
 63	os.WriteFile(filepath.Join(dir, "lib.txt"), []byte("v1\n"), 0o644)
 64	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 65	mustGit(t, dir, env, "add", ".")
 66	mustGit(t, dir, env, "commit", "-q", "-m", "base")
 67	mustGit(t, dir, env, "push", "-q", "origin", "main")
 68
 69	// Bob proposes a change and leaves a review thread on it.
 70	bobEnv := inst.gitEnv(bobKey)
 71	bobWork := t.TempDir()
 72	mustGit(t, bobWork, bobEnv, "clone", inst.sshURL("alice/lib"), "w")
 73	bobDir := filepath.Join(bobWork, "w")
 74	mustGit(t, bobDir, bobEnv, "checkout", "-q", "-b", "feature", "origin/main")
 75	os.WriteFile(filepath.Join(bobDir, "feature.txt"), []byte("bob's work\n"), 0o644)
 76	mustGit(t, bobDir, bobEnv, "add", ".")
 77	mustGit(t, bobDir, bobEnv, "commit", "-q", "-m", "add feature")
 78	mustGit(t, bobDir, bobEnv, "push", "-q", "origin", "feature")
 79	if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/lib",
 80		"--source", "feature", "--target", "main", "--title", "'add feature'"); code != 0 {
 81		t.Fatalf("mr create: %s", errOut)
 82	}
 83	if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "diff-comment", "alice/lib", "1",
 84		"--path", "feature.txt", "--line", "1", "--message", "'is this right?'"); code != 0 {
 85		t.Fatalf("diff-comment: %s", errOut)
 86	}
 87
 88	mrURL := inst.base() + "/alice/lib/mrs/1"
 89	alice := inst.login(t, aliceKey)
 90
 91	// The controls are on the page, and carry the thread to resolve.
 92	_, body := browserGet(t, alice, mrURL)
 93	for _, want := range []string{`value="approve"`, `action="/alice/lib/mrs/1/merge"`} {
 94		if !strings.Contains(body, want) {
 95			t.Fatalf("MR page missing %q", want)
 96		}
 97	}
 98	// Review threads live on the diff view, where their lines are.
 99	_, diffBody := browserGet(t, alice, mrURL+"?view=diff")
100	m := regexp.MustCompile(`name="thread" value="(\d+)"`).FindStringSubmatch(diffBody)
101	if m == nil {
102		t.Fatalf("no thread control on the diff view:\n%s", diffBody)
103	}
104	threadID := m[1]
105
106	// Approve from the browser; the CLI sees the review.
107	if status, _ := browserPost(t, alice, mrURL+"/review", url.Values{"verdict": {"approve"}}); status != 200 {
108		t.Fatalf("review post: %d", status)
109	}
110	show := inst.mrShow(t, aliceKey, "alice/lib", "1")
111	if len(show.Reviews) != 1 || show.Reviews[0].Reviewer != "alice" || show.Reviews[0].Verdict != "approve" {
112		t.Fatalf("review not recorded: %+v", show.Reviews)
113	}
114
115	// Merging is refused while the thread is open, and the page says why.
116	_, body = browserPost(t, alice, mrURL+"/merge", url.Values{"strategy": {"auto"}})
117	if !strings.Contains(body, "unresolved") {
118		t.Fatalf("merge gate not surfaced:\n%s", body)
119	}
120	if st := inst.mrShow(t, aliceKey, "alice/lib", "1").State; st != "open" {
121		t.Fatalf("blocked merge changed state to %s", st)
122	}
123
124	// Resolve the thread, then merge.
125	if status, _ := browserPost(t, alice, mrURL+"/thread",
126		url.Values{"thread": {threadID}, "action": {"resolve"}}); status != 200 {
127		t.Fatalf("resolve post: %d", status)
128	}
129	out, _, _ := inst.ssh(t, aliceKey, "", "mr", "threads", "alice/lib", "1")
130	if !strings.Contains(out, "resolved") {
131		t.Fatalf("thread not resolved:\n%s", out)
132	}
133	if status, _ := browserPost(t, alice, mrURL+"/merge", url.Values{"strategy": {"auto"}}); status != 200 {
134		t.Fatalf("merge post: %d", status)
135	}
136	merged := inst.mrShow(t, aliceKey, "alice/lib", "1")
137	if merged.State != "merged" {
138		t.Fatalf("MR state after web merge: %s", merged.State)
139	}
140	// Who merged it and when, so the page can stop saying alice wants to.
141	if merged.MergedBy != "alice" || merged.MergedAt == "" {
142		t.Fatalf("merge not attributed: %+v", merged)
143	}
144	if merged.Reviews[0].CreatedAt == "" {
145		t.Fatalf("review carries no timestamp: %+v", merged.Reviews[0])
146	}
147	_, body = browserGet(t, alice, mrURL)
148	if strings.Contains(body, "wants to merge") {
149		t.Fatalf("merged MR still wants to merge:\n%s", body)
150	}
151	if !strings.Contains(body, ">alice</a> merged") {
152		t.Fatalf("merged MR does not name the merger:\n%s", body)
153	}
154	mustGit(t, dir, env, "pull", "-q", "origin", "main")
155	if _, err := os.Stat(filepath.Join(dir, "feature.txt")); err != nil {
156		t.Fatal("merged content missing from main")
157	}
158
159	// Readers get no controls, and a forged POST is refused by the command.
160	_, anon := browserGet(t, newBrowser(t), mrURL)
161	if strings.Contains(anon, `value="approve"`) {
162		t.Fatal("anonymous visitor sees review controls")
163	}
164	carol := inst.newKey(t, "carol")
165	inst.admin(t, "admin", "user", "create", "carol", "--key", carol+".pub")
166	if _, errOut, code := inst.ssh(t, carol, "", "repo", "create", "carol/own"); code != 0 {
167		t.Fatalf("carol repo: %s", errOut)
168	}
169	_, denied := browserPost(t, inst.login(t, carol), mrURL+"/close", url.Values{})
170	if !strings.Contains(denied, `class="error"`) || !strings.Contains(denied, "write access") {
171		t.Fatalf("reader was not refused:\n%s", denied)
172	}
173}
174
175// TestMRWebCreate opens a merge request from the browser and checks the
176// form survives a refusal with the draft intact.
177func TestMRWebCreate(t *testing.T) {
178	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
179	aliceKey := inst.newKey(t, "alice")
180	inst.admin(t, "admin", "user", "create", "alice",
181		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
182	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/lib"); code != 0 {
183		t.Fatalf("repo create: %s", errOut)
184	}
185	env := inst.gitEnv(aliceKey)
186	work := t.TempDir()
187	mustGit(t, work, env, "clone", inst.sshURL("alice/lib"), "w")
188	dir := filepath.Join(work, "w")
189	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
190	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
191	mustGit(t, dir, env, "add", ".")
192	mustGit(t, dir, env, "commit", "-q", "-m", "base")
193	mustGit(t, dir, env, "push", "-q", "origin", "main")
194	mustGit(t, dir, env, "checkout", "-q", "-b", "topic")
195	os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644)
196	mustGit(t, dir, env, "add", ".")
197	mustGit(t, dir, env, "commit", "-q", "-m", "topic work")
198	mustGit(t, dir, env, "push", "-q", "origin", "topic")
199
200	alice := inst.login(t, aliceKey)
201	base := inst.base() + "/alice/lib"
202
203	// The list links to the form, and the form offers the pushed branches.
204	if _, body := browserGet(t, alice, base+"/mrs"); !strings.Contains(body, "/alice/lib/mrs/new") {
205		t.Fatalf("no create link on the list:\n%s", body)
206	}
207	_, form := browserGet(t, alice, base+"/mrs/new")
208	for _, want := range []string{`name="source"`, `value="topic"`, `value="main"`} {
209		if !strings.Contains(form, want) {
210			t.Fatalf("form missing %q:\n%s", want, form)
211		}
212	}
213
214	// A refusal keeps the draft: the branch does not exist.
215	_, retry := browserPost(t, alice, base+"/mrs/new", url.Values{
216		"source": {"nope"}, "target": {"main"}, "title": {"my title"}, "body": {"my body"}})
217	if !strings.Contains(retry, `class="error"`) || !strings.Contains(retry, "my title") ||
218		!strings.Contains(retry, "my body") {
219		t.Fatalf("refusal lost the draft:\n%s", retry)
220	}
221
222	// A real one lands on the merge request it created.
223	status, created := browserPost(t, alice, base+"/mrs/new", url.Values{
224		"source": {"topic"}, "target": {"main"}, "title": {"topic into main"}, "body": {"please review"}})
225	if status != 200 || !strings.Contains(created, "topic into main") {
226		t.Fatalf("create failed: %d\n%s", status, created)
227	}
228	show := inst.mrShow(t, aliceKey, "alice/lib", "1")
229	if show.State != "open" || show.Source != "topic" {
230		t.Fatalf("created MR wrong: %+v", show)
231	}
232}
233
234// TestMRWebDiffThreads opens a review thread on a diff line and replies to
235// it from the browser. The CLI's view of the threads afterwards is what
236// proves the page dispatched mr diff-comment rather than writing its own
237// rows.
238func TestMRWebDiffThreads(t *testing.T) {
239	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
240	aliceKey := inst.newKey(t, "alice")
241	inst.admin(t, "admin", "user", "create", "alice",
242		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
243
244	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/lib"); code != 0 {
245		t.Fatalf("repo create: %s", errOut)
246	}
247	env := inst.gitEnv(aliceKey)
248	work := t.TempDir()
249	mustGit(t, work, env, "clone", inst.sshURL("alice/lib"), "w")
250	dir := filepath.Join(work, "w")
251	os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() {\n}\n"), 0o644)
252	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
253	mustGit(t, dir, env, "add", ".")
254	mustGit(t, dir, env, "commit", "-q", "-m", "base")
255	mustGit(t, dir, env, "push", "-q", "origin", "main")
256	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
257	os.WriteFile(filepath.Join(dir, "main.go"),
258		[]byte("package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"hi\")\n}\n"), 0o644)
259	mustGit(t, dir, env, "add", ".")
260	mustGit(t, dir, env, "commit", "-q", "-m", "add greeting")
261	mustGit(t, dir, env, "push", "-q", "origin", "feat")
262	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/lib",
263		"--source", "feat", "--target", "main", "--title", "'greeting'"); code != 0 {
264		t.Fatalf("mr create: %s", errOut)
265	}
266
267	mrURL := inst.base() + "/alice/lib/mrs/1"
268	alice := inst.login(t, aliceKey)
269
270	// The gutter carries the handle, and following it renders the form
271	// anchored to that line. There is no JavaScript, so the anchor has to
272	// survive a round trip in the query.
273	_, body := browserGet(t, alice, mrURL+"?view=diff")
274	if !strings.Contains(body, "cpath=main.go&amp;cline=6&amp;cside=new") {
275		t.Fatalf("no comment handle in the diff gutter:\n%s", body)
276	}
277	_, body = browserGet(t, alice, mrURL+"?view=diff&cpath=main.go&cline=6&cside=new")
278	if !strings.Contains(body, `id="compose"`) || !strings.Contains(body, `name="line" value="6"`) {
279		t.Fatalf("compose form not rendered:\n%s", body)
280	}
281
282	if status, _ := browserPost(t, alice, mrURL+"/diff-comment", url.Values{
283		"path": {"main.go"}, "line": {"6"}, "side": {"new"},
284		"body": {"use log instead of fmt"}}); status != 200 {
285		t.Fatalf("open thread: %d", status)
286	}
287	threads := inst.mrThreads(t, aliceKey, "alice/lib", "1")
288	if len(threads) != 1 || threads[0].Path != "main.go" || threads[0].Line != 6 {
289		t.Fatalf("thread not anchored: %+v", threads)
290	}
291	if len(threads[0].Comments) != 1 || threads[0].Comments[0].Body != "use log instead of fmt" {
292		t.Fatalf("comment body not stored: %+v", threads[0].Comments)
293	}
294
295	// The rendered thread offers reply and resolve to its author.
296	_, body = browserGet(t, alice, mrURL+"?view=diff")
297	if !strings.Contains(body, `name="reply" value="`+strconv.FormatInt(threads[0].ID, 10)+`"`) {
298		t.Fatalf("no reply form on the thread:\n%s", body)
299	}
300	if !strings.Contains(body, `value="resolve"`) {
301		t.Fatalf("no resolve control on the thread:\n%s", body)
302	}
303
304	if status, _ := browserPost(t, alice, mrURL+"/diff-comment", url.Values{
305		"reply": {strconv.FormatInt(threads[0].ID, 10)}, "body": {"agreed, switching"}}); status != 200 {
306		t.Fatalf("reply: %d", status)
307	}
308	threads = inst.mrThreads(t, aliceKey, "alice/lib", "1")
309	if len(threads) != 1 || len(threads[0].Comments) != 2 ||
310		threads[0].Comments[1].Body != "agreed, switching" {
311		t.Fatalf("reply not on the thread: %+v", threads)
312	}
313
314	// An empty body is refused, and says so on the page it returns to.
315	_, body = browserPost(t, alice, mrURL+"/diff-comment", url.Values{
316		"reply": {strconv.FormatInt(threads[0].ID, 10)}, "body": {"  "}})
317	if !strings.Contains(body, "empty comment") {
318		t.Fatalf("empty reply not refused:\n%s", body)
319	}
320}
321
322// mrThread is one review thread as mr threads --json reports it.
323type mrThread struct {
324	ID       int64  `json:"id"`
325	Path     string `json:"path"`
326	Side     string `json:"side"`
327	Line     int64  `json:"line"`
328	Comments []struct {
329		Author string `json:"author"`
330		Body   string `json:"body"`
331	} `json:"comments"`
332}
333
334// mrThreads reads the review threads on a merge request over SSH.
335func (i *instance) mrThreads(t *testing.T, key, repo, n string) []mrThread {
336	t.Helper()
337	out, errOut, code := i.ssh(t, key, "", "mr", "threads", repo, n, "--json")
338	if code != 0 {
339		t.Fatalf("mr threads: %s", errOut)
340	}
341	var env struct {
342		Data []mrThread `json:"data"`
343	}
344	if err := json.Unmarshal([]byte(out), &env); err != nil {
345		t.Fatalf("mr threads json: %v", err)
346	}
347	return env.Data
348}