-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from meysamhadeli/develop
- add end-to-end test base
- Loading branch information
Showing
25 changed files
with
202 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/BuildingBlocks/TestBase/EndToEndTest/Auth/AuthServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BuildingBlocks.TestBase.EndToEndTest.Auth; | ||
|
||
//ref: https://blog.joaograssi.com/posts/2021/asp-net-core-testing-permission-protected-api-endpoints/ | ||
public static class AuthServiceCollectionExtensions | ||
{ | ||
public static AuthenticationBuilder AddTestAuthentication( | ||
this IServiceCollection services) | ||
{ | ||
services.AddAuthorization(options => | ||
{ | ||
// AuthConstants.Scheme is just a scheme we define. I called it "TestAuth" | ||
options.DefaultPolicy = new AuthorizationPolicyBuilder("Test") | ||
.RequireAuthenticatedUser() | ||
.Build(); | ||
}); | ||
|
||
// Register our custom authentication handler | ||
return services.AddAuthentication("Test") | ||
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { }); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/BuildingBlocks/TestBase/EndToEndTest/Auth/MockAuthUser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Security.Claims; | ||
|
||
namespace BuildingBlocks.TestBase.EndToEndTest.Auth; | ||
|
||
public class MockAuthUser | ||
{ | ||
public List<Claim> Claims { get; } | ||
public MockAuthUser(params Claim[] claims) => Claims = claims.ToList(); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/BuildingBlocks/TestBase/EndToEndTest/Auth/TestAuthHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace BuildingBlocks.TestBase.EndToEndTest.Auth; | ||
|
||
//ref: https://blog.joaograssi.com/posts/2021/asp-net-core-testing-permission-protected-api-endpoints/ | ||
public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions> | ||
{ | ||
private readonly MockAuthUser _mockAuthUser; | ||
|
||
public TestAuthHandler( | ||
IOptionsMonitor<AuthenticationSchemeOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder, | ||
ISystemClock clock, | ||
MockAuthUser mockAuthUser) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
// 1. We get a "mock" user instance here via DI. | ||
// we'll see how this work later, don't worry | ||
_mockAuthUser = mockAuthUser; | ||
} | ||
|
||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (_mockAuthUser.Claims.Count == 0) | ||
return Task.FromResult(AuthenticateResult.Fail("Mock auth user not configured.")); | ||
|
||
// 2. Create the principal and the ticket | ||
var identity = new ClaimsIdentity(_mockAuthUser.Claims, "Test"); | ||
var principal = new ClaimsPrincipal(identity); | ||
var ticket = new AuthenticationTicket(principal, "Test"); | ||
|
||
// 3. Authenticate the request | ||
var result = AuthenticateResult.Success(ticket); | ||
return Task.FromResult(result); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/BuildingBlocks/TestBase/EndToEndTest/EndToEndTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Net.Http.Json; | ||
using BuildingBlocks.Mongo; | ||
using BuildingBlocks.TestBase.IntegrationTest; | ||
using BuildingBlocks.Web; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit.Abstractions; | ||
|
||
namespace BuildingBlocks.TestBase.EndToEndTest; | ||
|
||
public class EndToEndTestBase<TEntryPoint, TWContext, TRContext> : | ||
IntegrationTestBase<TEntryPoint, TWContext, TRContext> | ||
where TWContext : DbContext | ||
where TRContext : MongoDbContext | ||
where TEntryPoint : class | ||
{ | ||
public EndToEndTestBase( | ||
IntegrationTestFactory<TEntryPoint, TWContext, TRContext> sharedFixture, | ||
ITestOutputHelper outputHelper = null) | ||
: base(sharedFixture, outputHelper) | ||
{ | ||
} | ||
|
||
public async Task<TResponse> GetAsync<TResponse>(string requestUrl, CancellationToken cancellationToken = default) | ||
{ | ||
return await Fixture.HttpClient.GetFromJsonAsync<TResponse>(requestUrl, cancellationToken: cancellationToken); | ||
} | ||
|
||
public async Task<TResponse> PostAsync<TRequest, TResponse>(string requestUrl, TRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await Fixture.HttpClient.PostAsJsonAsync<TRequest, TResponse>(requestUrl, request, cancellationToken); | ||
} | ||
|
||
public async Task<TResponse> PutAsync<TRequest, TResponse>( | ||
string requestUrl, | ||
TRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await Fixture.HttpClient.PutAsJsonAsync<TRequest, TResponse>(requestUrl, request, cancellationToken); | ||
} | ||
|
||
public async Task Delete(string requestUrl, CancellationToken cancellationToken = default) | ||
{ | ||
await Fixture.HttpClient.DeleteAsync(requestUrl, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
using Ardalis.GuardClauses; | ||
using System.Security.Claims; | ||
using Ardalis.GuardClauses; | ||
using BuildingBlocks.Core.Event; | ||
using BuildingBlocks.Core.Model; | ||
using BuildingBlocks.EFCore; | ||
using BuildingBlocks.MassTransit; | ||
using BuildingBlocks.Mongo; | ||
using BuildingBlocks.PersistMessageProcessor; | ||
using BuildingBlocks.TestBase.EndToEndTest.Auth; | ||
using BuildingBlocks.Web; | ||
using DotNet.Testcontainers.Containers; | ||
using EasyNetQ.Management.Client; | ||
|
@@ -27,8 +29,9 @@ | |
using Serilog; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using ILogger = Serilog.ILogger; | ||
|
||
namespace BuildingBlocks.TestBase; | ||
namespace BuildingBlocks.TestBase.IntegrationTest; | ||
|
||
public class IntegrationTestFactory<TEntryPoint> : IAsyncLifetime | ||
where TEntryPoint : class | ||
|
@@ -43,9 +46,7 @@ public class IntegrationTestFactory<TEntryPoint> : IAsyncLifetime | |
|
||
private ITestHarness TestHarness => ServiceProvider?.GetTestHarness(); | ||
public HttpClient HttpClient => _factory?.CreateClient(); | ||
|
||
public GrpcChannel Channel => GrpcChannel.ForAddress(HttpClient.BaseAddress!, new GrpcChannelOptions { HttpClient = HttpClient }); | ||
|
||
public Action<IServiceCollection> TestRegistrationServices { get; set; } | ||
public IServiceProvider ServiceProvider => _factory?.Services; | ||
public IConfiguration Configuration => _factory?.Services.GetRequiredService<IConfiguration>(); | ||
|
@@ -63,6 +64,11 @@ public IntegrationTestFactory() | |
{ | ||
TestRegistrationServices?.Invoke(services); | ||
services.ReplaceSingleton(AddHttpContextAccessorMock); | ||
// Add our custom handler | ||
services.AddTestAuthentication(); | ||
|
||
// Register a default user, so all requests have it by default | ||
services.AddScoped(_ => GetMockUser()); | ||
}); | ||
}); | ||
} | ||
|
@@ -255,6 +261,10 @@ private IHttpContextAccessor AddHttpContextAccessorMock(IServiceProvider service | |
|
||
return httpContextAccessorMock; | ||
} | ||
|
||
private MockAuthUser GetMockUser() => | ||
new MockAuthUser(new Claim("sub", Guid.NewGuid().ToString()), | ||
new Claim("email", "[email protected]")); | ||
} | ||
|
||
public class IntegrationTestFactory<TEntryPoint, TWContext> : IntegrationTestFactory<TEntryPoint> | ||
|
@@ -514,4 +524,3 @@ protected IntegrationTestBase( | |
|
||
public IntegrationTestFactory<TEntryPoint, TWContext, TRContext> Fixture { get; } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Net.Http.Json; | ||
|
||
namespace BuildingBlocks.Web; | ||
|
||
public static class HttpClientExtensions | ||
{ | ||
public static async Task<TResponse> | ||
PostAsJsonAsync<TRequest, TResponse>( | ||
this HttpClient httpClient, | ||
string requestUri, | ||
TRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var responseMessage = | ||
await httpClient.PostAsJsonAsync(requestUri, request, cancellationToken: cancellationToken); | ||
|
||
var result = await responseMessage.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken); | ||
|
||
return result; | ||
} | ||
|
||
public static async Task<TResponse?> | ||
PutAsJsonAsync<TRequest, TResponse>( | ||
this HttpClient httpClient, | ||
string requestUri, | ||
TRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var responseMessage = | ||
await httpClient.PutAsJsonAsync(requestUri, request, cancellationToken: cancellationToken); | ||
|
||
var result = await responseMessage.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken); | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Booking/tests/IntegrationTest/BookingIntegrationTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Aircraft/Features/CreateAircraftTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Airport/Features/CreateAirportTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Flight/Features/CreateFlightTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Flight/Features/DeleteFlightTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Flight/Features/GetAvailableFlightsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Flight/Features/GetFlightByIdTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Flight/Features/UpdateFlightTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/FlightIntegrationTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Seat/Features/GetAvailableSeatsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Flight/tests/IntegrationTest/Seat/Features/ReserveSeatTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Identity/tests/IntegrationTest/Identity/Features/RegisterNewUserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Services/Identity/tests/IntegrationTest/IdentityIntegrationTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ices/Passenger/tests/IntegrationTest/Passenger/Features/CompleteRegisterPassengerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.