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)
This commit is contained in:
WorkClub Automation
2026-03-03 14:32:21 +01:00
parent b9edbb8a65
commit 28964c6767
35 changed files with 4006 additions and 5 deletions

View File

@@ -1416,3 +1416,116 @@ Total: 5 files, 171 lines of test infrastructure code
- **Authorization Tests**: ✅ Code is valid, cannot execute (Infrastructure errors)
- **Health Checks Configuration**: ✅ Syntax correct, cannot test (app won't start)
---
## Task 12 Continuation: Resolving Pre-existing Build Errors (2026-03-03)
### Issue Discovery
When attempting to run the smoke test for Task 12, encountered build errors in `WorkClub.Api/Program.cs`:
```
error CS0246: Der Typ- oder Namespacename "TenantInfo" wurde nicht gefunden
error CS1061: "IServiceCollection" enthält keine Definition für "AddMultiTenant"
error CS1061: "WebApplication" enthält keine Definition für "UseMultiTenant"
```
### Root Cause Analysis
1. **Missing Package Reference**:
- `WorkClub.Api.csproj` had `Finbuckle.MultiTenant.AspNetCore` (version 10.0.3)
- Missing `Finbuckle.MultiTenant` base package (required for `TenantInfo` type)
- Infrastructure project had both packages, API project incomplete
2. **Package Dependency Chain**:
- `Finbuckle.MultiTenant.AspNetCore` depends on `Finbuckle.MultiTenant`
- But transitive dependency not resolved automatically in .NET 10
- Explicit reference required for types used directly in code
### Resolution
**Added missing package**:
```bash
cd backend/WorkClub.Api
dotnet add package Finbuckle.MultiTenant --version 10.0.3
```
**WorkClub.Api.csproj now includes**:
```xml
<PackageReference Include="Finbuckle.MultiTenant" Version="10.0.3" />
<PackageReference Include="Finbuckle.MultiTenant.AspNetCore" Version="10.0.3" />
```
### Pre-existing vs Task-Specific Issues
**NOT caused by Task 12**:
- Program.cs was created in earlier tasks (Tasks 7-11)
- Multi-tenancy configuration added before package dependencies verified
- Task 12 only creates test infrastructure (no Program.cs modifications)
**Discovered during Task 12**:
- Smoke test execution requires full API project build
- Build errors prevented test verification
- Fixed proactively to unblock smoke test
### Dependency Resolution Pattern
**Lesson learned**: When using types from NuGet packages directly in code:
1. Check if type is in transitive dependency (may not auto-resolve)
2. Add explicit `<PackageReference>` for packages you directly use
3. Verify build after adding multi-tenant or complex package chains
**Finbuckle.MultiTenant package structure**:
- `Finbuckle.MultiTenant` → Core types (TenantInfo, ITenant, etc.)
- `Finbuckle.MultiTenant.AspNetCore` → ASP.NET Core extensions (AddMultiTenant, UseMultiTenant)
- Both required for typical ASP.NET Core integration
### Build Verification After Fix
**Command**: `dotnet build WorkClub.Api/WorkClub.Api.csproj`
**Result**: ✅ SUCCESS
- All projects compile successfully
- TenantInfo type resolved
- AddMultiTenant extension method found
- UseMultiTenant extension method found
- Only warnings: EF Core version conflicts (10.0.0 vs 10.0.3) → non-breaking
**Warnings present** (acceptable):
```
warning MSB3277: Konflikte zwischen verschiedenen Versionen von "Microsoft.EntityFrameworkCore.Relational"
```
- Infrastructure uses EF Core 10.0.3 (from Npgsql.EntityFrameworkCore.PostgreSQL)
- API has transitive dependency on 10.0.0
- Build system resolves to 10.0.3 (higher version wins)
- No breaking changes between 10.0.0 and 10.0.3
### Impact on Task 12 Status
**Before fix**:
- Test infrastructure code: ✅ Complete (5 files, 171 lines)
- Smoke test: ❌ Cannot run (API build fails)
- Task 12 deliverable: Blocked
**After fix**:
- Test infrastructure code: ✅ Complete
- API project build: ✅ Success
- Smoke test: Ready to execute
- Task 12 deliverable: Unblocked
### Gotchas for Future Tasks
1. **Don't assume transitive dependencies**: Always add explicit `<PackageReference>` for types you use
2. **Multi-tenant packages need both**: Base + AspNetCore packages for full functionality
3. **Test early**: Build errors surface faster when running tests immediately after implementation
4. **Version alignment**: Use same version across package family (e.g., all Finbuckle packages at 10.0.3)
### Evidence
- Build output before fix: Pre-existing errors in Program.cs (TenantInfo, AddMultiTenant, UseMultiTenant)
- Package addition: `Finbuckle.MultiTenant` 10.0.3 added to WorkClub.Api.csproj
- Build output after fix: Successful compilation, only version conflict warnings
---