using System; using System.Collections.Generic; using System.Linq; using CleanArchitecture.Domain.Commands.Users.CreateUser; using CleanArchitecture.Domain.Errors; using Xunit; namespace CleanArchitecture.Domain.Tests.CommandHandler.User.CreateUser; public sealed class CreateUserCommandValidationTests : ValidationTestBase { public CreateUserCommandValidationTests() : base(new CreateUserCommandValidation()) { } [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"); } [Fact] public void Should_Be_Invalid_For_Empty_Password() { var command = CreateTestCommand(password: ""); var errors = new List { DomainErrorCodes.UserEmptyPassword, DomainErrorCodes.UserSpecialCharPassword, DomainErrorCodes.UserNumberPassword, DomainErrorCodes.UserLowercaseLetterPassword, DomainErrorCodes.UserUppercaseLetterPassword, DomainErrorCodes.UserShortPassword }; ShouldHaveExpectedErrors(command, errors.ToArray()); } [Fact] public void Should_Be_Invalid_For_Missing_Special_Character() { var command = CreateTestCommand(password: "z8tnayvd5FNLU9AQm"); ShouldHaveSingleError(command, DomainErrorCodes.UserSpecialCharPassword); } [Fact] public void Should_Be_Invalid_For_Missing_Number() { var command = CreateTestCommand(password: "z]tnayvdFNLU:]AQm"); ShouldHaveSingleError(command, DomainErrorCodes.UserNumberPassword); } [Fact] public void Should_Be_Invalid_For_Missing_Lowercase_Character() { var command = CreateTestCommand(password: "Z8]TNAYVDFNLU:]AQM"); ShouldHaveSingleError(command, DomainErrorCodes.UserLowercaseLetterPassword); } [Fact] public void Should_Be_Invalid_For_Missing_Uppercase_Character() { var command = CreateTestCommand(password: "z8]tnayvd5fnlu9:]aqm"); ShouldHaveSingleError(command, DomainErrorCodes.UserUppercaseLetterPassword); } [Fact] public void Should_Be_Invalid_For_Password_Too_Short() { var command = CreateTestCommand(password: "zA6{"); ShouldHaveSingleError(command, DomainErrorCodes.UserShortPassword); } [Fact] public void Should_Be_Invalid_For_Password_Too_Long() { var command = CreateTestCommand(password: string.Concat(Enumerable.Repeat("zA6{", 12), 12)); ShouldHaveSingleError(command, DomainErrorCodes.UserLongPassword); } private static CreateUserCommand CreateTestCommand( Guid? userId = null, Guid? tenantId = null, string? email = null, string? firstName = null, string? lastName = null, string? password = null) { return new( userId ?? Guid.NewGuid(), tenantId ?? Guid.NewGuid(), email ?? "test@email.com", firstName ?? "test", lastName ?? "email", password ?? "Po=PF]PC6t.?8?ks)A6W"); } }