Skip to content

Commit

Permalink
chore: Add Mock Auth test
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Aug 23, 2024
1 parent e5a745e commit eede1fe
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 13 deletions.
1 change: 1 addition & 0 deletions testing/TestHarness/TestHarness.Core/TestSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum TestSections
Apps_ToDo,
Apps_Regions,
Authentication_Custom,
Authentication_Custom_Mock,
Authentication_Custom_Service,
Authentication_Custom_TestBackend,
Authentication_Custom_TestBackend_Cookies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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))]
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, string>();
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<string, string>();
tokenDictionary[TokenCacheExtensions.AccessTokenKey] = "NewSampleToken";
tokenDictionary["Expiry"] = DateTime.Now.AddMinutes(5).ToString("g");
return tokenDictionary;
}
return default;
})
, name: "CustomAuth")
)
.ConfigureServices(services =>
services
.AddSingleton<IAuthenticationRouteInfo>(
_ => new AuthenticationRouteInfo<
CustomAuthenticationLoginViewModel,
CustomAuthenticationHomeViewModel>())
);
}


protected override void RegisterRoutes(IViewRegistry views, IRouteRegistry routes)
{

views.Register(
new ViewMap(ViewModel: typeof(AuthenticationShellViewModel)),
new ViewMap<CustomAuthenticationLoginPage, CustomAuthenticationLoginViewModel>(),
new ViewMap<CustomAuthenticationHomeTestBackendPage, CustomAuthenticationHomeTestBackendViewModel>()
);


routes
.Register(
new RouteMap("", View: views.FindByViewModel<AuthenticationShellViewModel>(),
Nested: new RouteMap[]
{
new RouteMap("Login", View: views.FindByViewModel<CustomAuthenticationLoginViewModel>()),
new RouteMap("Home", View: views.FindByViewModel<CustomAuthenticationHomeTestBackendViewModel>())
}));
}
}
3 changes: 3 additions & 0 deletions testing/TestHarness/TestHarness/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
29 changes: 16 additions & 13 deletions testing/TestHarness/TestHarness/TestFrameHost.xaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<UserControl
x:Class="TestHarness.TestFrameHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHarness"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
>
<UserControl x:Class="TestHarness.TestFrameHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestHarness"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Frame x:Name="TestFrame" SourcePageType="local:MainPage"/>
<Button Grid.Row="1" HorizontalAlignment="Center" Content="Exit Test" IsEnabled="{Binding ElementName=TestFrame, Path=CanGoBack}" Click="ExitTestClick" />
<Frame x:Name="TestFrame" SourcePageType="local:MainPage" />
<Button Grid.Row="1"
HorizontalAlignment="Center"
Content="Exit Test"
AutomationProperties.AutomationId="ExitTestButton"
IsEnabled="{Binding ElementName=TestFrame, Path=CanGoBack}"
Click="ExitTestClick" />
</Grid>
</UserControl>

0 comments on commit eede1fe

Please sign in to comment.