Skip to content

fix(core): ensure correct tenant isolation and fallback behavior for managers and mixins#90

Merged
CFBruna merged 20 commits intodevelopfrom
feature/p0-foundation-hardening
Mar 4, 2026
Merged

fix(core): ensure correct tenant isolation and fallback behavior for managers and mixins#90
CFBruna merged 20 commits intodevelopfrom
feature/p0-foundation-hardening

Conversation

@CFBruna
Copy link
Owner

@CFBruna CFBruna commented Mar 4, 2026

What Changed

  • Corrected TenantManager.get_queryset() to fallback to the base queryset instead of qs.none(), fixing breakages outside of request contexts (e.g., Celery, Admin).
  • Updated ClinicScopeMixin to correctly honor the get_active_clinic context, including overriding is_staff when a clinic header is provided, while maintaining cross-clinic access for floating employees.
  • Fixed string evaluation inside an f-string using str() for gettext_lazy translations in financial_service.py.
  • Added new regression tests with 8 distinct isolation/bypass test scenarios to test_tenant_middleware.py.
  • Replaced TenantManager.for_clinic() chain filtering with a direct super().get_queryset() call, preventing empty results when querying across clinics.
  • Renamed and initialized tests directories replacing standalone tests.py conflicts.

Why

Phase 0 Foundation Hardening requires the DB scope filtering to be solid. Returning qs.none() blindly prevented automated background jobs and command lines to operate correctly outside the HTTP flow. At the same time, Admin boundaries must still be strictly functional inside HTTP, so that providing an X-Clinic-ID accurately simulates clinic scopes exactly without hard-rejections.

How to Test

  • Authenticate via make start as an Admin user.
  • Add products pointing to Clinic 1 and Clinic 2.
  • Bypass tests simulating a superuser query through Postman/Curl: curl -s -H "Authorization: Token <TOKEN>" -H "X-Clinic-ID: 2" http://localhost:8000/api/v1/store/products/
  • Run cd backend && uv run pytest ensuring tests yield coverage for all 8 scenarios cleanly within src/apps/core/tests/test_tenant_middleware.py.
  • Utilize for_clinic manager in a terminal DB inspection seeing it retrieve correctly specific clinics without crashing.

Checklist

  • Service layer architecture followed
  • No ORM in views
  • No hardcoded strings (frontend and backend)
  • Translation keys added to en, pt, es
  • Tests added or updated
  • uv run pytest passes
  • npm run build passes
  • Turing Test passed (works with PYG .env)

CFBruna added 20 commits March 3, 2026 23:17
@CFBruna CFBruna merged commit 61dfa82 into develop Mar 4, 2026
1 check passed
@CFBruna CFBruna deleted the feature/p0-foundation-hardening branch March 4, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant