e2e/dashboard_test.go

6b01ef3788b21e3a4c027a2afe7e7a5bda9ca000
gitbay/e2e/dashboard_test.go history · blame · raw

112 lines · 4119 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, `logged in as <a href="/alice">alice</a>`) {
 93		t.Fatalf("dashboard: %d", status)
 94	}
 95	for _, want := range []string{
 96		">pinned</h2>", ">app<", // pinned card
 97		"alice/app!1 from bob", "alice/app#1 todo one",
 98		`href="/alice/app/mrs/1"`, `href="/alice/app/issues/1"`,
 99	} {
100		if !strings.Contains(body, want) {
101			t.Errorf("dashboard missing %q", want)
102		}
103	}
104
105	// The MR diff is collapsed by default in a <details> with stats.
106	_, body = inst.get(t, "/alice/app/mrs/1")
107	if !strings.Contains(body, `<details class="difffold">`) ||
108		strings.Contains(body, `<details class="difffold" open`) ||
109		!strings.Contains(body, "1 file changed") {
110		t.Fatal("diff not collapsed with stats")
111	}
112}