Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add E2E tests for ip logging enable/disable (#571)

Leendert de Borsta3b1fc0a21a6
diff --git a/src/AliasVault.Admin/Program.cs b/src/AliasVault.Admin/Program.cs
index f055df968..c093a9240 100644
--- a/src/AliasVault.Admin/Program.cs
+++ b/src/AliasVault.Admin/Program.cs
@@ -17,6 +17,7 @@ using AliasVault.Auth;
using AliasVault.Cryptography.Server;
using AliasVault.Logging;
using AliasVault.RazorComponents.Services;
+using AliasVault.Shared.Models.Configuration;
using AliasVault.Shared.Server.Services;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.HttpOverrides;
@@ -40,6 +41,7 @@ var ipLoggingEnabled = Environment.GetEnvironmentVariable("IP_LOGGING_ENABLED")
config.IpLoggingEnabled = bool.Parse(ipLoggingEnabled);
builder.Services.AddSingleton(config);
+builder.Services.AddSingleton<SharedConfig>(sp => sp.GetRequiredService<Config>());
builder.Services.AddAliasVaultDataProtection("AliasVault.Admin");
diff --git a/src/AliasVault.Api/Program.cs b/src/AliasVault.Api/Program.cs
index 078acb0f4..55f9525fb 100644
--- a/src/AliasVault.Api/Program.cs
+++ b/src/AliasVault.Api/Program.cs
@@ -14,6 +14,7 @@ using AliasVault.Api.Jwt;
using AliasVault.Auth;
using AliasVault.Cryptography.Server;
using AliasVault.Logging;
+using AliasVault.Shared.Models.Configuration;
using AliasVault.Shared.Providers.Time;
using AliasVault.Shared.Server.Services;
using Asp.Versioning;
@@ -38,6 +39,7 @@ var ipLoggingEnabled = Environment.GetEnvironmentVariable("IP_LOGGING_ENABLED")
config.IpLoggingEnabled = bool.Parse(ipLoggingEnabled);
builder.Services.AddSingleton(config);
+builder.Services.AddSingleton<SharedConfig>(sp => sp.GetRequiredService<Config>());
builder.Services.ConfigureLogging(builder.Configuration, Assembly.GetExecutingAssembly().GetName().Name!, "../../logs");
diff --git a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
index 95e150aeb..6499d9f1e 100644
--- a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
+++ b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
@@ -66,7 +66,7 @@ public abstract class PlaywrightTest
/// </summary>
/// <returns>Async task.</returns>
[OneTimeSetUp]
- public async Task OneTimeSetUp()
+ public virtual async Task OneTimeSetUp()
{
const int maxRetries = 10;
int currentRetry = 0;
@@ -230,7 +230,7 @@ public abstract class PlaywrightTest
}
/// <summary>
- /// Setup the Playwright test environment. This method is required to be implemented by the derived class.
+ /// Set up the Playwright test environment. This method is required to be implemented by the derived class.
/// </summary>
/// <returns>Async task.</returns>
protected abstract Task SetupEnvironment();
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs
index a31dd7e62..4f17aefa2 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard1/ApiTests.cs
@@ -8,6 +8,7 @@
namespace AliasVault.E2ETests.Tests.Client.Shard1;
using AliasServerDb;
+using AliasVault.Shared.Models.Enums;
using Microsoft.EntityFrameworkCore;
/// <summary>
@@ -18,6 +19,18 @@ using Microsoft.EntityFrameworkCore;
[NonParallelizable]
public class ApiTests : ClientPlaywrightTest
{
+ /// <summary>
+ /// Set the environment variables for the test.
+ /// </summary>
+ /// <returns>Async task.</returns>
+ [OneTimeSetUp]
+ public override async Task OneTimeSetUp()
+ {
+ // Enable IP logging for this test (default is false).
+ Environment.SetEnvironmentVariable("IP_LOGGING_ENABLED", "true");
+ await base.OneTimeSetUp();
+ }
+
/// <summary>
/// Test if an error in the API is logged to the database.
/// </summary>
@@ -43,6 +56,19 @@ public class ApiTests : ClientPlaywrightTest
Assert.That(logEntry.Exception, Does.Contain("Test error"), "Log entry in database does not contain expected message. Check exception and Serilog configuration.");
}
+ /// <summary>
+ /// Test if IP addresses are logged correctly when IP logging is enabled.
+ /// </summary>
+ /// <remarks>The opposite of this test is covered by AuthTests.cs.</remarks>
+ /// <returns>Async task.</returns>
+ [Test]
+ public async Task IpLoggingTest()
+ {
+ // Check if the IP address is not anonymized as we enabled IP logging for this test file, see OneTimeSetUp().
+ var authLogEntry = await ApiDbContext.AuthLogs.FirstAsync(x => x.Username == TestUserUsername && x.EventType == AuthEventType.Register);
+ Assert.That(authLogEntry.IpAddress, Is.EqualTo("::1"), "IP address is anonymized while IP logging should be enabled. Check test configuration.");
+ }
+
/// <summary>
/// Test if dates are stored and retrieved correctly without unwanted timezone conversions.
/// </summary>
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs
index 190bd8f45..41097ca87 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/Shard4/AuthTests.cs
@@ -32,6 +32,10 @@ public class AuthTests : ClientPlaywrightTest
var authLogEntry = await ApiDbContext.AuthLogs.FirstOrDefaultAsync(x => x.Username == TestUserUsername && x.EventType == AuthEventType.Register);
Assert.That(authLogEntry, Is.Not.Null, "Auth log entry not found in database after registration.");
+ // Check if the IP address is not anonymized as IP logging is disabled by default.
+ // The opposite of this is tested in the IP logging test in ApiTests.cs.
+ Assert.That(authLogEntry.IpAddress, Is.EqualTo("xxx.xxx.xxx.xxx"), "Auth log entry IP address is not anonymized while IP logging should be disabled. Check test configuration.");
+
// Check if the refresh token is stored in the database and its expiration date is set to the long lifetime
// after registration. The registration page does not have a "Remember me" checkbox, but it is assumed that
// the device is trusted so the refresh token will be valid for the extended duration.
diff --git a/src/Utilities/AliasVault.Auth/AuthLoggingService.cs b/src/Utilities/AliasVault.Auth/AuthLoggingService.cs
index ae15f86b8..78d17b7e0 100644
--- a/src/Utilities/AliasVault.Auth/AuthLoggingService.cs
+++ b/src/Utilities/AliasVault.Auth/AuthLoggingService.cs
@@ -33,8 +33,8 @@ public class AuthLoggingService(IServiceProvider serviceProvider, IHttpContextAc
var httpContext = httpContextAccessor.HttpContext;
var clientHeader = httpContext?.Request.Headers["X-AliasVault-Client"].FirstOrDefault();
- var config = httpContext?.RequestServices.GetService<SharedConfig>();
- var ipAddress = config?.IpLoggingEnabled == true ? IpAddressUtility.GetIpFromContext(httpContext) : "xxx.xxx.xxx.xxx";
+ var config = scope.ServiceProvider.GetRequiredService<SharedConfig>();
+ var ipAddress = config.IpLoggingEnabled ? IpAddressUtility.GetIpFromContext(httpContext) : "xxx.xxx.xxx.xxx";
var authAttempt = new AuthLog
{