diff --git a/EstateManagementUI.BlazorServer.Tests/Common/BootstrapperExtensionsTests.cs b/EstateManagementUI.BlazorServer.Tests/Common/BootstrapperExtensionsTests.cs new file mode 100644 index 00000000..d41e6288 --- /dev/null +++ b/EstateManagementUI.BlazorServer.Tests/Common/BootstrapperExtensionsTests.cs @@ -0,0 +1,42 @@ +using EstateManagementUI.BlazorServer.Common; +using EstateManagementUI.BlazorServer.Components; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; + +namespace EstateManagementUI.BlazorServer.Tests.Common; + +public sealed class BootstrapperExtensionsTests +{ + [Fact] + public async Task ConfigureLiveLogin_registers_only_the_live_authentication_route() + { + WebApplicationBuilder builder = WebApplication.CreateBuilder(); + builder.Services.AddRazorComponents(); + + WebApplication app = builder.Build(); + app.MapRazorComponents(); + + app.ConfigureLiveLogin(); + + await app.StartAsync(TestContext.Current.CancellationToken); + + var endpoints = app.Services.GetRequiredService().Endpoints; + var loginEndpoints = endpoints + .OfType() + .Where(endpoint => endpoint.RoutePattern.RawText == "/login") + .Where(endpoint => endpoint.Metadata.GetMetadata()?.HttpMethods.Contains("GET") == true) + .ToList(); + var authenticationLoginEndpoints = endpoints + .OfType() + .Where(endpoint => endpoint.RoutePattern.RawText == "/authentication/login") + .Where(endpoint => endpoint.Metadata.GetMetadata()?.HttpMethods.Contains("GET") == true) + .ToList(); + + loginEndpoints.Count.ShouldBe(1); + authenticationLoginEndpoints.Count.ShouldBe(1); + + await app.StopAsync(TestContext.Current.CancellationToken); + } +} diff --git a/EstateManagementUI.BlazorServer.Tests/Pages/EntryScreenPageTests.cs b/EstateManagementUI.BlazorServer.Tests/Pages/EntryScreenPageTests.cs index 9f49d0b4..8a5b72e4 100644 --- a/EstateManagementUI.BlazorServer.Tests/Pages/EntryScreenPageTests.cs +++ b/EstateManagementUI.BlazorServer.Tests/Pages/EntryScreenPageTests.cs @@ -1,5 +1,7 @@ using Bunit; using EstateManagementUI.BlazorServer.Components.Pages; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Shouldly; using TestContext = Bunit.TestContext; @@ -7,6 +9,16 @@ namespace EstateManagementUI.BlazorServer.Tests.Pages; public class EntryScreenPageTests : TestContext { + public EntryScreenPageTests() + { + Services.AddSingleton(new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + ["AppSettings:TestMode"] = "Disabled" + }) + .Build()); + } + [Fact] public void EntryScreen_RendersCorrectly() { @@ -75,4 +87,27 @@ public void EntryScreen_HasCorrectPageTitle() var pageTitle = cut.FindComponent(); pageTitle.Instance.ChildContent.ShouldNotBeNull(); } + + [Fact] + public void EntryScreen_live_mode_sign_in_link_goes_directly_to_authentication() + { + var cut = RenderComponent(); + + cut.Find("#loginButton").GetAttribute("href").ShouldBe("/authentication/login"); + } + + [Fact] + public void EntryScreen_test_mode_sign_in_link_goes_to_login_page() + { + Services.AddSingleton(new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + ["AppSettings:TestMode"] = "BackedByTestDataStore" + }) + .Build()); + + var cut = RenderComponent(); + + cut.Find("#loginButton").GetAttribute("href").ShouldBe("/login"); + } } diff --git a/EstateManagementUI.BlazorServer/Common/BoostrapperExtensions.cs b/EstateManagementUI.BlazorServer/Common/BoostrapperExtensions.cs index 912d9ba8..d4d283e2 100644 --- a/EstateManagementUI.BlazorServer/Common/BoostrapperExtensions.cs +++ b/EstateManagementUI.BlazorServer/Common/BoostrapperExtensions.cs @@ -25,7 +25,7 @@ namespace EstateManagementUI.BlazorServer.Common; public static class BoostrapperExtensions { public static WebApplication ConfigureLiveLogin(this WebApplication app) { - app.MapGet("/login", (HttpContext context) => + app.MapGet("/authentication/login", (HttpContext context) => { return Results.Challenge( properties: new Microsoft.AspNetCore.Authentication.AuthenticationProperties diff --git a/EstateManagementUI.BlazorServer/Components/Pages/EntryScreen.razor b/EstateManagementUI.BlazorServer/Components/Pages/EntryScreen.razor index f00bc25e..504e2929 100644 --- a/EstateManagementUI.BlazorServer/Components/Pages/EntryScreen.razor +++ b/EstateManagementUI.BlazorServer/Components/Pages/EntryScreen.razor @@ -1,6 +1,7 @@ @page "/entry" @using Microsoft.AspNetCore.Components.Authorization @layout Components.Layout.EntryLayout +@inject IConfiguration Configuration Welcome - Estate Management @@ -131,7 +132,7 @@

Sign in to access all features

- + @@ -141,3 +142,12 @@
+ +@code { + private string SignInHref => string.Equals( + Configuration["AppSettings:TestMode"], + "Disabled", + StringComparison.OrdinalIgnoreCase) + ? "/authentication/login" + : "/login"; +} diff --git a/EstateManagementUI.BlazorServer/Components/Pages/Login.razor b/EstateManagementUI.BlazorServer/Components/Pages/Login.razor index fccc3cf2..06665704 100644 --- a/EstateManagementUI.BlazorServer/Components/Pages/Login.razor +++ b/EstateManagementUI.BlazorServer/Components/Pages/Login.razor @@ -1,5 +1,6 @@ @page "/login" @layout Components.Layout.EntryLayout +@inject IConfiguration Configuration Sign in - Estate Management @@ -11,42 +12,58 @@

Use your estate credentials to continue.

-
- - -
- - -
- -
- - -
- - -
+ @if (IsTestMode) + { +
+ + +
+ + +
+ +
+ + +
+ + +
+ } + else + { +
+ Continue to sign in + + } @code { + private bool IsTestMode => !string.Equals( + Configuration["AppSettings:TestMode"], + "Disabled", + StringComparison.OrdinalIgnoreCase); + [SupplyParameterFromQuery] public string? ReturnUrl { get; set; } = "/"; }