Updates registration username validation rules

This commit is contained in:
BinaryAlley committed 2024-11-13 17:54:38 +02:00
1 parent 5ae095e213
commit 067682aa0b
3 files changed
+85 -12

No files matched your search

@@ -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);
}
}