backend: Enforce notification access and clear push devices
This commit is contained in:
1 parent
ee5f1d3d10
commit
4f95c8a379
12 files changed
+145
-46
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Helpdesk.Api.Controllers;
|
||||
using Helpdesk.Application.Auth;
|
||||
using Helpdesk.Domain.Auth;
|
||||
using Helpdesk.Infrastructure.Persistence;
|
||||
@@ -13,11 +14,13 @@ namespace Helpdesk.IntegrationTests;
|
||||
public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFixture>
|
||||
{
|
||||
[Fact]
|
||||
public async Task ChangingPasswordThroughAnEmailLinkRevokesTheOldSession()
|
||||
public async Task ChangingPasswordThroughAnEmailLinkRevokesTheOldSessionAndRemovesItsDevice()
|
||||
{
|
||||
using var anonymous = fixture.CreateClient();
|
||||
var user = await fixture.RegisterAsync(anonymous);
|
||||
using var oldSession = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
|
||||
await ExpectAsync(oldSession.PostAsJsonAsync("/api/v1/devices",
|
||||
new RegisterDeviceRequest("password-reset-device"), Cancellation), HttpStatusCode.NoContent);
|
||||
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/forgot-password",
|
||||
new ForgotPasswordRequest(user.Email), Cancellation), HttpStatusCode.NoContent);
|
||||
|
||||
@@ -31,6 +34,9 @@ public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFix
|
||||
var session = await ReadAsync<AuthResponse>(anonymous.PostAsJsonAsync("/api/v1/auth/login",
|
||||
new LoginRequest(user.Email, newPassword), Cancellation));
|
||||
Assert.Equal(user.Id, session.User.Id);
|
||||
await using var scope = fixture.Services.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
||||
Assert.False(await db.PushDevices.AnyAsync(device => device.UserId == user.Id, Cancellation));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -57,6 +63,29 @@ public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFix
|
||||
token.RevokeReason == RevokeReason.Logout, Cancellation));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LogoutAllRemovesAllDevicesForTheAccount()
|
||||
{
|
||||
using var anonymous = fixture.CreateClient();
|
||||
var user = await fixture.RegisterAsync(anonymous);
|
||||
var otherUser = await fixture.RegisterAsync(anonymous);
|
||||
using var first = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
|
||||
using var second = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
|
||||
using var unrelated = fixture.CreateClient((await LoginAsync(anonymous, otherUser.Email)).AccessToken);
|
||||
const string devices = "/api/v1/devices";
|
||||
await ExpectAsync(first.PostAsJsonAsync(devices, new RegisterDeviceRequest("logout-first"), Cancellation), HttpStatusCode.NoContent);
|
||||
await ExpectAsync(second.PostAsJsonAsync(devices, new RegisterDeviceRequest("logout-second"), Cancellation), HttpStatusCode.NoContent);
|
||||
await ExpectAsync(unrelated.PostAsJsonAsync(devices, new RegisterDeviceRequest("logout-unrelated"), Cancellation), HttpStatusCode.NoContent);
|
||||
|
||||
await ExpectAsync(first.PostAsync("/api/v1/auth/logout-all", null, Cancellation), HttpStatusCode.NoContent);
|
||||
await ExpectAsync(first.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
||||
await ExpectAsync(second.PostAsJsonAsync(devices, new RegisterDeviceRequest("logout-second"), Cancellation), HttpStatusCode.Unauthorized);
|
||||
await using var scope = fixture.Services.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
||||
Assert.False(await db.PushDevices.AnyAsync(device => device.UserId == user.Id, Cancellation));
|
||||
Assert.True(await db.PushDevices.AnyAsync(device => device.UserId == otherUser.Id, Cancellation));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConcurrentRefreshRevokesTheNewPairAndOtherSessions()
|
||||
{
|
||||
@@ -64,6 +93,9 @@ public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFix
|
||||
var user = await fixture.RegisterAsync(anonymous);
|
||||
var session = await LoginAsync(anonymous, user.Email);
|
||||
var otherSession = await LoginAsync(anonymous, user.Email);
|
||||
using var otherClient = fixture.CreateClient(otherSession.AccessToken);
|
||||
await ExpectAsync(otherClient.PostAsJsonAsync("/api/v1/devices",
|
||||
new RegisterDeviceRequest("refresh-replay-device"), Cancellation), HttpStatusCode.NoContent);
|
||||
var refresh = new RefreshTokenRequest(session.RefreshToken);
|
||||
|
||||
var pause = fixture.TokenSaves.PauseNextSave();
|
||||
@@ -87,11 +119,11 @@ public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFix
|
||||
var renewed = await ReadAsync<AuthResponse>(first);
|
||||
await ExpectAsync(duplicate, HttpStatusCode.Unauthorized);
|
||||
await using var after = fixture.Services.CreateAsyncScope();
|
||||
var tokens = after.ServiceProvider.GetRequiredService<HelpdeskDbContext>().AuthTokens;
|
||||
Assert.False(await tokens.AnyAsync(token => token.UserId == user.Id && token.RevokedAt == null, Cancellation));
|
||||
var afterDb = after.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
||||
Assert.False(await afterDb.AuthTokens.AnyAsync(token => token.UserId == user.Id && token.RevokedAt == null, Cancellation));
|
||||
Assert.False(await afterDb.PushDevices.AnyAsync(device => device.UserId == user.Id, Cancellation));
|
||||
|
||||
using var renewedClient = fixture.CreateClient(renewed.AccessToken);
|
||||
using var otherClient = fixture.CreateClient(otherSession.AccessToken);
|
||||
await ExpectAsync(renewedClient.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
||||
await ExpectAsync(otherClient.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user