e2e/milestone_test.go

v1.2.1
gitbay/e2e/milestone_test.go history · blame · raw

150 lines · 6300 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"os"
  6	"path/filepath"
  7	"strings"
  8	"testing"
  9)
 10
 11func TestMilestonesAndTemplates(t *testing.T) {
 12	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
 13	aliceKey := inst.newKey(t, "alice")
 14	bobKey := inst.newKey(t, "bob")
 15	inst.admin(t, "admin", "user", "create", "alice",
 16		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
 17	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
 18
 19	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
 20		t.Fatalf("repo create: %s", errOut)
 21	}
 22	work := t.TempDir()
 23	env := inst.gitEnv(aliceKey)
 24	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
 25	dir := filepath.Join(work, "w")
 26	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
 27	os.WriteFile(filepath.Join(dir, ".gitbay", "issue-template.md"),
 28		[]byte("## Steps to reproduce\n\n## Expected\n"), 0o644)
 29	os.WriteFile(filepath.Join(dir, ".gitbay", "issue-template-feature.md"),
 30		[]byte("## Motivation\n"), 0o644)
 31	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\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	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
 37	os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644)
 38	mustGit(t, dir, env, "add", ".")
 39	mustGit(t, dir, env, "commit", "-q", "-m", "feat")
 40	mustGit(t, dir, env, "push", "-q", "origin", "feat")
 41	if _, _, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
 42		"--source", "feat", "--target", "main", "--title", "'feat'"); code != 0 {
 43		t.Fatal("mr create failed")
 44	}
 45	for _, title := range []string{"'one'", "'two'"} {
 46		if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", title); code != 0 {
 47			t.Fatal("issue create failed")
 48		}
 49	}
 50
 51	// Milestone lifecycle and validation.
 52	if _, errOut, code := inst.ssh(t, aliceKey, "", "milestone", "create", "alice/app", "v1.0",
 53		"--description", "'first release'", "--due", "2027-01-01"); code != 0 {
 54		t.Fatalf("milestone create: %s", errOut)
 55	}
 56	if _, _, code := inst.ssh(t, aliceKey, "", "milestone", "create", "alice/app", "v1.0"); code != 2 {
 57		t.Fatal("duplicate milestone accepted")
 58	}
 59	if _, errOut, code := inst.ssh(t, aliceKey, "", "milestone", "create", "alice/app", "v2.0", "--due", "soon"); code != 2 || !strings.Contains(errOut, "YYYY-MM-DD") {
 60		t.Fatalf("bad due accepted: %s", errOut)
 61	}
 62	if _, _, code := inst.ssh(t, bobKey, "", "milestone", "create", "alice/app", "sneaky"); code != 4 {
 63		t.Fatal("read-only user created a milestone")
 64	}
 65
 66	// Attach: issue 1, MR 1; unknown title refused; clearing works.
 67	if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "milestone", "alice/app", "1", "v1.0"); code != 0 {
 68		t.Fatalf("issue milestone: %s", errOut)
 69	}
 70	if _, _, code := inst.ssh(t, aliceKey, "", "mr", "milestone", "alice/app", "1", "v1.0"); code != 0 {
 71		t.Fatal("mr milestone failed")
 72	}
 73	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "milestone", "alice/app", "2", "v9.9"); code != 3 {
 74		t.Fatal("unknown milestone accepted")
 75	}
 76	out, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "1", "--json")
 77	if !strings.Contains(out, `"milestone":"v1.0"`) {
 78		t.Fatalf("issue show milestone: %s", out)
 79	}
 80	out, _, _ = inst.ssh(t, aliceKey, "", "mr", "show", "alice/app", "1", "--json")
 81	if !strings.Contains(out, `"milestone":"v1.0"`) {
 82		t.Fatalf("mr show milestone: %s", out)
 83	}
 84
 85	// Progress: 2 open (issue 1 + MR 1); closing the issue moves it.
 86	out, _, _ = inst.ssh(t, aliceKey, "", "milestone", "list", "alice/app", "--json")
 87	if !strings.Contains(out, `"open":2`) || !strings.Contains(out, `"due":"2027-01-01"`) {
 88		t.Fatalf("milestone list: %s", out)
 89	}
 90	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "close", "alice/app", "1"); code != 0 {
 91		t.Fatal("issue close failed")
 92	}
 93	out, _, _ = inst.ssh(t, aliceKey, "", "milestone", "list", "alice/app", "--json")
 94	if !strings.Contains(out, `"open":1`) || !strings.Contains(out, `"closed":1`) {
 95		t.Fatalf("milestone progress: %s", out)
 96	}
 97
 98	// Close/reopen; closed milestones leave the default list.
 99	if _, _, code := inst.ssh(t, aliceKey, "", "milestone", "close", "alice/app", "v1.0"); code != 0 {
100		t.Fatal("milestone close failed")
101	}
102	out, _, _ = inst.ssh(t, aliceKey, "", "milestone", "list", "alice/app")
103	if strings.Contains(out, "v1.0") {
104		t.Fatal("closed milestone in open list")
105	}
106	if _, _, code := inst.ssh(t, aliceKey, "", "milestone", "reopen", "alice/app", "v1.0"); code != 0 {
107		t.Fatal("milestone reopen failed")
108	}
109
110	// Web: milestones page with progress; issue page links the milestone.
111	status, body := inst.get(t, "/alice/app/milestones")
112	if status != 200 || !strings.Contains(body, "v1.0") || !strings.Contains(body, "50%") ||
113		!strings.Contains(body, "first release") {
114		t.Fatalf("milestones page: %d\n%s", status, body)
115	}
116	_, body = inst.get(t, "/alice/app/issues/1")
117	if !strings.Contains(body, ">v1.0</a>") {
118		t.Fatal("issue page missing milestone link")
119	}
120
121	// Templates over SSH.
122	out, _, _ = inst.ssh(t, aliceKey, "", "issue", "templates", "alice/app", "--json")
123	if !strings.Contains(out, `"name":"issue-template.md"`) ||
124		!strings.Contains(out, "Steps to reproduce") ||
125		!strings.Contains(out, `"name":"issue-template-feature.md"`) {
126		t.Fatalf("issue templates: %s", out)
127	}
128
129	// Templates on the web form (login required).
130	out, errOut, code := inst.ssh(t, aliceKey, "", "web", "login", "--json")
131	if code != 0 {
132		t.Fatalf("web login: %s", errOut)
133	}
134	var env2 struct {
135		Data struct {
136			URL string `json:"url"`
137		} `json:"data"`
138	}
139	json.Unmarshal([]byte(out), &env2)
140	browser := newBrowser(t)
141	browserGet(t, browser, inst.base()+env2.Data.URL[strings.Index(env2.Data.URL, "/login"):])
142	_, body = browserGet(t, browser, inst.base()+"/alice/app/issues/new")
143	if !strings.Contains(body, "Steps to reproduce") || !strings.Contains(body, "issue-template-feature.md") {
144		t.Fatalf("issue form not prefilled:\n%s", body)
145	}
146	_, body = browserGet(t, browser, inst.base()+"/alice/app/issues/new?template=issue-template-feature.md")
147	if !strings.Contains(body, "Motivation") {
148		t.Fatal("template switch failed")
149	}
150}