Loading...
Loading...
Testing patterns for Prowler API: JSON:API, Celery tasks, RLS isolation, RBAC. Trigger: When writing tests for api/ (JSON:API requests/assertions, cross-tenant isolation, RBAC, Celery tasks, viewsets/serializers).
npx skill4agent add prowler-cloud/prowler prowler-test-apiresponse.json()["data"]response.datacontent_type = "application/vnd.api+json"format="vnd.api+json".delay()Task.objects.getcreate_test_user (session) ─► tenants_fixture (function) ─► authenticated_client
│
└─► providers_fixture ─► scans_fixture ─► findings_fixture| Fixture | Description |
|---|---|
| Session user ( |
| 3 tenants: [0],[1] have membership, [2] isolated |
| JWT client for tenant[0] |
| 9 providers in tenant[0] |
| 2 Celery tasks with TaskResult |
| Fixture | Permissions |
|---|---|
| All permissions (admin) |
| Membership but NO roles |
| All permissions = False |
response = client.post(
reverse("provider-list"),
data={"data": {"type": "providers", "attributes": {...}}},
format="vnd.api+json", # NOT content_type!
)response = client.patch(
reverse("provider-detail", kwargs={"pk": provider.id}),
data={"data": {"type": "providers", "id": str(provider.id), "attributes": {...}}},
content_type="application/vnd.api+json", # NOT format!
)data = response.json()["data"]
attrs = data["attributes"]
errors = response.json()["errors"] # For 400 responsesdef test_cross_tenant_access_denied(self, authenticated_client, tenants_fixture):
other_tenant = tenants_fixture[2] # Isolated tenant
foreign_provider = Provider.objects.create(tenant_id=other_tenant.id, ...)
response = authenticated_client.get(reverse("provider-detail", args=[foreign_provider.id]))
assert response.status_code == status.HTTP_404_NOT_FOUND # NOT 403!| Strategy | Use For |
|---|---|
Mock | Testing views that trigger tasks |
| Synchronous task logic testing |
Mock | Testing Canvas orchestration |
Mock | Testing |
Mock | Testing Beat scheduled tasks |
task_always_eager| Problem | Impact |
|---|---|
| No task serialization | Misses argument type errors |
| No broker interaction | Hides connection issues |
| Different execution context | |
task.apply()Full examples: See assets/api_test.py for,TestCeleryTaskLogic,TestCeleryCanvas,TestSetTenantDecorator.TestBeatScheduling
# BAD - TruffleHog flags these:
api_key = "sk-test1234567890T3BlbkFJtest1234567890"
# GOOD - obviously fake:
api_key = "sk-fake-test-key-for-unit-testing-only"| Scenario | Code |
|---|---|
| Successful GET | 200 |
| Successful POST | 201 |
| Async operation (DELETE/scan trigger) | 202 |
| Sync DELETE | 204 |
| Validation error | 400 |
| Missing permission (RBAC) | 403 |
| RLS isolation / not found | 404 |
cd api && poetry run pytest -x --tb=short
cd api && poetry run pytest -k "test_provider"
cd api && poetry run pytest api/src/backend/api/tests/test_rbac.pyapi/src/backend/conftest.py