From b48f8881b07977339b9095fdbd182a8a6e636048 Mon Sep 17 00:00:00 2001 From: eriklimakc Date: Fri, 23 Aug 2024 16:38:11 +0100 Subject: [PATCH] chore: Add Mock Auth test --- .../TestHarness.Core/TestSections.cs | 1 + .../Ext/Authentication/Custom/Given_Custom.cs | 23 ++++++ .../CustomAuthenticationMainPage.xaml.cs | 1 + .../CustomAuthenticationMockHostInit.cs | 71 +++++++++++++++++++ .../TestHarness/TestHarness/MainPage.xaml.cs | 3 + .../TestHarness/TestFrameHost.xaml | 29 ++++---- 6 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMockHostInit.cs diff --git a/testing/TestHarness/TestHarness.Core/TestSections.cs b/testing/TestHarness/TestHarness.Core/TestSections.cs index aff7592e3a..f4edf27f92 100644 --- a/testing/TestHarness/TestHarness.Core/TestSections.cs +++ b/testing/TestHarness/TestHarness.Core/TestSections.cs @@ -19,6 +19,7 @@ public enum TestSections Apps_ToDo, Apps_Regions, Authentication_Custom, + Authentication_Custom_Mock, Authentication_Custom_Service, Authentication_Custom_TestBackend, Authentication_Custom_TestBackend_Cookies, diff --git a/testing/TestHarness/TestHarness.UITest/Ext/Authentication/Custom/Given_Custom.cs b/testing/TestHarness/TestHarness.UITest/Ext/Authentication/Custom/Given_Custom.cs index a903eb50f5..0b9c518e16 100644 --- a/testing/TestHarness/TestHarness.UITest/Ext/Authentication/Custom/Given_Custom.cs +++ b/testing/TestHarness/TestHarness.UITest/Ext/Authentication/Custom/Given_Custom.cs @@ -23,4 +23,27 @@ public async Task When_Custom_Auth() } + [Test] + public async Task When_Custom_Mock_Auth() + { + InitTestSection(TestSections.Authentication_Custom_Mock); + + App.WaitThenTap("ShowAppButton"); + + // Make sure the app has loaded + App.WaitElement("LoginNavigationBar"); + + // Login + await App.TapAndWait("LoginButton", "HomeNavigationBar"); + + // Exit the test + App.WaitThenTap("ExitTestButton"); + + // Re-enter the test + InitTestSection(TestSections.Authentication_Custom_Mock); + + // Expect HomePage instead of LoginPage + App.WaitElement("HomeNavigationBar"); + } + } diff --git a/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMainPage.xaml.cs b/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMainPage.xaml.cs index db72d7c962..c249b253e0 100644 --- a/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMainPage.xaml.cs +++ b/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMainPage.xaml.cs @@ -1,6 +1,7 @@ namespace TestHarness.Ext.Authentication.Custom; [TestSectionRoot("Authentication: Custom",TestSections.Authentication_Custom, typeof(CustomAuthenticationHostInit))] +[TestSectionRoot("Authentication: Custom with Mock",TestSections.Authentication_Custom_Mock, typeof(CustomAuthenticationMockHostInit))] [TestSectionRoot("Authentication: Custom with Service",TestSections.Authentication_Custom_Service, typeof(CustomAuthenticationServiceHostInit))] [TestSectionRoot("Authentication: Custom with Test Backend",TestSections.Authentication_Custom_TestBackend, typeof(CustomAuthenticationTestBackendHostInit))] [TestSectionRoot("Authentication: Custom with Test Backend using Cookies", TestSections.Authentication_Custom_TestBackend_Cookies, typeof(CustomAuthenticationTestBackendCookieHostInit))] diff --git a/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMockHostInit.cs b/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMockHostInit.cs new file mode 100644 index 0000000000..3905847a94 --- /dev/null +++ b/testing/TestHarness/TestHarness/Ext/Authentication/Custom/CustomAuthenticationMockHostInit.cs @@ -0,0 +1,71 @@ +namespace TestHarness.Ext.Authentication.Custom; + +public class CustomAuthenticationMockHostInit : BaseHostInitialization +{ + protected override IHostBuilder Custom(IHostBuilder builder) + { + return builder.UseAuthentication(auth => + auth.AddCustom(custom => + custom + .Login(async (sp, dispatcher, credentials, cancellationToken) => + { + if (credentials?.TryGetValue(nameof(CustomAuthenticationCredentials.Username), out var username) ?? false && + !username.IsNullOrEmpty()) + { + credentials ??= new Dictionary(); + credentials[TokenCacheExtensions.AccessTokenKey] = "SampleToken"; + credentials[TokenCacheExtensions.RefreshTokenKey] = "RefreshToken"; + credentials["Expiry"] = DateTime.Now.AddMinutes(5).ToString("g"); + return credentials; + } + + return default; + }) + .Refresh(async (sp, tokenDictionary, cancellationToken) => + { + if ((tokenDictionary?.TryGetValue(TokenCacheExtensions.RefreshTokenKey, out var refreshToken) ?? false) && + !refreshToken.IsNullOrEmpty() && + (tokenDictionary?.TryGetValue("Expiry", out var expiry) ?? false) && + DateTime.TryParse(expiry, out var tokenExpiry) && + tokenExpiry > DateTime.Now) + { + tokenDictionary ??= new Dictionary(); + tokenDictionary[TokenCacheExtensions.AccessTokenKey] = "NewSampleToken"; + tokenDictionary["Expiry"] = DateTime.Now.AddMinutes(5).ToString("g"); + return tokenDictionary; + } + + return default; + }) + , name: "CustomAuth") + ) + .ConfigureServices(services => + services + .AddSingleton( + _ => new AuthenticationRouteInfo< + CustomAuthenticationLoginViewModel, + CustomAuthenticationHomeViewModel>()) + ); + } + + + protected override void RegisterRoutes(IViewRegistry views, IRouteRegistry routes) + { + + views.Register( + new ViewMap(ViewModel: typeof(AuthenticationShellViewModel)), + new ViewMap(), + new ViewMap() + ); + + + routes + .Register( + new RouteMap("", View: views.FindByViewModel(), + Nested: new RouteMap[] + { + new RouteMap("Login", View: views.FindByViewModel()), + new RouteMap("Home", View: views.FindByViewModel()) + })); + } +} diff --git a/testing/TestHarness/TestHarness/MainPage.xaml.cs b/testing/TestHarness/TestHarness/MainPage.xaml.cs index 2b92422f59..c6b24946f8 100644 --- a/testing/TestHarness/TestHarness/MainPage.xaml.cs +++ b/testing/TestHarness/TestHarness/MainPage.xaml.cs @@ -38,6 +38,9 @@ private void TestSectionSelectionChanged(object sender, SelectionChangedEventArg Activator.CreateInstance(section.HostInitializer) : default; this.Frame.Navigate(section.MainPage, hostInit); + + // Clear ListView selection + TestSectionsListView.SelectedItem = null; } } } diff --git a/testing/TestHarness/TestHarness/TestFrameHost.xaml b/testing/TestHarness/TestHarness/TestFrameHost.xaml index b26e08096a..741f7405d8 100644 --- a/testing/TestHarness/TestHarness/TestFrameHost.xaml +++ b/testing/TestHarness/TestHarness/TestFrameHost.xaml @@ -1,21 +1,24 @@ - + - -