| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo" |
| 10 | "amadan.net/oficina/equipo/internal/equipo/push" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | const pushToken = "aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11" |
| 15 | |
| 16 | func registerPush(t *testing.T, f *fixture, token string) { |
| 17 | t.Helper() |
| 18 | w := deviceRequest(t, f, token, "POST", "/api/v1/push-registrations", `{"token":"`+pushToken+`","environment":"production","platform":"ios","start_token":"`+strings.ToUpper(pushToken)+`"}`, "push", "") |
| 19 | expectHTTP(t, w, 200) |
| 20 | if strings.Contains(strings.ToLower(w.Body.String()), pushToken) || !strings.Contains(w.Body.String(), `"live_activities":true`) { |
| 21 | t.Fatal("registration answer", w.Body.String()) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // pushCurrent is the sender's own check of a device: nil while it may be |
| 26 | // pushed to. |
| 27 | func pushCurrent(t *testing.T, f *fixture, credential string) error { |
| 28 | t.Helper() |
| 29 | s := f.Store.Chat().PushSender(push.Logged{}) |
| 30 | var out error |
| 31 | must(t, f.DB.G.Transaction(func(tx *gorm.DB) error { out = s.Check(tx, owner.Address, credential); return nil })) |
| 32 | return out |
| 33 | } |
| 34 | |
| 35 | // TestPushRegistrationLifetime: a paired device registers with its own |
| 36 | // bearer (a browser session cannot); the registration is the |
| 37 | // credential's, never audited with its tokens, and it goes when the |
| 38 | // device is revoked or its person is removed, after which the sender's |
| 39 | // check refuses the device. |
| 40 | func TestPushRegistrationLifetime(t *testing.T) { |
| 41 | f := newFixture(t) |
| 42 | cred := approvedDevice(t, f) |
| 43 | expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/push-registrations", `{"token":"`+pushToken+`","environment":"production","platform":"ios"}`, "browser", ""), 401) |
| 44 | registerPush(t, f, cred.Token) |
| 45 | registerPush(t, f, cred.Token) |
| 46 | if f.count(t, "SELECT count(*) FROM push_registrations WHERE credential = ? AND person = ?", cred.ID, owner.Address) != 1 { |
| 47 | t.Fatal("not one registration for the device") |
| 48 | } |
| 49 | if f.count(t, "SELECT count(*) FROM audit_events WHERE kind = 'push.register' AND record_id = ?", cred.ID) != 2 || f.count(t, "SELECT count(*) FROM audit_events WHERE lower(record_id) LIKE ? OR lower(kind) LIKE ?", "%"+pushToken+"%", "%"+pushToken+"%") != 0 { |
| 50 | t.Fatal("registration audit") |
| 51 | } |
| 52 | if err := pushCurrent(t, f, cred.ID); err != nil { |
| 53 | t.Fatal("a current device refused", err) |
| 54 | } |
| 55 | // Sealed at rest with the instance key: no token in the table. |
| 56 | if f.count(t, "SELECT count(*) FROM push_registrations WHERE lower(token) LIKE ? OR lower(start_token) LIKE ?", "%"+pushToken+"%", "%"+pushToken+"%") != 0 { |
| 57 | t.Fatal("a token kept in the clear") |
| 58 | } |
| 59 | // Only revocation or removal is gone: an expired credential is not |
| 60 | // current now, and keeps its registration. |
| 61 | var expires time.Time |
| 62 | must(t, f.DB.G.Raw("SELECT token_expires_at FROM person_devices WHERE id = ?", cred.ID).Scan(&expires).Error) |
| 63 | must(t, f.DB.G.Exec("UPDATE person_devices SET token_expires_at = ? WHERE id = ?", time.Now().Add(-time.Hour).UTC(), cred.ID).Error) |
| 64 | if err := pushCurrent(t, f, cred.ID); !errors.Is(err, push.ErrNotCurrent) { |
| 65 | t.Fatal("an expired device", err) |
| 66 | } |
| 67 | must(t, f.DB.G.Exec("UPDATE person_devices SET token_expires_at = ? WHERE id = ?", expires, cred.ID).Error) |
| 68 | expectHTTP(t, deviceRequest(t, f, cred.Token, "DELETE", "/api/v1/auth/devices/"+cred.ID, `{}`, "revoke", `"3"`), 200) |
| 69 | if f.count(t, "SELECT count(*) FROM push_registrations") != 0 { |
| 70 | t.Fatal("a revoked device kept its registration") |
| 71 | } |
| 72 | if err := pushCurrent(t, f, cred.ID); !errors.Is(err, push.ErrGone) { |
| 73 | t.Fatal("a revoked device may still be pushed", err) |
| 74 | } |
| 75 | |
| 76 | second := approvedDevice(t, f) |
| 77 | registerPush(t, f, second.Token) |
| 78 | must(t, f.Store.SetMember(ctx, peer, owner.Address, equipo.RoleMember, false, 1)) |
| 79 | if f.count(t, "SELECT count(*) FROM push_registrations") != 0 { |
| 80 | t.Fatal("a removed person kept a registration") |
| 81 | } |
| 82 | if err := pushCurrent(t, f, second.ID); !errors.Is(err, push.ErrGone) { |
| 83 | t.Fatal("a removed person's device may still be pushed", err) |
| 84 | } |
| 85 | } |
| 86 | |