Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Data not saving #2471

Merged
merged 5 commits into from
Sep 6, 2024
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
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private bool TryGetSetting(string name, out object? value)
{
if (UseApplicationData)
{
return DataContainer.Values.TryGetValue(GetKey(name), out value);
return DataContainer.Values.TryGetValue(name, out value);
}
else
{
Expand All @@ -41,7 +41,7 @@ private void RemoveSetting(string name)
{
if (UseApplicationData)
{
_ = DataContainer.Values.Remove(GetKey(name));
_ = DataContainer.Values.Remove(name);
}
else
{
Expand All @@ -53,7 +53,7 @@ private void SetSetting(string name, object? value)
{
if (UseApplicationData)
{
DataContainer.Values[GetKey(name)] = value;
DataContainer.Values[name] = value;
}
else
{
Expand Down Expand Up @@ -84,7 +84,7 @@ protected override async ValueTask InternalClearAsync(string? name, Cancellation
if (name is not null &&
!string.IsNullOrEmpty(name))
{
RemoveSetting(name);
RemoveSetting(GetKey(name));
}
else
{
Expand Down
20 changes: 2 additions & 18 deletions src/Uno.Extensions.Storage.UI/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public static IServiceCollection AddKeyedStorage(this IServiceCollection service
)
#endif
.SetDefaultInstance<IKeyValueStorage>(
#if WINUI
#if __ANDROID__
KeyStoreKeyValueStorage.Name
#elif __IOS__
Expand All @@ -72,24 +71,9 @@ public static IServiceCollection AddKeyedStorage(this IServiceCollection service
EncryptedApplicationDataKeyValueStorage.Name
#else
// For WASM and other platforms where we don't currently have
// a secure storage option, we default to InMemory to avoid
// a secure storage option, we default to ApplicationDataKeyValueStorage to avoid
// security concerns with saving plain text
InMemoryKeyValueStorage.Name
#endif

#else
#if __ANDROID__
PasswordVaultKeyValueStorage.Name
#elif __IOS__
PasswordVaultKeyValueStorage.Name
#elif WINDOWS_UWP
PasswordVaultKeyValueStorage.Name
#else
// For WASM and other platforms where we don't currently have
// a secure storage option, we default to InMemory to avoid
// security concerns with saving plain text
InMemoryKeyValueStorage.Name
#endif
ApplicationDataKeyValueStorage.Name
#endif
);
}
Expand Down
1 change: 1 addition & 0 deletions testing/TestHarness/TestHarness.Core/TestSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,30 @@ 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

// This is not being found when running on CI
// Commented out to test - bring it back and investigate the issue
//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
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<linker>
<assembly fullname="TestHarness" />
<assembly fullname="TestHarness.Core" />
<assembly fullname="TestHarnessApp" />
<assembly fullname="Uno.UI" />
<assembly fullname="Microsoft.Extensions.Options" />
<assembly fullname="Refit" />
<assembly fullname="System.IdentityModel.Tokens.Jwt" />

<!-- This is required by Json.NET and any expression.Compile caller -->
<assembly fullname="System.Core">
<type fullname="System.Linq.Expressions*" />
</assembly>
<assembly fullname="TestHarness" />
<assembly fullname="TestHarness.Core" />
<assembly fullname="TestHarnessApp" />
<assembly fullname="Uno.UI" />
<assembly fullname="Microsoft.Extensions.Options" />
<assembly fullname="Refit" />
<assembly fullname="System.IdentityModel.Tokens.Jwt" />
<assembly fullname="Uno.Extensions.Authentication">
<type fullname="Uno.Extensions.Authentication.Custom.CustomAuthenticationProvider" />
</assembly>
<assembly fullname="Uno.Extensions.Storage">
<type fullname="Uno.Extensions.Storage.KeyValueStorage.InMemoryKeyValueStorage" />
</assembly>
<!-- This is required by Json.NET and any expression.Compile caller -->
<assembly fullname="System.Core">
<type fullname="System.Linq.Expressions*" />
</assembly>
</linker>
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>
Loading