From d233bf6b0a63255a9ee96e2ba9f860bdd8b34292 Mon Sep 17 00:00:00 2001 From: Andreas Mueller Date: Sun, 7 Aug 2022 20:57:11 +0200 Subject: [PATCH] Fixing UnitTests --- .../Attributes/BasicAuthenticationAttribute.cs | 2 +- .../BasicAuthentication/BasicAuthenticationHandler.cs | 2 +- .../BasicAuthentication/BasicAuthenticationMiddleware.cs | 2 +- CHANGELOG.md | 8 +++++++- .../Attributes/BasicAuthenticationAttributeTests.cs | 6 +++++- .../BasicAuthenticationMiddlewareTests.cs | 8 ++++++-- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs b/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs index f13044e..3a357d5 100644 --- a/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs +++ b/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs @@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.Authorization if (validator == null) return null; - var result = await validator.ValidateAsync(username, password, context.HttpContext.GetRemoteIpAddress()); + var result = await validator.ValidateAsync(username, password, context.HttpContext.GetRemoteIpAddress(), context.HttpContext.RequestAborted); if (result == null) return null; diff --git a/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationHandler.cs b/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationHandler.cs index ac0fd14..6d0f33b 100644 --- a/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationHandler.cs +++ b/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationHandler.cs @@ -58,7 +58,7 @@ namespace AMWD.Common.AspNetCore.BasicAuthentication string password = plain[(username.Length + 1)..]; var ipAddress = Context.GetRemoteIpAddress(); - principal = await validator.ValidateAsync(username, password, ipAddress); + principal = await validator.ValidateAsync(username, password, ipAddress, Context.RequestAborted); } catch (Exception ex) { diff --git a/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationMiddleware.cs b/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationMiddleware.cs index 0857f83..4e14801 100644 --- a/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationMiddleware.cs +++ b/AMWD.Common.AspNetCore/BasicAuthentication/BasicAuthenticationMiddleware.cs @@ -51,7 +51,7 @@ namespace AMWD.Common.AspNetCore.BasicAuthentication string username = plain.Split(':').First(); string password = plain[(username.Length + 1)..]; - var principal = await validator.ValidateAsync(username, password, httpContext.GetRemoteIpAddress()); + var principal = await validator.ValidateAsync(username, password, httpContext.GetRemoteIpAddress(), httpContext.RequestAborted); if (principal == null) { SetAuthenticateRequest(httpContext, validator.Realm); diff --git a/CHANGELOG.md b/CHANGELOG.md index 792e1c6..96dbdb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://git.am-wd.de/AM.WD/common/compare/v1.8.0...master) - 0000-00-00 +## [Unreleased](https://git.am-wd.de/AM.WD/common/compare/v1.8.1...master) - 0000-00-00 _nothing changed yet_ +## [v1.8.1](https://git.am-wd.de/AM.WD/common/compare/v1.8.0...v1.8.1) - 2022-08-07 +### Fixed +- UnitTests +- BasicAuthentication modules now all use the cancellation token + + ## [v1.8.0](https://git.am-wd.de/AM.WD/common/compare/v1.7.0...v1.8.0) - 2022-08-07 ### Added - Converters for `Newtonsoft.Json` diff --git a/UnitTests/AspNetCore/Attributes/BasicAuthenticationAttributeTests.cs b/UnitTests/AspNetCore/Attributes/BasicAuthenticationAttributeTests.cs index 51c49ac..31b490c 100644 --- a/UnitTests/AspNetCore/Attributes/BasicAuthenticationAttributeTests.cs +++ b/UnitTests/AspNetCore/Attributes/BasicAuthenticationAttributeTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Net; using System.Security.Claims; using System.Text; +using System.Threading; using System.Threading.Tasks; using AMWD.Common.AspNetCore.BasicAuthentication; using Microsoft.AspNetCore.Authorization; @@ -299,7 +300,7 @@ namespace UnitTests.AspNetCore.Attributes .Setup(v => v.Realm) .Returns(validatorRealm); validatorMock - .Setup(v => v.ValidateAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Setup(v => v.ValidateAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(validatorResult); requestServicesMock @@ -325,6 +326,9 @@ namespace UnitTests.AspNetCore.Attributes contextMock .Setup(c => c.Connection) .Returns(connectionInfoMock.Object); + contextMock + .Setup(c => c.RequestAborted) + .Returns(CancellationToken.None); var routeDataMock = new Mock(); diff --git a/UnitTests/AspNetCore/BasicAuthentication/BasicAuthenticationMiddlewareTests.cs b/UnitTests/AspNetCore/BasicAuthentication/BasicAuthenticationMiddlewareTests.cs index 4827566..3166753 100644 --- a/UnitTests/AspNetCore/BasicAuthentication/BasicAuthenticationMiddlewareTests.cs +++ b/UnitTests/AspNetCore/BasicAuthentication/BasicAuthenticationMiddlewareTests.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Security.Claims; using System.Text; +using System.Threading; using System.Threading.Tasks; using AMWD.Common.AspNetCore.BasicAuthentication; using Microsoft.AspNetCore.Http; @@ -142,8 +143,8 @@ namespace UnitTests.AspNetCore.BasicAuthentication .Setup(v => v.Realm) .Returns(validatorRealm); validatorMock - .Setup(v => v.ValidateAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((username, password, ipAddress) => validatorCallback.Add((username, password, ipAddress))) + .Setup(v => v.ValidateAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((username, password, ipAddress, _) => validatorCallback.Add((username, password, ipAddress))) .ReturnsAsync(validatorResponse); return new BasicAuthenticationMiddleware(nextMock.Object, validatorMock.Object); @@ -207,6 +208,9 @@ namespace UnitTests.AspNetCore.BasicAuthentication contextMock .Setup(c => c.RequestServices) .Returns(requestServicesMock.Object); + contextMock + .Setup(c => c.RequestAborted) + .Returns(CancellationToken.None); return contextMock.Object; }