backend + web: Set passwords through email confirmation
This commit is contained in:
1 parent
bd8eb65e56
commit
ee5f1d3d10
23 files changed
+281
-110
No files matched your search
@@ -56,7 +56,7 @@ public sealed class AdminAccountLifecycleTests(BackendFixture fixture) : IClassF
|
||||
HttpStatusCode.Unauthorized);
|
||||
await ExpectAsync(anonymous.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(edit.Email, edit.Password!), Cancellation),
|
||||
HttpStatusCode.Forbidden);
|
||||
await ConfirmEmailAsync(anonymous, await fixture.TakeConfirmationTokenAsync(edit.Email));
|
||||
await ConfirmEmailAsync(anonymous, await fixture.TakeConfirmationTokenAsync(edit.Email), password: edit.Password!);
|
||||
var edited = await ReadAsync<AuthResponse>(anonymous.PostAsJsonAsync("/api/v1/auth/login",
|
||||
new LoginRequest(edit.Email, edit.Password!), Cancellation));
|
||||
Assert.Equal((created.Id, edit.Email, edit.FirstName, edit.Role), (edited.User.Id, edited.User.Email, edited.User.FirstName, edited.User.Role));
|
||||
@@ -97,7 +97,7 @@ public sealed class AdminAccountLifecycleTests(BackendFixture fixture) : IClassF
|
||||
using var anonymous = fixture.CreateClient();
|
||||
using var admin = fixture.CreateClient((await LoginAsync(anonymous, AdminEmail)).AccessToken);
|
||||
var request = new SaveAdminUserRequest("[email protected]", "Test", "Uživatel", null,
|
||||
"STUDENT", true, false, false, Password);
|
||||
"STUDENT", true, false, false, null);
|
||||
var created = await ReadAsync<AdminUserDetailsDto>(admin.PostAsJsonAsync(Users, request, Cancellation), HttpStatusCode.Created);
|
||||
Assert.True(created.VerificationEmailSent);
|
||||
Assert.False(created.EmailConfirmed);
|
||||
@@ -230,8 +230,9 @@ public sealed class AdminAccountLifecycleTests(BackendFixture fixture) : IClassF
|
||||
await ExpectAsync(admin.GetAsync($"{Users}/{id}", Cancellation), HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
private static Task ConfirmEmailAsync(HttpClient client, string token, HttpStatusCode expected = HttpStatusCode.NoContent) =>
|
||||
ExpectAsync(client.PostAsJsonAsync("/api/v1/auth/confirm-email", new EmailTokenRequest(token), Cancellation), expected);
|
||||
private static Task ConfirmEmailAsync(HttpClient client, string token, HttpStatusCode expected = HttpStatusCode.NoContent,
|
||||
string password = Password) =>
|
||||
ExpectAsync(client.PostAsJsonAsync("/api/v1/auth/confirm-email", new ConfirmEmailRequest(token, password), Cancellation), expected);
|
||||
|
||||
private sealed record CurrentSession(int Id, string TokenId) : ICurrentUser;
|
||||
}
|
||||
@@ -75,9 +75,9 @@ public sealed class BackendFixture : IAsyncLifetime
|
||||
{
|
||||
var address = $"{Guid.NewGuid():N}@student.example.test";
|
||||
var user = await ReadAsync<UserDto>(client.PostAsJsonAsync("/api/v1/auth/register",
|
||||
new RegisterRequest(address, Password, "Test", "User"), Cancellation), HttpStatusCode.Created);
|
||||
new RegisterRequest(address, "Test", "User"), Cancellation), HttpStatusCode.Created);
|
||||
await ExpectAsync(client.PostAsJsonAsync("/api/v1/auth/confirm-email",
|
||||
new EmailTokenRequest(await TakeConfirmationTokenAsync(address)), Cancellation), HttpStatusCode.NoContent);
|
||||
new ConfirmEmailRequest(await TakeConfirmationTokenAsync(address), Password), Cancellation), HttpStatusCode.NoContent);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
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(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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user