e2e/dashboard_test.go

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

408 lines · 16590 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, "A git forge you drive from the terminal") {
 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		`aria-label="Pinned repositories"`, `class="pins"`, `</span>app</a>`, // the dashboard lists the pinned repo
 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		"Your MRs across all repos.",
101	} {
102		if !strings.Contains(body, want) {
103			t.Errorf("dashboard missing %q", want)
104		}
105	}
106
107	// The empty queue is a tile with a zero; a populated one is a tile
108	// that links to its rows, which render in the middle column.
109	if !strings.Contains(body, `<div class="tile"><b>0</b><span>assigned to you</span></div>`) {
110		t.Fatalf("dashboard missing the zero tile for assigned:\n%s", body)
111	}
112	if !strings.Contains(body, `<a class="tile wants" href="#reviews"><b>1</b><span>waiting on your review</span></a>`) {
113		t.Fatalf("dashboard missing the review tile:\n%s", body)
114	}
115	if !strings.Contains(body, `<h2 id="reviews">Waiting on your review`) || strings.Contains(body, `<h2 class="empty">`) {
116		t.Fatalf("queues do not render as tiles plus rows:\n%s", body)
117	}
118
119	// The diff has its own view rather than a fold at the foot of the
120	// conversation: the default view offers it, and asking for it renders
121	// the stat line and the patch.
122	_, body = inst.get(t, "/alice/app/mrs/1")
123	if !strings.Contains(body, `href="/alice/app/mrs/1?view=diff"`) {
124		t.Fatal("merge request missing the files-changed view")
125	}
126	if strings.Contains(body, `class="difftable"`) {
127		t.Fatal("diff rendered on the conversation view")
128	}
129	_, body = inst.get(t, "/alice/app/mrs/1?view=diff")
130	if !strings.Contains(body, "1 file changed") || !strings.Contains(body, `class="difftable"`) {
131		t.Fatalf("diff view missing stat or patch:\n%s", body)
132	}
133}
134
135// The dashboard control command returns the same aggregate as the web
136// dashboard — review queue, assigned/open work, pins, and activity — in
137// one read. Builds remain for clients that already consume them.
138func TestDashboardCommand(t *testing.T) {
139	inst := startInstance(t)
140	aliceKey := inst.newKey(t, "alice")
141	bobKey := inst.newKey(t, "bob")
142	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
143	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
144
145	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
146		t.Fatalf("repo create: %s", errOut)
147	}
148	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/app", "bob", "write"); code != 0 {
149		t.Fatal("grant failed")
150	}
151	work := t.TempDir()
152	env := inst.gitEnv(aliceKey)
153	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
154	dir := filepath.Join(work, "w")
155	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
156	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
157		"jobs:\n  test:\n    steps:\n      - echo ok\n"), 0o644)
158	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
159	mustGit(t, dir, env, "add", ".")
160	mustGit(t, dir, env, "commit", "-q", "-m", "base")
161	mustGit(t, dir, env, "push", "-q", "origin", "main")
162	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
163	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
164	mustGit(t, dir, env, "add", ".")
165	mustGit(t, dir, env, "commit", "-q", "-m", "feat")
166	mustGit(t, dir, env, "push", "-q", "origin", "feat")
167
168	if _, _, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
169		"--source", "feat", "--target", "main", "--title", "'from bob'"); code != 0 {
170		t.Fatal("mr create failed")
171	}
172	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "'todo one'"); code != 0 {
173		t.Fatal("issue create failed")
174	}
175	if _, _, code := inst.ssh(t, aliceKey, "", "issue", "assign", "alice/app", "1", "--add", "alice"); code != 0 {
176		t.Fatal("assign failed")
177	}
178	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "pin", "alice/app"); code != 0 {
179		t.Fatal("pin failed")
180	}
181	if _, _, code := inst.ssh(t, bobKey, "", "repo", "pin", "alice/app"); code != 0 {
182		t.Fatal("bob pin failed")
183	}
184
185	out, errOut, code := inst.ssh(t, aliceKey, "", "dashboard", "--json")
186	if code != 0 {
187		t.Fatalf("dashboard: %s", errOut)
188	}
189	var env2 struct {
190		Data struct {
191			Reviews []struct {
192				Repo   string `json:"repo"`
193				Number int64  `json:"number"`
194				Title  string `json:"title"`
195			} `json:"review_queue"`
196			Pinned []struct {
197				Path string `json:"path"`
198			} `json:"pinned"`
199			MRs []struct {
200				Repo   string `json:"repo"`
201				Number int64  `json:"number"`
202				Title  string `json:"title"`
203				Author string `json:"author"`
204				State  string `json:"state"`
205			} `json:"open_mrs"`
206			Assigned []struct {
207				Repo   string `json:"repo"`
208				Number int64  `json:"number"`
209				Title  string `json:"title"`
210			} `json:"assigned_issues"`
211			Issues []struct {
212				Repo   string `json:"repo"`
213				Number int64  `json:"number"`
214				Title  string `json:"title"`
215			} `json:"open_issues"`
216			Activity []struct {
217				Repo  string `json:"repo"`
218				Actor string `json:"actor"`
219				Kind  string `json:"kind"`
220			} `json:"recent_activity"`
221			Builds []struct {
222				Repo   string `json:"repo"`
223				Job    string `json:"job"`
224				Status string `json:"status"`
225				Ref    string `json:"ref"`
226			} `json:"builds"`
227		} `json:"data"`
228	}
229	if err := json.Unmarshal([]byte(out), &env2); err != nil {
230		t.Fatalf("bad json: %v\n%s", err, out)
231	}
232	d := env2.Data
233	if len(d.Pinned) != 1 || d.Pinned[0].Path != "alice/app" {
234		t.Fatalf("pinned = %+v", d.Pinned)
235	}
236	if len(d.MRs) != 1 || d.MRs[0].Repo != "alice/app" || d.MRs[0].Number != 1 ||
237		d.MRs[0].Title != "from bob" || d.MRs[0].Author != "bob" || d.MRs[0].State != "open" {
238		t.Fatalf("open_mrs = %+v", d.MRs)
239	}
240	if len(d.Reviews) != 1 || d.Reviews[0].Repo != "alice/app" || d.Reviews[0].Number != 1 ||
241		d.Reviews[0].Title != "from bob" {
242		t.Fatalf("review_queue = %+v", d.Reviews)
243	}
244	if len(d.Assigned) != 1 || d.Assigned[0].Repo != "alice/app" || d.Assigned[0].Number != 1 ||
245		d.Assigned[0].Title != "todo one" {
246		t.Fatalf("assigned_issues = %+v", d.Assigned)
247	}
248	if len(d.Issues) != 1 || d.Issues[0].Repo != "alice/app" || d.Issues[0].Number != 1 ||
249		d.Issues[0].Title != "todo one" {
250		t.Fatalf("open_issues = %+v", d.Issues)
251	}
252	if len(d.Activity) == 0 || d.Activity[0].Repo != "alice/app" {
253		t.Fatalf("recent_activity = %+v", d.Activity)
254	}
255	// Both pushes hit main and feat; each queues the ci.yml job.
256	if len(d.Builds) == 0 || d.Builds[0].Repo != "alice/app" || d.Builds[0].Job != "test" ||
257		d.Builds[0].Status != "pending" {
258		t.Fatalf("builds = %+v", d.Builds)
259	}
260
261	// Bob is not assigned and pinned repos he can no longer read disappear.
262	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "settings", "visibility", "alice/app", "private"); code != 0 {
263		t.Fatal("visibility failed")
264	}
265	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "revoke", "alice/app", "bob"); code != 0 {
266		t.Fatal("revoke failed")
267	}
268	out, _, code = inst.ssh(t, bobKey, "", "dashboard", "--json")
269	if code != 0 {
270		t.Fatal("bob dashboard failed")
271	}
272	if err := json.Unmarshal([]byte(out), &env2); err != nil {
273		t.Fatalf("bad json: %v\n%s", err, out)
274	}
275	if len(env2.Data.Pinned) != 0 {
276		t.Fatalf("bob still sees pinned = %+v", env2.Data.Pinned)
277	}
278	if len(env2.Data.Assigned) != 0 {
279		t.Fatalf("bob assigned = %+v", env2.Data.Assigned)
280	}
281	if len(env2.Data.Builds) != 0 {
282		t.Fatalf("bob builds = %+v", env2.Data.Builds)
283	}
284}
285
286// TestDashboardQueues covers the parts of the dashboard that answer "what
287// needs me": the review queue, assigned issues, and the activity feed.
288func TestDashboardQueues(t *testing.T) {
289	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
290	aliceKey := inst.newKey(t, "alice")
291	bobKey := inst.newKey(t, "bob")
292	inst.admin(t, "admin", "user", "create", "alice",
293		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
294	inst.admin(t, "admin", "user", "create", "bob",
295		"--key", bobKey+".pub", "--email", "bob@example.test", "--verified")
296
297	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
298		t.Fatalf("repo create: %s", errOut)
299	}
300	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/app", "bob", "write"); code != 0 {
301		t.Fatalf("grant: %s", errOut)
302	}
303	env := inst.gitEnv(aliceKey)
304	work := t.TempDir()
305	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
306	dir := filepath.Join(work, "w")
307	os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a\n"), 0o644)
308	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
309	mustGit(t, dir, env, "add", ".")
310	mustGit(t, dir, env, "commit", "-q", "-m", "base")
311	mustGit(t, dir, env, "push", "-q", "origin", "main")
312
313	// Bob opens a merge request: it lands in alice's review queue.
314	bobEnv := inst.gitEnv(bobKey)
315	bobWork := t.TempDir()
316	mustGit(t, bobWork, bobEnv, "clone", inst.sshURL("alice/app"), "w")
317	bobDir := filepath.Join(bobWork, "w")
318	mustGit(t, bobDir, bobEnv, "checkout", "-q", "-b", "fix", "origin/main")
319	os.WriteFile(filepath.Join(bobDir, "b.txt"), []byte("b\n"), 0o644)
320	mustGit(t, bobDir, bobEnv, "add", ".")
321	mustGit(t, bobDir, bobEnv, "commit", "-q", "-m", "the fix")
322	mustGit(t, bobDir, bobEnv, "push", "-q", "origin", "fix")
323	if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
324		"--source", "fix", "--target", "main", "--title", "'needs a look'"); code != 0 {
325		t.Fatalf("mr create: %s", errOut)
326	}
327	// And an issue assigned to alice.
328	if _, errOut, code := inst.ssh(t, bobKey, "", "issue", "create", "alice/app", "--title", "'please handle'"); code != 0 {
329		t.Fatalf("issue create: %s", errOut)
330	}
331	if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "assign", "alice/app", "1", "--add", "alice"); code != 0 {
332		t.Fatalf("assign: %s", errOut)
333	}
334
335	_, body := browserGet(t, inst.login(t, aliceKey), inst.base()+"/")
336	if !strings.Contains(body, "needs a look") {
337		t.Fatalf("review queue missing the MR:\n%s", body)
338	}
339	if !strings.Contains(body, "please handle") {
340		t.Fatalf("assigned issues missing the issue:\n%s", body)
341	}
342	// The feed reports what happened, phrased and linked.
343	for _, want := range []string{"opened merge request", "opened issue", `href="/alice/app/mrs/1"`} {
344		if !strings.Contains(body, want) {
345			t.Fatalf("feed missing %q:\n%s", want, body)
346		}
347	}
348
349	// Once alice reviews, the merge request leaves her queue.
350	if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "review", "alice/app", "1", "--approve"); code != 0 {
351		t.Fatalf("review: %s", errOut)
352	}
353	_, after := browserGet(t, inst.login(t, aliceKey), inst.base()+"/")
354	if !strings.Contains(after, `<div class="tile"><b>0</b><span>waiting on your review</span></div>`) {
355		t.Fatalf("reviewed MR still waiting:\n%s", after)
356	}
357}
358
359// D04/D05: two jobs on one commit fold into a single feed line, shown
360// with a relative time carrying the exact UTC time in its title. The
361// folded line's state is the worse of the two, which the feed no longer
362// draws; TestFeedLinesFoldsBuildRun covers that rule.
363func TestDashboardFeedFoldsBuildRun(t *testing.T) {
364	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
365	inst.runner = buildRunner(t)
366	aliceKey := inst.newKey(t, "alice")
367	inst.admin(t, "admin", "user", "create", "alice",
368		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
369	runnerKey := inst.newKey(t, "ci")
370	inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin")
371
372	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
373		t.Fatalf("repo create: %s", errOut)
374	}
375	work := t.TempDir()
376	env := inst.gitEnv(aliceKey)
377	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
378	dir := filepath.Join(work, "w")
379	os.MkdirAll(filepath.Join(dir, ".gitbay"), 0o755)
380	os.WriteFile(filepath.Join(dir, ".gitbay", "ci.yml"), []byte(
381		"jobs:\n  ok:\n    steps:\n      - echo fine\n  broken:\n    steps:\n      - \"false\"\n"), 0o644)
382	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
383	mustGit(t, dir, env, "add", ".")
384	mustGit(t, dir, env, "commit", "-q", "-m", "base")
385	mustGit(t, dir, env, "push", "-q", "origin", "main")
386	sha := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD"))
387
388	// The runner processes both jobs ("broken" sorts first).
389	inst.runnerOnce(t, runnerKey)
390	inst.runnerOnce(t, runnerKey)
391
392	_, body := browserGet(t, inst.login(t, aliceKey), inst.base()+"/")
393	if !strings.Contains(body, "ran 2 jobs on") {
394		t.Fatalf("feed did not fold the two jobs into one run:\n%s", body)
395	}
396	if !strings.Contains(body, sha[:10]) {
397		t.Fatalf("feed missing the short sha %q:\n%s", sha[:10], body)
398	}
399	// A build reported moments ago renders as "just now"; ago() only
400	// switches to "N ago" past a minute, so either form proves the
401	// relative-time rendering rather than the raw timestamp.
402	if !strings.Contains(body, ">just now<") && !strings.Contains(body, " ago<") {
403		t.Fatalf("feed missing a relative time:\n%s", body)
404	}
405	if !strings.Contains(body, "title=\"") || !strings.Contains(body, " UTC\"") {
406		t.Fatalf("feed missing the exact time in a title:\n%s", body)
407	}
408}