Add validator tests for brewery and user commands (#282)

This commit is contained in:
Aaron Po
2026-09-06 17:48:03 -04:00
committed by GitHub
parent 9d5a463d93
commit a3ec7d63f2
13 changed files with 977 additions and 3 deletions

View File

@@ -0,0 +1,54 @@
using Features.Users.Commands.Account.UpdateEmail;
using FluentValidation.TestHelper;
namespace Features.Users.Tests.Commands;
public class UpdateEmailValidatorTests
{
private readonly UpdateEmailValidator _validator = new();
private static UpdateEmailCommand ValidCommand() => new(Guid.NewGuid(), "user@example.com");
[Fact]
public void Validate_ValidCommand_HasNoValidationErrors()
{
TestValidationResult<UpdateEmailCommand> result = _validator.TestValidate(ValidCommand());
result.ShouldNotHaveAnyValidationErrors();
}
[Fact]
public void Validate_EmptyNewEmail_HasErrorForNewEmail()
{
UpdateEmailCommand command = ValidCommand() with { NewEmail = "" };
TestValidationResult<UpdateEmailCommand> result = _validator.TestValidate(command);
result.ShouldHaveValidationErrorFor(x => x.NewEmail).WithErrorMessage("Email is required");
}
[Fact]
public void Validate_InvalidEmailFormat_HasErrorForNewEmail()
{
UpdateEmailCommand command = ValidCommand() with { NewEmail = "not-an-email" };
TestValidationResult<UpdateEmailCommand> result = _validator.TestValidate(command);
result
.ShouldHaveValidationErrorFor(x => x.NewEmail)
.WithErrorMessage("Invalid email format");
}
[Fact]
public void Validate_NewEmailTooLong_HasErrorForNewEmail()
{
string localPart = new('a', 120);
UpdateEmailCommand command = ValidCommand() with { NewEmail = $"{localPart}@example.com" };
TestValidationResult<UpdateEmailCommand> result = _validator.TestValidate(command);
result
.ShouldHaveValidationErrorFor(x => x.NewEmail)
.WithErrorMessage("Email cannot exceed 128 characters");
}
}