Skip to content

Commit eb209c7

Browse files
authored
Add AspNet builder tests (#616)
This PR adds some rudimentary tests for our builders
1 parent b3d136d commit eb209c7

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
3+
using Fido2NetLib;
4+
5+
using Microsoft.Extensions.Caching.Distributed;
6+
using Microsoft.Extensions.Caching.Memory;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Internal;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Options;
12+
13+
namespace Fido2.AspNet.Tests;
14+
15+
public class AddFido2ExtensionTests
16+
{
17+
[Fact]
18+
public void AddFido2_WithConfiguration_RegistersServices()
19+
{
20+
// Arrange
21+
var services = new ServiceCollection();
22+
var configuration = new ConfigurationBuilder()
23+
.AddInMemoryCollection(new Dictionary<string, string>
24+
{
25+
["ServerName"] = "Test Server",
26+
["ServerDomain"] = "localhost",
27+
["Origins"] = "https://localhost:5001"
28+
})
29+
.Build();
30+
31+
// Act
32+
var builder = services.AddFido2(configuration);
33+
34+
// Assert
35+
Assert.NotNull(builder);
36+
Assert.IsAssignableFrom<IFido2NetLibBuilder>(builder);
37+
38+
var serviceProvider = services.BuildServiceProvider();
39+
40+
// Verify IFido2 can be resolved
41+
var fido2 = serviceProvider.GetService<IFido2>();
42+
Assert.NotNull(fido2);
43+
44+
// Verify Fido2Configuration can be resolved
45+
var config = serviceProvider.GetService<Fido2Configuration>();
46+
Assert.NotNull(config);
47+
Assert.Equal("Test Server", config.ServerName);
48+
Assert.Equal("localhost", config.ServerDomain);
49+
50+
// Verify ISystemClock is registered
51+
var systemClock = serviceProvider.GetService<ISystemClock>();
52+
Assert.NotNull(systemClock);
53+
54+
// Verify MDS is null
55+
// var mds = serviceProvider.GetService<IMetadataService>();
56+
// Assert.Null(mds);
57+
}
58+
59+
[Fact]
60+
public void AddFido2_WithSetupAction_RegistersServices()
61+
{
62+
// Arrange
63+
var services = new ServiceCollection();
64+
65+
// Act
66+
var builder = services.AddFido2(config =>
67+
{
68+
config.ServerName = "Action Server";
69+
config.ServerDomain = "example.com";
70+
config.Origins = new HashSet<string> { "https://example.com" };
71+
});
72+
73+
// Assert
74+
Assert.NotNull(builder);
75+
Assert.IsAssignableFrom<IFido2NetLibBuilder>(builder);
76+
77+
var serviceProvider = services.BuildServiceProvider();
78+
79+
// Verify IFido2 can be resolved
80+
var fido2 = serviceProvider.GetService<IFido2>();
81+
Assert.NotNull(fido2);
82+
83+
// Verify Fido2Configuration can be resolved with correct values
84+
var config = serviceProvider.GetService<Fido2Configuration>();
85+
Assert.NotNull(config);
86+
Assert.Equal("Action Server", config.ServerName);
87+
Assert.Equal("example.com", config.ServerDomain);
88+
Assert.Contains("https://example.com", config.Origins);
89+
90+
// Verify ISystemClock is registered
91+
var systemClock = serviceProvider.GetService<ISystemClock>();
92+
Assert.NotNull(systemClock);
93+
94+
// Verify MDS is null
95+
// var mds = serviceProvider.GetService<IMetadataService>();
96+
// Assert.Null(mds);
97+
}
98+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(SupportedTargetFrameworks)</TargetFrameworks>
5+
<NoWarn>$(NoWarm);CA1822;IDE0007;IDE0037;IDE0039;IDE0057;CA1825</NoWarn>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\Src\Fido2\Fido2.csproj" />
10+
<ProjectReference Include="..\..\Src\Fido2.AspNet\Fido2.AspNet.csproj" />
11+
<ProjectReference Include="..\..\Src\Fido2.Models\Fido2.Models.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="coverlet.collector" Version="6.0.2">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
20+
<PackageReference Include="Moq" Version="4.18.4" />
21+
<PackageReference Include="xunit" Version="2.9.2" />
22+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
23+
<PrivateAssets>all</PrivateAssets>
24+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
25+
</PackageReference>
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
30+
</ItemGroup>
31+
32+
<ItemGroup>
33+
<Using Include="Xunit" />
34+
</ItemGroup>
35+
</Project>

fido2-net-lib.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo", "Demo\Demo.csproj",
3838
EndProject
3939
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Demos", "Demos", "{9A6A08F5-14A7-4256-9717-2CF80B5B4AAC}"
4040
EndProject
41+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fido2.AspNet.Tests", "Tests\Fido2.AspNet.Tests\Fido2.AspNet.Tests.csproj", "{B4E8AF2B-E616-4197-8366-CAF1222B53DD}"
42+
EndProject
4143
Global
4244
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4345
Debug|Any CPU = Debug|Any CPU
@@ -88,6 +90,10 @@ Global
8890
{BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A}.Debug|Any CPU.Build.0 = Debug|Any CPU
8991
{BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A}.Release|Any CPU.ActiveCfg = Release|Any CPU
9092
{BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A}.Release|Any CPU.Build.0 = Release|Any CPU
93+
{B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94+
{B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
95+
{B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
96+
{B4E8AF2B-E616-4197-8366-CAF1222B53DD}.Release|Any CPU.Build.0 = Release|Any CPU
9197
EndGlobalSection
9298
GlobalSection(SolutionProperties) = preSolution
9399
HideSolutionNode = FALSE
@@ -107,5 +113,6 @@ Global
107113
{EAFF4332-145B-4719-B261-827CEB447A62} = {9A6A08F5-14A7-4256-9717-2CF80B5B4AAC}
108114
{0EC19932-290A-4D92-9603-B0533DD022D9} = {9A6A08F5-14A7-4256-9717-2CF80B5B4AAC}
109115
{BAD65EF0-0157-4DAA-85CD-6BF53ED2E39A} = {9A6A08F5-14A7-4256-9717-2CF80B5B4AAC}
116+
{B4E8AF2B-E616-4197-8366-CAF1222B53DD} = {B7A8B758-5566-4D58-9157-36D23395C0E3}
110117
EndGlobalSection
111118
EndGlobal

0 commit comments

Comments
 (0)