mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +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 PasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(PasswordConfirmCannotBeEmpty));
|
||||||
public static Error NewPasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(NewPasswordConfirmCannotBeEmpty));
|
public static Error NewPasswordConfirmCannotBeEmpty => Error.Validation(description: nameof(NewPasswordConfirmCannotBeEmpty));
|
||||||
public static Error InvalidPassword => Error.Validation(description: nameof(InvalidPassword));
|
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 PasswordsNotMatch => Error.Validation(description: nameof(PasswordsNotMatch));
|
||||||
public static Error InvalidTotpCode => Error.Validation(description: nameof(InvalidTotpCode));
|
public static Error InvalidTotpCode => Error.Validation(description: nameof(InvalidTotpCode));
|
||||||
public static Error InvalidUsernameOrPassword => Error.Failure(description: nameof(InvalidUsernameOrPassword));
|
public static Error InvalidUsernameOrPassword => Error.Failure(description: nameof(InvalidUsernameOrPassword));
|
||||||
|
|||||||
+4
-1
@@ -15,7 +15,10 @@ public class RegisterUserCommandValidator : AbstractValidator<RegisterUserComman
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public RegisterUserCommandValidator()
|
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)
|
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);
|
.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);
|
RuleFor(x => x.PasswordConfirm).NotEmpty().WithMessage(Errors.Authentication.PasswordConfirmCannotBeEmpty.Description);
|
||||||
|
|||||||
+79
-11
@@ -175,12 +175,12 @@ public class RegisterUserCommandValidatorTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("password123")] // Missing uppercase and special character
|
[InlineData("password123")] // missing uppercase and special character
|
||||||
[InlineData("PASSWORD123")] // Missing special character
|
[InlineData("PASSWORD123")] // missing special character
|
||||||
[InlineData("Password123")] // Missing special character
|
[InlineData("Password123")] // missing special character
|
||||||
[InlineData("Password!@#")] // Missing number
|
[InlineData("Password!@#")] // missing number
|
||||||
[InlineData("Pass1!")] // Too short
|
[InlineData("Pass1!")] // too short
|
||||||
[InlineData("password")] // Missing uppercase, number, and special character
|
[InlineData("password")] // missing uppercase, number, and special character
|
||||||
public void Validate_WhenPasswordDoesNotMatchPattern_ShouldHaveValidationError(string password)
|
public void Validate_WhenPasswordDoesNotMatchPattern_ShouldHaveValidationError(string password)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
@@ -196,10 +196,10 @@ public class RegisterUserCommandValidatorTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("Password1!")] // Valid password
|
[InlineData("Password1!")] // valid password
|
||||||
[InlineData("Complex1@Password")] // Valid password
|
[InlineData("Complex1@Password")] // valid password
|
||||||
[InlineData("MyP@ssw0rd")] // Valid password
|
[InlineData("MyP@ssw0rd")] // valid password
|
||||||
[InlineData("Abcd123!@#")] // Valid password
|
[InlineData("Abcd123!@#")] // valid password
|
||||||
public void Validate_WhenPasswordMatchesPattern_ShouldNotHaveValidationError(string password)
|
public void Validate_WhenPasswordMatchesPattern_ShouldNotHaveValidationError(string password)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
@@ -218,7 +218,12 @@ public class RegisterUserCommandValidatorTests
|
|||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
RegisterUserCommand command = RegisterUserCommandFixture.CreateRegisterCommand();
|
RegisterUserCommand command = RegisterUserCommandFixture.CreateRegisterCommand();
|
||||||
command = command with { Password = "$321Bcda", PasswordConfirm = "$321Bcda" };
|
command = command with
|
||||||
|
{
|
||||||
|
Username = "valid.user123",
|
||||||
|
Password = "$321Bcda",
|
||||||
|
PasswordConfirm = "$321Bcda"
|
||||||
|
};
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
|
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
|
||||||
@@ -228,4 +233,67 @@ public class RegisterUserCommandValidatorTests
|
|||||||
result.ShouldNotHaveValidationErrorFor(x => x.Password);
|
result.ShouldNotHaveValidationErrorFor(x => x.Password);
|
||||||
result.ShouldNotHaveValidationErrorFor(x => x.PasswordConfirm);
|
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