internal/control/dashboard_test.go

v1.40.0
gitbay/internal/control/dashboard_test.go history · blame · raw

117 lines · 3609 bytes

  1package control
  2
  3import (
  4	"bytes"
  5	"strings"
  6	"testing"
  7
  8	"gitbay.org/gitbay/internal/protocol"
  9	"gitbay.org/gitbay/internal/store"
 10)
 11
 12func TestDashboardEmptySectionsSayNone(t *testing.T) {
 13	st, _, uid := newQueueTestRepo(t)
 14	c, errOut := pruneCtx(st, t.TempDir(), store.User{ID: uid})
 15	if code := Dispatch(c, []string{"dashboard"}); code != protocol.ExitOK {
 16		t.Fatalf("exit %d: %s", code, errOut)
 17	}
 18	out := c.Stdout.(*bytes.Buffer).String()
 19	for _, h := range []string{"waiting on your review:", "assigned to you:", "open merge requests:", "open issues:"} {
 20		if !strings.Contains(out, h+"\n  none\n") {
 21			t.Errorf("%q not followed by none:\n%s", h, out)
 22		}
 23	}
 24}
 25
 26func TestDashboardActivityIsASentence(t *testing.T) {
 27	c := notifTestCtx(t, "cmc")
 28	repoID, err := c.Store.CreateRepo("user", c.User.ID, "gitbay", "public")
 29	if err != nil {
 30		t.Fatal(err)
 31	}
 32	repo, err := c.Store.RepoByID(repoID)
 33	if err != nil {
 34		t.Fatal(err)
 35	}
 36	c.Store.RecordEvent(repo.ID, c.User.ID, "issue.labeled", `{"number":262,"labels":["ops","security"]}`)
 37
 38	var out bytes.Buffer
 39	c.Stdout, c.Stderr = &out, &out
 40	if code := runDashboard(c, nil); code != 0 {
 41		t.Fatalf("exit %d: %s", code, out.String())
 42	}
 43	if strings.Contains(out.String(), `{"number"`) {
 44		t.Errorf("raw payload leaked into plain output:\n%s", out.String())
 45	}
 46	if !strings.Contains(out.String(), "cmc labelled cmc/gitbay#262 ops, security") {
 47		t.Errorf("no sentence in output:\n%s", out.String())
 48	}
 49}
 50
 51func TestFeedIsASentence(t *testing.T) {
 52	c := notifTestCtx(t, "cmc")
 53	repoID, err := c.Store.CreateRepo("user", c.User.ID, "gitbay", "public")
 54	if err != nil {
 55		t.Fatal(err)
 56	}
 57	repo, err := c.Store.RepoByID(repoID)
 58	if err != nil {
 59		t.Fatal(err)
 60	}
 61	c.Store.RecordEvent(repo.ID, c.User.ID, "issue.created", `{"number":1}`)
 62
 63	var out bytes.Buffer
 64	c.Stdout, c.Stderr = &out, &out
 65	if code := runFeed(c, nil); code != 0 {
 66		t.Fatalf("exit %d: %s", code, out.String())
 67	}
 68	if !strings.Contains(out.String(), "cmc opened issue cmc/gitbay#1") {
 69		t.Errorf("no sentence in output:\n%s", out.String())
 70	}
 71}
 72
 73// The push queue is the one worker queue whose worst failure — a key_id
 74// or team_id Apple did not issue, which config validation cannot check —
 75// dead-letters every row on its first attempt with nothing but a log
 76// line. dashboard is where an admin would see that, so it reports push
 77// beside the other five queues.
 78func TestDashboardReportsThePushQueue(t *testing.T) {
 79	c := notifTestCtx(t, "cmc")
 80	c.User.IsAdmin = true
 81	uid := c.User.ID
 82	if _, err := c.Store.AddPushDevice(uid, "tok-a", "iphone"); err != nil {
 83		t.Fatal(err)
 84	}
 85	if err := c.Store.EnqueuePush(uid, "krz/gitbay", "cmc opened issue #1", "krz/gitbay/issues/1"); err != nil {
 86		t.Fatal(err)
 87	}
 88	due, err := c.Store.DuePush(20)
 89	if err != nil || len(due) != 1 {
 90		t.Fatalf("DuePush: %v %+v", err, due)
 91	}
 92	if err := c.Store.MarkPushFailed(due[0].ID, "apns 403 InvalidProviderToken", nil); err != nil {
 93		t.Fatal(err)
 94	}
 95
 96	var out bytes.Buffer
 97	c.Stdout, c.Stderr = &out, &out
 98	if code := runDashboard(c, nil); code != 0 {
 99		t.Fatalf("exit %d: %s", code, out.String())
100	}
101	got := out.String()
102	if !strings.Contains(got, "push\tpending 0\tretrying 0\tfailed 1") {
103		t.Fatalf("no push queue row:\n%s", got)
104	}
105	if !strings.Contains(got, "apns 403 InvalidProviderToken") {
106		t.Fatalf("dead-lettered row not listed:\n%s", got)
107	}
108
109	out.Reset()
110	c.JSON = true
111	if code := runDashboard(c, nil); code != 0 {
112		t.Fatalf("exit %d: %s", code, out.String())
113	}
114	if !strings.Contains(out.String(), `"push":{`) {
115		t.Fatalf("no push key in the queues object:\n%s", out.String())
116	}
117}