Skip to content

Commit

Permalink
#2219 Remove the BDDfy framework from the unit testing project (#2235)
Browse files Browse the repository at this point in the history
* Day 1
* Day 2
* Day 3
* Day 4
* Day 5
* Day 6
  • Loading branch information
raman-m authored Dec 12, 2024
1 parent 29c72f8 commit 5c8938f
Show file tree
Hide file tree
Showing 153 changed files with 6,214 additions and 7,713 deletions.
11 changes: 11 additions & 0 deletions test/Ocelot.Testing/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Ocelot.Testing;

public static class StreamExtensions
{
public static string AsString(this Stream stream)
{
using var reader = new StreamReader(stream);
var text = reader.ReadToEnd();
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,33 @@ private static IWebHostEnvironment GetHostingEnvironment()

//keep
[Fact]
public void should_set_up_administration_with_identity_server_options()
public void Should_set_up_administration_with_identity_server_options()
{
Action<JwtBearerOptions> options = o => { };
// Arrange
static void options(JwtBearerOptions o)
{
}

this.Given(x => WhenISetUpOcelotServices())
.When(x => WhenISetUpAdministration(options))
.Then(x => ThenAnExceptionIsntThrown())
.Then(x => ThenTheCorrectAdminPathIsRegitered())
.BDDfy();
// Act
WhenISetUpOcelotServices();
WhenISetUpAdministration(options);

// Assert
ThenAnExceptionIsntThrown();
ThenTheCorrectAdminPathIsRegitered();
}

//keep
[Fact]
public void should_set_up_administration()
public void Should_set_up_administration()
{
this.Given(x => WhenISetUpOcelotServices())
.When(x => WhenISetUpAdministration())
.Then(x => ThenAnExceptionIsntThrown())
.Then(x => ThenTheCorrectAdminPathIsRegitered())
.BDDfy();
// Arrange, Act
WhenISetUpOcelotServices();
WhenISetUpAdministration();

// Assert
ThenAnExceptionIsntThrown();
ThenTheCorrectAdminPathIsRegitered();
}

private void WhenISetUpAdministration()
Expand Down
161 changes: 83 additions & 78 deletions test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AuthenticationMiddlewareTests : UnitTest
private readonly Mock<IOcelotLoggerFactory> _factory;
private readonly Mock<IOcelotLogger> _logger;
private readonly Mock<IServiceProvider> _serviceProvider;
private readonly HttpContext _httpContext;
private readonly DefaultHttpContext _httpContext;

private AuthenticationMiddleware _middleware;
private RequestDelegate _next;
Expand Down Expand Up @@ -64,112 +64,124 @@ public void MiddlewareName_Cstor_ReturnsTypeName()
[Fact]
public void Should_call_next_middleware_if_route_is_not_authenticated()
{
var methods = new List<string> { "Get" };
this.Given(x => GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithUpstreamHttpMethod(methods)
.Build()
))
.When(x => WhenICallTheMiddleware())
.Then(x => ThenTheUserIsAuthenticated())
.BDDfy();
// Arrange
GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithUpstreamHttpMethod(new() { HttpMethods.Get })
.Build());

// Act
WhenICallTheMiddleware();

// Assert
ThenTheUserIsAuthenticated();
}

[Fact]
public void Should_call_next_middleware_if_route_is_using_options_method()
{
const string OPTIONS = "OPTIONS";
var methods = new List<string> { OPTIONS };
this.Given(x => GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithUpstreamHttpMethod(methods)
.WithIsAuthenticated(true)
.Build()
))
.And(x => GivenTheRequestIsUsingMethod(OPTIONS))
.When(x => WhenICallTheMiddleware())
.Then(x => ThenTheUserIsAuthenticated())
.BDDfy();
// Arrange
GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithUpstreamHttpMethod(new() { HttpMethods.Options })
.WithIsAuthenticated(true)
.Build());
GivenTheRequestIsUsingMethod(HttpMethods.Options);

// Act
WhenICallTheMiddleware();

// Assert
ThenTheUserIsAuthenticated();
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void Should_call_next_middleware_if_route_is_using_several_options_authentication_providers(bool isMultipleKeys)
{
// Arrange
var multipleKeys = new string[] { string.Empty, "Fail", "Test" };
var options = new AuthenticationOptions(null,
!isMultipleKeys ? "Test" : null,
isMultipleKeys ? multipleKeys : null
);
var methods = new List<string> { "Get" };
this.Given(x => GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithAuthenticationOptions(options)
.WithIsAuthenticated(true)
.WithUpstreamHttpMethod(methods)
.Build()
))
.And(x => GivenTheRequestIsUsingMethod(methods.First()))
.And(x => GivenTheAuthenticationIsFail())
.And(x => GivenTheAuthenticationIsSuccess())
.And(x => GivenTheAuthenticationThrowsException())
.When(x => WhenICallTheMiddleware())
.Then(x => ThenTheUserIsAuthenticated())
.BDDfy();
var methods = new List<string> { HttpMethods.Get };
GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithAuthenticationOptions(options)
.WithIsAuthenticated(true)
.WithUpstreamHttpMethod(methods)
.Build());
GivenTheRequestIsUsingMethod(methods.First());
GivenTheAuthenticationIsFail();
GivenTheAuthenticationIsSuccess();
GivenTheAuthenticationThrowsException();

// Act
WhenICallTheMiddleware();

// Assert
ThenTheUserIsAuthenticated();
}

[Fact]
public void Should_provide_backward_compatibility_if_route_has_several_options_authentication_providers()
{
// Arrange
var options = new AuthenticationOptions(null,
"Test",
new string[] { string.Empty, "Fail", "Test" }
);
var methods = new List<string> { "Get" };
this.Given(x => GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithAuthenticationOptions(options)
.WithIsAuthenticated(true)
.WithUpstreamHttpMethod(methods)
.Build()
))
.And(x => GivenTheRequestIsUsingMethod(methods.First()))
.And(x => GivenTheAuthenticationIsFail())
.And(x => GivenTheAuthenticationIsSuccess())
.And(x => GivenTheAuthenticationThrowsException())
.When(x => WhenICallTheMiddleware())
.Then(x => ThenTheUserIsAuthenticated())
.BDDfy();
var methods = new List<string> { HttpMethods.Get };
GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithAuthenticationOptions(options)
.WithIsAuthenticated(true)
.WithUpstreamHttpMethod(methods)
.Build());
GivenTheRequestIsUsingMethod(methods.First());
GivenTheAuthenticationIsFail();
GivenTheAuthenticationIsSuccess();
GivenTheAuthenticationThrowsException();

// Act
WhenICallTheMiddleware();

// Assert
ThenTheUserIsAuthenticated();
}

[Fact]
public void Should_not_call_next_middleware_and_return_no_result_if_all_multiple_keys_were_failed()
{
// Arrange
var options = new AuthenticationOptions(null, null,
new string[] { string.Empty, "Fail", "Fail", "UnknownScheme" }
);
var methods = new List<string> { "Get" };
var methods = new List<string> { HttpMethods.Get };
GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithAuthenticationOptions(options)
.WithIsAuthenticated(true)
.WithUpstreamHttpMethod(methods)
.Build());
GivenTheRequestIsUsingMethod(methods.First());
GivenTheAuthenticationIsFail();
GivenTheAuthenticationIsSuccess();

// Act
WhenICallTheMiddleware();

this.Given(x => GivenTheDownStreamRouteIs(new DownstreamRouteBuilder()
.WithAuthenticationOptions(options)
.WithIsAuthenticated(true)
.WithUpstreamHttpMethod(methods)
.Build()
))
.And(x => GivenTheRequestIsUsingMethod(methods.First()))
.And(x => GivenTheAuthenticationIsFail())
.And(x => GivenTheAuthenticationIsSuccess())
.When(x => WhenICallTheMiddleware())
.Then(x => ThenTheUserIsNotAuthenticated())
.BDDfy();
// Assert
ThenTheUserIsNotAuthenticated();
_httpContext.User.Identity.IsAuthenticated.ShouldBeFalse();
_logWarningMessages.Count.ShouldBe(1);
_logWarningMessages.First().ShouldStartWith("Client has NOT been authenticated for path");
_httpContext.Items.Errors().First().ShouldBeOfType(typeof(UnauthenticatedError));
_httpContext.Items.Errors().First().ShouldBeOfType<UnauthenticatedError>();
}

[Theory]
[InlineData(0)]
[InlineData(2)]
public void Should_not_call_next_middleware_and_return_no_result_if_providers_keys_are_empty(int keysCount)
{
// Arrange
var emptyKeys = new string[keysCount];
for (int i = 0; i < emptyKeys.Length; i++)
{
Expand All @@ -184,19 +196,22 @@ public void Should_not_call_next_middleware_and_return_no_result_if_providers_ke
.WithUpstreamHttpMethod(methods)
.WithDownstreamPathTemplate("/" + nameof(Should_not_call_next_middleware_and_return_no_result_if_providers_keys_are_empty))
.Build();
this.Given(x => GivenTheDownStreamRouteIs(route))
.And(x => GivenTheRequestIsUsingMethod(methods.First()))
.When(x => WhenICallTheMiddleware())
.Then(x => ThenTheUserIsNotAuthenticated())
.BDDfy();
GivenTheDownStreamRouteIs(route);
GivenTheRequestIsUsingMethod(methods.First());

// Act
WhenICallTheMiddleware();

// Assert
ThenTheUserIsNotAuthenticated();
_httpContext.User.Identity.IsAuthenticated.ShouldBeFalse();
_logWarningMessages.Count.ShouldBe(2);
_logWarningMessages[0].ShouldStartWith($"Impossible to authenticate client for path '/{nameof(Should_not_call_next_middleware_and_return_no_result_if_providers_keys_are_empty)}':");
_logWarningMessages[1].ShouldStartWith("Client has NOT been authenticated for path");
_httpContext.Items.Errors().Count(e => e.GetType() == typeof(UnauthenticatedError)).ShouldBe(1);
}

private List<string> _logWarningMessages = new();
private readonly List<string> _logWarningMessages = new();

private void GivenTheAuthenticationIsFail()
{
Expand Down Expand Up @@ -263,13 +278,3 @@ private async void WhenICallTheMiddleware()
await _middleware.Invoke(_httpContext);
}
}

public static class StreamExtensions
{
public static string AsString(this Stream stream)
{
using var reader = new StreamReader(stream);
var text = reader.ReadToEnd();
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AuthorizationMiddlewareTests : UnitTest
private readonly Mock<IOcelotLogger> _logger;
private readonly AuthorizationMiddleware _middleware;
private readonly RequestDelegate _next;
private readonly HttpContext _httpContext;
private readonly DefaultHttpContext _httpContext;

public AuthorizationMiddlewareTests()
{
Expand All @@ -34,18 +34,23 @@ public AuthorizationMiddlewareTests()
}

[Fact]
public void should_call_authorization_service()
public async Task Should_call_authorization_service()
{
this.Given(x => x.GivenTheDownStreamRouteIs(new List<PlaceholderNameAndValue>(),
// Arrange
GivenTheDownStreamRouteIs(
new List<PlaceholderNameAndValue>(),
new DownstreamRouteBuilder()
.WithUpstreamPathTemplate(new UpstreamPathTemplateBuilder().Build())
.WithIsAuthorized(true)
.WithUpstreamHttpMethod(new List<string> { "Get" })
.Build()))
.And(x => x.GivenTheAuthServiceReturns(new OkResponse<bool>(true)))
.When(x => x.WhenICallTheMiddleware())
.Then(x => x.ThenTheAuthServiceIsCalledCorrectly())
.BDDfy();
.WithUpstreamHttpMethod(new() { HttpMethods.Get })
.Build());
GivenTheAuthServiceReturns(new OkResponse<bool>(true));

// Act
await WhenICallTheMiddleware();

// Assert
ThenTheAuthServiceIsCalledCorrectly();
}

private async Task WhenICallTheMiddleware()
Expand Down
Loading

0 comments on commit 5c8938f

Please sign in to comment.