Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static IServiceCollection AddToolInventoryCore(this IServiceCollection se

services.AddCors(options =>
{
var allowedOrigins = configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? ["http://localhost:4200"];
var allowedOrigins = configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? ["http://localhost:4200", "http://localhost:4201"];
options.AddPolicy("FrontendClients", policy =>
policy.WithOrigins(allowedOrigins)
.AllowAnyHeader()
Expand Down
8 changes: 8 additions & 0 deletions src/ToolInventory.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.EntityFrameworkCore;
using ToolInventory.API.Extensions;
using ToolInventory.Infrastructure.Data;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -8,6 +10,12 @@

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
dbContext.Database.Migrate();
}

app.UseToolInventoryPipeline();
app.Run();

Expand Down
3 changes: 2 additions & 1 deletion src/ToolInventory.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
},
"Cors": {
"AllowedOrigins": [
"http://localhost:4200"
"http://localhost:4200",
"http://localhost:4201"
]
},
"Jwt": {
Expand Down
17 changes: 11 additions & 6 deletions tests/ToolInventory.E2E.Tests/CheckoutsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ 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]");
var table = _fixture.Page.Locator("table[mat-table]");
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')");
Expand All @@ -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");
Expand All @@ -50,25 +53,27 @@ 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')");

await E2ETestHelper.ExpectSnackBarAsync(_fixture.Page, "checked out");

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();

Expand Down
86 changes: 85 additions & 1 deletion tests/ToolInventory.E2E.Tests/E2ETestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> CreateToolAsync(IPage page, string? toolName = null)
{
await page.GotoAsync("/tools");
Expand All @@ -29,11 +90,34 @@ public static async Task<string> 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<string> GetCurrentUserIdAsync(IPage page)
{
var userId = await page.EvaluateAsync<string>(
@"() => {
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);
Expand Down
7 changes: 6 additions & 1 deletion tests/ToolInventory.E2E.Tests/Fixtures/PlaywrightFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down Expand Up @@ -32,4 +32,9 @@ public async Task DisposeAsync()
await Browser.DisposeAsync();
Playwright.Dispose();
}

public async Task LoginForTestAsync()
{
await E2ETestHelper.LoginAsync(Page);
}
}
14 changes: 9 additions & 5 deletions tests/ToolInventory.E2E.Tests/MaintenanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ 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]");
var table = _fixture.Page.Locator("table[mat-table]");
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')");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");

Expand Down
12 changes: 8 additions & 4 deletions tests/ToolInventory.E2E.Tests/NavigationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -29,28 +30,31 @@ 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");

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");
Expand Down
Loading
Loading