using System.Net; using System.Net.Http.Json; using Helpdesk.Application.Abstractions; using Helpdesk.Application.Auth; using Helpdesk.Application.Users; using Helpdesk.Domain.Auth; using Helpdesk.Domain.Users; using Helpdesk.Infrastructure.Persistence; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using static Helpdesk.IntegrationTests.BackendFixture; namespace Helpdesk.IntegrationTests; public sealed class EmailConfirmationTests(BackendFixture fixture) : IClassFixture { private const string Register = "/api/v1/auth/register"; private const string Confirm = "/api/v1/auth/confirm-email"; private const string Login = "/api/v1/auth/login"; [Fact] public async Task RegistrationHasNoPasswordUntilTheMailboxOwnerSetsIt() { using var anonymous = fixture.CreateClient(); const string address = "new@student.example.test"; var user = await ReadAsync(anonymous.PostAsJsonAsync(Register, new RegisterRequest(address, "Jan", "Novák"), Cancellation), HttpStatusCode.Created); await using var scope = fixture.Services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); var pending = await db.Users.AsNoTracking().SingleAsync(u => u.Id == user.Id, Cancellation); Assert.False(pending.EmailConfirmed); Assert.Empty(pending.PasswordHash); await ExpectAsync(anonymous.PostAsJsonAsync(Login, new LoginRequest(address, Password), Cancellation), HttpStatusCode.Unauthorized); var token = await fixture.TakeConfirmationTokenAsync(address); await ExpectAsync(anonymous.PostAsJsonAsync(Confirm, new ConfirmEmailRequest(token, Password), Cancellation), HttpStatusCode.NoContent); Assert.Equal(user.Id, (await LoginAsync(anonymous, address)).User.Id); } [Fact] public async Task AnExistingConfirmationLinkRequiresANewPasswordAndCanOnlyBeUsedOnce() { using var anonymous = fixture.CreateClient(); await using var scope = fixture.Services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); var hasher = scope.ServiceProvider.GetRequiredService(); const string address = "legacy@student.example.test"; const string oldPassword = "Legacy-unconfirmed-password"; var pending = User.Create(address, hasher.Hash(oldPassword), "Eva", "Nováková", UserRole.Student); db.Users.Add(pending); await db.SaveChangesAsync(Cancellation); var original = scope.ServiceProvider.GetRequiredService().Issue(pending, TokenType.EmailConfirm); db.AuthTokens.Add(original.Record); await db.SaveChangesAsync(Cancellation); await ExpectAsync(anonymous.PostAsJsonAsync(Login, new LoginRequest(address, oldPassword), Cancellation), HttpStatusCode.Unauthorized); await ExpectAsync(anonymous.PostAsJsonAsync(Register, new RegisterRequest(address, "Jiné", "Jméno"), Cancellation), HttpStatusCode.Created); var otherToken = await fixture.TakeConfirmationTokenAsync(address); await ExpectAsync(anonymous.PostAsJsonAsync(Confirm, new { token = original.Token }, Cancellation), HttpStatusCode.BadRequest); var newPassword = new string('č', 36); var validation = await ReadAsync(anonymous.PostAsJsonAsync(Confirm, new ConfirmEmailRequest(original.Token, newPassword + "a"), Cancellation), HttpStatusCode.BadRequest); Assert.Contains("72 bajtů", Assert.Single(validation.Errors["newPassword"])); await ExpectAsync(anonymous.PostAsJsonAsync(Confirm, new ConfirmEmailRequest(original.Token, newPassword), Cancellation), HttpStatusCode.NoContent); await ExpectAsync(anonymous.PostAsJsonAsync(Confirm, new ConfirmEmailRequest(otherToken, oldPassword), Cancellation), HttpStatusCode.BadRequest); await ExpectAsync(anonymous.PostAsJsonAsync(Login, new LoginRequest(address, oldPassword), Cancellation), HttpStatusCode.Unauthorized); var session = await ReadAsync(anonymous.PostAsJsonAsync(Login, new LoginRequest(address, newPassword), Cancellation)); Assert.Equal((pending.Id, pending.FirstName, pending.LastName), (session.User.Id, session.User.FirstName, session.User.LastName)); } }