mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 16:11:10 +00:00
Adds book editing tests and parameterizes the shared test fixtures
This commit is contained in:
1 parent
1bfa07ba5d
commit
145185d715
354 files changed
+7118
-1743
No files matched your search
+6
-6
@@ -20,7 +20,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/change-password</c> route.
|
||||
/// Contains security tests for the <see cref="ChangePasswordEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ChangePasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
@@ -121,9 +121,9 @@ public class ChangePasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDis
|
||||
HttpResponseMessage response = await _client.PostAsJsonAsync("/api/v1/auth/change-password", request);
|
||||
|
||||
// Assert
|
||||
// the malicious username is queried by GetByUsernameAsync, so it must never be executed against the database;
|
||||
// The malicious username is queried by GetByUsernameAsync, so it must never be executed against the database;
|
||||
// if a boolean-injection regression returned the only user in the database, the current password would match and
|
||||
// the password would change, so the response must be a failure and the stored password hash must be unchanged
|
||||
// the password would change, so the response must be a failure and the stored password hash must be unchanged.
|
||||
Assert.NotEqual(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.DoesNotContain("SqliteException", await response.Content.ReadAsStringAsync(), StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
@@ -157,14 +157,14 @@ public class ChangePasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDis
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
|
||||
// create test user
|
||||
// Create test user.
|
||||
UserEntity user = _userEntityFixture.Create(username: _testUsername, password: _hashService.HashString("TestPass123!"));
|
||||
user.TotpSecret = null;
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
// authenticate user
|
||||
// Authenticate user.
|
||||
LoginRequest loginRequest = new(
|
||||
Username: user.Username,
|
||||
Password: "TestPass123!"
|
||||
@@ -174,7 +174,7 @@ public class ChangePasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDis
|
||||
string content = await loginResponse.Content.ReadAsStringAsync();
|
||||
LoginResponse? result = JsonSerializer.Deserialize<LoginResponse>(content, _jsonOptions);
|
||||
|
||||
// set auth header for subsequent requests
|
||||
// Set auth header for subsequent requests.
|
||||
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result!.Token);
|
||||
|
||||
return user;
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/users</c> route.
|
||||
/// Contains security tests for the <see cref="GetUsersEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetUsersEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
+8
-8
@@ -23,7 +23,7 @@ using System.Text.Json.Serialization;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/login</c> route.
|
||||
/// Contains security tests for the <see cref="LoginEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
@@ -50,7 +50,7 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// set a fake IP for this test instance
|
||||
// Set a fake IP for this test instance.
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", $"192.168.1.1");
|
||||
_testUsername = $"testuser_{Guid.NewGuid()}";
|
||||
@@ -85,7 +85,7 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
DateTimeOffset tokenExpiration = DateTimeOffset.FromUnixTimeSeconds(long.Parse(expValue));
|
||||
DateTimeOffset expectedExpiration = beforeRequest.AddMinutes(15);
|
||||
|
||||
// verify token duration (should be 15 minutes from request time - expiration value is set in tests setup class)
|
||||
// Verify token duration (should be 15 minutes from request time - expiration value is set in tests setup class).
|
||||
TimeSpan allowedVariance = TimeSpan.FromSeconds(30); // allow for processing time
|
||||
TimeSpan actualDifference = tokenExpiration - beforeRequest;
|
||||
TimeSpan expectedDifference = TimeSpan.FromMinutes(15);
|
||||
@@ -146,7 +146,7 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
public async Task Login_WhenRateLimitExceeded_ShouldReturnTooManyRequests()
|
||||
{
|
||||
// Arrange
|
||||
// set a fake IP for this test instance
|
||||
// Set a fake IP for this test instance.
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", $"192.168.1.3");
|
||||
LoginRequest request = _loginRequestFixture.Create(
|
||||
@@ -156,7 +156,7 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
|
||||
// Act
|
||||
List<HttpResponseMessage> responses = [];
|
||||
for (int i = 0; i < 11; i++) // exceed the 10 request limit
|
||||
for (int i = 0; i < 11; i++) // Exceed the 10 request limit.
|
||||
responses.Add(await _client.PostAsJsonAsync("/api/v1/auth/login", request));
|
||||
|
||||
// Assert
|
||||
@@ -200,10 +200,10 @@ public class LoginEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
|
||||
Assert.DoesNotContain("SQL", content);
|
||||
Assert.DoesNotContain("Exception", content);
|
||||
Assert.DoesNotContain(legitimateUser.Username, content); // shouldn't expose other usernames
|
||||
Assert.DoesNotContain(legitimateUser.Password, content); // shouldn't expose password hashes
|
||||
Assert.DoesNotContain(legitimateUser.Username, content); // Shouldn't expose other usernames.
|
||||
Assert.DoesNotContain(legitimateUser.Password, content); // Shouldn't expose password hashes.
|
||||
|
||||
// the injected statement must never be executed: the Users table and the seeded user must still be there
|
||||
// The injected statement must never be executed: the Users table and the seeded user must still be there.
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
Assert.NotNull(await dbContext.Users.FirstOrDefaultAsync(user => user.Id == legitimateUser.Id));
|
||||
|
||||
+6
-6
@@ -23,7 +23,7 @@ using System.Threading.Tasks;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/recover-password</c> route.
|
||||
/// Contains security tests for the <see cref="RecoverPasswordEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RecoverPasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
@@ -47,7 +47,7 @@ public class RecoverPasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDi
|
||||
{
|
||||
_apiFactory = apiFactory;
|
||||
_client = apiFactory.CreateClient();
|
||||
// set a fake IP for this test instance
|
||||
// Set a fake IP for this test instance.
|
||||
_client.DefaultRequestHeaders.Clear();
|
||||
_client.DefaultRequestHeaders.Add("X-Forwarded-For", $"192.168.1.4");
|
||||
_testUsername = $"testuser_{Guid.NewGuid()}";
|
||||
@@ -64,7 +64,7 @@ public class RecoverPasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDi
|
||||
|
||||
// Act
|
||||
List<HttpResponseMessage> responses = [];
|
||||
for (int i = 0; i < 11; i++) // exceed the 10 request limit
|
||||
for (int i = 0; i < 11; i++) // Exceed the 10 request limit.
|
||||
responses.Add(await _client.PostAsJsonAsync("/api/v1/auth/recover-password", request));
|
||||
|
||||
// Assert
|
||||
@@ -91,8 +91,8 @@ public class RecoverPasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDi
|
||||
public async Task RecoverPassword_WithSQLInjectionInUsername_ShouldNotCorruptOrDeleteData(string maliciousUsername)
|
||||
{
|
||||
// Arrange
|
||||
// use a dedicated unique IP, so the rate-limit test (which exhausts the 10-permit window on its own IP)
|
||||
// cannot run first and 429 this test; the NotEqual(TooManyRequests) assertion below guards against any collision
|
||||
// Use a dedicated unique IP, so the rate-limit test (which exhausts the 10-permit window on its own IP)
|
||||
// cannot run first and 429 this test; the NotEqual(TooManyRequests) assertion below guards against any collision.
|
||||
HttpClient client = _apiFactory.CreateClient();
|
||||
client.DefaultRequestHeaders.Clear();
|
||||
client.DefaultRequestHeaders.Add("X-Forwarded-For", LuminaApiFactory.GetUniqueTestIp());
|
||||
@@ -106,7 +106,7 @@ public class RecoverPasswordEndpointTests : IClassFixture<LuminaApiFactory>, IDi
|
||||
HttpResponseMessage response = await client.PostAsJsonAsync("/api/v1/auth/recover-password", request);
|
||||
|
||||
// Assert
|
||||
// the malicious username queries for a user, so it must never be executed against the database
|
||||
// The malicious username queries for a user, so it must never be executed against the database.
|
||||
Assert.NotEqual(HttpStatusCode.TooManyRequests, response.StatusCode);
|
||||
Assert.DoesNotContain("SqliteException", await response.Content.ReadAsStringAsync(), StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
|
||||
+3
-3
@@ -18,7 +18,7 @@ using System.Text.Json.Serialization;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/register</c> route.
|
||||
/// Contains security tests for the <see cref="RegisterEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RegisterEndpointTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
@@ -114,8 +114,8 @@ public class RegisterEndpointTests : IClassFixture<LuminaApiFactory>, IDisposabl
|
||||
public async Task Register_WithSQLInjectionAttempt_ShouldRemainSecure(string maliciousUsername)
|
||||
{
|
||||
// Arrange
|
||||
// note: the register validator rejects these usernames (invalid username format) with 422 before any DB access,
|
||||
// so the malicious username never reaches a query; the 422 assertion below is the security guarantee
|
||||
// Note: the register validator rejects these usernames (invalid username format) with 422 before any DB access,
|
||||
// so the malicious username never reaches a query; the 422 assertion below is the security guarantee.
|
||||
RegistrationRequest request = _registrationRequestFixture.Create(
|
||||
username: maliciousUsername,
|
||||
password: "TestPass123!",
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/get-authorization</c> route.
|
||||
/// Contains security tests for the <see cref="GetAuthorizationEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetAuthorizationEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/users/{userId}/permissions</c> route.
|
||||
/// Contains security tests for the <see cref="GetUserPermissionsEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetUserPermissionsEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/users/{userId}/role</c> route.
|
||||
/// Contains security tests for the <see cref="GetUserRoleEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetUserRoleEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/auth/users/{userId}/role-and-permissions</c> route.
|
||||
/// Contains security tests for the <see cref="UpdateUserRoleAndPermissionsEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UpdateUserRoleAndPermissionsEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Settings.GetUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/users/me/settings</c> route.
|
||||
/// Contains security tests for the <see cref="GetUserSettingsEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetUserSettingsEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ using System.Text.Json;
|
||||
namespace Lumina.Presentation.Api.SecurityTests.Core.Endpoints.UsersManagement.Settings.UpdateUserSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Contains security tests for the <c>/users/me/settings</c> route.
|
||||
/// Contains security tests for the <see cref="UpdateUserSettingsEndpoint"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UpdateUserSettingsEndpointTests : IClassFixture<LuminaApiFactory>
|
||||
|
||||
Reference in new issue
Block a user