mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 05:48:16 +08:00
all tests passing but i think im going to take registering auth our of the config
This commit is contained in:
@ -1,100 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Authentication.Handler;
|
||||
using Ocelot.Authentication.Handler.Creator;
|
||||
using Ocelot.Authentication.Handler.Factory;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using AuthenticationOptions = Ocelot.Configuration.AuthenticationOptions;
|
||||
|
||||
namespace Ocelot.UnitTests.Authentication
|
||||
{
|
||||
public class AuthenticationHandlerFactoryTests
|
||||
{
|
||||
private readonly IAuthenticationHandlerFactory _authenticationHandlerFactory;
|
||||
private readonly Mock<IApplicationBuilder> _app;
|
||||
private readonly Mock<IAuthenticationHandlerCreator> _creator;
|
||||
private AuthenticationOptions _authenticationOptions;
|
||||
private Response<AuthenticationHandler> _result;
|
||||
|
||||
public AuthenticationHandlerFactoryTests()
|
||||
{
|
||||
_app = new Mock<IApplicationBuilder>();
|
||||
_creator = new Mock<IAuthenticationHandlerCreator>();
|
||||
_authenticationHandlerFactory = new AuthenticationHandlerFactory(_creator.Object);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("IdentityServer")]
|
||||
[InlineData("Jwt")]
|
||||
public void should_return_access_token_handler(string provider)
|
||||
{
|
||||
var authenticationOptions = new AuthenticationOptionsBuilder()
|
||||
.WithProvider(provider)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheAuthenticationOptionsAre(authenticationOptions))
|
||||
.And(x => x.GivenTheCreatorReturns())
|
||||
.When(x => x.WhenIGetFromTheFactory())
|
||||
.Then(x => x.ThenTheHandlerIsReturned(provider))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_cannot_create_handler()
|
||||
{
|
||||
var authenticationOptions = new AuthenticationOptionsBuilder()
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheAuthenticationOptionsAre(authenticationOptions))
|
||||
.And(x => x.GivenTheCreatorReturnsAnError())
|
||||
.When(x => x.WhenIGetFromTheFactory())
|
||||
.Then(x => x.ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheAuthenticationOptionsAre(AuthenticationOptions authenticationOptions)
|
||||
{
|
||||
_authenticationOptions = authenticationOptions;
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturnsAnError()
|
||||
{
|
||||
_creator
|
||||
.Setup(x => x.Create(It.IsAny<IApplicationBuilder>(), It.IsAny<AuthenticationOptions>()))
|
||||
.Returns(new ErrorResponse<RequestDelegate>(new List<Error>
|
||||
{
|
||||
new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for xxx")
|
||||
}));
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturns()
|
||||
{
|
||||
_creator
|
||||
.Setup(x => x.Create(It.IsAny<IApplicationBuilder>(), It.IsAny<AuthenticationOptions>()))
|
||||
.Returns(new OkResponse<RequestDelegate>(x => Task.CompletedTask));
|
||||
}
|
||||
|
||||
private void WhenIGetFromTheFactory()
|
||||
{
|
||||
_result = _authenticationHandlerFactory.Get(_app.Object, _authenticationOptions);
|
||||
}
|
||||
|
||||
private void ThenTheHandlerIsReturned(string expected)
|
||||
{
|
||||
_result.Data.Provider.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenAnErrorResponseIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
@ -2,11 +2,9 @@
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Authentication.Handler.Factory;
|
||||
using Ocelot.Authentication.Middleware;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
@ -19,13 +17,10 @@
|
||||
|
||||
public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IAuthenticationHandlerFactory> _authFactory;
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthenticationMiddlewareTests()
|
||||
{
|
||||
_authFactory = new Mock<IAuthenticationHandlerFactory>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
@ -45,7 +40,6 @@
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_authFactory.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user