Files

98 lines
5.2 KiB
C#
Raw Permalink Normal View History

2026-12-02 23:31:22 +01:00
using System.Net;
using System.Net.Http.Json;
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>
{
[Fact]
public async Task ChangingPasswordThroughAnEmailLinkRevokesTheOldSession()
{
using var anonymous = fixture.CreateClient();
var user = await fixture.RegisterAsync(anonymous);
using var oldSession = fixture.CreateClient((await LoginAsync(anonymous, user.Email)).AccessToken);
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-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));
}
[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);
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();
var tokens = after.ServiceProvider.GetRequiredService<HelpdeskDbContext>().AuthTokens;
Assert.False(await tokens.AnyAsync(token => token.UserId == user.Id && token.RevokedAt == null, 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);
}
}