mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13:11:10 +00:00
Adds authentication unit tests
This commit is contained in:
1 parent
a6dcf65d1c
commit
4f91f29b62
101 files changed
+5549
-135
No files matched your search
+1
-1
@@ -60,4 +60,4 @@ public class DatabaseSettingsModelValidatorTests
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.DatabaseConnectionStringCannotBeEmpty.Description);
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using FluentAssertions;
|
||||
using Lumina.Infrastructure.Common.Errors;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using Lumina.Infrastructure.Common.Validators;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="EncryptionSettingsModelValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class EncryptionSettingsModelValidatorTests
|
||||
{
|
||||
private readonly EncryptionSettingsModelValidator _validator;
|
||||
private readonly IFixture _fixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EncryptionSettingsModelValidatorTests"/> class.
|
||||
/// </summary>
|
||||
public EncryptionSettingsModelValidatorTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
_validator = new();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncryptionSettingsModelValidator_WhenValidBase64SecretKeyProvided_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
EncryptionSettingsModel model = _fixture.Build<EncryptionSettingsModel>()
|
||||
.With(x => x.SecretKey, "dGhpcyBpcyBhIHZhbGlkIGJhc2U2NCBzdHJpbmc=") // "this is a valid base64 string" in base64
|
||||
.Create();
|
||||
|
||||
// Act
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EncryptionSettingsModelValidator_WhenSecretKeyIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
EncryptionSettingsModel model = _fixture.Build<EncryptionSettingsModel>()
|
||||
.With(x => x.SecretKey, string.Empty)
|
||||
.Create();
|
||||
|
||||
// Act
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.EncryptionSecretKeyCannotBeEmpty.Description);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("not-a-base64-string")]
|
||||
[InlineData("invalid!base64")]
|
||||
[InlineData("123")]
|
||||
public void EncryptionSettingsModelValidator_WhenSecretKeyIsNotBase64_ShouldHaveValidationError(string invalidBase64)
|
||||
{
|
||||
// Arrange
|
||||
EncryptionSettingsModel model = _fixture.Build<EncryptionSettingsModel>()
|
||||
.With(x => x.SecretKey, invalidBase64)
|
||||
.Create();
|
||||
|
||||
// Act
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.EncryptionSecretKeyMustBeABase64String.Description);
|
||||
}
|
||||
}
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using FluentAssertions;
|
||||
using Lumina.Infrastructure.Common.Errors;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using Lumina.Infrastructure.Common.Validators;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="JwtSettingsModelValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class JwtSettingsModelValidatorTests
|
||||
{
|
||||
private readonly JwtSettingsModelValidator _validator;
|
||||
private readonly IFixture _fixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JwtSettingsModelValidatorTests"/> class.
|
||||
/// </summary>
|
||||
public JwtSettingsModelValidatorTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
_validator = new();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JwtSettingsModelValidator_WhenAllPropertiesValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
JwtSettingsModel model = _fixture.Build<JwtSettingsModel>()
|
||||
.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
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JwtSettingsModelValidator_WhenSecretKeyIsEmpty_ShouldHaveValidationErrors()
|
||||
{
|
||||
// Arrange
|
||||
JwtSettingsModel model = _fixture.Build<JwtSettingsModel>()
|
||||
.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
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().HaveCount(2);
|
||||
result.Errors.Select(e => e.ErrorMessage).Should().Contain(new[]
|
||||
{
|
||||
Errors.Configuration.JwtSecretKeyCannotBeEmpty.Description,
|
||||
Errors.Configuration.JwtSecretKeyTooShort.Description
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JwtSettingsModelValidator_WhenSecretKeyTooShort_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
JwtSettingsModel model = _fixture.Build<JwtSettingsModel>()
|
||||
.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
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.JwtSecretKeyTooShort.Description);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
[InlineData(-60)]
|
||||
public void JwtSettingsModelValidator_WhenExpiryMinutesNotPositive_ShouldHaveValidationError(int minutes)
|
||||
{
|
||||
// Arrange
|
||||
JwtSettingsModel model = _fixture.Build<JwtSettingsModel>()
|
||||
.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
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.JwtExpiryMinutesMustBePositive.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JwtSettingsModelValidator_WhenIssuerIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
JwtSettingsModel model = _fixture.Build<JwtSettingsModel>()
|
||||
.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
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.JwtIssuerCannotBeEmpty.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JwtSettingsModelValidator_WhenAudienceIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
JwtSettingsModel model = _fixture.Build<JwtSettingsModel>()
|
||||
.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
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.JwtAudienceCannotBeEmpty.Description);
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using FluentAssertions;
|
||||
using Lumina.Infrastructure.Common.Errors;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using Lumina.Infrastructure.Common.Validators;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Common.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="MediaSettingsModelValidator"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaSettingsModelValidatorTests
|
||||
{
|
||||
private readonly MediaSettingsModelValidator _validator;
|
||||
private readonly IFixture _fixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaSettingsModelValidatorTests"/> class.
|
||||
/// </summary>
|
||||
public MediaSettingsModelValidatorTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
_validator = new();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MediaSettingsModelValidator_WhenRootDirectoryProvided_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
MediaSettingsModel model = _fixture.Build<MediaSettingsModel>()
|
||||
.With(x => x.RootDirectory, "/path/to/media")
|
||||
.Create();
|
||||
|
||||
// Act
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
result.Errors.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MediaSettingsModelValidator_WhenRootDirectoryIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
MediaSettingsModel model = _fixture.Build<MediaSettingsModel>()
|
||||
.With(x => x.RootDirectory, string.Empty)
|
||||
.Create();
|
||||
|
||||
// Act
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.MediaRootDirectoryCannotBeEmpty.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MediaSettingsModelValidator_WhenRootDirectoryIsWhitespace_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
MediaSettingsModel model = _fixture.Build<MediaSettingsModel>()
|
||||
.With(x => x.RootDirectory, " ")
|
||||
.Create();
|
||||
|
||||
// Act
|
||||
FluentValidation.Results.ValidationResult result = _validator.Validate(model);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which.ErrorMessage.Should().Be(Errors.Configuration.MediaRootDirectoryCannotBeEmpty.Description);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user