1
0

Adding support for .NET 8.0 LTS, renaming private fields to start with underscore

This commit is contained in:
2023-12-29 01:58:40 +01:00
parent 8bd511a936
commit 99d3f7758a
59 changed files with 922 additions and 871 deletions

View File

@@ -14,17 +14,17 @@ namespace UnitTests.AspNetCore.Security.PathProtection
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class ProtectedPathMiddlewareTests
{
private Mock<RequestDelegate> nextMock;
private Mock<HttpContext> httpContextMock;
private Mock<IAuthorizationService> authorizationServiceMock;
private Mock<IAuthenticationService> authenticationServiceMock;
private Mock<RequestDelegate> _nextMock;
private Mock<HttpContext> _httpContextMock;
private Mock<IAuthorizationService> _authorizationServiceMock;
private Mock<IAuthenticationService> _authenticationServiceMock;
private ProtectedPathOptions options;
private ProtectedPathOptions _options;
[TestInitialize]
public void InitializeTest()
{
options = new ProtectedPathOptions
_options = new ProtectedPathOptions
{
Path = "/secure/protected",
PolicyName = "Protection"
@@ -36,21 +36,21 @@ namespace UnitTests.AspNetCore.Security.PathProtection
{
// arrange
var middleware = GetMiddleware();
var context = GetHttpContext(options.Path);
var context = GetHttpContext(_options.Path);
var auth = GetAuthService(true);
// act
await middleware.InvokeAsync(context, auth);
// assert
authorizationServiceMock.Verify(s => s.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), options.PolicyName), Times.Once);
authorizationServiceMock.VerifyNoOtherCalls();
_authorizationServiceMock.Verify(s => s.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), _options.PolicyName), Times.Once);
_authorizationServiceMock.VerifyNoOtherCalls();
authenticationServiceMock.Verify(s => s.ChallengeAsync(It.IsAny<HttpContext>(), It.IsAny<string>(), It.IsAny<AuthenticationProperties>()), Times.Never);
authenticationServiceMock.VerifyNoOtherCalls();
_authenticationServiceMock.Verify(s => s.ChallengeAsync(It.IsAny<HttpContext>(), It.IsAny<string>(), It.IsAny<AuthenticationProperties>()), Times.Never);
_authenticationServiceMock.VerifyNoOtherCalls();
nextMock.Verify(n => n.Invoke(It.IsAny<HttpContext>()), Times.Once);
nextMock.VerifyNoOtherCalls();
_nextMock.Verify(n => n.Invoke(It.IsAny<HttpContext>()), Times.Once);
_nextMock.VerifyNoOtherCalls();
}
[TestMethod]
@@ -65,14 +65,14 @@ namespace UnitTests.AspNetCore.Security.PathProtection
await middleware.InvokeAsync(context, auth);
// assert
authorizationServiceMock.Verify(s => s.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), options.PolicyName), Times.Never);
authorizationServiceMock.VerifyNoOtherCalls();
_authorizationServiceMock.Verify(s => s.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), _options.PolicyName), Times.Never);
_authorizationServiceMock.VerifyNoOtherCalls();
authenticationServiceMock.Verify(s => s.ChallengeAsync(It.IsAny<HttpContext>(), It.IsAny<string>(), It.IsAny<AuthenticationProperties>()), Times.Never);
authenticationServiceMock.VerifyNoOtherCalls();
_authenticationServiceMock.Verify(s => s.ChallengeAsync(It.IsAny<HttpContext>(), It.IsAny<string>(), It.IsAny<AuthenticationProperties>()), Times.Never);
_authenticationServiceMock.VerifyNoOtherCalls();
nextMock.Verify(n => n.Invoke(It.IsAny<HttpContext>()), Times.Once);
nextMock.VerifyNoOtherCalls();
_nextMock.Verify(n => n.Invoke(It.IsAny<HttpContext>()), Times.Once);
_nextMock.VerifyNoOtherCalls();
}
[TestMethod]
@@ -80,27 +80,27 @@ namespace UnitTests.AspNetCore.Security.PathProtection
{
// arrange
var middleware = GetMiddleware();
var context = GetHttpContext(options.Path);
var context = GetHttpContext(_options.Path);
var auth = GetAuthService(false);
// act
await middleware.InvokeAsync(context, auth);
// assert
authorizationServiceMock.Verify(s => s.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), options.PolicyName), Times.Once);
authorizationServiceMock.VerifyNoOtherCalls();
_authorizationServiceMock.Verify(s => s.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), _options.PolicyName), Times.Once);
_authorizationServiceMock.VerifyNoOtherCalls();
authenticationServiceMock.Verify(s => s.ChallengeAsync(It.IsAny<HttpContext>(), It.IsAny<string>(), It.IsAny<AuthenticationProperties>()), Times.Once);
authenticationServiceMock.VerifyNoOtherCalls();
_authenticationServiceMock.Verify(s => s.ChallengeAsync(It.IsAny<HttpContext>(), It.IsAny<string>(), It.IsAny<AuthenticationProperties>()), Times.Once);
_authenticationServiceMock.VerifyNoOtherCalls();
nextMock.Verify(n => n.Invoke(It.IsAny<HttpContext>()), Times.Never);
nextMock.VerifyNoOtherCalls();
_nextMock.Verify(n => n.Invoke(It.IsAny<HttpContext>()), Times.Never);
_nextMock.VerifyNoOtherCalls();
}
private ProtectedPathMiddleware GetMiddleware()
{
nextMock = new Mock<RequestDelegate>();
return new ProtectedPathMiddleware(nextMock.Object, options);
_nextMock = new Mock<RequestDelegate>();
return new ProtectedPathMiddleware(_nextMock.Object, _options);
}
private HttpContext GetHttpContext(string requestPath)
@@ -110,32 +110,32 @@ namespace UnitTests.AspNetCore.Security.PathProtection
.Setup(r => r.Path)
.Returns(new PathString(requestPath));
authenticationServiceMock = new Mock<IAuthenticationService>();
_authenticationServiceMock = new Mock<IAuthenticationService>();
var requestServicesMock = new Mock<IServiceProvider>();
requestServicesMock
.Setup(s => s.GetService(typeof(IAuthenticationService)))
.Returns(authenticationServiceMock.Object);
.Returns(_authenticationServiceMock.Object);
httpContextMock = new Mock<HttpContext>();
httpContextMock
_httpContextMock = new Mock<HttpContext>();
_httpContextMock
.Setup(c => c.Request)
.Returns(requestMock.Object);
httpContextMock
_httpContextMock
.Setup(c => c.RequestServices)
.Returns(requestServicesMock.Object);
return httpContextMock.Object;
return _httpContextMock.Object;
}
private IAuthorizationService GetAuthService(bool success)
{
authorizationServiceMock = new Mock<IAuthorizationService>();
authorizationServiceMock
_authorizationServiceMock = new Mock<IAuthorizationService>();
_authorizationServiceMock
.Setup(service => service.AuthorizeAsync(It.IsAny<ClaimsPrincipal>(), It.IsAny<object>(), It.IsAny<string>()))
.ReturnsAsync(() => success ? AuthorizationResult.Success() : AuthorizationResult.Failed());
return authorizationServiceMock.Object;
return _authorizationServiceMock.Object;
}
}
}