mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 14:11:10 +00:00
Standardizes test conventions and documentation
This commit is contained in:
1 parent
7aeed04255
commit
f42dc709de
190 files changed
+2180
-2814
No files matched your search
@@ -5,6 +5,7 @@ using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.DTO.FileSystemManagement;
|
||||
using Lumina.Presentation.Web.Common.Exceptions;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.Api;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.DTO.Configuration;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
@@ -26,7 +27,7 @@ namespace Lumina.Presentation.Web.UnitTests.Common.Api;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ApiHttpClientTests
|
||||
{
|
||||
private const string TEST_ENCRYPTION_KEY = "FLYO0QRo6u2VzoFOgNkkEwYNGtqhJ3QGZd7iAHNEJeM=";
|
||||
private readonly ServerConfigurationDtoFixture _serverConfigurationDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public async Task GetAsync_WhenApiReturnsSuccess_ShouldDeserializeResponseContent()
|
||||
@@ -36,7 +37,7 @@ public class ApiHttpClientTests
|
||||
ApiHttpClient sut = CreateSut(messageHandler);
|
||||
|
||||
// Act
|
||||
Lumina.Presentation.Web.Common.DTO.Libraries.LibraryDto[] result = await sut.GetAsync<Lumina.Presentation.Web.Common.DTO.Libraries.LibraryDto[]>("libraries", CancellationToken.None);
|
||||
Web.Common.DTO.Libraries.LibraryDto[] result = await sut.GetAsync<Web.Common.DTO.Libraries.LibraryDto[]>("libraries", CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result);
|
||||
@@ -152,21 +153,35 @@ public class ApiHttpClientTests
|
||||
Assert.Equal("image/png", blob.ContentType);
|
||||
}
|
||||
|
||||
private static ApiHttpClient CreateSut(TestApiHttpMessageHandler messageHandler, string? token = null)
|
||||
/// <summary>
|
||||
/// Creates the system under test configured with the provided message handler.
|
||||
/// </summary>
|
||||
/// <param name="messageHandler">The message handler backing the inner <see cref="HttpClient"/>.</param>
|
||||
/// <param name="token">Optional token claim to place on the current HTTP context user.</param>
|
||||
/// <returns>The created <see cref="ApiHttpClient"/>.</returns>
|
||||
private ApiHttpClient CreateSut(TestApiHttpMessageHandler messageHandler, string? token = null)
|
||||
{
|
||||
HttpClient httpClient = new(messageHandler);
|
||||
IHttpContextAccessor httpContextAccessor = Substitute.For<IHttpContextAccessor>();
|
||||
if (token is not null)
|
||||
{
|
||||
DefaultHttpContext httpContext = new();
|
||||
httpContext.User = new ClaimsPrincipal(new ClaimsIdentity([new Claim("Token", token)], "TestAuthentication"));
|
||||
DefaultHttpContext httpContext = new()
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity([new Claim("Token", token)], "TestAuthentication"))
|
||||
};
|
||||
httpContextAccessor.HttpContext.Returns(httpContext);
|
||||
}
|
||||
IOptionsSnapshot<ServerConfigurationDto> serverConfigurationOptions = Substitute.For<IOptionsSnapshot<ServerConfigurationDto>>();
|
||||
serverConfigurationOptions.Value.Returns(new ServerConfigurationDto { ApiVersion = '1', BaseAddress = "http://localhost", Port = 5214 });
|
||||
serverConfigurationOptions.Value.Returns(_serverConfigurationDtoFixture.Create(apiVersion: '1', baseAddress: "http://localhost", port: 5214));
|
||||
return new ApiHttpClient(httpClient, httpContextAccessor, serverConfigurationOptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an <see cref="HttpResponseMessage"/> with the given status code and JSON body.
|
||||
/// </summary>
|
||||
/// <param name="statusCode">The HTTP status code of the response.</param>
|
||||
/// <param name="json">The JSON payload of the response body.</param>
|
||||
/// <returns>The created <see cref="HttpResponseMessage"/>.</returns>
|
||||
private static HttpResponseMessage CreateJsonResponse(HttpStatusCode statusCode, string json)
|
||||
{
|
||||
return new HttpResponseMessage(statusCode)
|
||||
|
||||
+7
-5
@@ -3,6 +3,7 @@ using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.Authorization;
|
||||
using Lumina.Presentation.Web.Common.Enums.Authorization;
|
||||
using Lumina.Presentation.Web.Common.Responses.Authorization;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.Responses.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
@@ -23,6 +24,7 @@ public class AuthorizationServiceTests
|
||||
private readonly IApiHttpClient _mockApiHttpClient;
|
||||
private readonly AuthorizationService _sut;
|
||||
private readonly Guid _userId;
|
||||
private readonly GetAuthorizationResponseFixture _getAuthorizationResponseFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AuthorizationServiceTests"/> class.
|
||||
@@ -43,7 +45,7 @@ public class AuthorizationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_mockApiHttpClient.GetAsync<GetAuthorizationResponse>(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new GetAuthorizationResponse(_userId, "Admin", [AuthorizationPermission.CanCreateLibraries]));
|
||||
.Returns(_getAuthorizationResponseFixture.Create(userId: _userId, role: "Admin", permissions: [AuthorizationPermission.CanCreateLibraries]));
|
||||
|
||||
// Act
|
||||
bool result = await _sut.HasPermissionAsync(AuthorizationPermission.CanCreateLibraries, CancellationToken.None);
|
||||
@@ -57,7 +59,7 @@ public class AuthorizationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_mockApiHttpClient.GetAsync<GetAuthorizationResponse>(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new GetAuthorizationResponse(_userId, "Admin", [AuthorizationPermission.CanViewUsers]));
|
||||
.Returns(_getAuthorizationResponseFixture.Create(userId: _userId, role: "Admin", permissions: [AuthorizationPermission.CanViewUsers]));
|
||||
|
||||
// Act
|
||||
bool result = await _sut.HasPermissionAsync(AuthorizationPermission.CanCreateLibraries, CancellationToken.None);
|
||||
@@ -71,7 +73,7 @@ public class AuthorizationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_mockApiHttpClient.GetAsync<GetAuthorizationResponse>(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new GetAuthorizationResponse(_userId, "Admin", []));
|
||||
.Returns(_getAuthorizationResponseFixture.Create(userId: _userId, role: "Admin", permissions: []));
|
||||
|
||||
// Act
|
||||
bool result = await _sut.IsInRoleAsync("Admin", CancellationToken.None);
|
||||
@@ -85,7 +87,7 @@ public class AuthorizationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_mockApiHttpClient.GetAsync<GetAuthorizationResponse>(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new GetAuthorizationResponse(_userId, "User", []));
|
||||
.Returns(_getAuthorizationResponseFixture.Create(userId: _userId, role: "User", permissions: []));
|
||||
|
||||
// Act
|
||||
bool result = await _sut.IsInRoleAsync("Admin", CancellationToken.None);
|
||||
@@ -99,7 +101,7 @@ public class AuthorizationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_mockApiHttpClient.GetAsync<GetAuthorizationResponse>(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new GetAuthorizationResponse(_userId, "Admin", []));
|
||||
.Returns(_getAuthorizationResponseFixture.Create(userId: _userId, role: "Admin", permissions: []));
|
||||
|
||||
// Act
|
||||
await _sut.HasPermissionAsync(AuthorizationPermission.CanViewUsers, CancellationToken.None);
|
||||
|
||||
+10
-8
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.Security;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.DTO.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -15,6 +16,7 @@ namespace Lumina.Presentation.Web.UnitTests.Common.Security;
|
||||
public class CryptographyServiceTests
|
||||
{
|
||||
private const string TEST_ENCRYPTION_KEY = "FLYO0QRo6u2VzoFOgNkkEwYNGtqhJ3QGZd7iAHNEJeM=";
|
||||
private readonly EncryptionSettingsDtoFixture _encryptionSettingsDtoFixture = new();
|
||||
private readonly CryptographyService _sut;
|
||||
|
||||
/// <summary>
|
||||
@@ -22,7 +24,7 @@ public class CryptographyServiceTests
|
||||
/// </summary>
|
||||
public CryptographyServiceTests()
|
||||
{
|
||||
EncryptionSettingsDto settings = new() { SecretKey = TEST_ENCRYPTION_KEY };
|
||||
EncryptionSettingsDto settings = _encryptionSettingsDtoFixture.Create(secretKey: TEST_ENCRYPTION_KEY);
|
||||
_sut = new CryptographyService(Options.Create(settings));
|
||||
}
|
||||
|
||||
@@ -30,25 +32,25 @@ public class CryptographyServiceTests
|
||||
public void Encrypt_WhenCalled_ShouldReturnCiphertextThatDecryptsToPlaintext()
|
||||
{
|
||||
// Arrange
|
||||
const string plaintext = "sensitive-data";
|
||||
const string PLAINTEXT = "sensitive-data";
|
||||
|
||||
// Act
|
||||
string ciphertext = _sut.Encrypt(plaintext);
|
||||
string ciphertext = _sut.Encrypt(PLAINTEXT);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(plaintext, ciphertext);
|
||||
Assert.Equal(plaintext, _sut.Decrypt(ciphertext));
|
||||
Assert.NotEqual(PLAINTEXT, ciphertext);
|
||||
Assert.Equal(PLAINTEXT, _sut.Decrypt(ciphertext));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Encrypt_WhenCalledTwice_ShouldReturnDifferentCiphertexts()
|
||||
{
|
||||
// Arrange
|
||||
const string plaintext = "sensitive-data";
|
||||
const string PLAINTEXT = "sensitive-data";
|
||||
|
||||
// Act
|
||||
string firstCiphertext = _sut.Encrypt(plaintext);
|
||||
string secondCiphertext = _sut.Encrypt(plaintext);
|
||||
string firstCiphertext = _sut.Encrypt(PLAINTEXT);
|
||||
string secondCiphertext = _sut.Encrypt(PLAINTEXT);
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(firstCiphertext, secondCiphertext);
|
||||
|
||||
+5
-3
@@ -2,6 +2,7 @@
|
||||
using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.Primitives;
|
||||
using Lumina.Presentation.Web.Common.Validators;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.UnitTests.Common.Setup;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -16,13 +17,14 @@ namespace Lumina.Presentation.Web.UnitTests.Common.Validators;
|
||||
public class EncryptionSettingsModelValidatorTests
|
||||
{
|
||||
private const string VALID_BASE64_KEY = "FLYO0QRo6u2VzoFOgNkkEwYNGtqhJ3QGZd7iAHNEJeM=";
|
||||
private readonly EncryptionSettingsDtoFixture _encryptionSettingsDtoFixture = new();
|
||||
private readonly EncryptionSettingsDtoValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenSecretKeyIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
EncryptionSettingsDto settings = new() { SecretKey = string.Empty };
|
||||
EncryptionSettingsDto settings = _encryptionSettingsDtoFixture.Create(secretKey: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(settings);
|
||||
@@ -35,7 +37,7 @@ public class EncryptionSettingsModelValidatorTests
|
||||
public void Validate_WhenSecretKeyIsNotBase64_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
EncryptionSettingsDto settings = new() { SecretKey = "not-base64-key!" };
|
||||
EncryptionSettingsDto settings = _encryptionSettingsDtoFixture.Create(secretKey: "not-base64-key!");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(settings);
|
||||
@@ -48,7 +50,7 @@ public class EncryptionSettingsModelValidatorTests
|
||||
public void Validate_WhenSecretKeyIsValidBase64_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
EncryptionSettingsDto settings = new() { SecretKey = VALID_BASE64_KEY };
|
||||
EncryptionSettingsDto settings = _encryptionSettingsDtoFixture.Create(secretKey: VALID_BASE64_KEY);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(settings);
|
||||
|
||||
+6
-4
@@ -2,6 +2,7 @@
|
||||
using Lumina.Presentation.Web.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.Common.Primitives;
|
||||
using Lumina.Presentation.Web.Common.Validators;
|
||||
using Lumina.Presentation.Web.Fixtures.Common.DTO.Configuration;
|
||||
using Lumina.Presentation.Web.UnitTests.Common.Setup;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -15,13 +16,14 @@ namespace Lumina.Presentation.Web.UnitTests.Common.Validators;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ServerConfigurationModelValidatorTests
|
||||
{
|
||||
private readonly ServerConfigurationDtoFixture _serverConfigurationDtoFixture = new();
|
||||
private readonly ServerConfigurationDtoValidator _validator = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBaseAddressIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = '1', BaseAddress = string.Empty, Port = 5214 };
|
||||
ServerConfigurationDto configuration = _serverConfigurationDtoFixture.Create(apiVersion: '1', baseAddress: string.Empty, port: 5214);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
@@ -36,7 +38,7 @@ public class ServerConfigurationModelValidatorTests
|
||||
public void Validate_WhenPortIsAtBoundary_ShouldNotHaveValidationError(ushort port)
|
||||
{
|
||||
// Arrange
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = '1', BaseAddress = "http://localhost", Port = port };
|
||||
ServerConfigurationDto configuration = _serverConfigurationDtoFixture.Create(apiVersion: '1', baseAddress: "http://localhost", port: port);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
@@ -51,7 +53,7 @@ public class ServerConfigurationModelValidatorTests
|
||||
public void Validate_WhenApiVersionIsAtBoundary_ShouldNotHaveValidationError(char apiVersion)
|
||||
{
|
||||
// Arrange
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = apiVersion, BaseAddress = "http://localhost", Port = 5214 };
|
||||
ServerConfigurationDto configuration = _serverConfigurationDtoFixture.Create(apiVersion: apiVersion, baseAddress: "http://localhost", port: 5214);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
@@ -64,7 +66,7 @@ public class ServerConfigurationModelValidatorTests
|
||||
public void Validate_WhenConfigurationIsValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
ServerConfigurationDto configuration = new() { ApiVersion = '1', BaseAddress = "http://localhost", Port = 5214 };
|
||||
ServerConfigurationDto configuration = _serverConfigurationDtoFixture.Create(apiVersion: '1', baseAddress: "http://localhost", port: 5214);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(configuration);
|
||||
|
||||
Reference in new issue
Block a user