mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 22:01:10 +00:00
148 lines
5.0 KiB
C#
148 lines
5.0 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using AutoFixture;
|
|
using AutoFixture.AutoNSubstitute;
|
|
using Nemera.Domain.Common.Primitives;
|
|
using Nemera.Infrastructure.Common.Errors;
|
|
using Nemera.Infrastructure.Common.Models.DTO.Configuration;
|
|
using Nemera.Infrastructure.Common.Validators;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
#endregion
|
|
|
|
namespace Nemera.Infrastructure.UnitTests.Common.Validators;
|
|
|
|
/// <summary>
|
|
/// Contains unit tests for the <see cref="JwtSettingsDtoValidator"/> class.
|
|
/// </summary>
|
|
[ExcludeFromCodeCoverage]
|
|
public class JwtSettingsDtoValidatorTests
|
|
{
|
|
private readonly JwtSettingsDtoValidator _validator = new();
|
|
private readonly IFixture _fixture;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="JwtSettingsDtoValidatorTests"/> class.
|
|
/// </summary>
|
|
public JwtSettingsDtoValidatorTests()
|
|
{
|
|
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenAllPropertiesValid_ShouldNotHaveValidationError()
|
|
{
|
|
// Arrange
|
|
JwtSettingsDto model = _fixture.Build<JwtSettingsDto>()
|
|
.With(x => x.SecretKey, "this-is-a-very-long-secret-key-for-testing")
|
|
.With(x => x.ExpiryMinutes, 30)
|
|
.With(x => x.Issuer, "test-issuer")
|
|
.With(x => x.Audience, "test-audience")
|
|
.Create();
|
|
|
|
// Act
|
|
List<Error> result = _validator.Validate(model);
|
|
|
|
// Assert
|
|
Assert.Empty(result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenSecretKeyIsEmpty_ShouldHaveValidationErrors()
|
|
{
|
|
// Arrange
|
|
JwtSettingsDto model = _fixture.Build<JwtSettingsDto>()
|
|
.With(x => x.SecretKey, string.Empty)
|
|
.With(x => x.ExpiryMinutes, 30)
|
|
.With(x => x.Issuer, "test-issuer")
|
|
.With(x => x.Audience, "test-audience")
|
|
.Create();
|
|
|
|
// Act
|
|
List<Error> result = _validator.Validate(model);
|
|
|
|
// Assert
|
|
Assert.Equal(2, result.Count);
|
|
Assert.Contains(result, error => error.Description == Errors.Configuration.JwtSecretKeyCannotBeEmpty.Description);
|
|
Assert.Contains(result, error => error.Description == Errors.Configuration.JwtSecretKeyTooShort.Description);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenSecretKeyTooShort_ShouldHaveValidationError()
|
|
{
|
|
// Arrange
|
|
JwtSettingsDto model = _fixture.Build<JwtSettingsDto>()
|
|
.With(x => x.SecretKey, "short-key")
|
|
.With(x => x.ExpiryMinutes, 30)
|
|
.With(x => x.Issuer, "test-issuer")
|
|
.With(x => x.Audience, "test-audience")
|
|
.Create();
|
|
|
|
// Act
|
|
List<Error> result = _validator.Validate(model);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal(Errors.Configuration.JwtSecretKeyTooShort.Description, result[0].Description);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0)]
|
|
[InlineData(-1)]
|
|
[InlineData(-60)]
|
|
public void Validate_WhenExpiryMinutesNotPositive_ShouldHaveValidationError(int minutes)
|
|
{
|
|
// Arrange
|
|
JwtSettingsDto model = _fixture.Build<JwtSettingsDto>()
|
|
.With(x => x.SecretKey, "this-is-a-very-long-secret-key-for-testing")
|
|
.With(x => x.ExpiryMinutes, minutes)
|
|
.With(x => x.Issuer, "test-issuer")
|
|
.With(x => x.Audience, "test-audience")
|
|
.Create();
|
|
|
|
// Act
|
|
List<Error> result = _validator.Validate(model);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal(Errors.Configuration.JwtExpiryMinutesMustBePositive.Description, result[0].Description);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenIssuerIsEmpty_ShouldHaveValidationError()
|
|
{
|
|
// Arrange
|
|
JwtSettingsDto model = _fixture.Build<JwtSettingsDto>()
|
|
.With(x => x.SecretKey, "this-is-a-very-long-secret-key-for-testing")
|
|
.With(x => x.ExpiryMinutes, 30)
|
|
.With(x => x.Issuer, string.Empty)
|
|
.With(x => x.Audience, "test-audience")
|
|
.Create();
|
|
|
|
// Act
|
|
List<Error> result = _validator.Validate(model);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal(Errors.Configuration.JwtIssuerCannotBeEmpty.Description, result[0].Description);
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_WhenAudienceIsEmpty_ShouldHaveValidationError()
|
|
{
|
|
// Arrange
|
|
JwtSettingsDto model = _fixture.Build<JwtSettingsDto>()
|
|
.With(x => x.SecretKey, "this-is-a-very-long-secret-key-for-testing")
|
|
.With(x => x.ExpiryMinutes, 30)
|
|
.With(x => x.Issuer, "test-issuer")
|
|
.With(x => x.Audience, string.Empty)
|
|
.Create();
|
|
|
|
// Act
|
|
List<Error> result = _validator.Validate(model);
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal(Errors.Configuration.JwtAudienceCannotBeEmpty.Description, result[0].Description);
|
|
}
|
|
}
|