2026-12-02 23:31:22 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http.Json;
|
2026-12-04 21:42:00 +01:00
|
|
|
using Helpdesk.Api.Controllers;
|
2026-12-02 23:31:22 +01:00
|
|
|
using Helpdesk.Application.Auth;
|
|
|
|
|
using Helpdesk.Domain.Auth;
|
|
|
|
|
using Helpdesk.Infrastructure.Persistence;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.IdentityModel.JsonWebTokens;
|
|
|
|
|
using static Helpdesk.IntegrationTests.BackendFixture;
|
|
|
|
|
|
|
|
|
|
namespace Helpdesk.IntegrationTests;
|
|
|
|
|
|
|
|
|
|
public sealed class AuthTests(BackendFixture fixture) : IClassFixture<BackendFixture>
|
|
|
|
|
{
|
2026-12-02 23:56:26 +01:00
|
|
|
[Fact]
|
2026-12-04 21:42:00 +01:00
|
|
|
public async Task ChangingPasswordThroughAnEmailLinkRevokesTheOldSessionAndRemovesItsDevice()
|
2026-12-02 23:56:26 +01:00
|
|
|
{
|
|
|
|
|
using var anonymous = fixture.CreateClient();
|
|
|
|
|
var user = await fixture.RegisterAsync(anonymous);
|
|
|
|
|
using var oldSession = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
|
2026-12-04 21:42:00 +01:00
|
|
|
await ExpectAsync(oldSession.PostAsJsonAsync("/api/v1/devices",
|
|
|
|
|
new RegisterDeviceRequest("password-reset-device"), Cancellation), HttpStatusCode.NoContent);
|
2026-12-02 23:56:26 +01:00
|
|
|
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/forgot-password",
|
|
|
|
|
new ForgotPasswordRequest(user.Email), Cancellation), HttpStatusCode.NoContent);
|
|
|
|
|
|
|
|
|
|
const string newPassword = "New-integration-password";
|
|
|
|
|
var reset = new ResetPasswordRequest(await fixture.TakeResetTokenAsync(user.Email), newPassword);
|
|
|
|
|
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/reset-password", reset, Cancellation), HttpStatusCode.NoContent);
|
|
|
|
|
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/reset-password", reset, Cancellation), HttpStatusCode.BadRequest);
|
|
|
|
|
await ExpectAsync(oldSession.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
|
|
|
|
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/login",
|
|
|
|
|
new LoginRequest(user.Email, Password), Cancellation), HttpStatusCode.Unauthorized);
|
|
|
|
|
var session = await ReadAsync<AuthResponse>(anonymous.PostAsJsonAsync("/api/v1/auth/login",
|
|
|
|
|
new LoginRequest(user.Email, newPassword), Cancellation));
|
|
|
|
|
Assert.Equal(user.Id, session.User.Id);
|
2026-12-04 21:42:00 +01:00
|
|
|
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));
|
2026-12-02 23:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
2026-12-02 23:31:22 +01:00
|
|
|
[Fact]
|
|
|
|
|
public async Task LogoutRevokesOnlyTheCurrentSession()
|
|
|
|
|
{
|
|
|
|
|
using var anonymous = fixture.CreateClient();
|
|
|
|
|
var user = await fixture.RegisterAsync(anonymous);
|
|
|
|
|
var session = await LoginAsync(anonymous, user.Email);
|
|
|
|
|
var otherSession = await LoginAsync(anonymous, user.Email);
|
|
|
|
|
using var client = fixture.CreateClient(session.AccessToken);
|
|
|
|
|
using var otherClient = fixture.CreateClient(otherSession.AccessToken);
|
|
|
|
|
var refresh = new RefreshTokenRequest(session.RefreshToken);
|
|
|
|
|
|
|
|
|
|
await ExpectAsync(client.PostAsJsonAsync("/api/v1/auth/logout", refresh, Cancellation), HttpStatusCode.NoContent);
|
|
|
|
|
|
|
|
|
|
await ExpectAsync(client.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
|
|
|
|
await ExpectAsync(otherClient.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.OK);
|
|
|
|
|
var accessId = new JsonWebToken(session.AccessToken).Id;
|
|
|
|
|
var refreshId = new JsonWebToken(session.RefreshToken).Id;
|
|
|
|
|
await using var scope = fixture.Services.CreateAsyncScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
|
|
|
|
Assert.Equal(2, await db.AuthTokens.CountAsync(token =>
|
|
|
|
|
token.UserId == user.Id && (token.Jti == accessId || token.Jti == refreshId) &&
|
|
|
|
|
token.RevokeReason == RevokeReason.Logout, Cancellation));
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-04 21:42:00 +01:00
|
|
|
[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));
|
|
|
|
|
}
|
|
|
|
|
|
2026-12-02 23:31:22 +01:00
|
|
|
[Fact]
|
|
|
|
|
public async Task ConcurrentRefreshRevokesTheNewPairAndOtherSessions()
|
|
|
|
|
{
|
|
|
|
|
using var anonymous = fixture.CreateClient();
|
|
|
|
|
var user = await fixture.RegisterAsync(anonymous);
|
|
|
|
|
var session = await LoginAsync(anonymous, user.Email);
|
|
|
|
|
var otherSession = await LoginAsync(anonymous, user.Email);
|
2026-12-04 21:42:00 +01:00
|
|
|
using var otherClient = fixture.CreateClient(otherSession.AccessToken);
|
|
|
|
|
await ExpectAsync(otherClient.PostAsJsonAsync("/api/v1/devices",
|
|
|
|
|
new RegisterDeviceRequest("refresh-replay-device"), Cancellation), HttpStatusCode.NoContent);
|
2026-12-02 23:31:22 +01:00
|
|
|
var refresh = new RefreshTokenRequest(session.RefreshToken);
|
|
|
|
|
|
|
|
|
|
var pause = fixture.TokenSaves.PauseNextSave();
|
|
|
|
|
Task<HttpResponseMessage> first;
|
|
|
|
|
Task<HttpResponseMessage> duplicate;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
first = anonymous.PostAsJsonAsync("/api/v1/auth/refresh", refresh, Cancellation);
|
|
|
|
|
await pause.Entered.Task.WaitAsync(TimeSpan.FromSeconds(15), Cancellation);
|
|
|
|
|
await using var scope = fixture.Services.CreateAsyncScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
|
|
|
|
|
Assert.Equal(2, await db.AuthTokens.CountAsync(token =>
|
|
|
|
|
token.UserId == user.Id && token.TokenType == TokenType.Refresh && token.RevokedAt == null, Cancellation));
|
|
|
|
|
duplicate = anonymous.PostAsJsonAsync("/api/v1/auth/refresh", refresh, Cancellation);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
pause.Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var renewed = await ReadAsync<AuthResponse>(first);
|
|
|
|
|
await ExpectAsync(duplicate, HttpStatusCode.Unauthorized);
|
|
|
|
|
await using var after = fixture.Services.CreateAsyncScope();
|
2026-12-04 21:42:00 +01:00
|
|
|
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));
|
2026-12-02 23:31:22 +01:00
|
|
|
|
|
|
|
|
using var renewedClient = fixture.CreateClient(renewed.AccessToken);
|
|
|
|
|
await ExpectAsync(renewedClient.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
|
|
|
|
await ExpectAsync(otherClient.GetAsync("/api/v1/users/me", Cancellation), HttpStatusCode.Unauthorized);
|
|
|
|
|
}
|
|
|
|
|
}
|