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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# moisson-project
Windows desktop application for accurate donation valuation and tax receipt generation for Moisson Montréal (Montréal, QC).
## Running tests

```bash
dotnet test # all tests (Docker must be running)
dotnet test --filter "Category!=Integration" # unit tests only, no Docker needed
```

Integration tests use Testcontainers to start a disposable PostgreSQL 17 container, so Docker Desktop (or Docker Engine) must be running.


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
###
21 changes: 20 additions & 1 deletion 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done, added the local dev connection string to appsettings.Development.json. A fresh clone now runs with docker compose up -d + dotnet run, and user-secrets/env vars can still override it.

?? 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point, thanks. With the dev connection string now in appsettings.Development.json, dotnet ef finds it locally. I'll keep IDesignTimeDbContextFactory in mind for the migrations issue if CI needs it.


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

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
Expand All @@ -14,10 +28,15 @@
app.MapOpenApi();
}

app.UseHttpsRedirection();
// Health probes call /health over plain HTTP, so it must not be redirected.
app.UseWhen(
context => !context.Request.Path.StartsWithSegments("/health"),
branch => branch.UseHttpsRedirection());

app.UseAuthorization();

app.MapControllers();

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

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).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done, /health is now excluded from the HTTPS redirect via UseWhen, so HTTP probes get the real status instead of a 307. Other routes are still redirected.


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

This file was deleted.

3 changes: 3 additions & 0 deletions src/MoissonMontreal.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"Moisson": "Host=localhost;Port=5432;Database=moisson_dev;Username=moisson;Password=moisson"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
40 changes: 40 additions & 0 deletions tests/MoissonMontreal.Tests/Api/HealthEndpointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using MoissonMontreal.Tests.Fixtures;

namespace MoissonMontreal.Tests.Api;

[Trait("Category", "Integration")]
public class HealthEndpointTests(PostgresFixture postgres) : IClassFixture<PostgresFixture>
{
[Fact]
public async Task Health_ReturnsHealthy_WhenDatabaseIsReachable()
{
await using var factory = CreateFactory(postgres.ConnectionString);
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_ReturnsUnhealthy_WhenDatabaseIsUnreachable()
{
// Nothing listens on port 1, so the connection is refused immediately.
await using var factory = CreateFactory("Host=127.0.0.1;Port=1;Database=unreachable;Username=test;Timeout=2");
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> CreateFactory(string connectionString) =>
new WebApplicationFactory<Program>().WithWebHostBuilder(builder => builder
.UseEnvironment("Testing")
.UseSetting("ConnectionStrings:Moisson", connectionString));
}
17 changes: 17 additions & 0 deletions tests/MoissonMontreal.Tests/Fixtures/PostgresFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Testcontainers.PostgreSql;

namespace MoissonMontreal.Tests.Fixtures;

/// <summary>
/// Starts a single PostgreSQL container shared by every test of a class.
/// </summary>
public class PostgresFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer _container = new PostgreSqlBuilder("postgres:17").Build();

public string ConnectionString => _container.GetConnectionString();

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

public Task DisposeAsync() => _container.DisposeAsync().AsTask();
}
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