mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 07:48:16 +08:00
Implementing jwt and adding tests
This commit is contained in:
@ -20,36 +20,78 @@ namespace Ocelot.UnitTests.Configuration
|
||||
_authOptionsCreator = new AuthenticationOptionsCreator();
|
||||
}
|
||||
|
||||
// [Fact]
|
||||
// public void should_return_auth_options()
|
||||
// {
|
||||
// var fileReRoute = new FileReRoute()
|
||||
// {
|
||||
// AuthenticationOptions = new FileAuthenticationOptions
|
||||
// {
|
||||
// Provider = "Geoff",
|
||||
// ProviderRootUrl = "http://www.bbc.co.uk/",
|
||||
//ApiName = "Laura",
|
||||
// RequireHttps = true,
|
||||
//AllowedScopes = new List<string> {"cheese"},
|
||||
// ApiSecret = "secret"
|
||||
// }
|
||||
// };
|
||||
[Fact]
|
||||
public void should_return_auth_options()
|
||||
{
|
||||
var fileReRoute = new FileReRoute()
|
||||
{
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
Provider = "Geoff",
|
||||
IdentityServerConfig = new FileIdentityServerConfig()
|
||||
{
|
||||
ProviderRootUrl = "http://www.bbc.co.uk/",
|
||||
ApiName = "Laura",
|
||||
RequireHttps = true,
|
||||
ApiSecret = "secret"
|
||||
},
|
||||
AllowedScopes = new List<string> { "cheese" },
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// var expected = new AuthenticationOptionsBuilder()
|
||||
// .WithProvider(fileReRoute.AuthenticationOptions?.Provider)
|
||||
// .WithProviderRootUrl(fileReRoute.AuthenticationOptions?.ProviderRootUrl)
|
||||
// .WithApiName(fileReRoute.AuthenticationOptions?.ApiName)
|
||||
// .WithRequireHttps(fileReRoute.AuthenticationOptions.RequireHttps)
|
||||
// .WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes)
|
||||
// .WithApiSecret(fileReRoute.AuthenticationOptions?.ApiSecret)
|
||||
// .Build();
|
||||
var authenticationConfig = new IdentityServerConfigBuilder()
|
||||
.WithProviderRootUrl(fileReRoute.AuthenticationOptions?.IdentityServerConfig?.ProviderRootUrl)
|
||||
.WithApiName(fileReRoute.AuthenticationOptions?.IdentityServerConfig?.ApiName)
|
||||
.WithRequireHttps(fileReRoute.AuthenticationOptions.IdentityServerConfig.RequireHttps)
|
||||
.WithApiSecret(fileReRoute.AuthenticationOptions?.IdentityServerConfig?.ApiSecret)
|
||||
.Build();
|
||||
|
||||
// this.Given(x => x.GivenTheFollowing(fileReRoute))
|
||||
// .When(x => x.WhenICreateTheAuthenticationOptions())
|
||||
// .Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
// .BDDfy();
|
||||
// }
|
||||
var expected = new AuthenticationOptionsBuilder()
|
||||
.WithProvider(fileReRoute.AuthenticationOptions?.Provider)
|
||||
.WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes)
|
||||
.WithConfig(authenticationConfig)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowing(fileReRoute))
|
||||
.When(x => x.WhenICreateTheAuthenticationOptions())
|
||||
.Then(x => x.ThenTheFollowingIdentityServerConfigIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_Jwt_auth_options()
|
||||
{
|
||||
var fileReRoute = new FileReRoute()
|
||||
{
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
Provider = "Jwt",
|
||||
JwtConfig = new FileJwtConfig()
|
||||
{
|
||||
Audience = "Audience",
|
||||
Authority = "Authority"
|
||||
},
|
||||
AllowedScopes = new List<string> { "cheese" }
|
||||
}
|
||||
};
|
||||
|
||||
var authenticationConfig = new JwtConfigBuilder()
|
||||
.WithAudience(fileReRoute.AuthenticationOptions?.JwtConfig?.Audience)
|
||||
.WithAuthority(fileReRoute.AuthenticationOptions?.JwtConfig?.Authority)
|
||||
.Build();
|
||||
|
||||
var expected = new AuthenticationOptionsBuilder()
|
||||
.WithProvider(fileReRoute.AuthenticationOptions?.Provider)
|
||||
.WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes)
|
||||
.WithConfig(authenticationConfig)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowing(fileReRoute))
|
||||
.When(x => x.WhenICreateTheAuthenticationOptions())
|
||||
.Then(x => x.ThenTheFollowingJwtConfigIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(FileReRoute fileReRoute)
|
||||
{
|
||||
@ -61,14 +103,31 @@ namespace Ocelot.UnitTests.Configuration
|
||||
_result = _authOptionsCreator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
//private void ThenTheFollowingIsReturned(AuthenticationOptions expected)
|
||||
//{
|
||||
// _result.AllowedScopes.ShouldBe(expected.AllowedScopes);
|
||||
// _result.Provider.ShouldBe(expected.Provider);
|
||||
// _result.ProviderRootUrl.ShouldBe(expected.ProviderRootUrl);
|
||||
// _result.RequireHttps.ShouldBe(expected.RequireHttps);
|
||||
// _result.ApiName.ShouldBe(expected.ApiName);
|
||||
// _result.ApiSecret.ShouldBe(expected.ApiSecret);
|
||||
//}
|
||||
private void ThenTheFollowingJwtConfigIsReturned(AuthenticationOptions expected)
|
||||
{
|
||||
_result.AllowedScopes.ShouldBe(expected.AllowedScopes);
|
||||
_result.Provider.ShouldBe(expected.Provider);
|
||||
|
||||
var _resultSettings = _result.Config as JwtConfig;
|
||||
var expectedSettngs = expected.Config as JwtConfig;
|
||||
|
||||
_resultSettings.Audience.ShouldBe(expectedSettngs.Audience);
|
||||
_resultSettings.Authority.ShouldBe(expectedSettngs.Authority);
|
||||
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIdentityServerConfigIsReturned(AuthenticationOptions expected)
|
||||
{
|
||||
_result.AllowedScopes.ShouldBe(expected.AllowedScopes);
|
||||
_result.Provider.ShouldBe(expected.Provider);
|
||||
|
||||
var _resultSettings = _result.Config as IdentityServerConfig;
|
||||
var expectedSettngs = expected.Config as IdentityServerConfig;
|
||||
|
||||
_resultSettings.ProviderRootUrl.ShouldBe(expectedSettngs.ProviderRootUrl);
|
||||
_resultSettings.RequireHttps.ShouldBe(expectedSettngs.RequireHttps);
|
||||
_resultSettings.ApiName.ShouldBe(expectedSettngs.ApiName);
|
||||
_resultSettings.ApiSecret.ShouldBe(expectedSettngs.ApiSecret);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user