using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Security.Claims; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Moq; using Ocelot.Authorisation; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; namespace Ocelot.UnitTests.Authorization { using Authorisation.Middleware; public class AuthorisationMiddlewareTests : IDisposable { private readonly Mock _scopedRepository; private readonly Mock _authService; private readonly string _url; private readonly TestServer _server; private readonly HttpClient _client; private HttpResponseMessage _result; private OkResponse _downstreamRoute; public AuthorisationMiddlewareTests() { _url = "http://localhost:51879"; _scopedRepository = new Mock(); _authService = new Mock(); var builder = new WebHostBuilder() .ConfigureServices(x => { x.AddSingleton(); x.AddLogging(); x.AddSingleton(_authService.Object); x.AddSingleton(_scopedRepository.Object); }) .UseUrls(_url) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseUrls(_url) .Configure(app => { app.UseAuthorisationMiddleware(); }); _server = new TestServer(builder); _client = _server.CreateClient(); } [Fact] public void should_call_authorisation_service() { this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List(), new ReRouteBuilder() .WithIsAuthorised(true) .WithUpstreamHttpMethod("Get") .WithUpstreamHttpMethod("Get") .Build()))) .And(x => x.GivenTheAuthServiceReturns(new OkResponse(true))) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheAuthServiceIsCalledCorrectly()) .BDDfy(); } private void GivenTheAuthServiceReturns(Response expected) { _authService .Setup(x => x.Authorise(It.IsAny(), It.IsAny>())) .Returns(expected); } private void ThenTheAuthServiceIsCalledCorrectly() { _authService .Verify(x => x.Authorise(It.IsAny(), It.IsAny>()), Times.Once); } private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute) { _downstreamRoute = new OkResponse(downstreamRoute); _scopedRepository .Setup(x => x.Get(It.IsAny())) .Returns(_downstreamRoute); } private void WhenICallTheMiddleware() { _result = _client.GetAsync(_url).Result; } public void Dispose() { _client.Dispose(); _server.Dispose(); } } }