add unit tests
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using Application.Models;
|
||||
using Application.Validators;
|
||||
|
||||
namespace Application.UnitTest.Validators;
|
||||
|
||||
public class LoginRequestValidatorTests
|
||||
{
|
||||
private readonly LoginRequestValidator _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldPass_WhenAllFieldsAreValid()
|
||||
{
|
||||
var request = new LoginRequest("test@example.com", "password123");
|
||||
|
||||
var result = _sut.Validate(request);
|
||||
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "password123", "Email")]
|
||||
[InlineData("not-an-email", "password123", "Email")]
|
||||
[InlineData("test@example.com", "", "Password")]
|
||||
public void Validate_ShouldFail_WhenFieldIsInvalid(string email, string password, string expectedProperty)
|
||||
{
|
||||
var request = new LoginRequest(email, password);
|
||||
|
||||
var result = _sut.Validate(request);
|
||||
|
||||
Assert.False(result.IsValid);
|
||||
Assert.Contains(result.Errors, e => e.PropertyName == expectedProperty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Application.Models;
|
||||
using Application.Validators;
|
||||
|
||||
namespace Application.UnitTest.Validators;
|
||||
|
||||
public class RegisterRequestValidatorTests
|
||||
{
|
||||
private readonly RegisterRequestValidator _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldPass_WhenAllFieldsAreValid()
|
||||
{
|
||||
var request = new RegisterRequest("ValidUser", "test@example.com", "ValidP@ss1");
|
||||
|
||||
var result = _sut.Validate(request);
|
||||
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ValidUser", "", "ValidP@ss1", "Email")]
|
||||
[InlineData("ValidUser", "invalid-email", "ValidP@ss1", "Email")]
|
||||
[InlineData("ValidUser", "test@example.com", "", "Password")]
|
||||
[InlineData("ValidUser", "test@example.com", "short1A@", "Password")]
|
||||
[InlineData("ValidUser", "test@example.com", "nouppercase1@", "Password")]
|
||||
[InlineData("ValidUser", "test@example.com", "NOLOWERCASE1@", "Password")]
|
||||
[InlineData("ValidUser", "test@example.com", "NoDigit@aa", "Password")]
|
||||
[InlineData("ValidUser", "test@example.com", "NoSpecialChar1", "Password")]
|
||||
[InlineData("", "test@example.com", "ValidP@ss1", "Username")]
|
||||
[InlineData("ab", "test@example.com", "ValidP@ss1", "Username")]
|
||||
[InlineData("user@name", "test@example.com", "ValidP@ss1", "Username")]
|
||||
public void Validate_ShouldFail_WhenFieldIsInvalid(string username, string email, string password, string expectedProperty)
|
||||
{
|
||||
var request = new RegisterRequest(username, email, password);
|
||||
|
||||
var result = _sut.Validate(request);
|
||||
|
||||
Assert.False(result.IsValid);
|
||||
Assert.Contains(result.Errors, e => e.PropertyName == expectedProperty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Application.Models;
|
||||
using Application.Validators;
|
||||
|
||||
namespace Application.UnitTest.Validators;
|
||||
|
||||
public class UserUpdateRequestValidatorTests
|
||||
{
|
||||
private readonly UserUpdateRequestValidator _sut = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_ShouldPass_WhenAllFieldsAreValid()
|
||||
{
|
||||
var request = new UserUpdateRequest(1, "ValidUser", "test@example.com");
|
||||
|
||||
var result = _sut.Validate(request);
|
||||
|
||||
Assert.True(result.IsValid);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, "ValidUser", "test@example.com", "Id")]
|
||||
[InlineData(1, "", "test@example.com", "Username")]
|
||||
[InlineData(1, "ValidUser", "", "Email")]
|
||||
[InlineData(1, "ValidUser", "invalid-email", "Email")]
|
||||
public void Validate_ShouldFail_WhenFieldIsInvalid(int id, string username, string email, string expectedProperty)
|
||||
{
|
||||
var request = new UserUpdateRequest(id, username, email);
|
||||
|
||||
var result = _sut.Validate(request);
|
||||
|
||||
Assert.False(result.IsValid);
|
||||
Assert.Contains(result.Errors, e => e.PropertyName == expectedProperty);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user