Harden push notification delivery

Keep encrypted message content out of push payloads and improve cross-platform notification data. Add token registration validation and per-session unregister support, batch and deduplicate sends, remove only confirmed unregistered tokens, and protect queued retries with leases. Add delivery tests and document the push API and Firebase setup.
This commit is contained in:
Artemy
2026-10-17 19:53:34 +07:00
parent 9aa7e017e2
commit e060b2bcbb
21 changed files with 16353 additions and 132 deletions
+37
View File
@@ -86,6 +86,43 @@ public class ReadApiTests
Assert.That(await count.Content.ReadAsStringAsync(), Does.Contain("\"unreadCount\":0"));
}
[Test]
public async Task PushRegistrationRotationAndUnregisterAffectOnlyTheAuthenticatedSession()
{
var otherSession = new UserSession { UserId = _db.Bob, ExpiresAt = DateTime.UtcNow.AddDays(1), RefreshTokenHash = "other-device" };
_db.Context.UserSessions.Add(otherSession);
_db.Context.UserPushTokens.Add(new UserPushToken
{ UserId = _db.Bob, UserSessionId = otherSession.Id, Token = "other-device", Platform = "android" });
await _db.Context.SaveChangesAsync();
Assert.That((await _client.DeleteAsync("/api/pushes/token")).StatusCode, Is.EqualTo(HttpStatusCode.Unauthorized));
Authorize();
foreach (var token in new[] { "original", "rotated" })
Assert.That((await _client.PostAsJsonAsync("/api/pushes/token/register", new { token, platform = "android" })).StatusCode,
Is.EqualTo(HttpStatusCode.NoContent));
var registered = await _db.Context.UserPushTokens.AsNoTracking().SingleAsync(t => t.UserSessionId == _session);
Assert.That(registered.Token, Is.EqualTo("rotated"));
foreach (var _ in Enumerable.Range(0, 2))
Assert.That((await _client.DeleteAsync("/api/pushes/token")).StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
Assert.That((await _db.Context.UserPushTokens.AsNoTracking().SingleAsync(t => t.UserSessionId == _session)).IsActive, Is.False);
Assert.That((await _db.Context.UserPushTokens.AsNoTracking().SingleAsync(t => t.UserSessionId == otherSession.Id)).IsActive, Is.True);
Assert.That((await _client.PostAsJsonAsync("/api/pushes/token/register", new { token = "rotated", platform = "android" })).StatusCode,
Is.EqualTo(HttpStatusCode.NoContent));
Assert.That((await _db.Context.UserPushTokens.AsNoTracking().SingleAsync(t => t.UserSessionId == _session)).IsActive, Is.True);
}
[TestCase(null, "android")]
[TestCase("", "android")]
[TestCase(" ", "android")]
[TestCase("token", " ")]
[TestCase("token", null)]
public async Task PushRegistrationRejectsMissingOrBlankFields(string? token, string? platform)
{
Authorize();
Assert.That((await _client.PostAsJsonAsync("/api/pushes/token/register", new { token, platform })).StatusCode,
Is.EqualTo(HttpStatusCode.BadRequest));
Assert.That(await _db.Context.UserPushTokens.CountAsync(), Is.Zero);
}
[Test]
public async Task AnonymousAndRefreshTokenCannotUseReadApi()
{