Files
NTBug/backend/tests/Helpdesk.IntegrationTests/EmailConfirmationTests.cs
T

77 lines
4.2 KiB
C#

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.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using static Helpdesk.IntegrationTests.BackendFixture;
namespace Helpdesk.IntegrationTests;
public sealed class EmailConfirmationTests(BackendFixture fixture) : IClassFixture<BackendFixture>
{
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 = "[email protected]";
var user = await ReadAsync<UserDto>(anonymous.PostAsJsonAsync(Register,
new RegisterRequest(address, "Jan", "Novák"), Cancellation), HttpStatusCode.Created);
await using var scope = fixture.Services.CreateAsyncScope();
var db = scope.ServiceProvider.GetRequiredService<HelpdeskDbContext>();
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<HelpdeskDbContext>();
var hasher = scope.ServiceProvider.GetRequiredService<IPasswordHasher>();
const string address = "[email protected]";
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<ITokenService>().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);
await ExpectAsync(anonymous.PostAsJsonAsync(Confirm, new ConfirmEmailRequest(original.Token, new string('č', 37)), Cancellation),
HttpStatusCode.BadRequest);
await ExpectAsync(anonymous.PostAsJsonAsync(Confirm, new ConfirmEmailRequest(original.Token, Password), 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 LoginAsync(anonymous, address);
Assert.Equal((pending.Id, pending.FirstName, pending.LastName), (session.User.Id, session.User.FirstName, session.User.LastName));
}
}