mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 22:30:50 +08:00 
			
		
		
		
	Can authorise routes based on claims, there is also a claims transformation middleware
This commit is contained in:
		
							
								
								
									
										145
									
								
								test/Ocelot.UnitTests/ClaimsBuilder/AddClaimsToRequestTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										145
									
								
								test/Ocelot.UnitTests/ClaimsBuilder/AddClaimsToRequestTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,145 @@
 | 
			
		||||
namespace Ocelot.UnitTests.ClaimsBuilder
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Security.Claims;
 | 
			
		||||
    using Errors;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.ClaimsBuilder;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Infrastructure.Claims.Parser;
 | 
			
		||||
    using Responses;
 | 
			
		||||
    using Shouldly;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
    public class AddClaimsToRequestTests
 | 
			
		||||
    {
 | 
			
		||||
        private readonly AddClaimsToRequest _addClaimsToRequest;
 | 
			
		||||
        private readonly Mock<IClaimsParser> _parser;
 | 
			
		||||
        private List<ClaimToThing> _claimsToThings;
 | 
			
		||||
        private HttpContext _context;
 | 
			
		||||
        private Response _result;
 | 
			
		||||
        private Response<string> _claimValue;
 | 
			
		||||
 | 
			
		||||
        public AddClaimsToRequestTests()
 | 
			
		||||
        {
 | 
			
		||||
            _parser = new Mock<IClaimsParser>();
 | 
			
		||||
            _addClaimsToRequest = new AddClaimsToRequest(_parser.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_add_claims_to_context()
 | 
			
		||||
        {
 | 
			
		||||
            var context = new DefaultHttpContext
 | 
			
		||||
            {
 | 
			
		||||
                User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
 | 
			
		||||
                {
 | 
			
		||||
                    new Claim("test", "data")
 | 
			
		||||
                }))
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenClaimsToThings(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("claim-key", "", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .Given(x => x.GivenHttpContext(context))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddClaimsToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void if_claims_exists_should_replace_it()
 | 
			
		||||
        {
 | 
			
		||||
            var context = new DefaultHttpContext
 | 
			
		||||
            {
 | 
			
		||||
                User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
 | 
			
		||||
                {
 | 
			
		||||
                    new Claim("existing-key", "data"),
 | 
			
		||||
                    new Claim("new-key", "data")
 | 
			
		||||
                })),
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            this.Given(
 | 
			
		||||
                x => x.GivenClaimsToThings(new List<ClaimToThing>
 | 
			
		||||
                {
 | 
			
		||||
                    new ClaimToThing("existing-key", "new-key", "", 0)
 | 
			
		||||
                }))
 | 
			
		||||
                .Given(x => x.GivenHttpContext(context))
 | 
			
		||||
                .And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
 | 
			
		||||
                .When(x => x.WhenIAddClaimsToTheRequest())
 | 
			
		||||
                .Then(x => x.ThenTheResultIsSuccess())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_error()
 | 
			
		||||
        {
 | 
			
		||||
            this.Given(
 | 
			
		||||
               x => x.GivenClaimsToThings(new List<ClaimToThing>
 | 
			
		||||
               {
 | 
			
		||||
                    new ClaimToThing("", "", "", 0)
 | 
			
		||||
               }))
 | 
			
		||||
               .Given(x => x.GivenHttpContext(new DefaultHttpContext()))
 | 
			
		||||
               .And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
 | 
			
		||||
               {
 | 
			
		||||
                   new AnyError()
 | 
			
		||||
               })))
 | 
			
		||||
               .When(x => x.WhenIAddClaimsToTheRequest())
 | 
			
		||||
               .Then(x => x.ThenTheResultIsError())
 | 
			
		||||
               .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        private void GivenClaimsToThings(List<ClaimToThing> configuration)
 | 
			
		||||
        {
 | 
			
		||||
            _claimsToThings = configuration;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenHttpContext(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            _context = context;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheClaimParserReturns(Response<string> claimValue)
 | 
			
		||||
        {
 | 
			
		||||
            _claimValue = claimValue;
 | 
			
		||||
            _parser
 | 
			
		||||
                .Setup(
 | 
			
		||||
                    x =>
 | 
			
		||||
                        x.GetValue(It.IsAny<IEnumerable<Claim>>(),
 | 
			
		||||
                        It.IsAny<string>(),
 | 
			
		||||
                        It.IsAny<string>(),
 | 
			
		||||
                        It.IsAny<int>()))
 | 
			
		||||
                .Returns(_claimValue);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenIAddClaimsToTheRequest()
 | 
			
		||||
        {
 | 
			
		||||
            _result = _addClaimsToRequest.SetClaimsOnContext(_claimsToThings, _context);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResultIsSuccess()
 | 
			
		||||
        {
 | 
			
		||||
            _result.IsError.ShouldBe(false);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheResultIsError()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            _result.IsError.ShouldBe(true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        class AnyError : Error
 | 
			
		||||
        {
 | 
			
		||||
            public AnyError()
 | 
			
		||||
                : base("blahh", OcelotErrorCode.UnknownError)
 | 
			
		||||
            {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,111 @@
 | 
			
		||||
namespace Ocelot.UnitTests.ClaimsBuilder
 | 
			
		||||
{
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.IO;
 | 
			
		||||
    using System.Net.Http;
 | 
			
		||||
    using Microsoft.AspNetCore.Hosting;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Microsoft.AspNetCore.TestHost;
 | 
			
		||||
    using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
    using Moq;
 | 
			
		||||
    using Ocelot.ClaimsBuilder;
 | 
			
		||||
    using Ocelot.ClaimsBuilder.Middleware;
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
    using Ocelot.Configuration.Builder;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder;
 | 
			
		||||
    using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
    using Responses;
 | 
			
		||||
    using ScopedData;
 | 
			
		||||
    using TestStack.BDDfy;
 | 
			
		||||
    using Xunit;
 | 
			
		||||
 | 
			
		||||
    public class ClaimsBuilderMiddlewareTests : IDisposable
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
 | 
			
		||||
        private readonly Mock<IAddClaimsToRequest> _addHeaders;
 | 
			
		||||
        private readonly string _url;
 | 
			
		||||
        private readonly TestServer _server;
 | 
			
		||||
        private readonly HttpClient _client;
 | 
			
		||||
        private Response<DownstreamRoute> _downstreamRoute;
 | 
			
		||||
        private HttpResponseMessage _result;
 | 
			
		||||
 | 
			
		||||
        public ClaimsBuilderMiddlewareTests()
 | 
			
		||||
        {
 | 
			
		||||
            _url = "http://localhost:51879";
 | 
			
		||||
            _scopedRepository = new Mock<IScopedRequestDataRepository>();
 | 
			
		||||
            _addHeaders = new Mock<IAddClaimsToRequest>();
 | 
			
		||||
            var builder = new WebHostBuilder()
 | 
			
		||||
              .ConfigureServices(x =>
 | 
			
		||||
              {
 | 
			
		||||
                  x.AddSingleton(_addHeaders.Object);
 | 
			
		||||
                  x.AddSingleton(_scopedRepository.Object);
 | 
			
		||||
              })
 | 
			
		||||
              .UseUrls(_url)
 | 
			
		||||
              .UseKestrel()
 | 
			
		||||
              .UseContentRoot(Directory.GetCurrentDirectory())
 | 
			
		||||
              .UseIISIntegration()
 | 
			
		||||
              .UseUrls(_url)
 | 
			
		||||
              .Configure(app =>
 | 
			
		||||
              {
 | 
			
		||||
                  app.UseClaimsBuilderMiddleware();
 | 
			
		||||
              });
 | 
			
		||||
 | 
			
		||||
            _server = new TestServer(builder);
 | 
			
		||||
            _client = _server.CreateClient();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void happy_path()
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
 | 
			
		||||
                new ReRouteBuilder()
 | 
			
		||||
                    .WithDownstreamTemplate("any old string")
 | 
			
		||||
                    .WithClaimsToClaims(new List<ClaimToThing>
 | 
			
		||||
                    {
 | 
			
		||||
                        new ClaimToThing("sub", "UserType", "|", 0)
 | 
			
		||||
                    })
 | 
			
		||||
                    .Build());
 | 
			
		||||
 | 
			
		||||
            this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
 | 
			
		||||
                .And(x => x.GivenTheAddClaimsToRequestReturns())
 | 
			
		||||
                .When(x => x.WhenICallTheMiddleware())
 | 
			
		||||
                .Then(x => x.ThenTheClaimsToRequestIsCalledCorrectly())
 | 
			
		||||
                .BDDfy();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheAddClaimsToRequestReturns()
 | 
			
		||||
        {
 | 
			
		||||
            _addHeaders
 | 
			
		||||
                .Setup(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
 | 
			
		||||
                It.IsAny<HttpContext>()))
 | 
			
		||||
                .Returns(new OkResponse());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void ThenTheClaimsToRequestIsCalledCorrectly()
 | 
			
		||||
        {
 | 
			
		||||
            _addHeaders
 | 
			
		||||
                .Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
 | 
			
		||||
                It.IsAny<HttpContext>()), Times.Once);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void WhenICallTheMiddleware()
 | 
			
		||||
        {
 | 
			
		||||
            _result = _client.GetAsync(_url).Result;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
 | 
			
		||||
            _scopedRepository
 | 
			
		||||
                .Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
 | 
			
		||||
                .Returns(_downstreamRoute);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Dispose()
 | 
			
		||||
        {
 | 
			
		||||
            _client.Dispose();
 | 
			
		||||
            _server.Dispose();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user