e2e/dashboard_test.go

v1.22.1
gitbay/e2e/dashboard_test.go history · blame · raw

345 lines · 13499 bytes

  1package e2e
  2
  3import (
  4	"encoding/json"
  5	"os"
  6	"path/filepath"
  7	"strings"
  8	"testing"
  9)
 10
 11func TestDashboard(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	// Alice's repo with an open issue and an open MR authored by bob.
 20	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
 21		t.Fatalf("repo create: %s", errOut)
 22	}
 23	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/app", "bob", "write"); code != 0 {
 24		t.Fatal("grant failed")
 25	}
 26	work := t.TempDir()
 27	env := inst.gitEnv(aliceKey)
 28	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
 29	dir := filepath.Join(work, "w")
 30	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
 31	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
 32	mustGit(t, dir, env, "add", ".")
 33	mustGit(t, dir, env, "commit", "-q", "-m", "base")
 34	mustGit(t, dir, env, "push", "-q", "origin", "main")
 35	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
 36	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\nb\n"), 0o644)
 37	mustGit(t, dir, env, "add", ".")
 38	mustGit(t, dir, env, "commit", "-q", "-m", "feat")
 39	mustGit(t, dir, env, "push", "-q", "origin", "feat")
 40	if _, _, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
 41		"--source", "feat", "--target", "main", "--title", "'from bob'"); code != 0 {
 42		t.Fatal("mr create failed")
 43	}
 44	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'todo one'"); code != 0 {
 45		t.Fatal("issue create failed")
 46	}
 47
 48	// Pins: read access required; unpinning something never pinned 404s.
 49	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "pin", "alice/app"); code != 0 {
 50		t.Fatal("pin failed")
 51	}
 52	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "unpin", "alice/app"); code != 0 {
 53		t.Fatal("unpin failed")
 54	}
 55	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "unpin", "alice/app"); code != 3 {
 56		t.Fatal("double unpin should be not-found")
 57	}
 58	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "pin", "alice/app"); code != 0 {
 59		t.Fatal("re-pin failed")
 60	}
 61
 62	// Anonymous homepage: landing with explore link, repos on /explore.
 63	status, body := inst.get(t, "/")
 64	if status != 200 || !strings.Contains(body, `href="/explore"`) || !strings.Contains(body, "CLI-first") {
 65		t.Fatalf("landing: %d", status)
 66	}
 67	if strings.Contains(body, "alice/app") {
 68		t.Fatal("landing lists repositories")
 69	}
 70	_, body = inst.get(t, "/explore")
 71	if !strings.Contains(body, "alice/app") {
 72		t.Fatal("explore missing public repo")
 73	}
 74
 75	// Logged-in homepage: dashboard with pinned repo and open items.
 76	out, errOut, code := inst.ssh(t, aliceKey, "", "web", "login", "--json")
 77	if code != 0 {
 78		t.Fatalf("web login: %s", errOut)
 79	}
 80	var env2 struct {
 81		Data struct {
 82			URL string `json:"url"`
 83		} `json:"data"`
 84	}
 85	json.Unmarshal([]byte(out), &env2)
 86	loginPath := env2.Data.URL[strings.Index(env2.Data.URL, "/login"):]
 87	browser := newBrowser(t)
 88	if status, _ := browserGet(t, browser, inst.base()+loginPath); status != 200 {
 89		t.Fatalf("login: %d", status)
 90	}
 91	status, body = browserGet(t, browser, inst.base()+"/")
 92	if status != 200 || !strings.Contains(body, ">Dashboard</h1>") {
 93		t.Fatalf("dashboard: %d", status)
 94	}
 95	for _, want := range []string{
 96		">Pinned</h2>", ">app<", // pinned group in the aside
 97		"Waiting on your review", "Assigned to you", "Recent activity",
 98		"from bob", "alice/app!1", "todo one", "alice/app#1",
 99		`href="/alice/app/mrs/1"`, `href="/alice/app/issues/1"`,
100	} {
101		if !strings.Contains(body, want) {
102			t.Errorf("dashboard missing %q", want)
103		}
104	}
105
106	// The diff has its own view rather than a fold at the foot of the
107	// conversation: the default view offers it, and asking for it renders
108	// the stat line and the patch.
109	_, body = inst.get(t, "/alice/app/mrs/1")
110	if !strings.Contains(body, `href="/alice/app/mrs/1?view=diff"`) {
111		t.Fatal("merge request missing the files-changed view")
112	}
113	if strings.Contains(body, `class="difftable"`) {
114		t.Fatal("diff rendered on the conversation view")
115	}
116	_, body = inst.get(t, "/alice/app/mrs/1?view=diff")
117	if !strings.Contains(body, "1 file changed") || !strings.Contains(body, `class="difftable"`) {
118		t.Fatalf("diff view missing stat or patch:\n%s", body)
119	}
120}
121
122// The dashboard control command returns the same aggregate as the web
123// dashboard — review queue, assigned/open work, pins, and activity — in
124// one read. Builds remain for clients that already consume them.
125func TestDashboardCommand(t *testing.T) {
126	inst := startInstance(t)
127	aliceKey := inst.newKey(t, "alice")
128	bobKey := inst.newKey(t, "bob")
129	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
130	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
131
132	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
133		t.Fatalf("repo create: %s", errOut)
134	}
135	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/app", "bob", "write"); code != 0 {
136		t.Fatal("grant failed")
137	}
138	work := t.TempDir()
139	env := inst.gitEnv(aliceKey)
140	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
141	dir := filepath.Join(work, "w")
142	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
143	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
144		"jobs:\n  test:\n    steps:\n      - echo ok\n"), 0o644)
145	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
146	mustGit(t, dir, env, "add", ".")
147	mustGit(t, dir, env, "commit", "-q", "-m", "base")
148	mustGit(t, dir, env, "push", "-q", "origin", "main")
149	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
150	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
151	mustGit(t, dir, env, "add", ".")
152	mustGit(t, dir, env, "commit", "-q", "-m", "feat")
153	mustGit(t, dir, env, "push", "-q", "origin", "feat")
154
155	if _, _, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
156		"--source", "feat", "--target", "main", "--title", "'from bob'"); code != 0 {
157		t.Fatal("mr create failed")
158	}
159	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'todo one'"); code != 0 {
160		t.Fatal("issue create failed")
161	}
162	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "assign", "alice/app", "1", "--add", "alice"); code != 0 {
163		t.Fatal("assign failed")
164	}
165	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "pin", "alice/app"); code != 0 {
166		t.Fatal("pin failed")
167	}
168	if _, _, code := inst.ssh(t, bobKey, "", "repo", "pin", "alice/app"); code != 0 {
169		t.Fatal("bob pin failed")
170	}
171
172	out, errOut, code := inst.ssh(t, aliceKey, "", "dashboard", "--json")
173	if code != 0 {
174		t.Fatalf("dashboard: %s", errOut)
175	}
176	var env2 struct {
177		Data struct {
178			Reviews []struct {
179				Repo   string `json:"repo"`
180				Number int64  `json:"number"`
181				Title  string `json:"title"`
182			} `json:"review_queue"`
183			Pinned []struct {
184				Path string `json:"path"`
185			} `json:"pinned"`
186			MRs []struct {
187				Repo   string `json:"repo"`
188				Number int64  `json:"number"`
189				Title  string `json:"title"`
190				Author string `json:"author"`
191				State  string `json:"state"`
192			} `json:"open_mrs"`
193			Assigned []struct {
194				Repo   string `json:"repo"`
195				Number int64  `json:"number"`
196				Title  string `json:"title"`
197			} `json:"assigned_issues"`
198			Issues []struct {
199				Repo   string `json:"repo"`
200				Number int64  `json:"number"`
201				Title  string `json:"title"`
202			} `json:"open_issues"`
203			Activity []struct {
204				Repo  string `json:"repo"`
205				Actor string `json:"actor"`
206				Kind  string `json:"kind"`
207			} `json:"recent_activity"`
208			Builds []struct {
209				Repo   string `json:"repo"`
210				Job    string `json:"job"`
211				Status string `json:"status"`
212				Ref    string `json:"ref"`
213			} `json:"builds"`
214		} `json:"data"`
215	}
216	if err := json.Unmarshal([]byte(out), &env2); err != nil {
217		t.Fatalf("bad json: %v\n%s", err, out)
218	}
219	d := env2.Data
220	if len(d.Pinned) != 1 || d.Pinned[0].Path != "alice/app" {
221		t.Fatalf("pinned = %+v", d.Pinned)
222	}
223	if len(d.MRs) != 1 || d.MRs[0].Repo != "alice/app" || d.MRs[0].Number != 1 ||
224		d.MRs[0].Title != "from bob" || d.MRs[0].Author != "bob" || d.MRs[0].State != "open" {
225		t.Fatalf("open_mrs = %+v", d.MRs)
226	}
227	if len(d.Reviews) != 1 || d.Reviews[0].Repo != "alice/app" || d.Reviews[0].Number != 1 ||
228		d.Reviews[0].Title != "from bob" {
229		t.Fatalf("review_queue = %+v", d.Reviews)
230	}
231	if len(d.Assigned) != 1 || d.Assigned[0].Repo != "alice/app" || d.Assigned[0].Number != 1 ||
232		d.Assigned[0].Title != "todo one" {
233		t.Fatalf("assigned_issues = %+v", d.Assigned)
234	}
235	if len(d.Issues) != 1 || d.Issues[0].Repo != "alice/app" || d.Issues[0].Number != 1 ||
236		d.Issues[0].Title != "todo one" {
237		t.Fatalf("open_issues = %+v", d.Issues)
238	}
239	if len(d.Activity) == 0 || d.Activity[0].Repo != "alice/app" {
240		t.Fatalf("recent_activity = %+v", d.Activity)
241	}
242	// Both pushes hit main and feat; each queues the ci.yml job.
243	if len(d.Builds) == 0 || d.Builds[0].Repo != "alice/app" || d.Builds[0].Job != "test" ||
244		d.Builds[0].Status != "pending" {
245		t.Fatalf("builds = %+v", d.Builds)
246	}
247
248	// Bob is not assigned and pinned repos he can no longer read disappear.
249	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "settings", "visibility", "alice/app", "private"); code != 0 {
250		t.Fatal("visibility failed")
251	}
252	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "revoke", "alice/app", "bob"); code != 0 {
253		t.Fatal("revoke failed")
254	}
255	out, _, code = inst.ssh(t, bobKey, "", "dashboard", "--json")
256	if code != 0 {
257		t.Fatal("bob dashboard failed")
258	}
259	if err := json.Unmarshal([]byte(out), &env2); err != nil {
260		t.Fatalf("bad json: %v\n%s", err, out)
261	}
262	if len(env2.Data.Pinned) != 0 {
263		t.Fatalf("bob still sees pinned = %+v", env2.Data.Pinned)
264	}
265	if len(env2.Data.Assigned) != 0 {
266		t.Fatalf("bob assigned = %+v", env2.Data.Assigned)
267	}
268	if len(env2.Data.Builds) != 0 {
269		t.Fatalf("bob builds = %+v", env2.Data.Builds)
270	}
271}
272
273// TestDashboardQueues covers the parts of the dashboard that answer "what
274// needs me": the review queue, assigned issues, and the activity feed.
275func TestDashboardQueues(t *testing.T) {
276	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
277	aliceKey := inst.newKey(t, "alice")
278	bobKey := inst.newKey(t, "bob")
279	inst.admin(t, "admin", "user", "create", "alice",
280		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
281	inst.admin(t, "admin", "user", "create", "bob",
282		"--key", bobKey+".pub", "--email", "bob@example.test", "--verified")
283
284	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
285		t.Fatalf("repo create: %s", errOut)
286	}
287	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/app", "bob", "write"); code != 0 {
288		t.Fatalf("grant: %s", errOut)
289	}
290	env := inst.gitEnv(aliceKey)
291	work := t.TempDir()
292	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
293	dir := filepath.Join(work, "w")
294	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
295	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
296	mustGit(t, dir, env, "add", ".")
297	mustGit(t, dir, env, "commit", "-q", "-m", "base")
298	mustGit(t, dir, env, "push", "-q", "origin", "main")
299
300	// Bob opens a merge request: it lands in alice's review queue.
301	bobEnv := inst.gitEnv(bobKey)
302	bobWork := t.TempDir()
303	mustGit(t, bobWork, bobEnv, "clone", inst.sshURL("alice/app"), "w")
304	bobDir := filepath.Join(bobWork, "w")
305	mustGit(t, bobDir, bobEnv, "checkout", "-q", "-b", "fix", "origin/main")
306	os.WriteFile(filepath.Join(bobDir, "b.txt"), []byte("b\n"), 0o644)
307	mustGit(t, bobDir, bobEnv, "add", ".")
308	mustGit(t, bobDir, bobEnv, "commit", "-q", "-m", "the fix")
309	mustGit(t, bobDir, bobEnv, "push", "-q", "origin", "fix")
310	if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
311		"--source", "fix", "--target", "main", "--title", "'needs a look'"); code != 0 {
312		t.Fatalf("mr create: %s", errOut)
313	}
314	// And an issue assigned to alice.
315	if _, errOut, code := inst.ssh(t, bobKey, "", "issue", "create", "alice/app", "--title", "'please handle'"); code != 0 {
316		t.Fatalf("issue create: %s", errOut)
317	}
318	if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "assign", "alice/app", "1", "--add", "alice"); code != 0 {
319		t.Fatalf("assign: %s", errOut)
320	}
321
322	_, body := browserGet(t, inst.login(t, aliceKey), inst.base()+"/")
323	if !strings.Contains(body, "needs a look") {
324		t.Fatalf("review queue missing the MR:\n%s", body)
325	}
326	if !strings.Contains(body, "please handle") {
327		t.Fatalf("assigned issues missing the issue:\n%s", body)
328	}
329	// The feed reports what happened, phrased and linked.
330	for _, want := range []string{"opened merge request", "opened issue", `href="/alice/app/mrs/1"`} {
331		if !strings.Contains(body, want) {
332			t.Fatalf("feed missing %q:\n%s", want, body)
333		}
334	}
335
336	// Once alice reviews, the merge request leaves her queue.
337	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "review", "alice/app", "1", "--approve"); code != 0 {
338		t.Fatalf("review: %s", errOut)
339	}
340	_, after := browserGet(t, inst.login(t, aliceKey), inst.base()+"/")
341	queue := after[strings.Index(after, "Waiting on your review"):strings.Index(after, "Assigned to you")]
342	if strings.Contains(queue, "needs a look") {
343		t.Fatalf("reviewed MR still waiting:\n%s", queue)
344	}
345}