From 883305a06d7268b178ada7b83a07672071963fe5 Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Tue, 13 Jan 2026 11:28:45 +0000 Subject: [PATCH 1/2] Refactor TestMode to enum, update config and services Refactored TestMode from boolean to enum for granular control of test behaviors. Updated appsettings.json and authentication client details. Adjusted Program.cs to use the new enum for service registration, authentication, and endpoint mapping. Updated RoleSwitcher.razor to use TestMode enum logic. Removed Counter.razor and its tests. Added TestMode enum definition. --- .../Pages/CounterPageTests.cs | 61 ------------------- .../Components/Layout/RoleSwitcher.razor | 12 ++-- .../Components/Pages/Counter.razor | 19 ------ EstateManagementUI.BlazorServer/Program.cs | 23 +++++-- .../appsettings.json | 8 +-- 5 files changed, 30 insertions(+), 93 deletions(-) delete mode 100644 EstateManagementUI.BlazorServer.Tests/Pages/CounterPageTests.cs delete mode 100644 EstateManagementUI.BlazorServer/Components/Pages/Counter.razor diff --git a/EstateManagementUI.BlazorServer.Tests/Pages/CounterPageTests.cs b/EstateManagementUI.BlazorServer.Tests/Pages/CounterPageTests.cs deleted file mode 100644 index 8769a007..00000000 --- a/EstateManagementUI.BlazorServer.Tests/Pages/CounterPageTests.cs +++ /dev/null @@ -1,61 +0,0 @@ -using Bunit; -using EstateManagementUI.BlazorServer.Components.Pages; -using Shouldly; - -namespace EstateManagementUI.BlazorServer.Tests.Pages; - -public class CounterPageTests : TestContext -{ - [Fact] - public void Counter_RendersCorrectly() - { - // Act - var cut = RenderComponent(); - - // Assert - cut.Find("h1").TextContent.ShouldBe("Counter"); - cut.Find("p[role='status']").TextContent.ShouldBe("Current count: 0"); - cut.Find("button").TextContent.Trim().ShouldBe("Click me"); - } - - [Fact] - public void Counter_IncrementButton_IncrementsCount() - { - // Arrange - var cut = RenderComponent(); - var button = cut.Find("button"); - - // Act - button.Click(); - - // Assert - cut.Find("p[role='status']").TextContent.ShouldBe("Current count: 1"); - } - - [Fact] - public void Counter_MultipleClicks_IncrementsCorrectly() - { - // Arrange - var cut = RenderComponent(); - var button = cut.Find("button"); - - // Act - button.Click(); - button.Click(); - button.Click(); - - // Assert - cut.Find("p[role='status']").TextContent.ShouldBe("Current count: 3"); - } - - [Fact] - public void Counter_HasCorrectPageTitle() - { - // Act - var cut = RenderComponent(); - - // Assert - var pageTitle = cut.FindComponent(); - pageTitle.Instance.ChildContent.ShouldNotBeNull(); - } -} diff --git a/EstateManagementUI.BlazorServer/Components/Layout/RoleSwitcher.razor b/EstateManagementUI.BlazorServer/Components/Layout/RoleSwitcher.razor index bcdd88f4..40d082d6 100644 --- a/EstateManagementUI.BlazorServer/Components/Layout/RoleSwitcher.razor +++ b/EstateManagementUI.BlazorServer/Components/Layout/RoleSwitcher.razor @@ -5,7 +5,7 @@ @inject IPermissionService PermissionService @inject IPermissionKeyProvider PermissionKeyProvider -@if (isTestMode) +@if (testMode == TestMode.Full || testMode == TestMode.AuthenticationOnly) {
- -@code { - private int currentCount = 0; - - private void IncrementCount() - { - currentCount++; - } -} diff --git a/EstateManagementUI.BlazorServer/Program.cs b/EstateManagementUI.BlazorServer/Program.cs index 4db12e7b..bf09e9de 100644 --- a/EstateManagementUI.BlazorServer/Program.cs +++ b/EstateManagementUI.BlazorServer/Program.cs @@ -66,7 +66,10 @@ JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // Check if running in test mode -var testMode = builder.Configuration.GetValue("AppSettings:TestMode", false); +var testModeConfig = builder.Configuration.GetValue("AppSettings:TestMode", "Disabled"); +// Convert to enum +var testMode = Enum.Parse(testModeConfig, ignoreCase: true); + Console.WriteLine($"Application running in Test Mode: {testMode}"); // Add services to the container. @@ -83,7 +86,7 @@ }); // Configure authentication based on mode -if (testMode) +if (testMode == TestMode.AuthenticationOnly || testMode == TestMode.Full) // TODO: bitmap?? { // Test mode: Use test authentication handler to bypass OIDC builder.Services.AddAuthentication(options => @@ -167,6 +170,7 @@ NameClaimType = "name", RoleClaimType = "role" }; + options.ClaimActions.MapAllExcept("iss", "nbf", "exp", "aud", "nonce", "iat", "c_hash"); // Set MetadataAddress to use the authority address options.MetadataAddress = $"{authorityAddress}/.well-known/openid-configuration"; @@ -200,7 +204,7 @@ Console.WriteLine("Registered Permission services"); // Register MediatR service based on test mode -if (testMode) +if (testMode == TestMode.BackedByTestDataStore || testMode == TestMode.Full) { Console.WriteLine("Registering TestMediatorService with in-memory test data store"); builder.Services.AddSingleton(); @@ -208,6 +212,7 @@ } else { + // TODO: this will be th real backedn services Console.WriteLine("Registering StubbedMediatorService"); builder.Services.AddSingleton(); } @@ -236,7 +241,7 @@ .AddInteractiveServerRenderMode(); // Add login endpoint - behavior depends on test mode -if (testMode) +if (testMode == TestMode.AuthenticationOnly || testMode == TestMode.Full) { app.MapGet("/login", (HttpContext context) => { @@ -263,7 +268,7 @@ } // Add logout endpoint - behavior depends on test mode -if (testMode) +if (testMode == TestMode.AuthenticationOnly || testMode == TestMode.Full) { app.MapGet("/logout", (HttpContext context) => { @@ -282,3 +287,11 @@ } app.Run(); + + +enum TestMode { + Disabled, + AuthenticationOnly, + BackedByTestDataStore, + Full +} \ No newline at end of file diff --git a/EstateManagementUI.BlazorServer/appsettings.json b/EstateManagementUI.BlazorServer/appsettings.json index f5f9ebc1..6a3cdd4c 100644 --- a/EstateManagementUI.BlazorServer/appsettings.json +++ b/EstateManagementUI.BlazorServer/appsettings.json @@ -9,13 +9,13 @@ "AppSettings": { "SecurityServiceLocalPort": null, "SecurityServicePort": null, - "HttpClientIgnoreCertificateErrors": false, - "TestMode": true + "HttpClientIgnoreCertificateErrors": false, + "TestMode": "BackedByTestDataStore" }, "Authentication": { "Authority": "https://localhost:5001", - "ClientId": "estateUIClient", - "ClientSecret": "Secret1", + "ClientId": "managementUIClient", + "ClientSecret": "d192cbc46d834d0da90e8a9d50ded543", "CallbackPath": "/signin-oidc", "ResponseType": "code id_token", "Scopes": [ From 46e131cee64985db6dab76a5ef8e975d068adee7 Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Tue, 13 Jan 2026 11:39:25 +0000 Subject: [PATCH 2/2] fix code quality issues --- EstateManagementUI.BlazorServer/Program.cs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/EstateManagementUI.BlazorServer/Program.cs b/EstateManagementUI.BlazorServer/Program.cs index bf09e9de..3d25ea30 100644 --- a/EstateManagementUI.BlazorServer/Program.cs +++ b/EstateManagementUI.BlazorServer/Program.cs @@ -86,7 +86,7 @@ }); // Configure authentication based on mode -if (testMode == TestMode.AuthenticationOnly || testMode == TestMode.Full) // TODO: bitmap?? +if (testMode == TestMode.AuthenticationOnly || testMode == TestMode.Full) { // Test mode: Use test authentication handler to bypass OIDC builder.Services.AddAuthentication(options => @@ -212,7 +212,6 @@ } else { - // TODO: this will be th real backedn services Console.WriteLine("Registering StubbedMediatorService"); builder.Services.AddSingleton(); } @@ -248,6 +247,12 @@ // In test mode, redirect directly to home since authentication is automatic return Results.Redirect("/"); }).AllowAnonymous(); + + app.MapGet("/logout", (HttpContext context) => + { + // In test mode, just redirect to home + return Results.Redirect("/"); + }).RequireAuthorization(); } else { @@ -265,19 +270,7 @@ authenticationSchemes: new[] { OpenIdConnectDefaults.AuthenticationScheme } ); }).AllowAnonymous(); -} -// Add logout endpoint - behavior depends on test mode -if (testMode == TestMode.AuthenticationOnly || testMode == TestMode.Full) -{ - app.MapGet("/logout", (HttpContext context) => - { - // In test mode, just redirect to home - return Results.Redirect("/"); - }).RequireAuthorization(); -} -else -{ app.MapGet("/logout", async (HttpContext context) => { await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);