e2e/mrforkweb_test.go

v1.43.0
gitbay/e2e/mrforkweb_test.go history · blame · raw

84 lines · 3353 bytes

 1package e2e
 2
 3import (
 4	"net/url"
 5	"os"
 6	"path/filepath"
 7	"strings"
 8	"testing"
 9)
10
11// A contributor without write access proposes a change from the browser:
12// the source picker offers the branches of a fork they can push to, and
13// the merge request opens against the parent (#168).
14func TestMRFromForkWeb(t *testing.T) {
15	t.Parallel()
16	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
17	aliceKey := inst.newKey(t, "alice")
18	bobKey := inst.newKey(t, "bob")
19	eveKey := inst.newKey(t, "eve")
20	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
21	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
22	inst.admin(t, "admin", "user", "create", "eve", "--key", eveKey+".pub")
23
24	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
25		t.Fatalf("repo create: %s", errOut)
26	}
27	env := inst.gitEnv(aliceKey)
28	work := t.TempDir()
29	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
30	dir := filepath.Join(work, "w")
31	os.WriteFile(filepath.Join(dir, "f.txt"), []byte("x\n"), 0o644)
32	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
33	mustGit(t, dir, env, "add", ".")
34	mustGit(t, dir, env, "commit", "-q", "-m", "base")
35	mustGit(t, dir, env, "push", "-q", "origin", "main")
36
37	// bob forks and pushes a branch to the fork.
38	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "fork", "alice/app"); code != 0 {
39		t.Fatalf("fork: %s", errOut)
40	}
41	benv := inst.gitEnv(bobKey)
42	bwork := t.TempDir()
43	mustGit(t, bwork, benv, "clone", inst.sshURL("bob/app"), "w")
44	bdir := filepath.Join(bwork, "w")
45	mustGit(t, bdir, benv, "checkout", "-q", "-b", "feat")
46	os.WriteFile(filepath.Join(bdir, "f.txt"), []byte("y\n"), 0o644)
47	mustGit(t, bdir, benv, "add", ".")
48	mustGit(t, bdir, benv, "commit", "-q", "-m", "change")
49	mustGit(t, bdir, benv, "push", "-q", "origin", "feat")
50
51	// bob's fork branch is offered on alice's repo; eve, who can push to
52	// neither, sees only the target's own branches.
53	bob := inst.login(t, bobKey)
54	eve := inst.login(t, eveKey)
55	base := inst.base() + "/alice/app"
56	_, page := browserGet(t, bob, base+"/mrs/new")
57	if !strings.Contains(page, `value="bob/app:feat"`) {
58		t.Fatalf("fork branch not offered:\n%s", page)
59	}
60	if _, p := browserGet(t, eve, base+"/mrs/new"); strings.Contains(p, "bob/app:feat") {
61		t.Fatal("a fork branch is offered to someone who cannot push to it")
62	}
63
64	// Opening it lands on the merge request, with the fork as its source.
65	if status, _ := browserPost(t, bob, base+"/mrs/new", url.Values{
66		"source": {"bob/app:feat"}, "target": {"main"}, "title": {"change"}}); status != 200 {
67		t.Fatal("mr create from a fork failed")
68	}
69	out, _, _ := inst.ssh(t, bobKey, "", "mr", "show", "alice/app", "1", "--json")
70	if !strings.Contains(out, `"source":"bob/app:feat"`) {
71		t.Fatalf("merge request source is not the fork:\n%s", out)
72	}
73
74	// A branch of a repository that is not a fork of the target is
75	// refused by the command, whatever the form posts.
76	if _, errOut, code := inst.ssh(t, eveKey, "", "repo", "create", "eve/other"); code != 0 {
77		t.Fatalf("repo create: %s", errOut)
78	}
79	_, body := browserPost(t, eve, base+"/mrs/new", url.Values{
80		"source": {"eve/other:main"}, "target": {"main"}, "title": {"sneak"}})
81	if !strings.Contains(body, `class="error"`) {
82		t.Errorf("a non-fork source was accepted:\n%s", body)
83	}
84}