backend: Add integration tests
This commit is contained in:
1 parent
fd37986f5e
commit
def23d306e
9 files changed
+526
No files matched your search
@@ -0,0 +1,77 @@
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user