-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests to Internal API. (#618)
I didn't get time to get auth working but we can still add least add tests for the anonymous routes and do auth'd routes later in a separate PR. I also tried out [FluentAssertions](https://fluentassertions.com/) which I'm liking a lot so I added it to the test project global usings.
- Loading branch information
1 parent
834cfdb
commit 02f4fe6
Showing
12 changed files
with
172 additions
and
26 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using FastEndpoints.Testing; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Aquifer.API.IntegrationTests; | ||
|
||
/// <summary> | ||
/// This is a FastEndpoints <see cref="App"/> that sits on top of a <see cref="WebApplicationFactory{TEntryPoint}"/> | ||
/// which allows for in-memory web requests to be sent and received without actual network traffic. | ||
/// Details: | ||
/// * https://fast-endpoints.com/docs/integration-unit-testing | ||
/// * https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests | ||
/// </summary> | ||
public sealed class App : AppFixture<Program> | ||
{ | ||
/// <summary> | ||
/// The app is configured in <see cref="Program"/> before this method is called. | ||
/// Only use this method to override or extend existing host configuration. | ||
/// </summary> | ||
protected override void ConfigureApp(IWebHostBuilder builder) | ||
{ | ||
// The environment must be explicitly set as it will not default to "Development". | ||
// On the build server this environment variable should be explicitly populated. | ||
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))) | ||
{ | ||
builder.UseEnvironment(Environments.Development); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The service registrations in <see cref="Program"/> run before this method is called. | ||
/// Only use this method to add additional services or to override existing registrations from the API. | ||
/// </summary> | ||
protected override void ConfigureServices(IServiceCollection services) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Configure Clients here. | ||
/// The default Client is anonymous and has no API Key header value. | ||
/// This is sufficient for anonymous Internal API tests because no actual web requests are sent via this fixture. | ||
/// The API key is enforced by Azure API Management when proxying requests, and thus it is not needed locally. | ||
/// Various authenticated client with different roles/permissions are also needed. | ||
/// </summary> | ||
/// <remarks> | ||
/// Example of adding a default header to the existing client: | ||
/// <example> | ||
/// <code> | ||
/// Client.DefaultRequestHeaders.Add("api-key", "TODO"); | ||
/// </code> | ||
/// </example> | ||
/// An entirely new client could be defined in this class and set up in this method as well (e.g. an authenticated client). | ||
/// </remarks> | ||
protected override Task SetupAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Use this method to dispose of any <see cref="HttpClient"/>s created in <see cref="SetupAsync"/>. | ||
/// </summary> | ||
protected override Task TearDownAsync() | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Aquifer.API.IntegrationTests/Aquifer.API.IntegrationTests.csproj
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FastEndpoints.Testing" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Aquifer.API\Aquifer.API.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
tests/Aquifer.API.IntegrationTests/Endpoints/Bibles/Books/List/EndpointTests.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,19 @@ | ||
using FastEndpoints; | ||
using System.Net; | ||
using Aquifer.API.Endpoints.BibleBooks.List; | ||
using FastEndpoints.Testing; | ||
|
||
namespace Aquifer.API.IntegrationTests.Endpoints.Bibles.Books.List; | ||
|
||
public sealed class EndpointTests(App _app) : TestBase<App> | ||
{ | ||
[Fact] | ||
public async Task ValidRequest_ShouldReturnSuccess() | ||
{ | ||
var (response, result) = await _app.Client.GETAsync<Endpoint, IReadOnlyList<Response>>(); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
result.Should().HaveCountGreaterThan(80); | ||
result.Should().Contain(r => r.Code == "4MA"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Aquifer.API.IntegrationTests/Endpoints/Resources/Content/Get/EndpointTests.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,21 @@ | ||
using System.Net; | ||
using Aquifer.API.Endpoints.Resources.Content.Get; | ||
using FastEndpoints; | ||
using FastEndpoints.Testing; | ||
|
||
namespace Aquifer.API.IntegrationTests.Endpoints.Resources.Content.Get; | ||
|
||
public sealed class EndpointTests(App _app) : TestBase<App> | ||
{ | ||
[Fact] | ||
public async Task UnauthenticatedRequest_ShouldReturnUnauthorized() | ||
{ | ||
var (response, _) = await _app.Client.GETAsync<Endpoint, Request, ErrorResponse>( | ||
new Request | ||
{ | ||
Id = 1890, | ||
}); | ||
|
||
response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); | ||
} | ||
} |
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
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