From bdb3ee17973519185f5f0b8ee2db622df2738c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20=C3=85berg?= Date: Wed, 3 Sep 2025 09:15:03 +0200 Subject: [PATCH] AspNet tests Add AspNet tests --- .../AddFido2ExtensionTests.cs | 98 +++++++++++++++++++ .../Fido2.AspNet.Tests.csproj | 35 +++++++ fido2-net-lib.sln | 7 ++ 3 files changed, 140 insertions(+) create mode 100644 Tests/Fido2.AspNet.Tests/AddFido2ExtensionTests.cs create mode 100644 Tests/Fido2.AspNet.Tests/Fido2.AspNet.Tests.csproj diff --git a/Tests/Fido2.AspNet.Tests/AddFido2ExtensionTests.cs b/Tests/Fido2.AspNet.Tests/AddFido2ExtensionTests.cs new file mode 100644 index 00000000..7cab3346 --- /dev/null +++ b/Tests/Fido2.AspNet.Tests/AddFido2ExtensionTests.cs @@ -0,0 +1,98 @@ +using System; + +using Fido2NetLib; + +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Internal; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Fido2.AspNet.Tests; + +public class AddFido2ExtensionTests +{ + [Fact] + public void AddFido2_WithConfiguration_RegistersServices() + { + // Arrange + var services = new ServiceCollection(); + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + ["ServerName"] = "Test Server", + ["ServerDomain"] = "localhost", + ["Origins"] = "https://localhost:5001" + }) + .Build(); + + // Act + var builder = services.AddFido2(configuration); + + // Assert + Assert.NotNull(builder); + Assert.IsAssignableFrom(builder); + + var serviceProvider = services.BuildServiceProvider(); + + // Verify IFido2 can be resolved + var fido2 = serviceProvider.GetService(); + Assert.NotNull(fido2); + + // Verify Fido2Configuration can be resolved + var config = serviceProvider.GetService(); + Assert.NotNull(config); + Assert.Equal("Test Server", config.ServerName); + Assert.Equal("localhost", config.ServerDomain); + + // Verify ISystemClock is registered + var systemClock = serviceProvider.GetService(); + Assert.NotNull(systemClock); + + // Verify MDS is null + // var mds = serviceProvider.GetService(); + // Assert.Null(mds); + } + + [Fact] + public void AddFido2_WithSetupAction_RegistersServices() + { + // Arrange + var services = new ServiceCollection(); + + // Act + var builder = services.AddFido2(config => + { + config.ServerName = "Action Server"; + config.ServerDomain = "example.com"; + config.Origins = new HashSet { "https://example.com" }; + }); + + // Assert + Assert.NotNull(builder); + Assert.IsAssignableFrom(builder); + + var serviceProvider = services.BuildServiceProvider(); + + // Verify IFido2 can be resolved + var fido2 = serviceProvider.GetService(); + Assert.NotNull(fido2); + + // Verify Fido2Configuration can be resolved with correct values + var config = serviceProvider.GetService(); + Assert.NotNull(config); + Assert.Equal("Action Server", config.ServerName); + Assert.Equal("example.com", config.ServerDomain); + Assert.Contains("https://example.com", config.Origins); + + // Verify ISystemClock is registered + var systemClock = serviceProvider.GetService(); + Assert.NotNull(systemClock); + + // Verify MDS is null + // var mds = serviceProvider.GetService(); + // Assert.Null(mds); + } +} diff --git a/Tests/Fido2.AspNet.Tests/Fido2.AspNet.Tests.csproj b/Tests/Fido2.AspNet.Tests/Fido2.AspNet.Tests.csproj new file mode 100644 index 00000000..e8abe5f3 --- /dev/null +++ b/Tests/Fido2.AspNet.Tests/Fido2.AspNet.Tests.csproj @@ -0,0 +1,35 @@ + + + + $(SupportedTargetFrameworks) + $(NoWarm);CA1822;IDE0007;IDE0037;IDE0039;IDE0057;CA1825 + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/fido2-net-lib.sln b/fido2-net-lib.sln index 710f20dc..d229ece3 100644 --- a/fido2-net-lib.sln +++ b/fido2-net-lib.sln @@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo", "Demo\Demo.csproj", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Demos", "Demos", "{9A6A08F5-14A7-4256-9717-2CF80B5B4AAC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fido2.AspNet.Tests", "Tests\Fido2.AspNet.Tests\Fido2.AspNet.Tests.csproj", "{B4E8AF2B-E616-4197-8366-CAF1222B53DD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -88,6 +90,10 @@ Global {BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A}.Debug|Any CPU.Build.0 = Debug|Any CPU {BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A}.Release|Any CPU.ActiveCfg = Release|Any CPU {BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A}.Release|Any CPU.Build.0 = Release|Any CPU + {B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -107,5 +113,6 @@ Global {EAFF4332-145B-4719-B261-827CEB447A62} = {9A6A08F5-14A7-4256-9717-2CF80B5B4AAC} {0EC19932-290A-4D92-9603-B0533DD022D9} = {9A6A08F5-14A7-4256-9717-2CF80B5B4AAC} {BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A} = {9A6A08F5-14A7-4256-9717-2CF80B5B4AAC} + {B4E8AF2B-E616-4197-8366-CAF1222B53DD} = {B7A8B758-5566-4D58-9157-36D23395C0E3} EndGlobalSection EndGlobal