feat(backend): add PostgreSQL schema, RLS policies, and multi-tenant middleware
- Add EF Core migrations for initial schema (clubs, members, work_items, shifts, shift_signups)
- Implement RLS policies with SET LOCAL for tenant isolation
- Add Finbuckle multi-tenant middleware with ClaimStrategy + HeaderStrategy fallback
- Create TenantValidationMiddleware to enforce JWT claims match X-Tenant-Id header
- Add tenant-aware DB interceptors (SaveChangesTenantInterceptor, TenantDbConnectionInterceptor)
- Configure AppDbContext with tenant scoping and RLS support
- Add test infrastructure: CustomWebApplicationFactory, TestAuthHandler, DatabaseFixture
- Write TDD integration tests for multi-tenant isolation and RLS enforcement
- Add health check null safety for connection string
Tasks: 7 (PostgreSQL schema + migrations + RLS), 8 (Finbuckle multi-tenancy + validation), 12 (test infrastructure)
2026-03-03 14:32:21 +01:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Testing;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
|
using Testcontainers.PostgreSql;
|
|
|
|
|
using WorkClub.Infrastructure.Data;
|
|
|
|
|
|
|
|
|
|
namespace WorkClub.Tests.Integration.Infrastructure;
|
|
|
|
|
|
|
|
|
|
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
|
|
|
|
|
{
|
|
|
|
|
private readonly PostgreSqlContainer _postgresContainer = new PostgreSqlBuilder()
|
|
|
|
|
.WithImage("postgres:16-alpine")
|
|
|
|
|
.WithDatabase("workclub_test")
|
|
|
|
|
.WithUsername("test")
|
|
|
|
|
.WithPassword("test")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
// Start container (async wait)
|
|
|
|
|
_postgresContainer.StartAsync().GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
|
|
builder.ConfigureAppConfiguration((context, config) =>
|
|
|
|
|
{
|
|
|
|
|
// Override connection string for tests
|
|
|
|
|
config.AddInMemoryCollection(new Dictionary<string, string?>
|
|
|
|
|
{
|
|
|
|
|
["ConnectionStrings:DefaultConnection"] = _postgresContainer.GetConnectionString()
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.ConfigureServices(services =>
|
|
|
|
|
{
|
|
|
|
|
// Remove existing DbContext registration
|
|
|
|
|
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AppDbContext>));
|
|
|
|
|
if (descriptor != null)
|
|
|
|
|
{
|
|
|
|
|
services.Remove(descriptor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add Testcontainers DbContext
|
|
|
|
|
services.AddDbContext<AppDbContext>(options =>
|
|
|
|
|
options.UseNpgsql(_postgresContainer.GetConnectionString()));
|
|
|
|
|
|
|
|
|
|
// Replace authentication with TestAuthHandler
|
|
|
|
|
services.RemoveAll<IAuthenticationSchemeProvider>();
|
|
|
|
|
services.AddAuthentication(defaultScheme: "Test")
|
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { });
|
|
|
|
|
|
2026-03-06 09:19:32 +01:00
|
|
|
// Build service provider and run migrations
|
feat(backend): add PostgreSQL schema, RLS policies, and multi-tenant middleware
- Add EF Core migrations for initial schema (clubs, members, work_items, shifts, shift_signups)
- Implement RLS policies with SET LOCAL for tenant isolation
- Add Finbuckle multi-tenant middleware with ClaimStrategy + HeaderStrategy fallback
- Create TenantValidationMiddleware to enforce JWT claims match X-Tenant-Id header
- Add tenant-aware DB interceptors (SaveChangesTenantInterceptor, TenantDbConnectionInterceptor)
- Configure AppDbContext with tenant scoping and RLS support
- Add test infrastructure: CustomWebApplicationFactory, TestAuthHandler, DatabaseFixture
- Write TDD integration tests for multi-tenant isolation and RLS enforcement
- Add health check null safety for connection string
Tasks: 7 (PostgreSQL schema + migrations + RLS), 8 (Finbuckle multi-tenancy + validation), 12 (test infrastructure)
2026-03-03 14:32:21 +01:00
|
|
|
var sp = services.BuildServiceProvider();
|
|
|
|
|
using var scope = sp.CreateScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
2026-03-06 09:19:32 +01:00
|
|
|
db.Database.Migrate();
|
|
|
|
|
|
|
|
|
|
using var conn = new Npgsql.NpgsqlConnection(_postgresContainer.GetConnectionString());
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = conn.CreateCommand();
|
|
|
|
|
cmd.CommandText = @"
|
|
|
|
|
DO $$ BEGIN
|
|
|
|
|
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'rls_test_user') THEN
|
|
|
|
|
CREATE USER rls_test_user WITH PASSWORD 'rlspass';
|
|
|
|
|
GRANT CONNECT ON DATABASE workclub_test TO rls_test_user;
|
|
|
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO rls_test_user;
|
|
|
|
|
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO rls_test_user;
|
|
|
|
|
END IF;
|
|
|
|
|
END $$;
|
|
|
|
|
";
|
|
|
|
|
cmd.ExecuteNonQuery();
|
feat(backend): add PostgreSQL schema, RLS policies, and multi-tenant middleware
- Add EF Core migrations for initial schema (clubs, members, work_items, shifts, shift_signups)
- Implement RLS policies with SET LOCAL for tenant isolation
- Add Finbuckle multi-tenant middleware with ClaimStrategy + HeaderStrategy fallback
- Create TenantValidationMiddleware to enforce JWT claims match X-Tenant-Id header
- Add tenant-aware DB interceptors (SaveChangesTenantInterceptor, TenantDbConnectionInterceptor)
- Configure AppDbContext with tenant scoping and RLS support
- Add test infrastructure: CustomWebApplicationFactory, TestAuthHandler, DatabaseFixture
- Write TDD integration tests for multi-tenant isolation and RLS enforcement
- Add health check null safety for connection string
Tasks: 7 (PostgreSQL schema + migrations + RLS), 8 (Finbuckle multi-tenancy + validation), 12 (test infrastructure)
2026-03-03 14:32:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.UseEnvironment("Test");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
await _postgresContainer.DisposeAsync();
|
|
|
|
|
await base.DisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|