Files
live_recorder/tests/LiveRecorder.Tests/AuthServiceTests.cs
T

39 lines
1.4 KiB
C#

using LiveRecorder.Application.Common;
using LiveRecorder.Application.Models.Auth;
using LiveRecorder.Application.Services;
using LiveRecorder.Domain.Entities;
using LiveRecorder.Infrastructure.Persistence;
using LiveRecorder.Infrastructure.Persistence.Repositories;
using Microsoft.EntityFrameworkCore;
namespace LiveRecorder.Tests;
public sealed class AuthServiceTests
{
[Theory]
[InlineData(false, 12)]
[InlineData(true, 720)]
public async Task Login_UsesExpectedSessionLifetime(bool rememberMe, int expectedHours)
{
var options = new DbContextOptionsBuilder<LiveRecorderDbContext>()
.UseInMemoryDatabase($"auth-{Guid.NewGuid():N}")
.Options;
await using var context = new LiveRecorderDbContext(options);
var user = new UserAccount("admin", "Admin", PasswordHasher.Hash("secret123"), DateTimeOffset.UtcNow);
context.UserAccounts.Add(user);
await context.SaveChangesAsync();
var service = new AuthService(new UserAccountRepository(context), new UserSessionRepository(context), context);
var before = DateTimeOffset.UtcNow;
var response = await service.LoginAsync(new LoginRequest
{
Username = "admin",
Password = "secret123",
RememberMe = rememberMe
});
var after = DateTimeOffset.UtcNow;
Assert.InRange(response.ExpiresAt, before.AddHours(expectedHours), after.AddHours(expectedHours));
}
}