mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-20 03:38:15 +08:00
rename authorisation to authorization
This commit is contained in:
@ -3,8 +3,8 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Authorisation.Middleware;
|
||||
using Ocelot.Authorization;
|
||||
using Ocelot.Authorization.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
@ -18,35 +18,35 @@ namespace Ocelot.UnitTests.Authorization
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class AuthorisationMiddlewareTests
|
||||
public class AuthorizationMiddlewareTests
|
||||
{
|
||||
private readonly Mock<IClaimsAuthoriser> _authService;
|
||||
private readonly Mock<IScopesAuthoriser> _authScopesService;
|
||||
private readonly Mock<IClaimsAuthorizer> _authService;
|
||||
private readonly Mock<IScopesAuthorizer> _authScopesService;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private readonly AuthorisationMiddleware _middleware;
|
||||
private readonly AuthorizationMiddleware _middleware;
|
||||
private RequestDelegate _next;
|
||||
private HttpContext _httpContext;
|
||||
|
||||
public AuthorisationMiddlewareTests()
|
||||
public AuthorizationMiddlewareTests()
|
||||
{
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_authService = new Mock<IClaimsAuthoriser>();
|
||||
_authScopesService = new Mock<IScopesAuthoriser>();
|
||||
_authService = new Mock<IClaimsAuthorizer>();
|
||||
_authScopesService = new Mock<IScopesAuthorizer>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<AuthorisationMiddleware>()).Returns(_logger.Object);
|
||||
_loggerFactory.Setup(x => x.CreateLogger<AuthorizationMiddleware>()).Returns(_logger.Object);
|
||||
_next = context => Task.CompletedTask;
|
||||
_middleware = new AuthorisationMiddleware(_next, _authService.Object, _authScopesService.Object, _loggerFactory.Object);
|
||||
_middleware = new AuthorizationMiddleware(_next, _authService.Object, _authScopesService.Object, _loggerFactory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authorisation_service()
|
||||
public void should_call_authorization_service()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(new List<PlaceholderNameAndValue>(),
|
||||
new DownstreamRouteBuilder()
|
||||
.WithUpstreamPathTemplate(new UpstreamPathTemplateBuilder().Build())
|
||||
.WithIsAuthorised(true)
|
||||
.WithIsAuthorized(true)
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build()))
|
||||
.And(x => x.GivenTheAuthServiceReturns(new OkResponse<bool>(true)))
|
||||
@ -69,7 +69,7 @@ namespace Ocelot.UnitTests.Authorization
|
||||
private void GivenTheAuthServiceReturns(Response<bool> expected)
|
||||
{
|
||||
_authService
|
||||
.Setup(x => x.Authorise(
|
||||
.Setup(x => x.Authorize(
|
||||
It.IsAny<ClaimsPrincipal>(),
|
||||
It.IsAny<Dictionary<string, string>>(),
|
||||
It.IsAny<List<PlaceholderNameAndValue>>()))
|
||||
@ -79,7 +79,7 @@ namespace Ocelot.UnitTests.Authorization
|
||||
private void ThenTheAuthServiceIsCalledCorrectly()
|
||||
{
|
||||
_authService
|
||||
.Verify(x => x.Authorise(
|
||||
.Verify(x => x.Authorize(
|
||||
It.IsAny<ClaimsPrincipal>(),
|
||||
It.IsAny<Dictionary<string, string>>(),
|
||||
It.IsAny<List<PlaceholderNameAndValue>>())
|
@ -1,4 +1,4 @@
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Authorization;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
@ -11,21 +11,21 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
|
||||
public class ClaimsAuthoriserTests
|
||||
public class ClaimsAuthorizerTests
|
||||
{
|
||||
private readonly ClaimsAuthoriser _claimsAuthoriser;
|
||||
private readonly ClaimsAuthorizer _claimsAuthorizer;
|
||||
private ClaimsPrincipal _claimsPrincipal;
|
||||
private Dictionary<string, string> _requirement;
|
||||
private List<PlaceholderNameAndValue> _urlPathPlaceholderNameAndValues;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ClaimsAuthoriserTests()
|
||||
public ClaimsAuthorizerTests()
|
||||
{
|
||||
_claimsAuthoriser = new ClaimsAuthoriser(new ClaimsParser());
|
||||
_claimsAuthorizer = new ClaimsAuthorizer(new ClaimsParser());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_authorise_user()
|
||||
public void should_authorize_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
@ -35,8 +35,8 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsAuthorised())
|
||||
.When(x => x.WhenICallTheAuthorizer())
|
||||
.Then(x => x.ThenTheUserIsAuthorized())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -55,8 +55,8 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
new PlaceholderNameAndValue("{userId}", "14")
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsAuthorised())
|
||||
.When(x => x.WhenICallTheAuthorizer())
|
||||
.Then(x => x.ThenTheUserIsAuthorized())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -75,13 +75,13 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
new PlaceholderNameAndValue("{userId}", "14")
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsntAuthorised())
|
||||
.When(x => x.WhenICallTheAuthorizer())
|
||||
.Then(x => x.ThenTheUserIsntAuthorized())
|
||||
.BDDfy();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_authorise_user_multiple_claims_of_same_type()
|
||||
public void should_authorize_user_multiple_claims_of_same_type()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
@ -92,21 +92,21 @@ namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsAuthorised())
|
||||
.When(x => x.WhenICallTheAuthorizer())
|
||||
.Then(x => x.ThenTheUserIsAuthorized())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_authorise_user()
|
||||
public void should_not_authorize_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>()))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{ "UserType", "registered" }
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsntAuthorised())
|
||||
.When(x => x.WhenICallTheAuthorizer())
|
||||
.Then(x => x.ThenTheUserIsntAuthorized())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -125,19 +125,19 @@ namespace Ocelot.UnitTests.Authorization
|
||||
_urlPathPlaceholderNameAndValues = urlPathPlaceholderNameAndValues;
|
||||
}
|
||||
|
||||
private void WhenICallTheAuthoriser()
|
||||
private void WhenICallTheAuthorizer()
|
||||
{
|
||||
_result = _claimsAuthoriser.Authorise(_claimsPrincipal, _requirement, _urlPathPlaceholderNameAndValues);
|
||||
_result = _claimsAuthorizer.Authorize(_claimsPrincipal, _requirement, _urlPathPlaceholderNameAndValues);
|
||||
}
|
||||
|
||||
private void ThenTheUserIsAuthorised()
|
||||
private void ThenTheUserIsAuthorized()
|
||||
{
|
||||
_result.Data.ShouldBe(true);
|
||||
}
|
||||
|
||||
private void ThenTheUserIsntAuthorised()
|
||||
private void ThenTheUserIsntAuthorized()
|
||||
{
|
||||
_result.Data.ShouldBe(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -46,7 +46,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
|
||||
var expected = new RouteOptionsBuilder()
|
||||
.WithIsAuthenticated(true)
|
||||
.WithIsAuthorised(true)
|
||||
.WithIsAuthorized(true)
|
||||
.WithIsCached(true)
|
||||
.WithRateLimiting(true)
|
||||
.WithUseServiceDiscovery(true)
|
||||
@ -71,7 +71,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
private void ThenTheFollowingIsReturned(RouteOptions expected)
|
||||
{
|
||||
_result.IsAuthenticated.ShouldBe(expected.IsAuthenticated);
|
||||
_result.IsAuthorised.ShouldBe(expected.IsAuthorised);
|
||||
_result.IsAuthorized.ShouldBe(expected.IsAuthorized);
|
||||
_result.IsCached.ShouldBe(expected.IsCached);
|
||||
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
|
||||
_result.UseServiceDiscovery.ShouldBe(expected.UseServiceDiscovery);
|
||||
|
@ -218,7 +218,7 @@
|
||||
{
|
||||
_result[routeIndex].DownstreamRoute[0].DownstreamHttpVersion.ShouldBe(_expectedVersion);
|
||||
_result[routeIndex].DownstreamRoute[0].IsAuthenticated.ShouldBe(_rro.IsAuthenticated);
|
||||
_result[routeIndex].DownstreamRoute[0].IsAuthorised.ShouldBe(_rro.IsAuthorised);
|
||||
_result[routeIndex].DownstreamRoute[0].IsAuthorized.ShouldBe(_rro.IsAuthorized);
|
||||
_result[routeIndex].DownstreamRoute[0].IsCached.ShouldBe(_rro.IsCached);
|
||||
_result[routeIndex].DownstreamRoute[0].EnableEndpointEndpointRateLimiting.ShouldBe(_rro.EnableRateLimiting);
|
||||
_result[routeIndex].DownstreamRoute[0].RequestIdKey.ShouldBe(_requestId);
|
||||
|
@ -2,7 +2,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Authorisation.Middleware;
|
||||
using Ocelot.Authorization.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
@ -38,7 +38,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
_postReplacer = new Mock<IHttpResponseHeaderReplacer>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory.Setup(x => x.CreateLogger<AuthorisationMiddleware>()).Returns(_logger.Object);
|
||||
_loggerFactory.Setup(x => x.CreateLogger<AuthorizationMiddleware>()).Returns(_logger.Object);
|
||||
_next = context => Task.CompletedTask;
|
||||
_addHeadersToResponse = new Mock<IAddHeadersToResponse>();
|
||||
_addHeadersToRequest = new Mock<IAddHeadersToRequest>();
|
||||
|
@ -1,5 +1,5 @@
|
||||
using Moq;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Authorization;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
@ -11,18 +11,18 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
public class ScopesAuthoriserTests
|
||||
public class ScopesAuthorizerTests
|
||||
{
|
||||
private ScopesAuthoriser _authoriser;
|
||||
private ScopesAuthorizer _authorizer;
|
||||
public Mock<IClaimsParser> _parser;
|
||||
private ClaimsPrincipal _principal;
|
||||
private List<string> _allowedScopes;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ScopesAuthoriserTests()
|
||||
public ScopesAuthorizerTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_authoriser = new ScopesAuthoriser(_parser.Object);
|
||||
_authorizer = new ScopesAuthorizer(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -30,7 +30,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing(new List<string>()))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
@ -40,7 +40,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing((List<string>)null))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
@ -52,7 +52,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheParserReturns(new ErrorResponse<List<string>>(fakeError)))
|
||||
.And(_ => GivenTheFollowing(new List<string>() { "doesntmatter" }))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
@ -66,7 +66,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(allowedScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
@ -82,7 +82,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(userScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
@ -102,9 +102,9 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
_allowedScopes = allowedScopes;
|
||||
}
|
||||
|
||||
private void WhenIAuthorise()
|
||||
private void WhenIAuthorize()
|
||||
{
|
||||
_result = _authoriser.Authorise(_principal, _allowedScopes);
|
||||
_result = _authorizer.Authorize(_principal, _allowedScopes);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(Response<bool> expected)
|
@ -29,10 +29,10 @@ namespace Ocelot.UnitTests.Responder
|
||||
|
||||
[Theory]
|
||||
[InlineData(OcelotErrorCode.CannotFindClaimError)]
|
||||
[InlineData(OcelotErrorCode.ClaimValueNotAuthorisedError)]
|
||||
[InlineData(OcelotErrorCode.ScopeNotAuthorisedError)]
|
||||
[InlineData(OcelotErrorCode.ClaimValueNotAuthorizedError)]
|
||||
[InlineData(OcelotErrorCode.ScopeNotAuthorizedError)]
|
||||
[InlineData(OcelotErrorCode.UnauthorizedError)]
|
||||
[InlineData(OcelotErrorCode.UserDoesNotHaveClaimError)]
|
||||
[InlineData(OcelotErrorCode.UserDoesNotHaveClaimError)]
|
||||
public void should_return_forbidden(OcelotErrorCode errorCode)
|
||||
{
|
||||
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.Forbidden);
|
||||
@ -52,8 +52,8 @@ namespace Ocelot.UnitTests.Responder
|
||||
public void should_return_internal_server_error(OcelotErrorCode errorCode)
|
||||
{
|
||||
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(OcelotErrorCode.ConnectionToDownstreamServiceError)]
|
||||
public void should_return_bad_gateway_error(OcelotErrorCode errorCode)
|
||||
@ -104,7 +104,7 @@ namespace Ocelot.UnitTests.Responder
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AuthorisationErrorsHaveSecondHighestPriority()
|
||||
public void AuthorizationErrorsHaveSecondHighestPriority()
|
||||
{
|
||||
var errors = new List<OcelotErrorCode>
|
||||
{
|
||||
@ -177,4 +177,4 @@ namespace Ocelot.UnitTests.Responder
|
||||
_result.ShouldBe((int)expectedCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user