Repository navigation
Feature/11 api postgres #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
c2d4584
4be3b27
0059579
b8e802b
d606f29
5f11bd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
|
||
|
|
This file was deleted.
| 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. | ||
| } |
| 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 | ||
| ### |
| 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") | ||
| ?? 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)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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),
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
This file was deleted.
| 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)); | ||
| } |
| 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(); | ||
| } |
There was a problem hiding this comment.
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:Moissonisn't defined anywhere, even thoughcompose.yamlalready sets the dev credentials. Could we add the local dev connection string toappsettings.Development.json?These are local-only Docker credentials, so committing them is fine. User-secrets/env vars can still override them.
There was a problem hiding this comment.
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.