internal/store/push_test.go

b81a25efa5dd60e3379bb1f0efaf6548f244e26c
gitbay/internal/store/push_test.go

188 lines · 5150 bytes

  1package store
  2
  3import (
  4	"testing"
  5	"time"
  6)
  7
  8func pushFixture(t *testing.T) *Store {
  9	t.Helper()
 10	s := open(t)
 11	if err := s.MigrateUp(); err != nil {
 12		t.Fatal(err)
 13	}
 14	return s
 15}
 16
 17func TestPushDevices(t *testing.T) {
 18	s := pushFixture(t)
 19	uid, err := s.CreateUser("alice", false)
 20	if err != nil {
 21		t.Fatal(err)
 22	}
 23
 24	firstID, err := s.AddPushDevice(uid, "tok-a", "iphone")
 25	if err != nil {
 26		t.Fatalf("AddPushDevice: %v", err)
 27	}
 28	devices, err := s.PushDevices(uid)
 29	if err != nil {
 30		t.Fatalf("PushDevices: %v", err)
 31	}
 32	if len(devices) != 1 || devices[0].Token != "tok-a" || devices[0].Label != "iphone" {
 33		t.Fatalf("got %+v", devices)
 34	}
 35	if firstID != devices[0].ID {
 36		t.Fatalf("AddPushDevice returned %d, row id is %d", firstID, devices[0].ID)
 37	}
 38
 39	// Apple reuses tokens: re-registering updates the label and the owner
 40	// rather than erroring, so a reinstall under another account works. The
 41	// returned id must be the existing row's, not an unrelated rowid left
 42	// over from SQLite's last real INSERT (the DO UPDATE arm does not
 43	// advance last_insert_rowid()).
 44	bob, err := s.CreateUser("bob", false)
 45	if err != nil {
 46		t.Fatal(err)
 47	}
 48	reregID, err := s.AddPushDevice(bob, "tok-a", "ipad")
 49	if err != nil {
 50		t.Fatalf("re-register: %v", err)
 51	}
 52	if d, _ := s.PushDevices(uid); len(d) != 0 {
 53		t.Fatalf("token still owned by alice: %+v", d)
 54	}
 55	d, _ := s.PushDevices(bob)
 56	if len(d) != 1 || d[0].Label != "ipad" {
 57		t.Fatalf("got %+v", d)
 58	}
 59	if reregID != d[0].ID {
 60		t.Fatalf("re-register returned %d, existing row id is %d", reregID, d[0].ID)
 61	}
 62	if reregID != firstID {
 63		t.Fatalf("re-register returned %d, want the reused row's original id %d", reregID, firstID)
 64	}
 65
 66	// Removal is scoped to the owner: alice cannot remove bob's device.
 67	if err := s.RemovePushDevice(uid, d[0].ID); err != ErrNotFound {
 68		t.Fatalf("cross-account remove: got %v, want ErrNotFound", err)
 69	}
 70	if err := s.RemovePushDevice(bob, d[0].ID); err != nil {
 71		t.Fatalf("RemovePushDevice: %v", err)
 72	}
 73	if d, _ := s.PushDevices(bob); len(d) != 0 {
 74		t.Fatalf("device survived removal: %+v", d)
 75	}
 76}
 77
 78func TestPushEnabledDefaultsOn(t *testing.T) {
 79	s := pushFixture(t)
 80	uid, err := s.CreateUser("alice", false)
 81	if err != nil {
 82		t.Fatal(err)
 83	}
 84	on, err := s.PushEnabled(uid)
 85	if err != nil {
 86		t.Fatalf("PushEnabled: %v", err)
 87	}
 88	if !on {
 89		t.Fatal("notify_push should default on")
 90	}
 91	if err := s.SetPushEnabled(uid, false); err != nil {
 92		t.Fatalf("SetPushEnabled: %v", err)
 93	}
 94	if on, _ := s.PushEnabled(uid); on {
 95		t.Fatal("SetPushEnabled(false) did not stick")
 96	}
 97}
 98
 99func TestEnqueuePush(t *testing.T) {
100	s := pushFixture(t)
101	uid, err := s.CreateUser("alice", false)
102	if err != nil {
103		t.Fatal(err)
104	}
105	s.AddPushDevice(uid, "tok-a", "iphone")
106	s.AddPushDevice(uid, "tok-b", "ipad")
107
108	// One row per device, so a retry to the phone does not resend to the
109	// iPad.
110	if err := s.EnqueuePush(uid, "krz/gitbay", "cmc opened issue #12", "krz/gitbay/issues/12"); err != nil {
111		t.Fatalf("EnqueuePush: %v", err)
112	}
113	due, err := s.DuePush(20)
114	if err != nil {
115		t.Fatalf("DuePush: %v", err)
116	}
117	if len(due) != 2 {
118		t.Fatalf("want a row per device, got %d", len(due))
119	}
120	if due[0].Token == "" || due[0].Body != "cmc opened issue #12" {
121		t.Fatalf("got %+v", due[0])
122	}
123
124	// Sent rows stop being due.
125	if err := s.MarkPushSent(due[0].ID); err != nil {
126		t.Fatalf("MarkPushSent: %v", err)
127	}
128	if due, _ := s.DuePush(20); len(due) != 1 {
129		t.Fatalf("sent row still due")
130	}
131
132	// A failure with a next attempt in the future is not due yet.
133	next := time.Now().Add(time.Hour)
134	if err := s.MarkPushFailed(due[1].ID, "503", &next); err != nil {
135		t.Fatalf("MarkPushFailed: %v", err)
136	}
137	if due, _ := s.DuePush(20); len(due) != 0 {
138		t.Fatalf("backed-off row is due too early")
139	}
140}
141
142func TestEnqueuePushRespectsSettingAndDevices(t *testing.T) {
143	s := pushFixture(t)
144	uid, err := s.CreateUser("alice", false)
145	if err != nil {
146		t.Fatal(err)
147	}
148
149	// No devices: nothing queued, no error.
150	if err := s.EnqueuePush(uid, "t", "b", "p"); err != nil {
151		t.Fatalf("EnqueuePush with no devices: %v", err)
152	}
153	if due, _ := s.DuePush(20); len(due) != 0 {
154		t.Fatalf("queued for an account with no devices")
155	}
156
157	// Setting off: nothing queued.
158	s.AddPushDevice(uid, "tok-a", "iphone")
159	s.SetPushEnabled(uid, false)
160	if err := s.EnqueuePush(uid, "t", "b", "p"); err != nil {
161		t.Fatalf("EnqueuePush with push off: %v", err)
162	}
163	if due, _ := s.DuePush(20); len(due) != 0 {
164		t.Fatalf("queued with notify_push off")
165	}
166}
167
168func TestDeletePushDeviceByTokenTakesItsQueue(t *testing.T) {
169	s := pushFixture(t)
170	uid, err := s.CreateUser("alice", false)
171	if err != nil {
172		t.Fatal(err)
173	}
174	s.AddPushDevice(uid, "tok-a", "iphone")
175	s.EnqueuePush(uid, "t", "b", "p")
176
177	if err := s.DeletePushDeviceByToken("tok-a"); err != nil {
178		t.Fatalf("DeletePushDeviceByToken: %v", err)
179	}
180	if d, _ := s.PushDevices(uid); len(d) != 0 {
181		t.Fatalf("device survived")
182	}
183	// push_queue.device_id is ON DELETE CASCADE, so the queued rows go
184	// with it rather than being retried at a dead token forever.
185	if due, _ := s.DuePush(20); len(due) != 0 {
186		t.Fatalf("queued rows outlived their device")
187	}
188}