Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 0 additions & 25 deletions src/MoissonMontreal.Api/Controllers/WeatherForecastController.cs

This file was deleted.

8 changes: 8 additions & 0 deletions src/MoissonMontreal.Api/Data/MoissonDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.EntityFrameworkCore;

namespace MoissonMontreal.Api.Data;

public class MoissonDbContext(DbContextOptions<MoissonDbContext> options) : DbContext(options)
{
// Les tables (DbSet<Don>, DbSet<Recu>, ...) seront ajoutées avec l'issue du schéma.
}
4 changes: 3 additions & 1 deletion src/MoissonMontreal.Api/MoissonMontreal.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>61115b38-e487-4411-983f-5d85d34daf9f</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,6 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="10.0.13" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
</ItemGroup>

Expand Down
4 changes: 1 addition & 3 deletions src/MoissonMontreal.Api/MoissonMontreal.Api.http
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@MoissonMontreal.Api_HostAddress = http://localhost:5297

GET {{MoissonMontreal.Api_HostAddress}}/weatherforecast/
Accept: application/json

GET {{MoissonMontreal.Api_HostAddress}}/health
###
16 changes: 16 additions & 0 deletions src/MoissonMontreal.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
using Microsoft.EntityFrameworkCore;
using MoissonMontreal.Api.Data;


var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("Moisson")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cloning the repo causes a crash on dotnet run: ConnectionStrings:Moisson isn't defined anywhere, even though compose.yaml already sets the dev credentials. Could we add the local dev connection string to appsettings.Development.json?

"ConnectionStrings": {
  "Moisson": "Host=localhost;Port=5432;Database=moisson_dev;Username=moisson;Password=moisson"
}

These are local-only Docker credentials, so committing them is fine. User-secrets/env vars can still override them.

?? throw new InvalidOperationException(
"La chaîne de connexion 'Moisson' est absente. Définissez ConnectionStrings__Moisson " +
"ou utilisez 'dotnet user-secrets'.");


// Add services to the container.
builder.Services.AddDbContext<MoissonDbContext>(options => options.UseNpgsql(connectionString));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads-up for the migrations issue: because the connection string is read and validated at startup (line 7), dotnet ef migrations add will throw on any machine/CI without the secret, since EF runs Program to build the DbContext. Fixing the dev config (comment above) mostly solves it. Otherwise, add an IDesignTimeDbContextFactory.


builder.Services.AddHealthChecks()
.AddDbContextCheck<MoissonDbContext>("database");

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
Expand All @@ -20,4 +34,6 @@

app.MapControllers();

app.MapHealthChecks("/health");
Comment on lines 14 to +37

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MVC configuration is currently pointless, but we will most likely add controllers soon. We can keep them for now

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/health sits behind UseHttpsRedirection(), so an HTTP health probe (Docker, Kubernetes, load balancer) gets a 307 instead of the status and may mark the service as down. Not a problem today, but worth keeping in mind when we deploy (e.g. skip the redirect for /health).


app.Run();
12 changes: 0 additions & 12 deletions src/MoissonMontreal.Api/WeatherForecast.cs

This file was deleted.

45 changes: 45 additions & 0 deletions tests/MoissonMontreal.Tests/Api/HealthEndpointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Testcontainers.PostgreSql;

namespace MoissonMontreal.Tests.Api;

public class HealthEndpointTests : IAsyncLifetime

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests require Docker to be running, otherwise dotnet test fails with DockerUnavailableException. Could we tag them (e.g. [Trait("Category", "Integration")]) so they can be filtered, and mention the Docker requirement in the README?

{
private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder("postgres:17").Build();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xUnit creates a new instance of the class per test, so this starts a new Postgres container for each test (including the "unreachable" one, which doesn't use it). Moving the container into a shared IClassFixture<PostgresFixture> would start it only once and speed up the suite.


public Task InitializeAsync() => _postgres.StartAsync();

public Task DisposeAsync() => _postgres.DisposeAsync().AsTask();

[Fact]
public async Task Health_RetourneHealthy_QuandLaBaseRepond()
{
await using var factory = CreerFactory(_postgres.GetConnectionString());
using var client = factory.CreateClient();

var response = await client.GetAsync("/health");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task Health_RetourneUnhealthy_QuandLaBaseEstInjoignable()
{
// Rien n'écoute sur le port 1 : la connexion échoue immédiatement.
await using var factory = CreerFactory("Host=localhost;Port=1;Database=inexistante;Username=test;Timeout=2");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: localhost can resolve to both ::1 and 127.0.0.1, and some CI firewalls drop packets instead of refusing them, so this can wait for the timeout twice instead of failing immediately. Using Host=127.0.0.1 makes it more predictable.

using var client = factory.CreateClient();

var response = await client.GetAsync("/health");

Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
Assert.Equal("Unhealthy", await response.Content.ReadAsStringAsync());
}

private static WebApplicationFactory<Program> CreerFactory(string connectionString) =>
new WebApplicationFactory<Program>().WithWebHostBuilder(builder => builder
.UseEnvironment("Testing")
.UseSetting("ConnectionStrings:Moisson", connectionString));
}
2 changes: 2 additions & 0 deletions tests/MoissonMontreal.Tests/MoissonMontreal.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.16.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
Expand Down
Loading