Added a get authentication test, removed the infrastructure name space as it seemed pointless, started thinking about how to pass claims on with the request

This commit is contained in:
tom.pallister
2016-10-17 18:00:36 +01:00
parent ce84ad4fc2
commit 3d60602c7e
97 changed files with 614 additions and 508 deletions

View File

@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Ocelot.Library.Infrastructure.Configuration.Yaml;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
@ -21,6 +20,9 @@ using YamlDotNet.Serialization;
namespace Ocelot.AcceptanceTests
{
using System.Security.Claims;
using Library.Configuration.Yaml;
public class AuthenticationTests : IDisposable
{
private TestServer _ocelotServer;
@ -28,7 +30,7 @@ namespace Ocelot.AcceptanceTests
private HttpResponseMessage _response;
private readonly string _configurationPath;
private StringContent _postContent;
private IWebHost _ocelotBbuilder;
private IWebHost _servicebuilder;
// Sadly we need to change this when we update the netcoreapp version to make the test update the config correctly
private double _netCoreAppVersion = 1.4;
@ -106,6 +108,42 @@ namespace Ocelot.AcceptanceTests
.BDDfy();
}
[Fact]
public void should_return_response_200_using_identity_server()
{
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
.And(x => x.GivenIHaveAToken("http://localhost:51888"))
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
{
ReRoutes = new List<YamlReRoute>
{
new YamlReRoute
{
DownstreamTemplate = "http://localhost:51876/",
UpstreamTemplate = "/",
UpstreamHttpMethod = "Get",
AuthenticationOptions = new YamlAuthenticationOptions
{
AdditionalScopes = new List<string>(),
Provider = "IdentityServer",
ProviderRootUrl = "http://localhost:51888",
RequireHttps = false,
ScopeName = "api",
ScopeSecret = "secret"
}
}
}
}))
.And(x => x.GivenTheApiGatewayIsRunning())
.And(x => x.GivenIHaveAddedATokenToMyRequest())
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => x.ThenTheResponseBodyShouldBe("Hello from Laura"))
.BDDfy();
}
[Fact]
public void should_return_201_using_identity_server_access_token()
{
@ -176,6 +214,21 @@ namespace Ocelot.AcceptanceTests
.BDDfy();
}
private void WhenIGetUrlOnTheApiGateway(string url)
{
_response = _ocelotClient.GetAsync(url).Result;
}
private void WhenIPostUrlOnTheApiGateway(string url)
{
_response = _ocelotClient.PostAsync(url, _postContent).Result;
}
private void ThenTheResponseBodyShouldBe(string expectedBody)
{
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
}
private void GivenThePostHasContent(string postcontent)
{
_postContent = new StringContent(postcontent);
@ -209,7 +262,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
{
_ocelotBbuilder = new WebHostBuilder()
_servicebuilder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
@ -225,7 +278,7 @@ namespace Ocelot.AcceptanceTests
})
.Build();
_ocelotBbuilder.Start();
_servicebuilder.Start();
}
private void GivenThereIsAnIdentityServerOn(string url, string scopeName, AccessTokenType tokenType)
@ -240,41 +293,52 @@ namespace Ocelot.AcceptanceTests
{
services.AddLogging();
services.AddDeveloperIdentityServer()
.AddInMemoryScopes(new List<Scope> { new Scope
{
Name = scopeName,
Description = "My API",
Enabled = true,
AllowUnrestrictedIntrospection = true,
ScopeSecrets = new List<Secret>()
.AddInMemoryScopes(new List<Scope>
{
new Secret
new Scope
{
Value = "secret".Sha256()
Name = scopeName,
Description = "My API",
Enabled = true,
AllowUnrestrictedIntrospection = true,
ScopeSecrets = new List<Secret>()
{
new Secret
{
Value = "secret".Sha256()
}
}
},
StandardScopes.OpenId,
StandardScopes.OfflineAccess
})
.AddInMemoryClients(new List<Client>
{
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
AllowedScopes = new List<string> { scopeName, "openid", "offline_access" },
AccessTokenType = tokenType,
Enabled = true,
RequireClientSecret = false
}
}
}})
.AddInMemoryClients(new List<Client> {
new Client
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets = new List<Secret> { new Secret("secret".Sha256()) },
AllowedScopes = new List<string> { scopeName },
AccessTokenType = tokenType,
Enabled = true,
RequireClientSecret = false
} })
.AddInMemoryUsers(new List<InMemoryUser> { new InMemoryUser
{
Username = "test",
Password = "test",
Enabled = true,
Subject = "asdads"
}});
})
.AddInMemoryUsers(new List<InMemoryUser>
{
new InMemoryUser
{
Username = "test",
Password = "test",
Enabled = true,
Subject = "asdads"
}
});
})
.Configure(app =>
{
{
app.UseIdentityServer();
})
.Build();
@ -322,11 +386,6 @@ namespace Ocelot.AcceptanceTests
_ocelotClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
}
private void WhenIPostUrlOnTheApiGateway(string url)
{
_response = _ocelotClient.PostAsync(url, _postContent).Result;
}
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
{
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
@ -334,7 +393,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose()
{
_ocelotBbuilder?.Dispose();
_servicebuilder?.Dispose();
_ocelotClient?.Dispose();
_ocelotServer?.Dispose();
_identityServerBuilder?.Dispose();

View File

@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Ocelot.Library.Infrastructure.Configuration.Yaml;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
@ -15,6 +14,8 @@ using YamlDotNet.Serialization;
namespace Ocelot.AcceptanceTests
{
using Library.Configuration.Yaml;
public class OcelotTests : IDisposable
{
private TestServer _server;

View File

@ -3,21 +3,23 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Moq;
using Ocelot.Library.Infrastructure.Authentication;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Authentication
{
using Library.Authentication;
using Library.Configuration;
using Library.Errors;
using Library.Responses;
public class AuthenticationHandlerFactoryTests
{
private readonly IAuthenticationHandlerFactory _authenticationHandlerFactory;
private readonly Mock<IApplicationBuilder> _app;
private readonly Mock<IAuthenticationHandlerCreator> _creator;
private Library.Infrastructure.Configuration.AuthenticationOptions _authenticationOptions;
private AuthenticationOptions _authenticationOptions;
private Response<AuthenticationHandler> _result;
public AuthenticationHandlerFactoryTests()
@ -30,7 +32,7 @@ namespace Ocelot.UnitTests.Authentication
[Fact]
public void should_return_identity_server_access_token_handler()
{
this.Given(x => x.GivenTheAuthenticationOptionsAre(new Library.Infrastructure.Configuration.AuthenticationOptions("IdentityServer", "","",false, new List<string>(), "")))
this.Given(x => x.GivenTheAuthenticationOptionsAre(new AuthenticationOptions("IdentityServer", "","",false, new List<string>(), "")))
.And(x => x.GivenTheCreatorReturns())
.When(x => x.WhenIGetFromTheFactory())
.Then(x => x.ThenTheHandlerIsReturned("IdentityServer"))
@ -40,14 +42,14 @@ namespace Ocelot.UnitTests.Authentication
[Fact]
public void should_return_error_if_cannot_create_handler()
{
this.Given(x => x.GivenTheAuthenticationOptionsAre(new Library.Infrastructure.Configuration.AuthenticationOptions("IdentityServer", "", "", false, new List<string>(), "")))
this.Given(x => x.GivenTheAuthenticationOptionsAre(new AuthenticationOptions("IdentityServer", "", "", false, new List<string>(), "")))
.And(x => x.GivenTheCreatorReturnsAnError())
.When(x => x.WhenIGetFromTheFactory())
.Then(x => x.ThenAnErrorResponseIsReturned())
.BDDfy();
}
private void GivenTheAuthenticationOptionsAre(Library.Infrastructure.Configuration.AuthenticationOptions authenticationOptions)
private void GivenTheAuthenticationOptionsAre(AuthenticationOptions authenticationOptions)
{
_authenticationOptions = authenticationOptions;
}
@ -55,7 +57,7 @@ namespace Ocelot.UnitTests.Authentication
private void GivenTheCreatorReturnsAnError()
{
_creator
.Setup(x => x.CreateIdentityServerAuthenticationHandler(It.IsAny<IApplicationBuilder>(), It.IsAny<Library.Infrastructure.Configuration.AuthenticationOptions>()))
.Setup(x => x.CreateIdentityServerAuthenticationHandler(It.IsAny<IApplicationBuilder>(), It.IsAny<AuthenticationOptions>()))
.Returns(new ErrorResponse<RequestDelegate>(new List<Error>
{
new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for xxx")
@ -65,7 +67,7 @@ namespace Ocelot.UnitTests.Authentication
private void GivenTheCreatorReturns()
{
_creator
.Setup(x => x.CreateIdentityServerAuthenticationHandler(It.IsAny<IApplicationBuilder>(), It.IsAny<Library.Infrastructure.Configuration.AuthenticationOptions>()))
.Setup(x => x.CreateIdentityServerAuthenticationHandler(It.IsAny<IApplicationBuilder>(), It.IsAny<AuthenticationOptions>()))
.Returns(new OkResponse<RequestDelegate>(x => Task.CompletedTask));
}

View File

@ -1,12 +1,13 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Configuration.Yaml;
using Ocelot.Library.Infrastructure.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Configuration
{
using Library.Configuration.Yaml;
using Library.Responses;
public class ConfigurationValidationTests
{
private YamlConfiguration _yamlConfiguration;

View File

@ -1,16 +1,17 @@
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Moq;
using Ocelot.Library.Infrastructure.Builder;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.Configuration.Yaml;
using Ocelot.Library.Infrastructure.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Configuration
{
using Library.Builder;
using Library.Configuration;
using Library.Configuration.Yaml;
using Library.Responses;
public class OcelotConfigurationTests
{
private readonly Mock<IOptions<YamlConfiguration>> _yamlConfig;

View File

@ -1,16 +1,17 @@
using System.Collections.Generic;
using Moq;
using Ocelot.Library.Infrastructure.Builder;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.DownstreamRouteFinder
{
using Library.Builder;
using Library.Configuration;
using Library.DownstreamRouteFinder;
using Library.Responses;
using Library.UrlMatcher;
public class DownstreamRouteFinderTests
{
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
@ -28,7 +29,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder
_mockConfig = new Mock<IOcelotConfiguration>();
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
_finder = new Mock<ITemplateVariableNameAndValueFinder>();
_downstreamRouteFinder = new Library.Infrastructure.DownstreamRouteFinder.DownstreamRouteFinder(_mockConfig.Object, _mockMatcher.Object, _finder.Object);
_downstreamRouteFinder = new DownstreamRouteFinder(_mockConfig.Object, _mockMatcher.Object, _finder.Object);
}
[Fact]

View File

@ -6,19 +6,18 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Library.Infrastructure.Authentication;
using Ocelot.Library.Infrastructure.Builder;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Middleware;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Middleware
{
using Library.Infrastructure.Configuration;
using Library.Authentication;
using Library.Builder;
using Library.DownstreamRouteFinder;
using Library.Middleware;
using Library.Repository;
using Library.Responses;
using Library.UrlMatcher;
public class AuthenticationMiddlewareTests : IDisposable
{

View File

@ -6,17 +6,18 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Library.Infrastructure.Builder;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Middleware;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Middleware
{
using Library.Builder;
using Library.DownstreamRouteFinder;
using Library.Middleware;
using Library.Repository;
using Library.Responses;
using Library.UrlMatcher;
public class DownstreamRouteFinderMiddlewareTests : IDisposable
{
private readonly Mock<IDownstreamRouteFinder> _downstreamRouteFinder;

View File

@ -1,18 +1,16 @@
using Ocelot.Library.Infrastructure.Builder;
using Ocelot.Library.Infrastructure.Middleware;
namespace Ocelot.UnitTests.Middleware
namespace Ocelot.UnitTests.Middleware
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using Library.Infrastructure.Configuration;
using Library.Infrastructure.DownstreamRouteFinder;
using Library.Infrastructure.Repository;
using Library.Infrastructure.Responses;
using Library.Infrastructure.UrlMatcher;
using Library.Infrastructure.UrlTemplateReplacer;
using Library.Builder;
using Library.DownstreamRouteFinder;
using Library.Middleware;
using Library.Repository;
using Library.Responses;
using Library.UrlMatcher;
using Library.UrlTemplateReplacer;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;

View File

@ -7,15 +7,16 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Library.Infrastructure.Middleware;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Responses;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Middleware
{
using Library.Middleware;
using Library.Repository;
using Library.RequestBuilder;
using Library.Responses;
public class HttpRequestBuilderMiddlewareTests : IDisposable
{
private readonly Mock<IRequestBuilder> _requestBuilder;

View File

@ -6,16 +6,17 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Library.Infrastructure.Middleware;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Requester;
using Ocelot.Library.Infrastructure.Responses;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Middleware
{
using Library.Middleware;
using Library.Repository;
using Library.RequestBuilder;
using Library.Requester;
using Library.Responses;
public class HttpRequesterMiddlewareTests : IDisposable
{
private readonly Mock<IHttpRequester> _requester;

View File

@ -6,15 +6,16 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.Library.Infrastructure.Middleware;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responder;
using Ocelot.Library.Infrastructure.Responses;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Middleware
{
using Library.Middleware;
using Library.Repository;
using Library.Responder;
using Library.Responses;
public class HttpResponderMiddlewareTests : IDisposable
{
private readonly Mock<IHttpResponder> _responder;

View File

@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Repository
{
using Library.Repository;
using Library.Responses;
public class ScopedRequestDataRepositoryTests
{
private IScopedRequestDataRepository _scopedRequestDataRepository;

View File

@ -5,14 +5,15 @@ using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.RequestBuilder
{
using Library.RequestBuilder;
using Library.Responses;
public class RequestBuilderTests
{
private string _httpMethod;
@ -28,7 +29,7 @@ namespace Ocelot.UnitTests.RequestBuilder
public RequestBuilderTests()
{
_content = new StringContent(string.Empty);
_requestBuilder = new Library.Infrastructure.RequestBuilder.HttpRequestBuilder();
_requestBuilder = new HttpRequestBuilder();
}
[Fact]

View File

@ -2,16 +2,17 @@
using System.IO;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Middleware;
using Ocelot.Library.Infrastructure.Responder;
using Ocelot.Library.Infrastructure.Responses;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.Responder
{
using Library.Errors;
using Library.Middleware;
using Library.Responder;
using Library.Responses;
public class ErrorsToHttpStatusCodeMapperTests
{
private readonly IErrorsToHttpStatusCodeMapper _codeMapper;

View File

@ -1,11 +1,12 @@
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.UrlMatcher
{
using Library.Responses;
using Library.UrlMatcher;
public class RegExUrlMatcherTests
{
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;

View File

@ -1,13 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.UrlMatcher
{
using Library.Responses;
using Library.UrlMatcher;
public class UrlPathToUrlTemplateMatcherTests
{
private readonly ITemplateVariableNameAndValueFinder _finder;

View File

@ -1,16 +1,15 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Builder;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.UnitTests.UrlTemplateReplacer
{
using Library.Infrastructure.Configuration;
using Library.Builder;
using Library.DownstreamRouteFinder;
using Library.Responses;
using Library.UrlMatcher;
using Library.UrlTemplateReplacer;
public class UpstreamUrlPathTemplateVariableReplacerTests
{