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 Dapper;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using Testcontainers.PostgreSql;
|
|
|
|
|
using WorkClub.Infrastructure.Data;
|
|
|
|
|
|
|
|
|
|
namespace WorkClub.Tests.Integration.Data;
|
|
|
|
|
|
|
|
|
|
public class MigrationTests : IAsyncLifetime
|
|
|
|
|
{
|
|
|
|
|
private PostgreSqlContainer? _container;
|
|
|
|
|
private string? _connectionString;
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync()
|
|
|
|
|
{
|
|
|
|
|
_container = new PostgreSqlBuilder()
|
|
|
|
|
.WithImage("postgres:16-alpine")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
await _container.StartAsync();
|
|
|
|
|
_connectionString = _container.GetConnectionString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_container != null)
|
|
|
|
|
{
|
|
|
|
|
await _container.DisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Migration_AppliesSuccessfully_CreatesAllTables()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
|
|
|
.UseNpgsql(_connectionString)
|
|
|
|
|
.Options;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
await using var context = new AppDbContext(options);
|
|
|
|
|
await context.Database.MigrateAsync();
|
|
|
|
|
|
|
|
|
|
// Assert - verify all expected tables exist
|
|
|
|
|
await using var connection = new NpgsqlConnection(_connectionString);
|
|
|
|
|
var tables = (await connection.QueryAsync<string>(
|
|
|
|
|
@"SELECT table_name
|
|
|
|
|
FROM information_schema.tables
|
|
|
|
|
WHERE table_schema = 'public'
|
|
|
|
|
ORDER BY table_name")).ToList();
|
|
|
|
|
|
|
|
|
|
Assert.Contains("clubs", tables);
|
|
|
|
|
Assert.Contains("members", tables);
|
|
|
|
|
Assert.Contains("work_items", tables);
|
|
|
|
|
Assert.Contains("shifts", tables);
|
|
|
|
|
Assert.Contains("shift_signups", tables);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Migration_CreatesCorrectIndexes()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
|
|
|
.UseNpgsql(_connectionString)
|
|
|
|
|
.Options;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
await using var context = new AppDbContext(options);
|
|
|
|
|
await context.Database.MigrateAsync();
|
|
|
|
|
|
|
|
|
|
// Assert - verify critical indexes exist
|
|
|
|
|
await using var connection = new NpgsqlConnection(_connectionString);
|
|
|
|
|
var indexes = (await connection.QueryAsync<string>(
|
|
|
|
|
@"SELECT indexname
|
|
|
|
|
FROM pg_indexes
|
|
|
|
|
WHERE schemaname = 'public'
|
|
|
|
|
ORDER BY indexname")).ToList();
|
|
|
|
|
|
|
|
|
|
// TenantId indexes
|
|
|
|
|
Assert.Contains(indexes, i => i.Contains("tenant_id"));
|
2026-03-05 11:07:19 +01:00
|
|
|
|
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
|
|
|
// ClubId indexes
|
|
|
|
|
Assert.Contains(indexes, i => i.Contains("club_id"));
|
2026-03-05 11:07:19 +01:00
|
|
|
|
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
|
|
|
// Status indexes for WorkItem
|
|
|
|
|
Assert.Contains(indexes, i => i.Contains("status"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-03-06 09:19:32 +01:00
|
|
|
public async Task Migration_CreatesExpectedSchema()
|
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
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
|
|
|
.UseNpgsql(_connectionString)
|
|
|
|
|
.Options;
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
await using var context = new AppDbContext(options);
|
|
|
|
|
await context.Database.MigrateAsync();
|
|
|
|
|
|
2026-03-06 09:19:32 +01:00
|
|
|
// Assert - verify schema integrity (RLS and policies are applied via SeedDataService, not 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
|
|
|
await using var connection = new NpgsqlConnection(_connectionString);
|
2026-03-06 09:19:32 +01:00
|
|
|
|
|
|
|
|
// Verify tenant_id columns exist on all tables
|
|
|
|
|
var tenantColumns = (await connection.QueryAsync<string>(
|
|
|
|
|
@"SELECT table_name
|
|
|
|
|
FROM information_schema.columns
|
|
|
|
|
WHERE table_schema = 'public'
|
|
|
|
|
AND column_name = 'TenantId'
|
|
|
|
|
ORDER BY table_name")).ToList();
|
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
|
|
|
|
2026-03-06 09:19:32 +01:00
|
|
|
Assert.Contains("clubs", tenantColumns);
|
|
|
|
|
Assert.Contains("members", tenantColumns);
|
|
|
|
|
Assert.Contains("work_items", tenantColumns);
|
|
|
|
|
Assert.Contains("shifts", tenantColumns);
|
|
|
|
|
Assert.Contains("shift_signups", tenantColumns);
|
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
|
|
|
}
|
|
|
|
|
}
|