Skip to content

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

Add host project for in-memory testing of WASM app (#67)

Leendert de Borst889dc81404be
diff --git a/aliasvault.sln b/aliasvault.sln
index 07939a525..32df43f5d 100644
--- a/aliasvault.sln
+++ b/aliasvault.sln
@@ -25,9 +25,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AliasVault.E2ETests", "src\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Database", "Database", "{5F7417F6-4388-49CC-9511-ED63C4A6488A}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasServerDb", "src\Databases\AliasServerDb\AliasServerDb.csproj", "{1277105D-50CD-4CE0-9C2C-549F46867E54}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AliasServerDb", "src\Databases\AliasServerDb\AliasServerDb.csproj", "{1277105D-50CD-4CE0-9C2C-549F46867E54}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasClientDb", "src\Databases\AliasClientDb\AliasClientDb.csproj", "{FE10F294-817F-477E-A24F-8597A15AF0B5}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AliasClientDb", "src\Databases\AliasClientDb\AliasClientDb.csproj", "{FE10F294-817F-477E-A24F-8597A15AF0B5}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.E2ETests.WebApp.Server", "src\Tests\Server\AliasVault.E2ETests.WebApp.Server\AliasVault.E2ETests.WebApp.Server.csproj", "{DD1F496F-CF10-47D1-A57F-5FA256479332}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Server", "Server", "{607945F3-9896-4544-99EC-F3496CF4D36B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -75,6 +79,10 @@ Global
{FE10F294-817F-477E-A24F-8597A15AF0B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE10F294-817F-477E-A24F-8597A15AF0B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE10F294-817F-477E-A24F-8597A15AF0B5}.Release|Any CPU.Build.0 = Release|Any CPU
+ {DD1F496F-CF10-47D1-A57F-5FA256479332}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {DD1F496F-CF10-47D1-A57F-5FA256479332}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {DD1F496F-CF10-47D1-A57F-5FA256479332}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {DD1F496F-CF10-47D1-A57F-5FA256479332}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -86,5 +94,10 @@ Global
{AF013D08-1BF6-4E23-87D2-37F614BE7952} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}
{1277105D-50CD-4CE0-9C2C-549F46867E54} = {5F7417F6-4388-49CC-9511-ED63C4A6488A}
{FE10F294-817F-477E-A24F-8597A15AF0B5} = {5F7417F6-4388-49CC-9511-ED63C4A6488A}
+ {DD1F496F-CF10-47D1-A57F-5FA256479332} = {607945F3-9896-4544-99EC-F3496CF4D36B}
+ {607945F3-9896-4544-99EC-F3496CF4D36B} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {FEE82475-C009-4762-8113-A6563D9DC49E}
EndGlobalSection
EndGlobal
diff --git a/src/AliasVault.WebApp/Program.cs b/src/AliasVault.WebApp/Program.cs
index 6f1a39b2f..bf9d73270 100644
--- a/src/AliasVault.WebApp/Program.cs
+++ b/src/AliasVault.WebApp/Program.cs
@@ -5,7 +5,6 @@
// </copyright>
//-----------------------------------------------------------------------
-using AliasClientDb;
using AliasVault.WebApp;
using AliasVault.WebApp.Auth.Providers;
using AliasVault.WebApp.Auth.Services;
@@ -14,7 +13,6 @@ using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
-using Microsoft.EntityFrameworkCore;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
diff --git a/src/Tests/AliasVault.E2ETests/AliasVault.E2ETests.csproj b/src/Tests/AliasVault.E2ETests/AliasVault.E2ETests.csproj
index eea02f4a7..ade11ffcd 100644
--- a/src/Tests/AliasVault.E2ETests/AliasVault.E2ETests.csproj
+++ b/src/Tests/AliasVault.E2ETests/AliasVault.E2ETests.csproj
@@ -46,6 +46,7 @@
<ItemGroup>
<ProjectReference Include="..\..\AliasVault.Api\AliasVault.Api.csproj" />
<ProjectReference Include="..\..\Databases\AliasServerDb\AliasServerDb.csproj" />
+ <ProjectReference Include="..\Server\AliasVault.E2ETests.WebApp.Server\AliasVault.E2ETests.WebApp.Server.csproj" />
</ItemGroup>
</Project>
diff --git a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
index 778d68fbb..d2b82a3f3 100644
--- a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
+++ b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs
@@ -21,12 +21,12 @@ public class PlaywrightTest
/// <summary>
/// For starting the WebAPI project in-memory.
/// </summary>
- private readonly WebApplicationFactoryFixture<AliasVault.Api.Program> _factory = new();
+ private readonly WebApplicationApiFactoryFixture<AliasVault.Api.Program> _apiFactory = new();
/// <summary>
- /// The BlazorWasmAppManager instance.
+ /// For starting the WASM WebApp project in-memory.
/// </summary>
- private BlazorWasmAppManager _blazorWasmAppManager;
+ private readonly WebApplicationWasmFactoryFixture<AliasVault.E2ETests.WebApp.Server.Program> _wasmFactory = new();
/// <summary>
/// Gets or sets base URL where the Blazor WASM app runs on including random port.
@@ -83,12 +83,12 @@ public class PlaywrightTest
AppBaseUrl = "http://localhost:" + appPort + "/";
// Start WebAPI in-memory.
- _factory.HostUrl = "http://localhost:" + apiPort;
- _factory.CreateDefaultClient();
+ _apiFactory.HostUrl = "http://localhost:" + apiPort;
+ _apiFactory.CreateDefaultClient();
// Start Blazor WASM app out-of-process.
- _blazorWasmAppManager = new BlazorWasmAppManager();
- await _blazorWasmAppManager.StartBlazorWasmAsync(appPort);
+ _wasmFactory.HostUrl = "http://localhost:" + appPort;
+ _wasmFactory.CreateDefaultClient();
// Set Playwright headless mode true if not in debug mode.
bool isDebugMode = System.Diagnostics.Debugger.IsAttached;
@@ -130,8 +130,8 @@ public class PlaywrightTest
await Context.CloseAsync();
await Browser.CloseAsync();
- await _factory.DisposeAsync();
- _blazorWasmAppManager.StopBlazorWasm();
+ await _apiFactory.DisposeAsync();
+ await _wasmFactory.DisposeAsync();
}
/// <summary>
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/BlazorWasmAppManager.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/BlazorWasmAppManager.cs
deleted file mode 100644
index e074ef090..000000000
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/BlazorWasmAppManager.cs
+++ /dev/null
@@ -1,203 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="BlazorWasmAppManager.cs" company="lanedirt">
-// Copyright (c) lanedirt. All rights reserved.
-// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
-// </copyright>
-//-----------------------------------------------------------------------
-namespace AliasVault.E2ETests.Infrastructure;
-
-using System.Diagnostics;
-using System.Net;
-
-/// <summary>
-/// A class for managing the Blazor WebAssembly application in out-of-process mode for E2E testing.
-/// </summary>
-public class BlazorWasmAppManager
-{
- private readonly List<string> _blazorWasmErrors = [];
- private Process? _blazorWasmProcess;
-
- /// <summary>
- /// Starts the Blazor WebAssembly application in out-of-process mode.
- /// </summary>
- /// <param name="port">The port number to run the app under.</param>
- /// <returns>Async task.</returns>
- public async Task StartBlazorWasmAsync(int port)
- {
- var projectPath = $"{GetBaseDirectory()}/../../AliasVault.WebApp/AliasVault.WebApp.csproj";
-
- _blazorWasmProcess = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = "dotnet",
- Arguments = $"run --project {projectPath} --urls=http://localhost:{port}",
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false,
- CreateNoWindow = true,
- },
- };
-
- _blazorWasmProcess.OutputDataReceived += (sender, args) =>
- {
- TestContext.Out.WriteLine(args.Data);
- };
- _blazorWasmProcess.ErrorDataReceived += (sender, args) =>
- {
- if (args.Data is null)
- {
- return;
- }
-
- TestContext.Out.WriteLine(args.Data);
- _blazorWasmErrors.Add(args.Data);
- };
-
- try
- {
- _blazorWasmProcess.Start();
- _blazorWasmProcess.BeginOutputReadLine();
- _blazorWasmProcess.BeginErrorReadLine();
- await WaitForStartupAsync(port);
- }
- catch (Exception ex)
- {
- // Retry up to 3 times
- for (int i = 0; i < 3; i++)
- {
- try
- {
- _blazorWasmProcess.Start();
- _blazorWasmProcess.BeginOutputReadLine();
- _blazorWasmProcess.BeginErrorReadLine();
- await WaitForStartupAsync(port);
- break; // Success, exit the loop
- }
- catch (Exception)
- {
- // Retry failed, log the exception and continue to the next attempt
- await TestContext.Out.WriteLineAsync($"Failed to start Blazor WebAssembly application. Retry attempt {i + 1}");
- await TestContext.Out.WriteLineAsync(ex.Message);
- }
- }
- }
- }
-
- /// <summary>
- /// Stops the Blazor WebAssembly application process.
- /// </summary>
- public void StopBlazorWasm()
- {
- if (_blazorWasmProcess is not null && !_blazorWasmProcess.HasExited)
- {
-#if WINDOWS
- KillProcessAndChildrenWindows(_blazorWasmProcess.Id);
-#else
- KillProcessAndChildrenUnix(_blazorWasmProcess.Id);
-#endif
- _blazorWasmProcess.Dispose();
- }
- }
-
- private static string GetBaseDirectory()
- {
- string currentDir = Directory.GetCurrentDirectory();
- string baseDir = string.Empty;
- var parentDir = Directory.GetParent(currentDir);
- if (parentDir?.Parent?.Parent != null)
- {
- baseDir = parentDir.Parent.Parent.FullName;
- }
-
- return baseDir;
- }
-
-#if WINDOWS
- private static void KillProcessAndChildrenWindows(int pid)
- {
- var searcher = new ManagementObjectSearcher($"Select * From Win32_Process Where ParentProcessID={pid}");
- var managementObjects = searcher.Get();
-
- foreach (var obj in managementObjects)
- {
- int childProcessId = Convert.ToInt32(obj["ProcessID"]);
- KillProcessAndChildrenWindows(childProcessId);
- }
-
- try
- {
- Process process = Process.GetProcessById(pid);
- process.Kill();
- }
- catch (ArgumentException)
- {
- // Process already exited.
- }
- }
-#else
- private static void KillProcessAndChildrenUnix(int pid)
- {
- try
- {
- var startInfo = new ProcessStartInfo
- {
- FileName = "pkill",
- Arguments = $"-TERM -P {pid}",
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false,
- CreateNoWindow = true,
- };
-
- var pkillProcess = new Process
- {
- StartInfo = startInfo,
- };
-
- pkillProcess.Start();
- pkillProcess.WaitForExit();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
-
- try
- {
- Process process = Process.GetProcessById(pid);
- process.Kill();
- }
- catch (ArgumentException e)
- {
- Console.WriteLine(e.Message);
- }
- }
-#endif
-
- private async Task WaitForStartupAsync(int port)
- {
- // Wait for the application to start up
- var started = false;
- while (!started)
- {
- try
- {
- using (var client = new HttpClient())
- {
- var response = await client.GetAsync($"http://localhost:{port}");
- started = response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.NotFound;
- }
- }
- catch (Exception e)
- {
- await TestContext.Out.WriteLineAsync(e.Message);
-
- if (_blazorWasmErrors.Count > 0)
- {
- throw new Exception($"WASM failed to start: {string.Join(Environment.NewLine, _blazorWasmErrors)}");
- }
- }
- }
- }
-}
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
similarity index 94%
rename from src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationFactoryFixture.cs
rename to src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
index 08f38888b..efc62be53 100644
--- a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationFactoryFixture.cs
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationApiFactoryFixture.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="WebApplicationFactoryFixture.cs" company="lanedirt">
+// <copyright file="WebApplicationApiFactoryFixture.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
@@ -20,7 +20,7 @@ using Microsoft.Extensions.Hosting;
/// Web application factory fixture for integration tests.
/// </summary>
/// <typeparam name="TEntryPoint">The entry point.</typeparam>
-public class WebApplicationFactoryFixture<TEntryPoint> : WebApplicationFactory<TEntryPoint>
+public class WebApplicationApiFactoryFixture<TEntryPoint> : WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
{
/// <summary>
diff --git a/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs
new file mode 100644
index 000000000..b1d71dea8
--- /dev/null
+++ b/src/Tests/AliasVault.E2ETests/Infrastructure/WebApplicationWasmFactoryFixture.cs
@@ -0,0 +1,44 @@
+//-----------------------------------------------------------------------
+// <copyright file="WebApplicationWasmFactoryFixture.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.E2ETests.Infrastructure;
+
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Mvc.Testing;
+using Microsoft.Extensions.Hosting;
+
+/// <summary>
+/// Web application factory fixture for integration tests.
+/// </summary>
+/// <typeparam name="TEntryPoint">The entry point.</typeparam>
+public class WebApplicationWasmFactoryFixture<TEntryPoint> : WebApplicationFactory<TEntryPoint>
+ where TEntryPoint : class
+{
+ /// <summary>
+ /// Gets or sets the URL the web application host will listen on.
+ /// </summary>
+ public string HostUrl { get; set; } = "https://localhost:5002";
+
+ /// <inheritdoc />
+ protected override void ConfigureWebHost(IWebHostBuilder builder)
+ {
+ builder.UseUrls(HostUrl);
+ }
+
+ /// <inheritdoc />
+ protected override IHost CreateHost(IHostBuilder builder)
+ {
+ var dummyHost = builder.Build();
+
+ builder.ConfigureWebHost(webHostBuilder => webHostBuilder.UseKestrel());
+
+ var host = builder.Build();
+ host.Start();
+
+ return dummyHost;
+ }
+}
diff --git a/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/AliasVault.E2ETests.WebApp.Server.csproj b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/AliasVault.E2ETests.WebApp.Server.csproj
new file mode 100644
index 000000000..83a2e47fb
--- /dev/null
+++ b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/AliasVault.E2ETests.WebApp.Server.csproj
@@ -0,0 +1,17 @@
+<Project Sdk="Microsoft.NET.Sdk.Web">
+
+ <PropertyGroup>
+ <TargetFramework>net8.0</TargetFramework>
+ <Nullable>enable</Nullable>
+ <ImplicitUsings>enable</ImplicitUsings>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.6" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\..\..\AliasVault.WebApp\AliasVault.WebApp.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/Program.cs b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/Program.cs
new file mode 100644
index 000000000..80a2fe99e
--- /dev/null
+++ b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/Program.cs
@@ -0,0 +1,30 @@
+var builder = WebApplication.CreateBuilder(args);
+
+builder.Services.AddRazorPages();
+
+var app = builder.Build();
+
+app.UseHttpsRedirection();
+
+app.UseBlazorFrameworkFiles();
+
+app.UseStaticFiles();
+
+app.UseRouting();
+
+app.MapRazorPages();
+
+app.MapFallbackToFile("index.html");
+
+app.Run();
+
+namespace AliasVault.E2ETests.WebApp.Server
+{
+ /// <summary>
+ /// Explicit program class definition. This is required in order to start the WebAPI project
+ /// in-memory from E2ETests project via WebApplicationFactory.
+ /// </summary>
+ public partial class Program
+ {
+ }
+}
diff --git a/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/Properties/launchSettings.json b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/Properties/launchSettings.json
new file mode 100644
index 000000000..a95f78d9e
--- /dev/null
+++ b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/Properties/launchSettings.json
@@ -0,0 +1,31 @@
+{
+ "$schema": "http://json.schemastore.org/launchsettings.json",
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:22992",
+ "sslPort": 0
+ }
+ },
+ "profiles": {
+ "http": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
+ "applicationUrl": "http://localhost:5105",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ }
+ }
+ }
diff --git a/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/README.md b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/README.md
new file mode 100644
index 000000000..57dbc54f5
--- /dev/null
+++ b/src/Tests/Server/AliasVault.E2ETests.WebApp.Server/README.md
@@ -0,0 +1,2 @@
+This project is meant to be used only for the E2E tests of the AliasVault.WebApp project.
+This separate project is made to be able to host the AliasVault.WebApp project in-memory via WebApplicationFactory.