|
| 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 | +} |
0 commit comments