Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Elzik.Breef.Api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ RUN dotnet publish "./Elzik.Breef.Api.csproj" -c "$BUILD_CONFIGURATION" -o /app/
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_ENVIRONMENT=Production
ENTRYPOINT ["dotnet", "Elzik.Breef.Api.dll"]
16 changes: 15 additions & 1 deletion src/Elzik.Breef.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

builder.Services.AddProblemDetails();

builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.AllowAnyOrigin()

Check warning on line 54 in src/Elzik.Breef.Api/Program.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

Make sure this permissive CORS policy is safe here. (https://rules.sonarsource.com/csharp/RSPEC-5122)

Check warning on line 54 in src/Elzik.Breef.Api/Program.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

Make sure this permissive CORS policy is safe here. (https://rules.sonarsource.com/csharp/RSPEC-5122)

Check warning on line 54 in src/Elzik.Breef.Api/Program.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

Make sure this permissive CORS policy is safe here. (https://rules.sonarsource.com/csharp/RSPEC-5122)

Check warning on line 54 in src/Elzik.Breef.Api/Program.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu

Make sure this permissive CORS policy is safe here. (https://rules.sonarsource.com/csharp/RSPEC-5122)
.AllowAnyMethod()
.AllowAnyHeader();
});
Expand Down Expand Up @@ -137,11 +139,23 @@
builder.Services.AddTransient<IBreefGenerator, BreefGenerator>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler();
}

app.UseStatusCodePages();

app.UseCors();
app.UseAuth();

app.MapGet("/health", () => Results.Ok(new { status = "Healthy" }))
.AllowAnonymous();
.AllowAnonymous();

app.AddBreefEndpoints();

Expand Down
8 changes: 8 additions & 0 deletions src/Elzik.Breef.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5079"
},
"Local (Production)": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5079"
},
"Docker": {
"commandName": "Docker",
"environmentVariables": {
Expand Down
10 changes: 9 additions & 1 deletion tests/Elzik.Breef.Api.Tests.Functional/BreefTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Elzik.Breef.Api.Presentation;
using Elzik.Breef.Infrastructure.Wallabag;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Shouldly;
using System.Net;
Expand Down Expand Up @@ -78,8 +79,15 @@ public async Task Unauthorised()
challenge.Scheme.ShouldBe("ApiKey");
challenge.Parameter.ShouldNotBeNullOrEmpty();
challenge.Parameter.ShouldContain("BREEF-API-KEY");

var responseString = await response.Content.ReadAsStringAsync();
responseString.ShouldBeEmpty();
responseString.ShouldNotBeNullOrEmpty();
var problemDetails = JsonSerializer
.Deserialize<ProblemDetails>(responseString, JsonSerializerOptions);
problemDetails.ShouldNotBeNull();
problemDetails.Status.ShouldBe(401);
problemDetails.Title.ShouldBe("Unauthorized");
problemDetails.Type.ShouldNotBeNullOrWhiteSpace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;

namespace Elzik.Breef.Api.Tests.Functional;

public class DevelopmentWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
}
}
44 changes: 37 additions & 7 deletions tests/Elzik.Breef.Api.Tests.Functional/HealthTestsNative.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Shouldly;
using System.Net;
using System.Net.Http.Json;
using Xunit.Abstractions;

namespace Elzik.Breef.Api.Tests.Functional;

public class HealthTestsNative : HealthTestsBase, IClassFixture<WebApplicationFactory<Program>>
public class HealthTestsNative
{
private readonly WebApplicationFactory<Program> _webApplicationFactory;
private readonly HttpClient _client;
private readonly ITestOutputHelper _output;

protected override HttpClient Client => _client;
public HealthTestsNative(ITestOutputHelper output)
{
_output = output;
}

public static IEnumerable<object[]> WebApplicationFactories()
{
yield return new object[] { new DevelopmentWebApplicationFactory(), "Development" };
yield return new object[] { new ProductionWebApplicationFactory(), "Production" };
}

[Theory]
[MemberData(nameof(WebApplicationFactories))]
public async Task Health_Called_ReturnsOK(WebApplicationFactory<Program> factory, string environmentName)
{
// Arrange
_output.WriteLine($"Testing health endpoint in {environmentName} environment");
using var client = factory.CreateClient();
const string baseUrl = "http://localhost";

// Act
var response = await client.GetAsync($"{baseUrl}/health");

// Assert
response.StatusCode.ShouldBe(HttpStatusCode.OK);
var body = await response.Content.ReadFromJsonAsync<HealthResponse>();
body.ShouldNotBeNull();
body!.Status.ShouldBe("Healthy");
}

public HealthTestsNative(WebApplicationFactory<Program> webAppFactory)
private class HealthResponse
{
_webApplicationFactory = webAppFactory;
_client = _webApplicationFactory.CreateClient();
public string Status { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;

namespace Elzik.Breef.Api.Tests.Functional;

public class ProductionWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Production");
}
}