started adding authentication stack, thing that decides if we should be authenticated is in

This commit is contained in:
tom.pallister
2016-10-12 13:40:46 +01:00
parent 1fddcf0836
commit 58393f07ec
15 changed files with 241 additions and 13 deletions

View File

@ -0,0 +1,96 @@
namespace Ocelot.UnitTests.Authentication
{
using System.Collections.Generic;
using Library.Infrastructure.Authentication;
using Library.Infrastructure.Configuration;
using Library.Infrastructure.DownstreamRouteFinder;
using Library.Infrastructure.Responses;
using Library.Infrastructure.UrlMatcher;
using Moq;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class RequiresAuthenticationTests
{
private readonly RouteRequiresAuthentication _routeRequiresAuthentication;
private string _url;
private readonly Mock<IOcelotConfiguration> _config;
private Response<bool> _result;
private string _httpMethod;
public RequiresAuthenticationTests()
{
_config = new Mock<IOcelotConfiguration>();
_routeRequiresAuthentication = new RouteRequiresAuthentication(_config.Object);
}
[Fact]
public void should_return_true_if_route_requires_authentication()
{
this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk"))
.And(
x =>
x.GivenTheConfigurationForTheRouteIs(new ReRoute("http://www.bbc.co.uk", "/api/poo", "get",
"/api/poo$", true)))
.When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated())
.Then(x => x.ThenTheResultIs(true))
.BDDfy();
}
[Fact]
public void should_return_false_if_route_requires_authentication()
{
this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk"))
.And(
x =>
x.GivenTheConfigurationForTheRouteIs(new ReRoute("http://www.bbc.co.uk", "/api/poo", "get",
"/api/poo$", false)))
.When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated())
.Then(x => x.ThenTheResultIs(false))
.BDDfy();
}
[Fact]
public void should_return_error_if_no_matching_config()
{
this.Given(x => x.GivenIHaveADownstreamUrl("http://www.bbc.co.uk"))
.And(x => x.GivenTheConfigurationForTheRouteIs(new ReRoute(string.Empty, string.Empty, string.Empty, string.Empty,false)))
.When(x => x.WhenICheckToSeeIfTheRouteShouldBeAuthenticated())
.Then(x => x.ThenAnErrorIsReturned())
.BDDfy();
}
private void ThenAnErrorIsReturned()
{
_result.IsError.ShouldBeTrue();
}
public void GivenIHaveADownstreamUrl(string url)
{
_url = url;
}
private void GivenTheConfigurationForTheRouteIs(ReRoute reRoute)
{
_httpMethod = reRoute.UpstreamHttpMethod;
_config
.Setup(x => x.ReRoutes)
.Returns(new List<ReRoute>
{
reRoute
});
}
private void WhenICheckToSeeIfTheRouteShouldBeAuthenticated()
{
_result = _routeRequiresAuthentication.IsAuthenticated(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), _url), _httpMethod);
}
private void ThenTheResultIs(bool expected)
{
_result.Data.ShouldBe(expected);
}
}
}

View File

@ -38,7 +38,7 @@ namespace Ocelot.UnitTests.Configuration
.When(x => x.WhenIInstanciateTheOcelotConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$")
new ReRoute("/products/{productId}","/api/products/{productId}", "Get", "/api/products/.*$", false)
}))
.BDDfy();
}
@ -61,7 +61,7 @@ namespace Ocelot.UnitTests.Configuration
.When(x => x.WhenIInstanciateTheOcelotConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$")
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}", "Get", "/api/products/.*/variants/.*$", false)
}))
.BDDfy();
}
@ -84,7 +84,7 @@ namespace Ocelot.UnitTests.Configuration
.When(x => x.WhenIInstanciateTheOcelotConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$")
new ReRoute("/products/{productId}","/api/products/{productId}/variants/{variantId}/", "Get", "/api/products/.*/variants/.*/$", false)
}))
.BDDfy();
}
@ -107,7 +107,7 @@ namespace Ocelot.UnitTests.Configuration
.When(x => x.WhenIInstanciateTheOcelotConfig())
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
{
new ReRoute("/api/products/","/", "Get", "/$")
new ReRoute("/api/products/","/", "Get", "/$", false)
}))
.BDDfy();
}

View File

@ -38,7 +38,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
{
new ReRoute("someDownstreamPath","someUpstreamPath", "Get", "someUpstreamPath")
new ReRoute("someDownstreamPath","someUpstreamPath", "Get", "someUpstreamPath", false)
}
))
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
@ -57,8 +57,8 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(new OkResponse<List<TemplateVariableNameAndValue>>(new List<TemplateVariableNameAndValue>())))
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
{
new ReRoute("someDownstreamPath", "someUpstreamPath", "Get", string.Empty),
new ReRoute("someDownstreamPathForAPost", "someUpstreamPath", "Post", string.Empty)
new ReRoute("someDownstreamPath", "someUpstreamPath", "Get", string.Empty, false),
new ReRoute("someDownstreamPathForAPost", "someUpstreamPath", "Post", string.Empty, false)
}
))
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
@ -75,7 +75,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath"))
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
{
new ReRoute("somPath", "somePath", "Get", "somePath")
new ReRoute("somPath", "somePath", "Get", "somePath", false)
}
))
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(false))))