internal/control/stage3notify_test.go
101 lines · 3828 bytes
4 symbols in this file
1package control
2
3import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "strings"
8 "testing"
9
10 "gitbay.org/gitbay/internal/protocol"
11 "gitbay.org/gitbay/internal/store"
12)
13
14// notifyFixture is a user with an unread and a read notification, a push
15// device, and a repository with a webhook and one delivery.
16func notifyFixture(t *testing.T) (*store.Store, store.Repo, store.User) {
17 t.Helper()
18 st, repo, uid := newQueueTestRepo(t)
19 u := store.User{ID: uid, Username: "alice"}
20 if err := st.AddNotice(uid, repo.ID, "issue", "bob", "opened #3 Crash on start", repo.Path()+"/issues/3"); err != nil {
21 t.Fatal(err)
22 }
23 if err := st.AddNotice(uid, repo.ID, "mr", "bob", "opened !4 Fix crash", repo.Path()+"/mrs/4"); err != nil {
24 t.Fatal(err)
25 }
26 if _, err := st.AddPushDevice(uid, strings.Repeat("f", 64), "phone"); err != nil {
27 t.Fatal(err)
28 }
29 if _, err := st.AddWebhook(repo.ID, "https://hooks.example.test/in/s3cretpath", "whsecret", "issue.open"); err != nil {
30 t.Fatal(err)
31 }
32 st.RecordEvent(repo.ID, uid, "issue.open", `{"number":3}`)
33 return st, repo, u
34}
35
36func TestNotifyPlainPinned(t *testing.T) {
37 st, repo, u := notifyFixture(t)
38 p := repo.Path()
39 for name, argv := range map[string][]string{
40 "notifications-list": {"notifications", "list"},
41 "notifications-settings-show": {"notifications", "settings", "show"},
42 "notifications-device-list": {"notifications", "device", "list"},
43 "webhook-list": {"webhook", "list", p},
44 "webhook-deliveries": {"webhook", "deliveries", p},
45 } {
46 pinPlain(t, name, dispatchAs(t, st, u, "", argv...))
47 }
48 pinPlain(t, "notifications-settings-mail", dispatchAs(t, st, u, "", "notifications", "settings", "mail", "off"))
49}
50
51func TestNotifyScreens(t *testing.T) {
52 st, repo, u := notifyFixture(t)
53 p := repo.Path()
54 for _, tc := range []struct {
55 argv []string
56 want []string
57 }{
58 {[]string{"notifications", "list"}, []string{"Notifications (2)\n", "●", "bob opened !4 Fix crash", "gitbay notifications read --all"}},
59 {[]string{"notifications", "settings", "show"}, []string{"Mail:", "Reply:", "Watch:", "Push:", "gitbay notifications settings mail "}},
60 {[]string{"notifications", "settings", "mail", "off"}, []string{"Mail: off", "gitbay notifications settings mail on"}},
61 {[]string{"notifications", "device", "list"}, []string{"Push devices (1)\n", "phone"}},
62 {[]string{"webhook", "list", p}, []string{"Webhooks (1)\n", "https://hooks.example.test/…", "issue.open · signed"}},
63 {[]string{"webhook", "deliveries", p}, []string{"Deliveries (1)\n", "◐", "issue.open"}},
64 } {
65 out := atTerminal(t, st, u, tc.argv...)
66 for _, w := range tc.want {
67 if !strings.Contains(out, w) {
68 t.Errorf("%v: missing %q in:\n%s", tc.argv, w, out)
69 }
70 }
71 for _, secret := range []string{"whsecret", "s3cretpath", strings.Repeat("f", 64)} {
72 if strings.Contains(out, secret) {
73 t.Errorf("%v: %q on screen:\n%s", tc.argv, secret, out)
74 }
75 }
76 checkLegend(t, out)
77 }
78}
79
80// A client shows the reply switch only where the instance reads replies,
81// as the account page does; turning it on is refused elsewhere.
82func TestNotificationSettingsSayWhetherRepliesAreRead(t *testing.T) {
83 st, _, u := notifyFixture(t)
84 for _, enabled := range []bool{false, true} {
85 var out bytes.Buffer
86 c := &Ctx{User: u, Scope: "full", Store: st, Stdout: &out, Stderr: io.Discard, JSON: true}
87 c.Cfg.Mail.Inbound.Enabled = enabled
88 if code := Dispatch(c, []string{"notifications", "settings", "show"}); code != protocol.ExitOK {
89 t.Fatalf("exit %d", code)
90 }
91 var env struct {
92 Data map[string]bool `json:"data"`
93 }
94 if err := json.Unmarshal(out.Bytes(), &env); err != nil {
95 t.Fatal(err)
96 }
97 if got, ok := env.Data["reply_available"]; !ok || got != enabled {
98 t.Errorf("inbound %v: reply_available %v (present %v)", enabled, got, ok)
99 }
100 }
101}