diff --git a/Directory.Packages.props b/Directory.Packages.props index d911c69d3..4b469a6ee 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -82,6 +82,7 @@ that supports the target frameworks our products target (8, 9, 10) --> + diff --git a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/AspNetCore.Authentication.JwtBearer.Tests.csproj b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/AspNetCore.Authentication.JwtBearer.Tests.csproj index f5b5e3efd..c9d971d50 100644 --- a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/AspNetCore.Authentication.JwtBearer.Tests.csproj +++ b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/AspNetCore.Authentication.JwtBearer.Tests.csproj @@ -15,6 +15,7 @@ + diff --git a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/DPoPProofValidatorTestBase.cs b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/DPoPProofValidatorTestBase.cs index 0fdb255b9..34079f762 100644 --- a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/DPoPProofValidatorTestBase.cs +++ b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/DPoPProofValidatorTestBase.cs @@ -7,9 +7,9 @@ using System.Security.Cryptography; using System.Text; using System.Text.Json; using Duende.AspNetCore.Authentication.JwtBearer.DPoP.TestFramework; -using Duende.AspNetCore.TestFramework; using Duende.IdentityModel; using Microsoft.AspNetCore.DataProtection; +using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Time.Testing; using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Tokens; @@ -20,25 +20,25 @@ public abstract class DPoPProofValidatorTestBase { public DPoPProofValidatorTestBase() { - Logger = MockLogger.Create(); + ExpirationLogger = new FakeLogger(); Clock = new FakeTimeProvider(); DataProtectionProvider = new EphemeralDataProtectionProvider(); OptionsMonitor = new TestOptionsMonitor(Options); ExpirationValidator = new DPoPExpirationValidator( Clock, - Logger.For()); + ExpirationLogger); NonceValidator = new DefaultDPoPNonceValidator( OptionsMonitor, DataProtectionProvider, Clock, - Logger.For(), + new FakeLogger(), ExpirationValidator); ProofValidator = new( OptionsMonitor, NonceValidator, ReplayCache, Clock, - Logger.For(), + new FakeLogger(), ExpirationValidator); var jtiBytes = Encoding.UTF8.GetBytes(TokenId); TokenIdHash = Base64Url.EncodeToString(SHA256.HashData(jtiBytes)); @@ -53,7 +53,7 @@ public abstract class DPoPProofValidatorTestBase }; } - protected MockLogger Logger { get; } + internal FakeLogger ExpirationLogger { get; } protected FakeTimeProvider Clock { get; } protected IDataProtectionProvider DataProtectionProvider { get; } protected IDataProtector DataProtector => NonceValidator.DataProtector; diff --git a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/FreshnessTests.cs b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/FreshnessTests.cs index c1b5a00c5..aa9895420 100644 --- a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/FreshnessTests.cs +++ b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/DPoP/FreshnessTests.cs @@ -110,7 +110,7 @@ public class FreshnessTests : DPoPProofValidatorTestBase actual.ShouldBe(expected); if (expected) { - Logger.LogMessages.ShouldContain(msg => msg.StartsWith("Expiration check failed. Expiration has already happened.")); + ExpirationLogger.Collector.GetSnapshot().ShouldContain(record => record.Message.StartsWith("Expiration check failed. Expiration has already happened.")); } } @@ -136,7 +136,7 @@ public class FreshnessTests : DPoPProofValidatorTestBase actual.ShouldBe(expected); if (expected) { - Logger.LogMessages.ShouldContain(msg => msg.StartsWith("Expiration check failed. Creation time was too far in the future.")); + ExpirationLogger.Collector.GetSnapshot().ShouldContain(record => record.Message.StartsWith("Expiration check failed. Creation time was too far in the future.")); } } [Theory] diff --git a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/TestFramework/MockLogger.cs b/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/TestFramework/MockLogger.cs deleted file mode 100644 index 613d0731b..000000000 --- a/aspnetcore-authentication-jwtbearer/test/AspNetCore.Authentication.JwtBearer.Tests/TestFramework/MockLogger.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) Duende Software. All rights reserved. -// See LICENSE in the project root for license information. - -using Microsoft.Extensions.Logging; - -namespace Duende.AspNetCore.TestFramework; - -public class MockLogger : ILogger -{ - public static MockLogger Create() => new MockLogger(new LoggerExternalScopeProvider()); - public MockLogger(LoggerExternalScopeProvider scopeProvider) => _scopeProvider = scopeProvider; - - public readonly List LogMessages = new(); - - - private readonly LoggerExternalScopeProvider _scopeProvider; - - - public IDisposable BeginScope(TState state) where TState : notnull => _scopeProvider.Push(state); - - public bool IsEnabled(LogLevel logLevel) => true; - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) => LogMessages.Add(formatter(state, exception)); - - /// - /// Creates a strongly-typed ILogger<T> wrapper that shares the same log messages collection. - /// - public ILogger For() => new MockLogger(this); -} - -public class MockLogger : ILogger -{ - private readonly MockLogger _inner; - - internal MockLogger(MockLogger inner) => _inner = inner; - - public IDisposable BeginScope(TState state) where TState : notnull => _inner.BeginScope(state); - - public bool IsEnabled(LogLevel logLevel) => _inner.IsEnabled(logLevel); - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) - => _inner.Log(logLevel, eventId, state, exception, formatter); -} - -public class MockLoggerProvider(MockLogger logger) : ILoggerProvider -{ - public void Dispose() - { - } - - public ILogger CreateLogger(string categoryName) => logger; -}