From 8617c6d182a32913136fb56ab87ea7195692ddd6 Mon Sep 17 00:00:00 2001 From: kashingl Date: Sat, 20 Jun 2026 14:31:43 +0200 Subject: [PATCH] Update E2E tests and fix CORS configuration - Enable and align E2E tests with latest implementation - Add resilient login helper and JWT user id extraction for tests - Fix fixture/frontend port alignment for Playwright - Fix CORS origins for localhost:4201 - Apply EF Core migrations at API startup - Update .gitignore for generated artifacts Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .gitignore | 18 ++++ .../Extensions/ServiceCollectionExtensions.cs | 2 +- src/ToolInventory.API/Program.cs | 8 ++ src/ToolInventory.API/appsettings.json | 3 +- .../ToolInventory.E2E.Tests/CheckoutsTests.cs | 17 ++-- .../ToolInventory.E2E.Tests/E2ETestHelper.cs | 86 ++++++++++++++++++- .../Fixtures/PlaywrightFixture.cs | 7 +- .../MaintenanceTests.cs | 14 +-- .../NavigationTests.cs | 12 ++- tests/ToolInventory.E2E.Tests/ToolsTests.cs | 36 ++++---- 10 files changed, 163 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index ca5dd03..f287221 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,21 @@ id_dsa Thumbs.db .vscode/ .idea/ + +# Build outputs +**/bin/ +**/obj/ + +# Node / frontend artifacts +**/node_modules/ +**/.angular/ +**/dist/ +**/coverage/ + +# E2E / Playwright artifacts +**/.playwright/ +**/playwright-report/ +**/test-results/ + +# Logs +*.log diff --git a/src/ToolInventory.API/Extensions/ServiceCollectionExtensions.cs b/src/ToolInventory.API/Extensions/ServiceCollectionExtensions.cs index fc808a0..88f66e3 100644 --- a/src/ToolInventory.API/Extensions/ServiceCollectionExtensions.cs +++ b/src/ToolInventory.API/Extensions/ServiceCollectionExtensions.cs @@ -74,7 +74,7 @@ public static IServiceCollection AddToolInventoryCore(this IServiceCollection se services.AddCors(options => { - var allowedOrigins = configuration.GetSection("Cors:AllowedOrigins").Get() ?? ["http://localhost:4200"]; + var allowedOrigins = configuration.GetSection("Cors:AllowedOrigins").Get() ?? ["http://localhost:4200", "http://localhost:4201"]; options.AddPolicy("FrontendClients", policy => policy.WithOrigins(allowedOrigins) .AllowAnyHeader() diff --git a/src/ToolInventory.API/Program.cs b/src/ToolInventory.API/Program.cs index f97cb1e..d6b32f5 100644 --- a/src/ToolInventory.API/Program.cs +++ b/src/ToolInventory.API/Program.cs @@ -1,4 +1,6 @@ +using Microsoft.EntityFrameworkCore; using ToolInventory.API.Extensions; +using ToolInventory.Infrastructure.Data; var builder = WebApplication.CreateBuilder(args); @@ -8,6 +10,12 @@ var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var dbContext = scope.ServiceProvider.GetRequiredService(); + dbContext.Database.Migrate(); +} + app.UseToolInventoryPipeline(); app.Run(); diff --git a/src/ToolInventory.API/appsettings.json b/src/ToolInventory.API/appsettings.json index 4320cb6..0584f0c 100644 --- a/src/ToolInventory.API/appsettings.json +++ b/src/ToolInventory.API/appsettings.json @@ -4,7 +4,8 @@ }, "Cors": { "AllowedOrigins": [ - "http://localhost:4200" + "http://localhost:4200", + "http://localhost:4201" ] }, "Jwt": { diff --git a/tests/ToolInventory.E2E.Tests/CheckoutsTests.cs b/tests/ToolInventory.E2E.Tests/CheckoutsTests.cs index 08a65f6..8f79f56 100644 --- a/tests/ToolInventory.E2E.Tests/CheckoutsTests.cs +++ b/tests/ToolInventory.E2E.Tests/CheckoutsTests.cs @@ -13,9 +13,10 @@ public CheckoutsTests(PlaywrightFixture fixture) _fixture = fixture; } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task CheckoutsPage_ShowsTable() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/checkouts"); await _fixture.Page.WaitForSelectorAsync("table[mat-table]"); @@ -23,9 +24,10 @@ public async Task CheckoutsPage_ShowsTable() await Assertions.Expect(table).ToBeVisibleAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task CheckoutButton_OpensDialog() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/checkouts"); await _fixture.Page.ClickAsync("button:has-text('Check Out Tool')"); @@ -37,9 +39,10 @@ public async Task CheckoutButton_OpensDialog() await Assertions.Expect(title).ToContainTextAsync("Check Out Tool"); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task CheckoutDialog_Cancel_ClosesDialog() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/checkouts"); await _fixture.Page.ClickAsync("button:has-text('Check Out Tool')"); await _fixture.Page.WaitForSelectorAsync("mat-dialog-container"); @@ -50,17 +53,19 @@ public async Task CheckoutDialog_Cancel_ClosesDialog() await Assertions.Expect(dialog).ToBeHiddenAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task CheckoutAndCheckIn_FlowUpdatesStatus() { + await _fixture.LoginForTestAsync(); var toolName = await E2ETestHelper.CreateToolAsync(_fixture.Page); + var currentUserId = await E2ETestHelper.GetCurrentUserIdAsync(_fixture.Page); await _fixture.Page.GotoAsync("/checkouts"); await _fixture.Page.ClickAsync("button:has-text('Check Out Tool')"); await _fixture.Page.WaitForSelectorAsync("mat-dialog-container"); await E2ETestHelper.SelectMatOptionAsync(_fixture.Page, "mat-dialog-container mat-select[formcontrolname='toolId']", toolName); - await _fixture.Page.FillAsync("mat-dialog-container input[formcontrolname='userId']", "1001"); + await _fixture.Page.FillAsync("mat-dialog-container input[formcontrolname='userId']", currentUserId); await _fixture.Page.FillAsync("mat-dialog-container textarea[formcontrolname='notes']", "E2E checkout"); await _fixture.Page.ClickAsync("mat-dialog-actions button:has-text('Check Out')"); @@ -68,7 +73,7 @@ public async Task CheckoutAndCheckIn_FlowUpdatesStatus() var row = _fixture.Page.Locator($"tr[mat-row]:has-text('{toolName}')"); await Assertions.Expect(row).ToBeVisibleAsync(); - await Assertions.Expect(row).ToContainTextAsync("Checked Out"); + await Assertions.Expect(row).ToContainTextAsync("Active"); await row.Locator("button:has-text('Check In')").ClickAsync(); diff --git a/tests/ToolInventory.E2E.Tests/E2ETestHelper.cs b/tests/ToolInventory.E2E.Tests/E2ETestHelper.cs index bf0aedc..91f691a 100644 --- a/tests/ToolInventory.E2E.Tests/E2ETestHelper.cs +++ b/tests/ToolInventory.E2E.Tests/E2ETestHelper.cs @@ -4,6 +4,67 @@ namespace ToolInventory.E2E.Tests; internal static class E2ETestHelper { + private const string TestEmail = "test@example.com"; + private const string TestPassword = "Test123!@#"; + + public static async Task LoginAsync(IPage page) + { + await page.GotoAsync("/login"); + await page.WaitForSelectorAsync("input[formcontrolname='email']"); + + var emailField = page.Locator("input[formcontrolname='email']"); + await emailField.FillAsync(TestEmail); + + var passwordField = page.Locator("input[formcontrolname='password']"); + await passwordField.FillAsync(TestPassword); + + var signInButton = page.Locator("button:has-text('Sign In')"); + + try + { + // Try to sign in + await signInButton.ClickAsync(); + await page.WaitForURLAsync("**/tools", new PageWaitForURLOptions { Timeout = 3000 }); + } + catch + { + // Login failed, try to register + await page.GotoAsync("/register"); + await page.WaitForSelectorAsync("input[formcontrolname='displayName']"); + + var displayName = page.Locator("input[formcontrolname='displayName']"); + await displayName.FillAsync("E2E Test User"); + + var regEmail = page.Locator("input[formcontrolname='email']"); + await regEmail.FillAsync(TestEmail); + + var regPassword = page.Locator("input[formcontrolname='password']"); + await regPassword.FillAsync(TestPassword); + + var registerButton = page.Locator("button:has-text('Register')"); + await registerButton.ClickAsync(); + + // Wait a bit for the response + await Task.Delay(2000); + + // If we're on login page, try login again + if (page.Url.Contains("/login")) + { + var emailFieldAgain = page.Locator("input[formcontrolname='email']"); + await emailFieldAgain.FillAsync(TestEmail); + + var passwordFieldAgain = page.Locator("input[formcontrolname='password']"); + await passwordFieldAgain.FillAsync(TestPassword); + + var signInButtonAgain = page.Locator("button:has-text('Sign In')"); + await signInButtonAgain.ClickAsync(); + } + + // Wait for tools page + await page.WaitForURLAsync("**/tools", new PageWaitForURLOptions { Timeout = 5000 }); + } + } + public static async Task CreateToolAsync(IPage page, string? toolName = null) { await page.GotoAsync("/tools"); @@ -29,11 +90,34 @@ public static async Task CreateToolAsync(IPage page, string? toolName = public static async Task ExpectSnackBarAsync(IPage page, string expectedText) { - var snackBar = page.Locator("mat-snack-bar-container"); + var snackBar = page.Locator("mat-snack-bar-container").Last; await page.WaitForSelectorAsync("mat-snack-bar-container"); await Assertions.Expect(snackBar).ToContainTextAsync(expectedText); } + public static async Task GetCurrentUserIdAsync(IPage page) + { + var userId = await page.EvaluateAsync( + @"() => { + const raw = localStorage.getItem('tool_inventory_user'); + if (!raw) return ''; + const token = JSON.parse(raw)?.token; + if (!token) return ''; + const parts = token.split('.'); + if (parts.length < 2) return ''; + const base64 = parts[1].replace(/-/g, '+').replace(/_/g, '/'); + const payload = JSON.parse(atob(base64)); + return payload?.sub ?? ''; + }"); + + if (string.IsNullOrWhiteSpace(userId)) + { + throw new InvalidOperationException("Could not resolve authenticated user id from JWT token."); + } + + return userId; + } + public static async Task FilterToolsAsync(IPage page, string filterText) { await page.FillAsync("input[placeholder='Search tools...']", filterText); diff --git a/tests/ToolInventory.E2E.Tests/Fixtures/PlaywrightFixture.cs b/tests/ToolInventory.E2E.Tests/Fixtures/PlaywrightFixture.cs index 35a806a..bf2e55f 100644 --- a/tests/ToolInventory.E2E.Tests/Fixtures/PlaywrightFixture.cs +++ b/tests/ToolInventory.E2E.Tests/Fixtures/PlaywrightFixture.cs @@ -4,7 +4,7 @@ namespace ToolInventory.E2E.Tests.Fixtures; public class PlaywrightFixture : IAsyncLifetime { - public const string BaseUrl = "http://localhost:4200"; + public const string BaseUrl = "http://localhost:4201"; public IPlaywright Playwright { get; private set; } = null!; public IBrowser Browser { get; private set; } = null!; @@ -32,4 +32,9 @@ public async Task DisposeAsync() await Browser.DisposeAsync(); Playwright.Dispose(); } + + public async Task LoginForTestAsync() + { + await E2ETestHelper.LoginAsync(Page); + } } diff --git a/tests/ToolInventory.E2E.Tests/MaintenanceTests.cs b/tests/ToolInventory.E2E.Tests/MaintenanceTests.cs index c1e1ecb..5c558a4 100644 --- a/tests/ToolInventory.E2E.Tests/MaintenanceTests.cs +++ b/tests/ToolInventory.E2E.Tests/MaintenanceTests.cs @@ -13,9 +13,10 @@ public MaintenanceTests(PlaywrightFixture fixture) _fixture = fixture; } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task MaintenancePage_ShowsTable() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/maintenance"); await _fixture.Page.WaitForSelectorAsync("table[mat-table]"); @@ -23,9 +24,10 @@ public async Task MaintenancePage_ShowsTable() await Assertions.Expect(table).ToBeVisibleAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task AddRecordButton_OpensDialog() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/maintenance"); await _fixture.Page.ClickAsync("button:has-text('Add Record')"); @@ -37,9 +39,10 @@ public async Task AddRecordButton_OpensDialog() await Assertions.Expect(title).ToContainTextAsync("Add Maintenance Record"); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task AddRecordDialog_Cancel_ClosesDialog() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/maintenance"); await _fixture.Page.ClickAsync("button:has-text('Add Record')"); await _fixture.Page.WaitForSelectorAsync("mat-dialog-container"); @@ -50,9 +53,10 @@ public async Task AddRecordDialog_Cancel_ClosesDialog() await Assertions.Expect(dialog).ToBeHiddenAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task AddMaintenanceRecord_Submit_ShowsNewRecord() { + await _fixture.LoginForTestAsync(); var toolName = await E2ETestHelper.CreateToolAsync(_fixture.Page); await _fixture.Page.GotoAsync("/maintenance"); @@ -63,7 +67,7 @@ public async Task AddMaintenanceRecord_Submit_ShowsNewRecord() await _fixture.Page.FillAsync("mat-dialog-container textarea[formcontrolname='description']", "E2E maintenance record"); await _fixture.Page.FillAsync("mat-dialog-container input[formcontrolname='performedBy']", "E2E Tester"); await _fixture.Page.FillAsync("mat-dialog-container input[formcontrolname='cost']", "25.50"); - await _fixture.Page.ClickAsync("mat-dialog-actions button:has-text('Create')"); + await _fixture.Page.ClickAsync("mat-dialog-actions button:has-text('Save')"); await E2ETestHelper.ExpectSnackBarAsync(_fixture.Page, "created"); diff --git a/tests/ToolInventory.E2E.Tests/NavigationTests.cs b/tests/ToolInventory.E2E.Tests/NavigationTests.cs index a4bf525..ce772e8 100644 --- a/tests/ToolInventory.E2E.Tests/NavigationTests.cs +++ b/tests/ToolInventory.E2E.Tests/NavigationTests.cs @@ -13,9 +13,10 @@ public NavigationTests(PlaywrightFixture fixture) _fixture = fixture; } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task SideNav_ShowsAllMenuItems() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/"); await _fixture.Page.WaitForSelectorAsync("mat-sidenav"); @@ -29,18 +30,20 @@ public async Task SideNav_ShowsAllMenuItems() await Assertions.Expect(maintenanceLink).ToBeVisibleAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task RootUrl_RedirectsToTools() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/"); await _fixture.Page.WaitForURLAsync("**/tools"); Assert.Contains("/tools", _fixture.Page.Url); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task CheckoutsLink_NavigatesToCheckoutsPage() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/"); await _fixture.Page.ClickAsync("mat-nav-list a:has-text('Check-in / Out')"); await _fixture.Page.WaitForURLAsync("**/checkouts"); @@ -48,9 +51,10 @@ public async Task CheckoutsLink_NavigatesToCheckoutsPage() Assert.Contains("/checkouts", _fixture.Page.Url); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task MaintenanceLink_NavigatesToMaintenancePage() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/"); await _fixture.Page.ClickAsync("mat-nav-list a:has-text('Maintenance')"); await _fixture.Page.WaitForURLAsync("**/maintenance"); diff --git a/tests/ToolInventory.E2E.Tests/ToolsTests.cs b/tests/ToolInventory.E2E.Tests/ToolsTests.cs index 1135fbd..3bfd4fc 100644 --- a/tests/ToolInventory.E2E.Tests/ToolsTests.cs +++ b/tests/ToolInventory.E2E.Tests/ToolsTests.cs @@ -13,9 +13,10 @@ public ToolsTests(PlaywrightFixture fixture) _fixture = fixture; } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task ToolsPage_ShowsTable() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/tools"); await _fixture.Page.WaitForSelectorAsync("table[mat-table]"); @@ -23,9 +24,10 @@ public async Task ToolsPage_ShowsTable() await Assertions.Expect(table).ToBeVisibleAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task ToolsPage_AddToolButton_OpensDialog() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/tools"); await _fixture.Page.ClickAsync("button:has-text('Add Tool')"); @@ -37,17 +39,19 @@ public async Task ToolsPage_AddToolButton_OpensDialog() await Assertions.Expect(title).ToContainTextAsync("Add Tool"); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task CreateTool_FillFormAndSubmit_ToolAppearsInList() { + await _fixture.LoginForTestAsync(); var toolName = await E2ETestHelper.CreateToolAsync(_fixture.Page); await Assertions.Expect(E2ETestHelper.ToolRow(_fixture.Page, toolName)).ToBeVisibleAsync(); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task ToolCrud_CreateEditDelete_WorksEndToEnd() { + await _fixture.LoginForTestAsync(); var originalName = await E2ETestHelper.CreateToolAsync(_fixture.Page); var originalRow = E2ETestHelper.ToolRow(_fixture.Page, originalName); @@ -63,30 +67,19 @@ public async Task ToolCrud_CreateEditDelete_WorksEndToEnd() await E2ETestHelper.FilterToolsAsync(_fixture.Page, updatedName); await Assertions.Expect(E2ETestHelper.ToolRow(_fixture.Page, updatedName)).ToBeVisibleAsync(); - var dialogTaskCompletionSource = new TaskCompletionSource(); - _fixture.Page.Dialog += HandleDialog; - - try - { - await E2ETestHelper.ToolRow(_fixture.Page, updatedName).Locator("button").Nth(1).ClickAsync(); - var dialog = await dialogTaskCompletionSource.Task; - await dialog.AcceptAsync(); - } - finally - { - _fixture.Page.Dialog -= HandleDialog; - } - - void HandleDialog(object? _, IDialog dialog) => dialogTaskCompletionSource.TrySetResult(dialog); + await E2ETestHelper.ToolRow(_fixture.Page, updatedName).Locator("button").Nth(1).ClickAsync(); + await _fixture.Page.WaitForSelectorAsync("mat-dialog-container"); + await _fixture.Page.ClickAsync("mat-dialog-actions button:has-text('Delete')"); await E2ETestHelper.ExpectSnackBarAsync(_fixture.Page, "deleted"); await E2ETestHelper.FilterToolsAsync(_fixture.Page, updatedName); await Assertions.Expect(E2ETestHelper.ToolRow(_fixture.Page, updatedName)).ToHaveCountAsync(0); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task FilterBox_FiltersTableResults() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/tools"); await _fixture.Page.WaitForSelectorAsync("table[mat-table]"); @@ -96,9 +89,10 @@ public async Task FilterBox_FiltersTableResults() await Assertions.Expect(rows).ToHaveCountAsync(0); } - [Fact(Skip = "Requires running Angular app at http://localhost:4200")] + [Fact] public async Task ScanButton_OpensScannerDialog() { + await _fixture.LoginForTestAsync(); await _fixture.Page.GotoAsync("/tools"); await _fixture.Page.ClickAsync("button:has-text('Scan')");