internal/control/notifications_test.go
185 lines · 5528 bytes
1package control
2
3import (
4 "bytes"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/store"
9)
10
11// notifTestCtx opens an in-memory store, migrates it, and creates one user
12// to act as. Modeled on the store setup in snippet_test.go; this package
13// has no shared testCtx helper.
14func notifTestCtx(t *testing.T, username string) *Ctx {
15 t.Helper()
16 st, err := store.Open(":memory:")
17 if err != nil {
18 t.Fatal(err)
19 }
20 t.Cleanup(func() { st.Close() })
21 if err := st.MigrateUp(); err != nil {
22 t.Fatal(err)
23 }
24 uid, err := st.CreateUser(username, false)
25 if err != nil {
26 t.Fatal(err)
27 }
28 var out bytes.Buffer
29 return &Ctx{
30 User: store.User{ID: uid, Username: username},
31 Scope: "full",
32 Store: st,
33 Stdin: strings.NewReader(""),
34 Stdout: &out,
35 Stderr: &out,
36 }
37}
38
39// testRepoWithWatcher returns a Ctx acting as alice, a repository she
40// owns with issue #1 open on it, and bob's user id with a watch row on
41// it — the shared setup for notify's recipient-widening tests.
42func testRepoWithWatcher(t *testing.T) (*Ctx, store.Repo, int64) {
43 t.Helper()
44 c := notifTestCtx(t, "alice")
45 repoID, err := c.Store.CreateRepo("user", c.User.ID, "app", "public")
46 if err != nil {
47 t.Fatal(err)
48 }
49 repo, err := c.Store.RepoByID(repoID)
50 if err != nil {
51 t.Fatal(err)
52 }
53 if _, err := c.Store.CreateIssue(repo.ID, c.User.ID, "title", "", "markdown"); err != nil {
54 t.Fatal(err)
55 }
56 bob, err := c.Store.CreateUser("bob", false)
57 if err != nil {
58 t.Fatal(err)
59 }
60 if err := c.Store.SetRepoWatch(repo.ID, bob, "watching"); err != nil {
61 t.Fatal(err)
62 }
63 return c, repo, bob
64}
65
66func TestNotifyQueuesPush(t *testing.T) {
67 c, repo, bob := testRepoWithWatcher(t) // alice acts, bob watches
68 c.Store.AddPushDevice(bob, "tok-b", "iphone")
69
70 notify(c, []int64{bob}, notice{repo: repo, kind: "issue",
71 subject: "[alice/app] #1: title",
72 action: "opened issue #1",
73 path: "alice/app/issues/1"})
74
75 due, err := c.Store.DuePush(20)
76 if err != nil {
77 t.Fatalf("DuePush: %v", err)
78 }
79 if len(due) != 1 {
80 t.Fatalf("want one queued push, got %d", len(due))
81 }
82 // The push body is the inbox row's summary, so the two surfaces
83 // cannot disagree about what happened.
84 if due[0].Title != "alice/app" {
85 t.Fatalf("title = %q", due[0].Title)
86 }
87 if due[0].Body != "alice opened issue #1" {
88 t.Fatalf("body = %q", due[0].Body)
89 }
90 if due[0].Path != "alice/app/issues/1" {
91 t.Fatalf("path = %q", due[0].Path)
92 }
93}
94
95func TestNotifyQueuesNoPushForTheActor(t *testing.T) {
96 c, repo, _ := testRepoWithWatcher(t)
97 c.Store.AddPushDevice(c.User.ID, "tok-self", "iphone")
98
99 notify(c, []int64{c.User.ID}, notice{repo: repo, kind: "issue",
100 subject: "s", action: "opened issue #1", path: "alice/app/issues/1"})
101
102 // NotifyRecipients already drops the actor; push inherits that and
103 // must not find its own way around it.
104 if due, _ := c.Store.DuePush(20); len(due) != 0 {
105 t.Fatalf("queued a push to the actor")
106 }
107}
108
109func TestNotificationsDeviceAddReadsStdin(t *testing.T) {
110 c := notifTestCtx(t, "alice")
111 c.Stdin = strings.NewReader("DEVTOKEN\n")
112 if code := runNotificationsDeviceAdd(c, []string{"--label", "iphone"}); code != 0 {
113 t.Fatalf("exit %d", code)
114 }
115 devices, _ := c.Store.PushDevices(c.User.ID)
116 if len(devices) != 1 || devices[0].Token != "DEVTOKEN" {
117 t.Fatalf("got %+v", devices)
118 }
119 if devices[0].Label != "iphone" {
120 t.Fatalf("label = %q", devices[0].Label)
121 }
122}
123
124func TestNotificationsDeviceListTruncatesTheToken(t *testing.T) {
125 c := notifTestCtx(t, "alice")
126 long := strings.Repeat("a", 64)
127 c.Store.AddPushDevice(c.User.ID, long, "iphone")
128 var out bytes.Buffer
129 c.Stdout = &out
130 if code := runNotificationsDeviceList(c, nil); code != 0 {
131 t.Fatalf("exit %d", code)
132 }
133 if strings.Contains(out.String(), long) {
134 t.Fatal("the full token was printed")
135 }
136}
137
138// TestNotificationsDeviceListTruncatesTheTokenJSON is the JSON-path twin
139// of the above: the plain and JSON output share the same rows slice, but
140// nothing enforces that beyond reading the code, so both paths get their
141// own test of the guarantee.
142func TestNotificationsDeviceListTruncatesTheTokenJSON(t *testing.T) {
143 c := notifTestCtx(t, "alice")
144 long := strings.Repeat("a", 64)
145 c.Store.AddPushDevice(c.User.ID, long, "iphone")
146 var out bytes.Buffer
147 c.Stdout, c.JSON = &out, true
148 if code := runNotificationsDeviceList(c, nil); code != 0 {
149 t.Fatalf("exit %d", code)
150 }
151 if strings.Contains(out.String(), long) {
152 t.Fatal("the full token was printed")
153 }
154}
155
156// TestNotificationsDeviceListMasksAShortToken: a token at or under the
157// truncation cut length is not returned unchanged. shortToken's short
158// path used to return the token verbatim, a full echo of anything eight
159// characters or fewer; runNotificationsDeviceAdd enforces no minimum
160// length, so a short token is a value the command will store.
161func TestNotificationsDeviceListMasksAShortToken(t *testing.T) {
162 c := notifTestCtx(t, "alice")
163 short := "abc123"
164 c.Store.AddPushDevice(c.User.ID, short, "iphone")
165 var out bytes.Buffer
166 c.Stdout = &out
167 if code := runNotificationsDeviceList(c, nil); code != 0 {
168 t.Fatalf("exit %d", code)
169 }
170 if strings.Contains(out.String(), short) {
171 t.Fatal("the short token was printed verbatim")
172 }
173}
174
175func TestNotificationsSettingsShowsPush(t *testing.T) {
176 c := notifTestCtx(t, "alice")
177 var out bytes.Buffer
178 c.Stdout, c.JSON = &out, true
179 if code := runNotificationsSettingsShow(c, nil); code != 0 {
180 t.Fatalf("exit %d", code)
181 }
182 if !strings.Contains(out.String(), `"push":true`) {
183 t.Fatalf("no push key: %s", out.String())
184 }
185}