Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor E2E registration part (#233)
Leendert de Borst1f88d5678b97
diff --git a/src/AliasVault.Client/Main/Models/CredentialEdit.cs b/src/AliasVault.Client/Main/Models/CredentialEdit.csindex 649dae4f3..fcbbe5f2e 100644--- a/src/AliasVault.Client/Main/Models/CredentialEdit.cs+++ b/src/AliasVault.Client/Main/Models/CredentialEdit.cs@@ -9,7 +9,6 @@ namespace AliasVault.Client.Main.Models;using System;using System.Collections.Generic;-using System.ComponentModel;using System.ComponentModel.DataAnnotations;using AliasClientDb;using AliasVault.Client.Main.Models.FormValidation;diff --git a/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.csindex cf09f48f3..f00219079 100644--- a/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs+++ b/src/Tests/AliasVault.E2ETests/Common/ClientPlaywrightTest.cs@@ -280,15 +280,10 @@ public class ClientPlaywrightTest : PlaywrightTestawait Page.GotoAsync(AppBaseUrl);await WaitForUrlAsync("user/login", "Your username");- // Wait for 100ms to ensure the page has loaded.- // Note: the default Playwright "waitfor" logic didn't work here and caused the form to be filled- // before it was actually fully rendered thus losing the input...- await Task.Delay(100);-// Try to log in with test credentials.- var emailField = Page.Locator("input[id='email']");- var passwordField = Page.Locator("input[id='password']");- var rememberMeCheckbox = Page.Locator("input[id='remember']");+ var emailField = await WaitForAndGetElement("input[id='email']");+ var passwordField = await WaitForAndGetElement("input[id='password']");+ var rememberMeCheckbox = await WaitForAndGetElement("input[id='remember']");await emailField.FillAsync(TestUserUsername);await passwordField.FillAsync(TestUserPassword);@@ -326,8 +321,7 @@ public class ClientPlaywrightTest : PlaywrightTestawait Logout();// Reset username and password so a new random account is created.- TestUserUsername = string.Empty;- TestUserPassword = string.Empty;+ SetRandomTestUserCredentials();// Register a new account random account.await Register();@@ -336,37 +330,39 @@ public class ClientPlaywrightTest : PlaywrightTest/// <summary>/// Register a new random account./// </summary>+ /// <param name="checkForSuccess">Whether to check for successful registration. Default is true.</param>+ /// <param name="username">Username to use for registration. If empty then it defaults to the TestUserUsername.</param>+ /// <param name="password">Password to use for registration. If empty then it defaults to the TestUserPassword.</param>/// <returns>Async task.</returns>- private async Task Register()+ protected async Task Register(bool checkForSuccess = true, string? username = null, string? password = null){- // If email is not set by test explicitly, generate a random email.- TestUserUsername = TestUserUsername.Length > 0 ? TestUserUsername : $"{Guid.NewGuid()}@test.com";-- // If password is not set by test explicitly, generate a random password.- TestUserPassword = TestUserPassword.Length > 0 ? TestUserPassword : Guid.NewGuid().ToString();-// Try to register a new account.- var registerButton = Page.Locator("a[href='/user/register']");+ var registerButton = await WaitForAndGetElement("a[href='/user/register']");await registerButton.ClickAsync();await WaitForUrlAsync("user/register");// Try to register an account with the generated test credentials.- var emailField = Page.Locator("input[id='email']");- var passwordField = Page.Locator("input[id='password']");- var password2Field = Page.Locator("input[id='password2']");- await emailField.FillAsync(TestUserUsername);- await passwordField.FillAsync(TestUserPassword);- await password2Field.FillAsync(TestUserPassword);+ var emailField = await WaitForAndGetElement("input[id='email']");+ var passwordField = await WaitForAndGetElement("input[id='password']");+ var password2Field = await WaitForAndGetElement("input[id='password2']");++ // Use the provided username and password if available, otherwise use the default test credentials.+ await emailField.FillAsync(username ?? TestUserUsername);+ await passwordField.FillAsync(password ?? TestUserPassword);+ await password2Field.FillAsync(password ?? TestUserPassword);// Check the terms of service checkbox- var termsCheckbox = Page.Locator("input[id='terms']");+ var termsCheckbox = await WaitForAndGetElement("input[id='terms']");await termsCheckbox.CheckAsync();// Check if we get redirected when clicking on the register button.- var submitButton = Page.Locator("button[type='submit']");+ var submitButton = await WaitForAndGetElement("button[type='submit']");await submitButton.ClickAsync();// Check if we get redirected to the root URL after registration which means we are logged in.- await WaitForUrlAsync("welcome**", "Getting Started");+ if (checkForSuccess)+ {+ await WaitForUrlAsync("welcome**", "Getting Started");+ }}}diff --git a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.csindex e02779f30..821786f19 100644--- a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs+++ b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs@@ -29,12 +29,12 @@ public abstract class PlaywrightTest/// <summary>/// Gets or sets random unique account email that is used for the test./// </summary>- protected virtual string TestUserUsername { get; set; } = string.Empty;+ protected virtual string TestUserUsername { get; set; } = $"{Guid.NewGuid()}@test.com";/// <summary>/// Gets or sets random unique account password that is used for the test./// </summary>- protected virtual string TestUserPassword { get; set; } = string.Empty;+ protected virtual string TestUserPassword { get; set; } = Guid.NewGuid().ToString();/// <summary>/// Gets or sets the Playwright browser instance.@@ -196,6 +196,35 @@ public abstract class PlaywrightTestContext = await Browser.NewContextAsync();}+ /// <summary>+ /// Sets new random test user credentials that are used for signing up and logging in.+ /// </summary>+ protected void SetRandomTestUserCredentials()+ {+ TestUserUsername = $"{Guid.NewGuid()}@test.com";+ TestUserPassword = Guid.NewGuid().ToString();+ }++ /// <summary>+ /// Wait for the page to be fully loaded and the specified element to be visible and enabled.+ /// </summary>+ /// <param name="selector">The element to wait for and get.</param>+ /// <returns>The requested element or a timeout if element was not found in time.</returns>+ protected async Task<ILocator> WaitForAndGetElement(string selector)+ {+ var requestedElement = Page.Locator(selector);+ await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);++ // Wait for the requested element to be visible and enabled.+ await requestedElement.WaitForAsync(new LocatorWaitForOptions+ {+ State = WaitForSelectorState.Visible,+ Timeout = 10000,+ });++ return requestedElement;+ }+/// <summary>/// Setup the Playwright test environment. This method is required to be implemented by the derived class./// </summary>diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/AuthPasswordChangeTest.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/AuthPasswordChangeTest.csindex f26482ae9..e78d22a44 100644--- a/src/Tests/AliasVault.E2ETests/Tests/Client/AuthPasswordChangeTest.cs+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/AuthPasswordChangeTest.cs@@ -41,9 +41,9 @@ public class AuthPasswordChangeTest : ClientPlaywrightTestawait WaitForUrlAsync("settings/security/change-password", "Current Password");// Fill in the form.- var currentPasswordField = Page.Locator("input[id='currentPassword']");- var newPasswordField = Page.Locator("input[id='newPassword']");- var confirmPasswordField = Page.Locator("input[id='newPasswordConfirm']");+ var currentPasswordField = await WaitForAndGetElement("input[id='currentPassword']");+ var newPasswordField = await WaitForAndGetElement("input[id='newPassword']");+ var confirmPasswordField = await WaitForAndGetElement("input[id='newPasswordConfirm']");var newPassword = TestUserPassword + "123";diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/AuthTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/AuthTests.csindex 205e73be4..321ddb85e 100644--- a/src/Tests/AliasVault.E2ETests/Tests/Client/AuthTests.cs+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/AuthTests.cs@@ -104,33 +104,8 @@ public class AuthTests : ClientPlaywrightTest[Order(4)]public async Task RegisterFormWarningTest(){- // Logout.await Logout();-- // Try to register a new account.- var registerButton = Page.Locator("a[href='/user/register']");- await registerButton.ClickAsync();- await WaitForUrlAsync("user/register", "Create a new AliasVault account");-- // Wait for .1 seconds to ensure the page is fully loaded.- // Note: this is a workaround for a Playwright issue where the page is not fully loaded when the test continues.- await Task.Delay(100);-- // Register account with same test credentials as used in the initial registration bootstrap method.- var emailField = Page.Locator("input[id='email']");- var passwordField = Page.Locator("input[id='password']");- var password2Field = Page.Locator("input[id='password2']");- await emailField.FillAsync(TestUserUsername);- await passwordField.FillAsync(TestUserPassword);- await password2Field.FillAsync(TestUserPassword);-- // Check the terms of service checkbox- var termsCheckbox = Page.Locator("input[id='terms']");- await termsCheckbox.CheckAsync();-- // Check if we get a visible warning when trying to register.- var submitButton = Page.Locator("button[type='submit']");- await submitButton.ClickAsync();+ await Register(checkForSuccess: false);var warning = await Page.TextContentAsync("div[role='alert']");Assert.That(warning, Does.Contain("is already taken."), "No visible warning when registering with existing email address.");@@ -151,12 +126,12 @@ public class AuthTests : ClientPlaywrightTest// on the next attempt, so we need to try 11 times to see the lockout message.for (var i = 0; i < 11; i++){- var emailField = Page.Locator("input[id='email']");- var passwordField = Page.Locator("input[id='password']");+ var emailField = await WaitForAndGetElement("input[id='email']");+ var passwordField = await WaitForAndGetElement("input[id='password']");await emailField.FillAsync(TestUserUsername);await passwordField.FillAsync("wrongpassword");- var loginButton = Page.Locator("button[type='submit']");+ var loginButton = await WaitForAndGetElement("button[type='submit']");await loginButton.ClickAsync();if (i == 10)diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.csindex 1b6ef812a..53ad2f620 100644--- a/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs@@ -37,12 +37,12 @@ public class SmtpServerTests/// <summary>/// The test host instance./// </summary>- private IHost _testHost = null!;+ private IHost _testHost;/// <summary>/// The test host builder instance./// </summary>- private TestHostBuilder _testHostBuilder = null!;+ private TestHostBuilder _testHostBuilder;/// <summary>/// Setup logic for every test.