package plan3test import ( "errors" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/push" "gorm.io/gorm" ) const pushToken = "aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11aa11" func registerPush(t *testing.T, f *fixture, token string) { t.Helper() w := deviceRequest(t, f, token, "POST", "/api/v1/push-registrations", `{"token":"`+pushToken+`","environment":"production","platform":"ios","start_token":"`+strings.ToUpper(pushToken)+`"}`, "push", "") expectHTTP(t, w, 200) if strings.Contains(strings.ToLower(w.Body.String()), pushToken) || !strings.Contains(w.Body.String(), `"live_activities":true`) { t.Fatal("registration answer", w.Body.String()) } } // pushCurrent is the sender's own check of a device: nil while it may be // pushed to. func pushCurrent(t *testing.T, f *fixture, credential string) error { t.Helper() s := f.Store.Chat().PushSender(push.Logged{}) var out error must(t, f.DB.G.Transaction(func(tx *gorm.DB) error { out = s.Check(tx, owner.Address, credential); return nil })) return out } // TestPushRegistrationLifetime: a paired device registers with its own // bearer (a browser session cannot); the registration is the // credential's, never audited with its tokens, and it goes when the // device is revoked or its person is removed, after which the sender's // check refuses the device. func TestPushRegistrationLifetime(t *testing.T) { f := newFixture(t) cred := approvedDevice(t, f) expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/push-registrations", `{"token":"`+pushToken+`","environment":"production","platform":"ios"}`, "browser", ""), 401) registerPush(t, f, cred.Token) registerPush(t, f, cred.Token) if f.count(t, "SELECT count(*) FROM push_registrations WHERE credential = ? AND person = ?", cred.ID, owner.Address) != 1 { t.Fatal("not one registration for the device") } 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 { t.Fatal("registration audit") } if err := pushCurrent(t, f, cred.ID); err != nil { t.Fatal("a current device refused", err) } // Sealed at rest with the instance key: no token in the table. if f.count(t, "SELECT count(*) FROM push_registrations WHERE lower(token) LIKE ? OR lower(start_token) LIKE ?", "%"+pushToken+"%", "%"+pushToken+"%") != 0 { t.Fatal("a token kept in the clear") } // Only revocation or removal is gone: an expired credential is not // current now, and keeps its registration. var expires time.Time must(t, f.DB.G.Raw("SELECT token_expires_at FROM person_devices WHERE id = ?", cred.ID).Scan(&expires).Error) must(t, f.DB.G.Exec("UPDATE person_devices SET token_expires_at = ? WHERE id = ?", time.Now().Add(-time.Hour).UTC(), cred.ID).Error) if err := pushCurrent(t, f, cred.ID); !errors.Is(err, push.ErrNotCurrent) { t.Fatal("an expired device", err) } must(t, f.DB.G.Exec("UPDATE person_devices SET token_expires_at = ? WHERE id = ?", expires, cred.ID).Error) expectHTTP(t, deviceRequest(t, f, cred.Token, "DELETE", "/api/v1/auth/devices/"+cred.ID, `{}`, "revoke", `"3"`), 200) if f.count(t, "SELECT count(*) FROM push_registrations") != 0 { t.Fatal("a revoked device kept its registration") } if err := pushCurrent(t, f, cred.ID); !errors.Is(err, push.ErrGone) { t.Fatal("a revoked device may still be pushed", err) } second := approvedDevice(t, f) registerPush(t, f, second.Token) must(t, f.Store.SetMember(ctx, peer, owner.Address, equipo.RoleMember, false, 1)) if f.count(t, "SELECT count(*) FROM push_registrations") != 0 { t.Fatal("a removed person kept a registration") } if err := pushCurrent(t, f, second.ID); !errors.Is(err, push.ErrGone) { t.Fatal("a removed person's device may still be pushed", err) } }