mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-07-01 19:12:57 +00:00
126 lines
3.4 KiB
C#
126 lines
3.4 KiB
C#
using System;
|
|
using CleanArchitecture.Domain.Commands.Users.UpdateUser;
|
|
using CleanArchitecture.Domain.Enums;
|
|
using CleanArchitecture.Domain.Errors;
|
|
using Xunit;
|
|
|
|
namespace CleanArchitecture.Domain.Tests.CommandHandler.User.UpdateUser;
|
|
|
|
public sealed class UpdateUserCommandValidationTests :
|
|
ValidationTestBase<UpdateUserCommand, UpdateUserCommandValidation>
|
|
{
|
|
public UpdateUserCommandValidationTests() : base(new UpdateUserCommandValidation())
|
|
{
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Valid()
|
|
{
|
|
var command = CreateTestCommand();
|
|
|
|
ShouldBeValid(command);
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Empty_User_Id()
|
|
{
|
|
var command = CreateTestCommand(Guid.Empty);
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserEmptyId,
|
|
"User id may not be empty");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Empty_Email()
|
|
{
|
|
var command = CreateTestCommand(email: string.Empty);
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserInvalidEmail,
|
|
"Email is not a valid email address");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Invalid_Email()
|
|
{
|
|
var command = CreateTestCommand(email: "not a email");
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserInvalidEmail,
|
|
"Email is not a valid email address");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Email_Exceeds_Max_Length()
|
|
{
|
|
var command = CreateTestCommand(email: new string('a', 320) + "@test.com");
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserEmailExceedsMaxLength,
|
|
"Email may not be longer than 320 characters");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Empty_First_Name()
|
|
{
|
|
var command = CreateTestCommand(firstName: "");
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserEmptyFirstName,
|
|
"FirstName may not be empty");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_First_Name_Exceeds_Max_Length()
|
|
{
|
|
var command = CreateTestCommand(firstName: new string('a', 101));
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserFirstNameExceedsMaxLength,
|
|
"FirstName may not be longer than 100 characters");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Empty_Last_Name()
|
|
{
|
|
var command = CreateTestCommand(lastName: "");
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserEmptyLastName,
|
|
"LastName may not be empty");
|
|
}
|
|
|
|
[Fact]
|
|
public void Should_Be_Invalid_For_Last_Name_Exceeds_Max_Length()
|
|
{
|
|
var command = CreateTestCommand(lastName: new string('a', 101));
|
|
|
|
ShouldHaveSingleError(
|
|
command,
|
|
DomainErrorCodes.UserLastNameExceedsMaxLength,
|
|
"LastName may not be longer than 100 characters");
|
|
}
|
|
|
|
private static UpdateUserCommand CreateTestCommand(
|
|
Guid? userId = null,
|
|
string? email = null,
|
|
string? firstName = null,
|
|
string? lastName = null,
|
|
UserRole? role = null)
|
|
{
|
|
return new(
|
|
userId ?? Guid.NewGuid(),
|
|
email ?? "test@email.com",
|
|
firstName ?? "test",
|
|
lastName ?? "email",
|
|
role ?? UserRole.User);
|
|
}
|
|
} |