internal/control/stage3notify_test.go

v1.43.0
gitbay/internal/control/stage3notify_test.go history · blame · raw

74 lines · 2887 bytes

 1package control
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gitbay.org/gitbay/internal/store"
 8)
 9
10// notifyFixture is a user with an unread and a read notification, a push
11// device, and a repository with a webhook and one delivery.
12func notifyFixture(t *testing.T) (*store.Store, store.Repo, store.User) {
13	t.Helper()
14	st, repo, uid := newQueueTestRepo(t)
15	u := store.User{ID: uid, Username: "alice"}
16	if err := st.AddNotice(uid, repo.ID, "issue", "bob", "opened #3 Crash on start", repo.Path()+"/issues/3"); err != nil {
17		t.Fatal(err)
18	}
19	if err := st.AddNotice(uid, repo.ID, "mr", "bob", "opened !4 Fix crash", repo.Path()+"/mrs/4"); err != nil {
20		t.Fatal(err)
21	}
22	if _, err := st.AddPushDevice(uid, strings.Repeat("f", 64), "phone"); err != nil {
23		t.Fatal(err)
24	}
25	if _, err := st.AddWebhook(repo.ID, "https://hooks.example.test/in/s3cretpath", "whsecret", "issue.open"); err != nil {
26		t.Fatal(err)
27	}
28	st.RecordEvent(repo.ID, uid, "issue.open", `{"number":3}`)
29	return st, repo, u
30}
31
32func TestNotifyPlainPinned(t *testing.T) {
33	st, repo, u := notifyFixture(t)
34	p := repo.Path()
35	for name, argv := range map[string][]string{
36		"notifications-list":          {"notifications", "list"},
37		"notifications-settings-show": {"notifications", "settings", "show"},
38		"notifications-device-list":   {"notifications", "device", "list"},
39		"webhook-list":                {"webhook", "list", p},
40		"webhook-deliveries":          {"webhook", "deliveries", p},
41	} {
42		pinPlain(t, name, dispatchAs(t, st, u, "", argv...))
43	}
44	pinPlain(t, "notifications-settings-mail", dispatchAs(t, st, u, "", "notifications", "settings", "mail", "off"))
45}
46
47func TestNotifyScreens(t *testing.T) {
48	st, repo, u := notifyFixture(t)
49	p := repo.Path()
50	for _, tc := range []struct {
51		argv []string
52		want []string
53	}{
54		{[]string{"notifications", "list"}, []string{"Notifications (2)\n", "●", "bob opened !4 Fix crash", "gitbay notifications read --all"}},
55		{[]string{"notifications", "settings", "show"}, []string{"Mail:", "Reply:", "Watch:", "Push:", "gitbay notifications settings mail "}},
56		{[]string{"notifications", "settings", "mail", "off"}, []string{"Mail:   off", "gitbay notifications settings mail on"}},
57		{[]string{"notifications", "device", "list"}, []string{"Push devices (1)\n", "phone"}},
58		{[]string{"webhook", "list", p}, []string{"Webhooks (1)\n", "https://hooks.example.test/…", "issue.open · signed"}},
59		{[]string{"webhook", "deliveries", p}, []string{"Deliveries (1)\n", "◐", "issue.open"}},
60	} {
61		out := atTerminal(t, st, u, tc.argv...)
62		for _, w := range tc.want {
63			if !strings.Contains(out, w) {
64				t.Errorf("%v: missing %q in:\n%s", tc.argv, w, out)
65			}
66		}
67		for _, secret := range []string{"whsecret", "s3cretpath", strings.Repeat("f", 64)} {
68			if strings.Contains(out, secret) {
69				t.Errorf("%v: %q on screen:\n%s", tc.argv, secret, out)
70			}
71		}
72		checkLegend(t, out)
73	}
74}