mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 05:11:10 +00:00
Implements tests for commit 2e9f7d8496
This commit is contained in:
1 parent
a75447062f
commit
468f1846f1
18 files changed
+1711
-48
No files matched your search
+220
@@ -6,6 +6,7 @@ using Lumina.Contracts.Fixtures.Core.Requests.UsersManagement.Settings;
|
||||
using Lumina.Contracts.Requests.UsersManagement.Settings;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Playback;
|
||||
using Lumina.Infrastructure.Core.Security;
|
||||
using Lumina.Presentation.Api.SecurityTests.Common.Setup;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -14,7 +15,9 @@ using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Settings.UpdateUserSettings;
|
||||
@@ -122,6 +125,223 @@ public class UpdateUserSettingsEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
Assert.DoesNotContain("stack trace", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(int.MaxValue)] // far above the maximum
|
||||
[InlineData(int.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterSegmentCountIsAtAnIntExtreme_ShouldReturnValidationErrorWithoutCrashing(int vuMeterSegmentCount)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(vuMeterSegmentCount: vuMeterSegmentCount);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(int.MaxValue)] // far above the maximum
|
||||
[InlineData(int.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterSegmentGapIsAtAnIntExtreme_ShouldReturnValidationErrorWithoutCrashing(int vuMeterSegmentGap)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(vuMeterSegmentGap: vuMeterSegmentGap);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(double.MaxValue)] // far above the maximum
|
||||
[InlineData(double.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterMediumThresholdIsAtADoubleExtreme_ShouldReturnValidationErrorWithoutCrashing(double vuMeterMediumThreshold)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(
|
||||
vuMeterMediumThreshold: vuMeterMediumThreshold,
|
||||
vuMeterHighThreshold: 1.0);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(double.MaxValue)] // far above the maximum
|
||||
[InlineData(double.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterHighThresholdIsAtADoubleExtreme_ShouldReturnValidationErrorWithoutCrashing(double vuMeterHighThreshold)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(
|
||||
vuMeterMediumThreshold: 0.0,
|
||||
vuMeterHighThreshold: vuMeterHighThreshold);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(int.MaxValue)] // far above the maximum
|
||||
[InlineData(int.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterPeakHoldDurationIsAtAnIntExtreme_ShouldReturnValidationErrorWithoutCrashing(int vuMeterPeakHoldDurationInMilliseconds)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(
|
||||
vuMeterPeakHoldDurationInMilliseconds: vuMeterPeakHoldDurationInMilliseconds);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(double.MaxValue)] // far above the maximum
|
||||
[InlineData(double.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterReleasePerSecondIsAtADoubleExtreme_ShouldReturnValidationErrorWithoutCrashing(double vuMeterReleasePerSecond)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(vuMeterReleasePerSecond: vuMeterReleasePerSecond);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(double.MaxValue)] // far above the maximum
|
||||
[InlineData(double.MinValue)] // far below the minimum
|
||||
public async Task UpdateUserSettings_WhenVuMeterPeakReleasePerSecondIsAtADoubleExtreme_ShouldReturnValidationErrorWithoutCrashing(double vuMeterPeakReleasePerSecond)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(vuMeterPeakReleasePerSecond: vuMeterPeakReleasePerSecond);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateUserSettings_WhenVuMeterOrientationIsAnUndefinedValue_ShouldReturnValidationErrorWithoutCrashing()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest request = _updateUserSettingsRequestFixture.Create(vuMeterOrientation: (VuMeterOrientation)999);
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsJsonAsync("/api/v1/users/me/settings", request);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
}
|
||||
|
||||
[Theory(Skip = "Production bug: a JSON payload whose VuMeterOrientation is not a valid enumeration value makes the FastEndpoints binder throw a System.Text.Json.JsonException, which escapes because UpdateUserSettingsEndpoint calls DontCatchExceptions(), so the API responds with 500 instead of 400 or 422. The body is the generic ProblemDetails and leaks no internals. The test stays skipped until the endpoint maps binding failures to a client error.")]
|
||||
[InlineData("'; DROP TABLE UserSettings; --")] // SQL injection
|
||||
[InlineData("$(rm -rf /)")] // command injection
|
||||
[InlineData("{{7*7}}")] // template injection
|
||||
public async Task UpdateUserSettings_WhenVuMeterOrientationIsAMaliciousString_ShouldReturnClientErrorWithoutLeakingInternals(string maliciousOrientation)
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest validRequest = _updateUserSettingsRequestFixture.Create();
|
||||
JsonObject payload = JsonSerializer.SerializeToNode(
|
||||
validRequest,
|
||||
new JsonSerializerOptions(JsonSerializerDefaults.Web))!.AsObject();
|
||||
payload["vuMeterOrientation"] = maliciousOrientation;
|
||||
StringContent requestContent = new(payload.ToJsonString(), Encoding.UTF8, "application/json");
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsync("/api/v1/users/me/settings", requestContent);
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Assert
|
||||
Assert.True(
|
||||
response.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.UnprocessableEntity,
|
||||
$"Expected a 400 or 422 response, but got {(int)response.StatusCode}.");
|
||||
AssertResponseDoesNotLeakInternalDetails(content);
|
||||
Assert.DoesNotContain("DROP TABLE", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("rm -rf", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateUserSettings_WhenTheRawJsonPayloadIsValid_ShouldUpdateSettings()
|
||||
{
|
||||
// Arrange
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
await CreateAndAuthenticateUserAsync(client);
|
||||
UpdateUserSettingsRequest validRequest = _updateUserSettingsRequestFixture.Create();
|
||||
JsonObject payload = JsonSerializer.SerializeToNode(
|
||||
validRequest,
|
||||
new JsonSerializerOptions(JsonSerializerDefaults.Web))!.AsObject();
|
||||
StringContent requestContent = new(payload.ToJsonString(), Encoding.UTF8, "application/json");
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await client.PutAsync("/api/v1/users/me/settings", requestContent);
|
||||
|
||||
// Assert
|
||||
response.EnsureSuccessStatusCode();
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the response body does not expose exception details, stack frames, or database internals.
|
||||
/// </summary>
|
||||
/// <param name="content">The response body to inspect.</param>
|
||||
private static void AssertResponseDoesNotLeakInternalDetails(string content)
|
||||
{
|
||||
Assert.DoesNotContain("Exception", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("stack trace", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain(" at Lumina.", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Microsoft.Data.Sqlite", content, StringComparison.OrdinalIgnoreCase);
|
||||
Assert.DoesNotContain("Microsoft.EntityFrameworkCore", content, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a test user and authenticates it on <paramref name="client"/>.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user