Skip to content

Commit

Permalink
merge results
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed Oct 30, 2024
2 parents fc3aff4 + 239d52c commit bf1f5ff
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 37 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/bit.full.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ jobs:
run: |
dotnet new bit-bp --name TestSqlite --database Sqlite --advancedTests --framework net9.0
cd TestSqlite/src/Server/TestSqlite.Server.Api/
dotnet build
dotnet tool restore
dotnet ef migrations add InitialMigration
dotnet ef database update
cd ../../Tests
dotnet build
pwsh bin/Debug/net9.0/playwright.ps1 install --with-deps
dotnet test --logger GitHubActions
dotnet test --logger GitHubActions --filter "FullyQualifiedName~Boilerplate.Tests.PageTests.BlazorServer|FullyQualifiedName!~PageTests"
dotnet test --logger GitHubActions --filter Boilerplate.Tests.PageTests.BlazorWebAssembly
- name: Upload Tests Artifact
uses: actions/[email protected]
Expand All @@ -79,13 +79,12 @@ jobs:
run: |
dotnet new bit-bp --name TestSqlServer --database SqlServer
cd TestSqlServer/src/Server/TestSqlServer.Server.Api/
dotnet build
dotnet tool restore
dotnet ef migrations add InitialMigration
dotnet ef database update
cd ../../Tests
dotnet build
dotnet test --logger GitHubActions
dotnet test --logger GitHubActions --filter "FullyQualifiedName~Boilerplate.Tests.PageTests.BlazorServer|FullyQualifiedName!~PageTests"
dotnet test --logger GitHubActions --filter Boilerplate.Tests.PageTests.BlazorWebAssembly
- name: Upload Tests Artifact
uses: actions/[email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,8 @@
"src/Tests/Services/EmailReaderService.cs",
"src/Tests/Services/UserService.cs",
"src/Tests/Services/FakePhoneService.cs",
"src/Tests/Services/FakeGoogleRecaptchaHttpClient.cs"
"src/Tests/Services/FakeGoogleRecaptchaHttpClient.cs",
"src/Tests/Extensions/PlaywrightCacheExtensions.cs"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
//#if (api == "Integrated")
"ServerAddress": "http://localhost:5030/",
"ServerAddress": "/",
//#endif
//#if (IsInsideProjectTemplate)
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Text.RegularExpressions;

namespace Boilerplate.Tests.Extensions;

public static partial class PlaywrightCacheExtensions
{
public static Task EnableBlazorWasmCaching(this IPage page) => page.EnableAssetCaching(BlazorWasmRegex());

public static Task EnableBlazorWasmCaching(this IBrowserContext context) => context.EnableAssetCaching(BlazorWasmRegex());

public static Task EnableAssetCaching(this IPage page, string routePattern) => page.RouteAsync(routePattern, CacheHandler);

public static Task EnableAssetCaching(this IPage page, Regex routePattern) => page.RouteAsync(routePattern, CacheHandler);

public static Task EnableAssetCaching(this IPage page, Func<string, bool> predicate) => page.RouteAsync(predicate, CacheHandler);

public static Task EnableAssetCaching(this IBrowserContext context, string routePattern) => context.RouteAsync(routePattern, CacheHandler);

public static Task EnableAssetCaching(this IBrowserContext context, Regex routePattern) => context.RouteAsync(routePattern, CacheHandler);

public static Task EnableAssetCaching(this IBrowserContext context, Func<string, bool> predicate) => context.RouteAsync(predicate, CacheHandler);

private static async Task CacheHandler(this IRoute route)
{
var url = new Uri(route.Request.Url).PathAndQuery;

if (cachedResponses.TryGetValue(url, out var cachedResponse) is false)
{
var response = await route.FetchAsync();
var body = await response.BodyAsync();
cachedResponse = (body, response.Headers);
cachedResponses[url] = cachedResponse;
}

await route.FulfillAsync(new RouteFulfillOptions
{
Status = 200,
BodyBytes = cachedResponse.Body,
Headers = cachedResponse.Headers
});
}
public static void ClearCache() => cachedResponses.Clear();

private static Dictionary<string, (byte[] Body, Dictionary<string, string> Headers)> cachedResponses = [];

[GeneratedRegex(@"\/_framework\/[\w\.]+\.((wasm)|(pdb)|(dat))\?v=sha256-.+")]
private static partial Regex BlazorWasmRegex();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Boilerplate.Client.Web;

namespace Boilerplate.Tests.PageTests.BlazorWebAssembly;

[TestClass]
public partial class IdentityPagesTests : BlazorServer.IdentityPagesTests
{
public override BlazorWebAppMode BlazorRenderMode => BlazorWebAppMode.BlazorWebAssembly;
}

[TestClass]
public partial class LocalizationTests : BlazorServer.LocalizationTests
{
public override BlazorWebAppMode BlazorRenderMode => BlazorWebAppMode.BlazorWebAssembly;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//+:cnd:noEmit
using Boilerplate.Tests.PageTests.PageModels.Identity;
using Boilerplate.Server.Api.Data;
using Boilerplate.Tests.Services;
using Boilerplate.Server.Api.Data;
using Boilerplate.Tests.PageTests.PageModels;
using Boilerplate.Tests.PageTests.PageModels.Identity;

namespace Boilerplate.Tests.PageTests;
namespace Boilerplate.Tests.PageTests.BlazorServer;

[TestClass]
public partial class IdentityPagesTests : PageTestBase
Expand Down Expand Up @@ -89,23 +89,24 @@ public async Task SignUp(string mode)
var confirmPage = await signupPage.SignUp(email);
await confirmPage.AssertOpen();

var confirmationEmail = await signupPage.OpenConfirmationEmail();
await confirmationEmail.AssertContent();

IdentityHomePage identityHomePage;
switch (mode)
{
case "Token":
var confirmationEmail = await signupPage.OpenConfirmationEmail();
await confirmationEmail.AssertContent();
var token = await confirmationEmail.GetToken();
identityHomePage = await confirmPage.ConfirmByToken(token);
break;
case "MagicLink":
confirmationEmail = await signupPage.OpenConfirmationEmail();
await confirmationEmail.AssertContent();
identityHomePage = await confirmationEmail.OpenMagicLink<IdentityHomePage>();
break;
case "InvalidToken":
await confirmPage.ConfirmByToken("111111");
await confirmPage.AssertInvalidToken();
return;
case "MagicLink":
identityHomePage = await confirmationEmail.OpenMagicLink<IdentityHomePage>();
break;
default:
throw new NotSupportedException();
}
Expand Down Expand Up @@ -139,17 +140,18 @@ public async Task ForgotPassword(string mode)
var resetPasswordPage = await forgotPasswordPage.ForgotPassword(email);
await resetPasswordPage.AssertOpen();

var resetPasswordEmail = await forgotPasswordPage.OpenResetPasswordEmail();
await resetPasswordEmail.AssertContent();

const string newPassword = "new_password";
switch (mode)
{
case "Token":
var resetPasswordEmail = await forgotPasswordPage.OpenResetPasswordEmail();
await resetPasswordEmail.AssertContent();
var token = await resetPasswordEmail.GetToken();
await resetPasswordPage.ContinueByToken(token);
break;
case "MagicLink":
resetPasswordEmail = await forgotPasswordPage.OpenResetPasswordEmail();
await resetPasswordEmail.AssertContent();
resetPasswordPage = await resetPasswordEmail.OpenMagicLink();
break;
case "InvalidToken":
Expand Down Expand Up @@ -208,16 +210,17 @@ public async Task ChangeEmail(string mode)
await settingsPage.ChangeEmail(newEmail);
await settingsPage.AssertChangeEmail();

var confirmationEmail = await settingsPage.OpenConfirmationEmail();
await confirmationEmail.AssertContent();

switch (mode)
{
case "Token":
var confirmationEmail = await settingsPage.OpenConfirmationEmail();
await confirmationEmail.AssertContent();
var token = await confirmationEmail.GetToken();
await settingsPage.ConfirmEmailByToken(token);
break;
case "MagicLink":
confirmationEmail = await settingsPage.OpenConfirmationEmail();
await confirmationEmail.AssertContent();
settingsPage = await confirmationEmail.OpenMagicLink();
break;
case "InvalidToken":
Expand Down Expand Up @@ -280,7 +283,6 @@ public async Task ChangePhone(string mode)
case "Token":
var token = settingsPage.GetPhoneToken();
await settingsPage.ConfirmPhoneByToken(token);

await settingsPage.AssertConfirmPhoneSuccess();

signInPage = await settingsPage.SignOut();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
using Boilerplate.Tests.PageTests.PageModels;
using Boilerplate.Tests.Services;
using Boilerplate.Tests.PageTests.PageModels;

namespace Boilerplate.Tests.PageTests;
namespace Boilerplate.Tests.PageTests.BlazorServer;

[TestClass]
public partial class LocalizationTests : PageTestBase
Expand All @@ -17,7 +17,7 @@ public async Task AcceptLanguageHeader(string cultureName, string cultureDisplay
await homePage.Open();
await homePage.AssertLocalized(localizer, cultureName, cultureDisplayName);
}
private async Task SetCultureInBrowserContext(BrowserNewContextOptions options, string cultureName, string _) => options.Locale = cultureName;
public async Task SetCultureInBrowserContext(BrowserNewContextOptions options, string cultureName, string _) => options.Locale = cultureName;

[TestMethod]
[CultureData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class ConfirmationEmail<TPage>(IBrowserContext context, Uri serve
: TokenMagicLinkEmail<TPage>(context, serverAddress)
where TPage : RootLayout
{
protected override bool WaitForRedirectOnMagicLink => false;
protected override bool WaitForRedirectOnMagicLink => true;
protected override string EmailSubject => EmailStrings.ConfirmationEmailSubject.Replace("{0}", @"\b\d{6}\b");

protected override async Task AssertContentCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public async Task ConfirmEmailByToken(string token)

public async Task AssertConfirmEmailSuccess()
{
//TODO: Remove the line below when the problem with refreshing page is solved.
await Task.Delay(1000);
await Page.GotoAsync(new Uri(WebAppServerAddress, PagePath).ToString());

await Assertions.Expect(Page.Locator(".bit-prs.persona").Last).ToContainTextAsync(newEmail);
await ExpandAccount();
await AssertExpandAccount(newEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task ConfirmPhoneByToken(string token)

public async Task AssertConfirmPhoneSuccess()
{
await Task.Delay(1000); //Wait for redirection to complete
await Page.WaitForTimeoutAsync(1000); //Wait for redirection to complete
await ExpandAccount();
await ClickOnPhoneTab();
await AssertPhoneTab(newPhone);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using System.Reflection;
using Boilerplate.Client.Web;
using Boilerplate.Tests.Extensions;
using Microsoft.AspNetCore.Builder;

namespace Boilerplate.Tests.PageTests;

[TestClass]
public partial class PageTestBase : PageTest
public abstract partial class PageTestBase : PageTest
{
public AppTestServer TestServer { get; set; } = new();
public WebApplication WebApp => TestServer.WebApp;
public Uri WebAppServerAddress => TestServer.WebAppServerAddress;
public virtual BlazorWebAppMode BlazorRenderMode => BlazorWebAppMode.BlazorServer;

[TestInitialize]
public async Task InitializeTestServer()
{
await Context.EnableBlazorWasmCaching();

var currentTestMethod = GetType().GetMethod(TestContext.TestName!);

var configureTestServer = currentTestMethod!.GetCustomAttribute<ConfigureTestServerAttribute>();
if (configureTestServer is not null)
{
var configureTestServerMethod = GetType()
.GetMethod(configureTestServer.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.GetMethod(configureTestServer.MethodName, BindingFlags.Public | BindingFlags.Instance)
?? throw new InvalidOperationException($"Method '{configureTestServer.MethodName}' not found.");
var currentTestMethodArguments = GetTestMethodArguments();
await (Task)configureTestServerMethod.Invoke(this, [TestServer, .. currentTestMethodArguments])!;
Expand All @@ -29,7 +34,10 @@ public async Task InitializeTestServer()
var autoStartTestServer = currentTestMethod!.GetCustomAttribute<AutoStartTestServerAttribute>();
if (autoStartTestServer is null || autoStartTestServer.AutoStart)
{
await TestServer.Build().Start();
await TestServer.Build(configureTestConfigurations: configuration =>
{
configuration["WebAppRender:BlazorMode"] = BlazorRenderMode.ToString();
}).Start();
}
}

Expand Down Expand Up @@ -67,7 +75,7 @@ public override BrowserNewContextOptions ContextOptions()
if (configureBrowserContext is not null)
{
var configureBrowserContextMethod = GetType()
.GetMethod(configureBrowserContext.MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.GetMethod(configureBrowserContext.MethodName, BindingFlags.Public | BindingFlags.Instance)
?? throw new InvalidOperationException($"Method '{configureBrowserContext.MethodName}' not found.");
var currentTestMethodArguments = GetTestMethodArguments();
configureBrowserContextMethod.Invoke(this, [options, .. currentTestMethodArguments]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//#endif
//#if (advancedTests == true)
using Boilerplate.Tests.PageTests.PageModels.Identity;
using Boilerplate.Tests.Extensions;
using Boilerplate.Client.Web;
//#endif
using Microsoft.Extensions.Hosting;

Expand All @@ -23,7 +25,15 @@ public static async Task Initialize(TestContext _)
{
await using var testServer = new AppTestServer();

await testServer.Build().Start();
await testServer.Build(
//#if (advancedTests == true)
configureTestConfigurations: configuration =>
{
//Run assembly initialization test in BlazorWebAssembly mode to cache .wasm files
configuration["WebAppRender:BlazorMode"] = BlazorWebAppMode.BlazorWebAssembly.ToString();
}
//#endif
).Start();

await InitializeDatabase(testServer);

Expand Down Expand Up @@ -63,11 +73,17 @@ private static async Task InitializeAuthenticationState(AppTestServer testServer
await playwrightPage.ContextSetup();
await playwrightPage.PageSetup();

await playwrightPage.Context.EnableBlazorWasmCaching();

var signinPage = new SignInPage(playwrightPage.Page, testServer.WebAppServerAddress);

Assertions.SetDefaultExpectTimeout(30_000); //Set global timeout to 30 seconds for the first time of app loading in WebAssembly mode + Caching .wasm files

await signinPage.Open();
await signinPage.AssertOpen();

Assertions.SetDefaultExpectTimeout(5_000); //Set global timeout to 5 seconds for rest of the tests

var signedInPage = await signinPage.SignInWithEmail();
await signedInPage.AssertSignInSuccess();

Expand Down

0 comments on commit bf1f5ff

Please sign in to comment.