mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-09-17 23:25:35 +08:00
+semver: upgrade to net5.0 (#1390)
* breaking upgrade base build image to net5.0 * add make and build tools to image * fix code broken after net5.0 upgrade * fix warnings * fix tests and line endings * upgrade dotnet test and coverages packages * update circle build image * removed rafty and updated more packages * bring back develop * rename authorisation to authorization
This commit is contained in:
@@ -1,88 +1,88 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
public class HttpDataRepositoryTests
|
||||
{
|
||||
private readonly HttpContext _httpContext;
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly HttpDataRepository _httpDataRepository;
|
||||
private object _result;
|
||||
|
||||
public HttpDataRepositoryTests()
|
||||
{
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_httpContextAccessor = new HttpContextAccessor { HttpContext = _httpContext };
|
||||
_httpDataRepository = new HttpDataRepository(_httpContextAccessor);
|
||||
}
|
||||
|
||||
/*
|
||||
TODO - Additional tests -> Type mistmatch aka Add string, request int
|
||||
TODO - Additional tests -> HttpContent null. This should never happen
|
||||
*/
|
||||
|
||||
[Fact]
|
||||
public void get_returns_correct_key_from_http_context()
|
||||
{
|
||||
this.Given(x => x.GivenAHttpContextContaining("key", "string"))
|
||||
.When(x => x.GetIsCalledWithKey<string>("key"))
|
||||
.Then(x => x.ThenTheResultIsAnOkResponse<string>("string"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void get_returns_error_response_if_the_key_is_not_found() //Therefore does not return null
|
||||
{
|
||||
this.Given(x => x.GivenAHttpContextContaining("key", "string"))
|
||||
.When(x => x.GetIsCalledWithKey<string>("keyDoesNotExist"))
|
||||
.Then(x => x.ThenTheResultIsAnErrorReposnse<string>("string1"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_update()
|
||||
{
|
||||
this.Given(x => x.GivenAHttpContextContaining("key", "string"))
|
||||
.And(x => x.UpdateIsCalledWith<string>("key", "new string"))
|
||||
.When(x => x.GetIsCalledWithKey<string>("key"))
|
||||
.Then(x => x.ThenTheResultIsAnOkResponse<string>("new string"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void UpdateIsCalledWith<T>(string key, string value)
|
||||
{
|
||||
_httpDataRepository.Update(key, value);
|
||||
}
|
||||
|
||||
private void GivenAHttpContextContaining(string key, object o)
|
||||
{
|
||||
_httpContext.Items.Add(key, o);
|
||||
}
|
||||
|
||||
private void GetIsCalledWithKey<T>(string key)
|
||||
{
|
||||
_result = _httpDataRepository.Get<T>(key);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsAnErrorReposnse<T>(object resultValue)
|
||||
{
|
||||
_result.ShouldBeOfType<ErrorResponse<T>>();
|
||||
((ErrorResponse<T>)_result).Data.ShouldBeNull();
|
||||
((ErrorResponse<T>)_result).IsError.ShouldBe(true);
|
||||
((ErrorResponse<T>)_result).Errors.ShouldHaveSingleItem()
|
||||
.ShouldBeOfType<CannotFindDataError>()
|
||||
.Message.ShouldStartWith("Unable to find data for key: ");
|
||||
}
|
||||
|
||||
private void ThenTheResultIsAnOkResponse<T>(object resultValue)
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse<T>>();
|
||||
((OkResponse<T>)_result).Data.ShouldBe(resultValue);
|
||||
}
|
||||
}
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
public class HttpDataRepositoryTests
|
||||
{
|
||||
private readonly HttpContext _httpContext;
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly HttpDataRepository _httpDataRepository;
|
||||
private object _result;
|
||||
|
||||
public HttpDataRepositoryTests()
|
||||
{
|
||||
_httpContext = new DefaultHttpContext();
|
||||
_httpContextAccessor = new HttpContextAccessor { HttpContext = _httpContext };
|
||||
_httpDataRepository = new HttpDataRepository(_httpContextAccessor);
|
||||
}
|
||||
|
||||
/*
|
||||
TODO - Additional tests -> Type mistmatch aka Add string, request int
|
||||
TODO - Additional tests -> HttpContent null. This should never happen
|
||||
*/
|
||||
|
||||
[Fact]
|
||||
public void get_returns_correct_key_from_http_context()
|
||||
{
|
||||
this.Given(x => x.GivenAHttpContextContaining("key", "string"))
|
||||
.When(x => x.GetIsCalledWithKey<string>("key"))
|
||||
.Then(x => x.ThenTheResultIsAnOkResponse<string>("string"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void get_returns_error_response_if_the_key_is_not_found() //Therefore does not return null
|
||||
{
|
||||
this.Given(x => x.GivenAHttpContextContaining("key", "string"))
|
||||
.When(x => x.GetIsCalledWithKey<string>("keyDoesNotExist"))
|
||||
.Then(x => x.ThenTheResultIsAnErrorReposnse<string>("string1"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_update()
|
||||
{
|
||||
this.Given(x => x.GivenAHttpContextContaining("key", "string"))
|
||||
.And(x => x.UpdateIsCalledWith<string>("key", "new string"))
|
||||
.When(x => x.GetIsCalledWithKey<string>("key"))
|
||||
.Then(x => x.ThenTheResultIsAnOkResponse<string>("new string"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void UpdateIsCalledWith<T>(string key, string value)
|
||||
{
|
||||
_httpDataRepository.Update(key, value);
|
||||
}
|
||||
|
||||
private void GivenAHttpContextContaining(string key, object o)
|
||||
{
|
||||
_httpContext.Items.Add(key, o);
|
||||
}
|
||||
|
||||
private void GetIsCalledWithKey<T>(string key)
|
||||
{
|
||||
_result = _httpDataRepository.Get<T>(key);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsAnErrorReposnse<T>(object resultValue)
|
||||
{
|
||||
_result.ShouldBeOfType<ErrorResponse<T>>();
|
||||
((ErrorResponse<T>)_result).Data.ShouldBe(default(T));
|
||||
((ErrorResponse<T>)_result).IsError.ShouldBe(true);
|
||||
((ErrorResponse<T>)_result).Errors.ShouldHaveSingleItem()
|
||||
.ShouldBeOfType<CannotFindDataError>()
|
||||
.Message.ShouldStartWith("Unable to find data for key: ");
|
||||
}
|
||||
|
||||
private void ThenTheResultIsAnOkResponse<T>(object resultValue)
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse<T>>();
|
||||
((OkResponse<T>)_result).Data.ShouldBe(resultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
using Moq;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Authorization;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
@@ -11,18 +11,18 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
public class ScopesAuthoriserTests
|
||||
public class ScopesAuthorizerTests
|
||||
{
|
||||
private ScopesAuthoriser _authoriser;
|
||||
private ScopesAuthorizer _authorizer;
|
||||
public Mock<IClaimsParser> _parser;
|
||||
private ClaimsPrincipal _principal;
|
||||
private List<string> _allowedScopes;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ScopesAuthoriserTests()
|
||||
public ScopesAuthorizerTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_authoriser = new ScopesAuthoriser(_parser.Object);
|
||||
_authorizer = new ScopesAuthorizer(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -30,7 +30,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing(new List<string>()))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
@@ -40,7 +40,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheFollowing((List<string>)null))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
@@ -52,7 +52,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
|
||||
.And(_ => GivenTheParserReturns(new ErrorResponse<List<string>>(fakeError)))
|
||||
.And(_ => GivenTheFollowing(new List<string>() { "doesntmatter" }))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
@@ -66,7 +66,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(allowedScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new OkResponse<bool>(true)))
|
||||
.BDDfy();
|
||||
}
|
||||
@@ -82,7 +82,7 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
this.Given(_ => GivenTheFollowing(claimsPrincipal))
|
||||
.And(_ => GivenTheParserReturns(new OkResponse<List<string>>(userScopes)))
|
||||
.And(_ => GivenTheFollowing(allowedScopes))
|
||||
.When(_ => WhenIAuthorise())
|
||||
.When(_ => WhenIAuthorize())
|
||||
.Then(_ => ThenTheFollowingIsReturned(new ErrorResponse<bool>(fakeError)))
|
||||
.BDDfy();
|
||||
}
|
||||
@@ -102,9 +102,9 @@ namespace Ocelot.UnitTests.Infrastructure
|
||||
_allowedScopes = allowedScopes;
|
||||
}
|
||||
|
||||
private void WhenIAuthorise()
|
||||
private void WhenIAuthorize()
|
||||
{
|
||||
_result = _authoriser.Authorise(_principal, _allowedScopes);
|
||||
_result = _authorizer.Authorize(_principal, _allowedScopes);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(Response<bool> expected)
|
Reference in New Issue
Block a user