mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 23:11:10 +00:00
Replaces FluentValidation with the custom validation framework in the web project
This commit is contained in:
1 parent
587daaabb7
commit
57e8e2ed65
19 files changed
+1020
-104
No files matched your search
+56
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Primitives;
|
||||
using Lumina.Presentation.Web.Common.Validation;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.UnitTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Validator extension methods for <see cref="IValidator{TRequest}"/> for unit tests.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static class ValidatorTestExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the validator against the specified request instance and returns the resulting errors.
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
|
||||
/// <param name="validator">The validator instance to execute.</param>
|
||||
/// <param name="request">The request instance to validate.</param>
|
||||
/// <returns>A list of <see cref="Error"/> produced by the validator. If the request is valid, the list will be empty.</returns>
|
||||
public static List<Error> TestValidate<TRequest>(this IValidator<TRequest> validator, TRequest request)
|
||||
{
|
||||
return validator.Validate(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the validation results do not contain the specified error.
|
||||
/// </summary>
|
||||
/// <param name="errors">The list of errors returned by the validator.</param>
|
||||
/// <param name="expectedError">The error that should not be present in the validation results.</param>
|
||||
public static void ShouldNotHaveValidationError(this List<Error> errors, Error expectedError)
|
||||
{
|
||||
Assert.DoesNotContain(errors, error => error.Description == expectedError.Description);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the validation results contain the specified error.
|
||||
/// </summary>
|
||||
/// <param name="errors">The list of errors returned by the validator.</param>
|
||||
/// <param name="expectedError">The error that should be present in the validation results.</param>
|
||||
public static void ShouldHaveValidationError(this List<Error> errors, Error expectedError)
|
||||
{
|
||||
Assert.Contains(errors, error => error.Description == expectedError.Description);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the validation results are completely empty, indicating the request is valid.
|
||||
/// </summary>
|
||||
/// <param name="errors">The list of errors returned by the validator.</param>
|
||||
public static void ShouldNotHaveAnyValidationErrors(this List<Error> errors)
|
||||
{
|
||||
Assert.Empty(errors);
|
||||
}
|
||||
}
|
||||
+11
-12
@@ -1,21 +1,22 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.Primitives;
|
||||
using Lumina.Presentation.Web.Common.Validators;
|
||||
using Lumina.Presentation.Web.UnitTests.Common.Setup;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.UnitTests.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="EncryptionSettingsModelValidator"/> class.
|
||||
/// Contains unit tests for the <see cref="EncryptionSettingsDtoValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class EncryptionSettingsModelValidatorTests
|
||||
{
|
||||
private const string VALID_BASE64_KEY = "FLYO0QRo6u2VzoFOgNkkEwYNGtqhJ3QGZd7iAHNEJeM=";
|
||||
private readonly EncryptionSettingsModelValidator _validator = new();
|
||||
private readonly EncryptionSettingsDtoValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenSecretKeyIsEmpty_ShouldHaveValidationError()
|
||||
@@ -24,11 +25,10 @@ public class EncryptionSettingsModelValidatorTests
|
||||
EncryptionSettingsDto settings = new() { SecretKey = string.Empty };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(settings);
|
||||
List<Error> result = _validator.TestValidate(settings);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsValid);
|
||||
Assert.Contains(result.Errors, error => error.PropertyName == nameof(EncryptionSettingsDto.SecretKey) && error.ErrorMessage == "Encryption secret key cannot be empty!");
|
||||
result.ShouldHaveValidationError(Error.Validation(description: "Encryption secret key cannot be empty!"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -38,11 +38,10 @@ public class EncryptionSettingsModelValidatorTests
|
||||
EncryptionSettingsDto settings = new() { SecretKey = "not-base64-key!" };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(settings);
|
||||
List<Error> result = _validator.TestValidate(settings);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsValid);
|
||||
Assert.Contains(result.Errors, error => error.PropertyName == nameof(EncryptionSettingsDto.SecretKey) && error.ErrorMessage == "Encryption secret key must be a base64 string!");
|
||||
result.ShouldHaveValidationError(Error.Validation(description: "Encryption secret key must be a base64 string!"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -52,9 +51,9 @@ public class EncryptionSettingsModelValidatorTests
|
||||
EncryptionSettingsDto settings = new() { SecretKey = VALID_BASE64_KEY };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(settings);
|
||||
List<Error> result = _validator.TestValidate(settings);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsValid);
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
+13
-13
@@ -1,20 +1,21 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.Primitives;
|
||||
using Lumina.Presentation.Web.Common.Validators;
|
||||
using Lumina.Presentation.Web.UnitTests.Common.Setup;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.UnitTests.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ServerConfigurationModelValidator"/> class.
|
||||
/// Contains unit tests for the <see cref="ServerConfigurationDtoValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ServerConfigurationModelValidatorTests
|
||||
{
|
||||
private readonly ServerConfigurationModelValidator _validator = new();
|
||||
private readonly ServerConfigurationDtoValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBaseAddressIsEmpty_ShouldHaveValidationError()
|
||||
@@ -23,11 +24,10 @@ public class ServerConfigurationModelValidatorTests
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = '1', BaseAddress = string.Empty, Port = 5214 };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(configuration);
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsValid);
|
||||
Assert.Contains(result.Errors, error => error.PropertyName == nameof(ServerConfigurationDto.BaseAddress) && error.ErrorMessage == "Base address cannot be empty!");
|
||||
result.ShouldHaveValidationError(Error.Validation(description: "Base address cannot be empty!"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -39,10 +39,10 @@ public class ServerConfigurationModelValidatorTests
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = '1', BaseAddress = "http://localhost", Port = port };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(configuration);
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsValid);
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -54,10 +54,10 @@ public class ServerConfigurationModelValidatorTests
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = apiVersion, BaseAddress = "http://localhost", Port = 5214 };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(configuration);
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsValid);
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -67,9 +67,9 @@ public class ServerConfigurationModelValidatorTests
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = '1', BaseAddress = "http://localhost", Port = 5214 };
|
||||
|
||||
// Act
|
||||
ValidationResult result = _validator.Validate(configuration);
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsValid);
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user