-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add testclass for middleware; fix bug where status set to 401 on succ…
…essful auth in middleware
- Loading branch information
Friedemann Braune
committed
May 4, 2024
1 parent
377c015
commit 4ccedef
Showing
4 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...eycloak.AuthServices.Authorization.Tests/Keycloak.AuthServices.Authorization.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Keycloak.AuthServices.Authorization\Keycloak.AuthServices.Authorization.csproj" /> | ||
<ProjectReference Include="..\..\src\Keycloak.AuthServices.Sdk\Keycloak.AuthServices.Sdk.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
</ItemGroup> | ||
|
||
</Project> |
223 changes: 223 additions & 0 deletions
223
...thorization.Tests/UriBasedResourceProtection/UriBasedResourceProtectionMiddlewareTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
namespace Keycloak.AuthServices.Authorization.Tests; | ||
|
||
using FluentAssertions; | ||
using Keycloak.AuthServices.Sdk.AuthZ; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Moq; | ||
|
||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "<Pending>")] | ||
public class UriBasedResourceProtectionMiddlewareTests | ||
{ | ||
[Fact] | ||
public void Constructor_DelegateNull_ThrowArgumentNullException() | ||
{ | ||
// Arrange | ||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
|
||
// Act & Assert | ||
Assert.Throws<ArgumentNullException>(() => _ = new UriBasedResourceProtectionMiddleware(null, clientMock.Object)); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_KeycloakClientNull_ThrowArgumentNullException() | ||
{ | ||
// Arrange | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
// Act & Assert | ||
Assert.Throws<ArgumentNullException>(() => _ = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, null)); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_NoAdditionalAttributesGiven_CallAuthorizationClientWithExpectedParameters() | ||
{ | ||
// Arrange | ||
var resourceName = "/resourceName"; | ||
var scope = "GET"; | ||
|
||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
clientMock.Setup(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(true); | ||
|
||
var httpRequestMock = new Mock<HttpRequest>(); | ||
httpRequestMock.Setup(x => x.Path).Returns(resourceName); | ||
httpRequestMock.Setup(x => x.Method).Returns(scope); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(httpRequestMock.Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
clientMock.Verify(x => x.VerifyAccessToResource(resourceName, scope, CancellationToken.None), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_DisableAttributesGiven_DontCallAuthorizationClientButCallDelegate() | ||
{ | ||
// Arrange | ||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(new Mock<HttpRequest>().Object); | ||
SetAttributesOnContextMock(httpContextMock, | ||
new List<Attribute>() { new DisableUriBasedResourceProtectionAttribute() }, | ||
requestDelegateMock.Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
clientMock.Verify(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never); | ||
requestDelegateMock.Verify(x => x.Invoke(httpContextMock.Object), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_AllowAnonymousAttributesGiven_DontCallAuthorizationClientButCallDelegate() | ||
{ | ||
// Arrange | ||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(new Mock<HttpRequest>().Object); | ||
SetAttributesOnContextMock(httpContextMock, | ||
new List<Attribute>() { new AllowAnonymousAttribute() }, | ||
requestDelegateMock.Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
clientMock.Verify(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never); | ||
requestDelegateMock.Verify(x => x.Invoke(httpContextMock.Object), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_DisableAndExplicitAuthAttributesGiven_DontCallAuthorizationClientButCallDelegate() | ||
{ | ||
// Arrange | ||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(new Mock<HttpRequest>().Object); | ||
SetAttributesOnContextMock(httpContextMock, | ||
new List<Attribute>() { | ||
new DisableUriBasedResourceProtectionAttribute(), | ||
new ExplicitResourceProtectionAttribute(It.IsAny<string>(), It.IsAny<string>()) | ||
}, | ||
requestDelegateMock.Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
clientMock.Verify(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never); | ||
requestDelegateMock.Verify(x => x.Invoke(httpContextMock.Object), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_ExplicitAuthAttributesGiven_CallAuthorizationClientWithAttributeProperties() | ||
{ | ||
// Arrange | ||
var resourceName = "/resourceName"; | ||
var scope = "GET"; | ||
var resourceNameFromAttribute = "/resourceNameFromAttribute"; | ||
var scopeFromAttribute = "GET"; | ||
|
||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
clientMock.Setup(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(true); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
var httpRequestMock = new Mock<HttpRequest>(); | ||
httpRequestMock.Setup(x => x.Path).Returns(resourceName); | ||
httpRequestMock.Setup(x => x.Method).Returns(scope); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(httpRequestMock.Object); | ||
SetAttributesOnContextMock(httpContextMock, | ||
new List<Attribute>() { | ||
new ExplicitResourceProtectionAttribute(resourceNameFromAttribute, scopeFromAttribute) | ||
}, | ||
requestDelegateMock.Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
clientMock.Verify(x => x.VerifyAccessToResource(resourceNameFromAttribute, scopeFromAttribute, CancellationToken.None), Times.Once); | ||
clientMock.Verify(x => x.VerifyAccessToResource(resourceName, scope, CancellationToken.None), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_ClientReturnsTrue_CallDelegate() | ||
{ | ||
// Arrange | ||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
clientMock.Setup(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(true); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(new Mock<HttpRequest>().Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
requestDelegateMock.Verify(x => x.Invoke(httpContextMock.Object), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public async void EvaluateAuthorization_ClientReturnsFalse_DontCallDelegateAndSet401() | ||
{ | ||
// Arrange | ||
|
||
var clientMock = new Mock<IKeycloakProtectionClient>(); | ||
clientMock.Setup(x => x.VerifyAccessToResource(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(false); | ||
var requestDelegateMock = new Mock<RequestDelegate>(); | ||
|
||
var httpResponseMock = new Mock<HttpResponse>(); | ||
httpResponseMock.SetupAllProperties(); | ||
|
||
var httpContextMock = new Mock<HttpContext>(); | ||
httpContextMock.Setup(x => x.Request).Returns(new Mock<HttpRequest>().Object); | ||
httpContextMock.Setup(x => x.Response).Returns(httpResponseMock.Object); | ||
|
||
// Act | ||
var target = new UriBasedResourceProtectionMiddleware(requestDelegateMock.Object, clientMock.Object); | ||
await target.InvokeAsync(httpContextMock.Object); | ||
|
||
// Assert | ||
requestDelegateMock.Verify(x => x.Invoke(httpContextMock.Object), Times.Never); | ||
httpContextMock.Object.Response.StatusCode.Should().Be(StatusCodes.Status401Unauthorized); | ||
} | ||
|
||
private static void SetAttributesOnContextMock(Mock<HttpContext> httpContextMock, IEnumerable<Attribute> attributes, RequestDelegate requestDelegate) | ||
{ | ||
var endpoint = new Endpoint(requestDelegate, new EndpointMetadataCollection(attributes), null); | ||
|
||
var endpointFeatureMock = new Mock<IEndpointFeature>(); | ||
endpointFeatureMock.Setup(x => x.Endpoint).Returns(endpoint); | ||
|
||
var featuresMock = new Mock<IFeatureCollection>(); | ||
featuresMock.Setup(x => x.Get<IEndpointFeature>()).Returns(endpointFeatureMock.Object); | ||
|
||
httpContextMock.Setup(x => x.Features).Returns(featuresMock.Object); | ||
} | ||
} |