mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Merge pull request #60 from BinaryAlley/refactor/updates-registration-username-validation-rules
Updates registration username validation rules
This commit is contained in:
3 files changed
+85
-12
No files matched your search
@@ -21,6 +21,8 @@ public static partial class Errors
|
||||
public static Error PasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(PasswordConfirmCannotBeEmpty));
|
||||
public static Error NewPasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(NewPasswordConfirmCannotBeEmpty));
|
||||
public static Error InvalidPassword => Error.Validation(description: nameof(InvalidPassword));
|
||||
public static Error UsernameMustBeBetween3And255CharactersLong => Error.Validation(description: nameof(UsernameMustBeBetween3And255CharactersLong));
|
||||
public static Error InvalidUsername => Error.Validation(description: nameof(InvalidUsername));
|
||||
public static Error PasswordsNotMatch => Error.Validation(description: nameof(PasswordsNotMatch));
|
||||
public static Error InvalidTotpCode => Error.Validation(description: nameof(InvalidTotpCode));
|
||||
public static Error InvalidUsernameOrPassword => Error.Failure(description: nameof(InvalidUsernameOrPassword));
|
||||
|
||||
+4
-1
@@ -15,7 +15,10 @@ public class RegisterUserCommandValidator : AbstractValidator<RegisterUserComman
|
||||
/// </summary>
|
||||
public RegisterUserCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.Username).NotEmpty().WithMessage(Errors.Authentication.UsernameCannotBeEmpty.Description);
|
||||
RuleFor(x => x.Username).NotEmpty().WithMessage(Errors.Authentication.UsernameCannotBeEmpty.Description)
|
||||
.Length(3, 255).WithMessage(Errors.Authentication.UsernameMustBeBetween3And255CharactersLong.Description)
|
||||
.Matches("^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$") // only allow letters, numbers, dots, underscores and hyphens, and must start and end with letter or number
|
||||
.WithMessage(Errors.Authentication.InvalidUsername.Description);
|
||||
RuleFor(x => x.Password).NotEmpty().WithMessage(Errors.Authentication.PasswordCannotBeEmpty.Description)
|
||||
.Matches("^(?=.*[A-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{8,}$").WithMessage(Errors.Authentication.InvalidPassword.Description);
|
||||
RuleFor(x => x.PasswordConfirm).NotEmpty().WithMessage(Errors.Authentication.PasswordConfirmCannotBeEmpty.Description);
|
||||
|
||||
+79
-11
@@ -175,12 +175,12 @@ public class RegisterUserCommandValidatorTests
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("password123")] // Missing uppercase and special character
|
||||
[InlineData("PASSWORD123")] // Missing special character
|
||||
[InlineData("Password123")] // Missing special character
|
||||
[InlineData("Password!@#")] // Missing number
|
||||
[InlineData("Pass1!")] // Too short
|
||||
[InlineData("password")] // Missing uppercase, number, and special character
|
||||
[InlineData("password123")] // missing uppercase and special character
|
||||
[InlineData("PASSWORD123")] // missing special character
|
||||
[InlineData("Password123")] // missing special character
|
||||
[InlineData("Password!@#")] // missing number
|
||||
[InlineData("Pass1!")] // too short
|
||||
[InlineData("password")] // missing uppercase, number, and special character
|
||||
public void Validate_WhenPasswordDoesNotMatchPattern_ShouldHaveValidationError(string password)
|
||||
{
|
||||
// Arrange
|
||||
@@ -196,10 +196,10 @@ public class RegisterUserCommandValidatorTests
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Password1!")] // Valid password
|
||||
[InlineData("Complex1@Password")] // Valid password
|
||||
[InlineData("MyP@ssw0rd")] // Valid password
|
||||
[InlineData("Abcd123!@#")] // Valid password
|
||||
[InlineData("Password1!")] // valid password
|
||||
[InlineData("Complex1@Password")] // valid password
|
||||
[InlineData("MyP@ssw0rd")] // valid password
|
||||
[InlineData("Abcd123!@#")] // valid password
|
||||
public void Validate_WhenPasswordMatchesPattern_ShouldNotHaveValidationError(string password)
|
||||
{
|
||||
// Arrange
|
||||
@@ -218,7 +218,12 @@ public class RegisterUserCommandValidatorTests
|
||||
{
|
||||
// Arrange
|
||||
RegisterUserCommand command = RegisterUserCommandFixture.CreateRegisterCommand();
|
||||
command = command with { Password = "$321Bcda", PasswordConfirm = "$321Bcda" };
|
||||
command = command with
|
||||
{
|
||||
Username = "valid.user123",
|
||||
Password = "$321Bcda",
|
||||
PasswordConfirm = "$321Bcda"
|
||||
};
|
||||
|
||||
// Act
|
||||
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
|
||||
@@ -228,4 +233,67 @@ public class RegisterUserCommandValidatorTests
|
||||
result.ShouldNotHaveValidationErrorFor(x => x.Password);
|
||||
result.ShouldNotHaveValidationErrorFor(x => x.PasswordConfirm);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("a")] // too short
|
||||
[InlineData("ab")] // too short
|
||||
[InlineData("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")] // too long (256 chars)
|
||||
public void Validate_WhenUsernameHasInvalidLength_ShouldHaveValidationError(string username)
|
||||
{
|
||||
// Arrange
|
||||
RegisterUserCommand command = RegisterUserCommandFixture.CreateRegisterCommand();
|
||||
command = command with { Username = username };
|
||||
|
||||
// Act
|
||||
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor(x => x.Username)
|
||||
.WithErrorMessage(Errors.Authentication.UsernameMustBeBetween3And255CharactersLong.Description);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(".username")] // starts with special character
|
||||
[InlineData("username.")] // ends with special character
|
||||
[InlineData("_username")] // starts with special character
|
||||
[InlineData("username_")] // ends with special character
|
||||
[InlineData("-username")] // starts with special character
|
||||
[InlineData("username-")] // ends with special character
|
||||
[InlineData("user name")] // contains space
|
||||
[InlineData("user@name")] // contains invalid special char
|
||||
[InlineData("user#name")] // contains invalid special char
|
||||
[InlineData("user$name")] // contains invalid special char
|
||||
public void Validate_WhenUsernameHasInvalidFormat_ShouldHaveValidationError(string username)
|
||||
{
|
||||
// Arrange
|
||||
RegisterUserCommand command = RegisterUserCommandFixture.CreateRegisterCommand();
|
||||
command = command with { Username = username };
|
||||
|
||||
// Act
|
||||
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationErrorFor(x => x.Username);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("user123")] // basic alphanumeric
|
||||
[InlineData("user.name")] // with dot
|
||||
[InlineData("user_name")] // with underscore
|
||||
[InlineData("user-name")] // with hyphen
|
||||
[InlineData("User.Name123")] // mixed case with special chars and numbers
|
||||
[InlineData("a1b")] // minimum length
|
||||
[InlineData("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")] // maximum length (255 chars)
|
||||
public void Validate_WhenUsernameHasValidFormat_ShouldNotHaveValidationError(string username)
|
||||
{
|
||||
// Arrange
|
||||
RegisterUserCommand command = RegisterUserCommandFixture.CreateRegisterCommand();
|
||||
command = command with { Username = username };
|
||||
|
||||
// Act
|
||||
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationErrorFor(x => x.Username);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user