| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/push" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | func TestNotificationPayloadPrivacyAndOptIn(t *testing.T) { |
| 13 | for _, mode := range []string{"text", "options", "clipped", "unknown", "disabled", "closed", "malformed"} { |
| 14 | t.Run(mode, func(t *testing.T) { |
| 15 | r := newPushRig(t) |
| 16 | r.s.SetNotificationActions(true) |
| 17 | r.register(hkOwner, "cr_phone", wire.RegisterPush{Token: pushDevice, Environment: wire.APNsProduction, Platform: wire.PlatformIOS, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"}) |
| 18 | r.register(hkOwner, "cr_mac", wire.RegisterPush{Token: pushMac, Environment: wire.APNsProduction, Platform: wire.PlatformMacOS}) |
| 19 | c := r.card("notification-card", wire.CardOpen) |
| 20 | c.Options = "[]" |
| 21 | switch mode { |
| 22 | case "options": |
| 23 | c.Options = `["PRIVATE option not in the alert"]` |
| 24 | case "clipped": |
| 25 | c.Question = strings.Repeat("Question ", 100) |
| 26 | case "unknown": |
| 27 | c.Kind = "future" |
| 28 | case "disabled": |
| 29 | r.s.SetNotificationActions(false) |
| 30 | case "closed": |
| 31 | c.State = wire.CardAnswered |
| 32 | case "malformed": |
| 33 | c.Options = `{"bad":"options"}` |
| 34 | } |
| 35 | r.must(r.s.db.G.Save(&c).Error) |
| 36 | r.clear() |
| 37 | in := push.Intent{Kind: push.KindAlert, Body: push.Clip(c.Question), Category: push.CategoryNeedsYou, Data: wire.PushData{Kind: wire.PushCard, CardID: c.ID}} |
| 38 | r.must(r.tx(func(tx *txn) error { |
| 39 | devices, err := push.Device(tx.db, hkOwner, "cr_phone") |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | legacy, err := push.Device(tx.db, hkOwner, "cr_mac") |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | _, err = tx.enqueuePush(hkOwner, append(devices, legacy...), in) |
| 48 | return err |
| 49 | })) |
| 50 | rows := r.outbox() |
| 51 | if len(rows) != 2 { |
| 52 | t.Fatal(len(rows)) |
| 53 | } |
| 54 | for _, row := range rows { |
| 55 | var payload struct { |
| 56 | APS struct { |
| 57 | Category string |
| 58 | Alert struct{ Body string } |
| 59 | } |
| 60 | Equipo wire.PushData |
| 61 | } |
| 62 | r.must(json.Unmarshal([]byte(row.Payload), &payload)) |
| 63 | if payload.APS.Alert.Body != in.Body || strings.Contains(row.Payload, "PRIVATE") || len(row.Payload) > push.MaxPayload { |
| 64 | t.Fatal("changed preview or leaked options", mode) |
| 65 | } |
| 66 | active := row.Credential == "cr_phone" && (mode == "text" || mode == "options" || mode == "clipped") |
| 67 | if !active { |
| 68 | if payload.Equipo.Actions != nil || payload.APS.Category != push.CategoryNeedsYou { |
| 69 | t.Fatal("not open-only", mode, row.Credential) |
| 70 | } |
| 71 | continue |
| 72 | } |
| 73 | a := payload.Equipo.Actions |
| 74 | if a == nil || a.Version != 1 || a.BindingID != "12345678-1234-4234-8234-123456789abc" || a.CardRevision != c.Revision || a.CardKind != c.Kind || a.NotificationID == "" { |
| 75 | t.Fatal("missing binding", a) |
| 76 | } |
| 77 | want := "equipo.question.choices.v1" |
| 78 | if mode == "text" { |
| 79 | want = "equipo.question.text.v1" |
| 80 | } |
| 81 | if payload.APS.Category != want { |
| 82 | t.Fatal(payload.APS.Category, want) |
| 83 | } |
| 84 | } |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestNotificationRegistrationGates(t *testing.T) { |
| 90 | r := newPushRig(t) |
| 91 | in := wire.RegisterPush{Token: pushDevice, Environment: wire.APNsProduction, Platform: wire.PlatformIOS, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"} |
| 92 | if _, err := r.s.RegisterPush(r.ctx, hkOwner, "cr_phone", in); err == nil { |
| 93 | t.Fatal("off by default") |
| 94 | } |
| 95 | r.s.SetNotificationActions(true) |
| 96 | for _, mode := range []string{"version", "missing", "invalid", "unexpected"} { |
| 97 | bad := in |
| 98 | switch mode { |
| 99 | case "version": |
| 100 | bad.ActionsVersion = 2 |
| 101 | case "missing": |
| 102 | bad.BindingID = "" |
| 103 | case "invalid": |
| 104 | bad.BindingID = "https://untrusted.invalid" |
| 105 | case "unexpected": |
| 106 | bad.ActionsVersion = 0 |
| 107 | } |
| 108 | if _, err := r.s.RegisterPush(r.ctx, hkOwner, "cr_phone", bad); err == nil { |
| 109 | t.Fatal(mode) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestNotificationRetryPayloadBindsOnlyTheCurrentFailure(t *testing.T) { |
| 115 | for _, mode := range []string{"valid", "creator", "done", "cancel", "copy", "moving", "running", "unterminated", "revision", "attempt"} { |
| 116 | t.Run(mode, func(t *testing.T) { |
| 117 | r := newPushRig(t) |
| 118 | r.s.SetNotificationActions(true) |
| 119 | k := r.task("tk-bind", "Fix", hkOwner) |
| 120 | r.step(&k, "st-bind", wire.SpecialistEngineering, wire.StepFailed) |
| 121 | k = r.load(k.ID) |
| 122 | device := push.Registration{Person: hkOwner, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"} |
| 123 | switch mode { |
| 124 | case "creator": |
| 125 | device.Person = "other@example.com" |
| 126 | case "done": |
| 127 | k.Stage = wire.StageDone |
| 128 | case "cancel": |
| 129 | k.CancelRequested = true |
| 130 | case "copy": |
| 131 | k.CopyHold = true |
| 132 | case "moving": |
| 133 | k.MoveProject = "another" |
| 134 | case "running": |
| 135 | r.must(r.s.db.G.Model(&stepRecord{}).Where("id = ?", "st-bind").Update("state", wire.StepRunning).Error) |
| 136 | case "unterminated": |
| 137 | r.must(r.s.db.G.Model(&stepRecord{}).Where("id = ?", "st-bind").Update("terminated", false).Error) |
| 138 | case "revision": |
| 139 | r.must(r.s.db.G.Model(&stepRecord{}).Where("id = ?", "st-bind").Update("revision", 0).Error) |
| 140 | case "attempt": |
| 141 | r.must(r.s.db.G.Model(&stepRecord{}).Where("id = ?", "st-bind").Update("attempt", 0).Error) |
| 142 | } |
| 143 | r.must(r.s.db.G.Save(&k).Error) |
| 144 | in := push.Intent{Kind: push.KindPassive, Data: wire.PushData{Kind: wire.PushTask, TaskID: k.ID}} |
| 145 | var out push.Intent |
| 146 | r.must(r.tx(func(tx *txn) error { out = tx.notificationIntent(in, device); return nil })) |
| 147 | if mode != "valid" { |
| 148 | if out.Data.Actions != nil { |
| 149 | t.Fatal(mode) |
| 150 | } |
| 151 | return |
| 152 | } |
| 153 | a := out.Data.Actions |
| 154 | if a == nil || a.TaskRevision != k.Revision || a.StepID != "st-bind" || a.StepRevision != 1 || a.Attempt != 1 || out.Category != "equipo.stopped.v1" { |
| 155 | t.Fatal(a) |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestNotificationOversizeBindingFallsBackToOriginalAlert(t *testing.T) { |
| 162 | r := newPushRig(t) |
| 163 | r.s.SetNotificationActions(true) |
| 164 | r.register(hkOwner, "cr_phone", wire.RegisterPush{Token: pushDevice, Environment: wire.APNsProduction, Platform: wire.PlatformIOS, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"}) |
| 165 | c := r.card("c-size", wire.CardOpen) |
| 166 | r.clear() |
| 167 | in := push.Intent{Kind: push.KindAlert, Category: push.CategoryNeedsYou, Body: strings.Repeat("x", 3800), Data: wire.PushData{Kind: wire.PushCard, CardID: c.ID}} |
| 168 | raw, _, err := push.Render(in, r.s.authority.Copy, 0, r.now) |
| 169 | r.must(err) |
| 170 | if len(raw) > push.MaxPayload { |
| 171 | t.Fatal(len(raw)) |
| 172 | } |
| 173 | r.must(r.tx(func(tx *txn) error { |
| 174 | d, e := push.Device(tx.db, hkOwner, "cr_phone") |
| 175 | if e != nil { |
| 176 | return e |
| 177 | } |
| 178 | _, e = tx.enqueuePush(hkOwner, d, in) |
| 179 | return e |
| 180 | })) |
| 181 | rows := r.outbox() |
| 182 | if len(rows) != 1 || rows[0].Payload != string(raw) { |
| 183 | t.Fatal("lost alert or retained partial binding", len(rows)) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestNotificationMergePayloadIsNavigationOnly(t *testing.T) { |
| 188 | for _, mode := range []string{"valid", "not_now", "wait_mac", "unknown-option", "no-merge", "no-task", "other-task", "other-person", "held", "revision"} { |
| 189 | t.Run(mode, func(t *testing.T) { |
| 190 | r := newPushRig(t) |
| 191 | r.s.SetNotificationActions(true) |
| 192 | c := r.card("c-merge", wire.CardOpen) |
| 193 | c.Kind = wire.CardMerge |
| 194 | c.TaskID = "tk" |
| 195 | c.Options = `["merge","wait"]` |
| 196 | switch mode { |
| 197 | case "not_now", "wait_mac": |
| 198 | c.Options = `["merge","` + mode + `"]` |
| 199 | case "unknown-option": |
| 200 | c.Options = `["merge","future"]` |
| 201 | case "no-merge": |
| 202 | c.Options = `["wait"]` |
| 203 | case "no-task": |
| 204 | c.TaskID = "" |
| 205 | case "other-task": |
| 206 | c.TaskID = "other" |
| 207 | case "other-person": |
| 208 | c.Person = "other@example.com" |
| 209 | case "held": |
| 210 | c.Held = true |
| 211 | case "revision": |
| 212 | c.Revision = 0 |
| 213 | } |
| 214 | r.must(r.s.db.G.Save(&c).Error) |
| 215 | in := push.Intent{Kind: push.KindAlert, Category: push.CategoryNeedsYou, Data: wire.PushData{Kind: wire.PushCard, CardID: c.ID, TaskID: "tk"}} |
| 216 | var out push.Intent |
| 217 | r.must(r.tx(func(tx *txn) error { |
| 218 | out = tx.notificationIntent(in, push.Registration{Person: hkOwner, ActionsVersion: 1, BindingID: "12345678-1234-4234-8234-123456789abc"}) |
| 219 | return nil |
| 220 | })) |
| 221 | if mode != "valid" && mode != "not_now" && mode != "wait_mac" { |
| 222 | if out.Data.Actions != nil { |
| 223 | t.Fatal(mode) |
| 224 | } |
| 225 | return |
| 226 | } |
| 227 | if out.Category != "equipo.merge.v1" || out.Data.Actions == nil || out.Data.Actions.CardRevision != c.Revision { |
| 228 | t.Fatal(out) |
| 229 | } |
| 230 | if out.Data.Actions.StepID != "" || out.Data.Actions.TaskRevision != 0 { |
| 231 | t.Fatal("merge must not carry write authority") |
| 232 | } |
| 233 | }) |
| 234 | } |
| 235 | } |
| 236 | |