API Integration Testing
Purpose
Prove the HTTP API behaves correctly end-to-end within the backend: real requests through routing, middleware, validation, authorization, services, and a real database — using Supertest. This is where the API's validation and authorization guarantees get verified. The backend-pack ../../backend/backend-integration-testing owns the same discipline from the framework side; this testing-pack skill is its Supertest-centered expression.
When to Use
- For backend API endpoint behavior, auth flows, and DB-touching request paths.
- Not for pure logic (
unit-testing), non-HTTP integration (integration-testing), or browser/device journeys (playwright-e2e/maestro-e2e).
Inputs
- Endpoint design + authorization matrix (
../../backend/rest-api-design, ../../backend/backend-authorization).
- Real test DB + isolation (
test-environment-management), personas/factories (test-data-management).
Discovery Questions
- How does a test authenticate as each persona (anonymous, user A, user B, admin, other-tenant)?
- How is the database provisioned and isolated per test (containerized, transaction-rollback)?
- Which endpoints are release-critical and covered first?
Responsibilities
- Take the behavior to be covered from its Gherkin scenarios (
gherkin-specifications) — implement one test per scenario rather than inventing a parallel case list.
- Send real HTTP via Supertest through the assembled app + real production-engine DB (migrations applied, seeded to known state).
- Cover per endpoint the required cases: success (status + contract shape —
../../backend/api-contracts), invalid input (malformed / unknown-field / oversize rejected — ../../backend/backend-validation), error path (mapped status + safe shape — ../../backend/backend-error-handling), and the authorization-denial suite:
- anonymous → 401 on protected routes;
- User A cannot access User B's resource;
- non-admin cannot invoke an admin action;
- client-supplied ownership/tenant IDs are ignored (scope from session);
- cross-tenant access is rejected with a valid guessed ID.
- Cover auth flows: signup/login/refresh/logout, expired/tampered token, single-use reset token.
- Fake only true externals (provider clients, email —
../../backend/third-party-integrations sinks).
- Keep hermetic, parallelizable, CI-gating (
../../devops/ci-cd).
Required Workflow
- Build helpers: app bootstrap, persona auth, seed factories.
- Choose DB provisioning + per-test isolation.
- Per endpoint: success + invalid-input + error + authorization-denial cases.
- Add auth-flow and (where present) webhook/job-path tests.
- Wire as a CI gate; keep runtime bounded (parallel, scoped seeds).
Decision Rules
- Production-engine database, always; no SQLite substitution.
- The authorization-denial suite is mandatory per scoped resource — omission is a hole in a security control.
- Assert response contracts (status + shape), not implementation artifacts.
- Flaky API tests are fixed or quarantined same-day (
flaky-test-audit).
Rules
- No real third-party or email sends from tests.
- Tests own their data; no order dependence.
- Every endpoint bug fix lands with its regression test.
Anti-Patterns
- Mocking the data layer — that's a unit test wearing an integration label.
- Happy-path-only coverage; skipping the denial suite.
- Shared mutable seed database.
- Asserting exact error strings over codes/shapes.
- A suite too slow to run locally.
Validation Checklist
Definition of Done
A Supertest suite driving real HTTP against the app and a production-engine database — success, invalid-input, error, and the full authorization-denial suite per endpoint — hermetic, CI-gating, reproducible locally.
Related Skills
../../backend/backend-integration-testing, testing-selection, integration-testing, test-data-management, test-environment-management, ../../backend/backend-authorization, ../../backend/ownership-authorization, contract-testing, ../../devops/ci-cd.
Related Knowledge
../../../knowledge/ (critical endpoints, personas/tenancy).
Related References
../../../references/testing/ (helper patterns, when populated).
Context Loading Guidance
- Requires: endpoint + authorization matrix, DB/seed strategy.
- Does not require: service internals (tests go over HTTP), provider docs.
- May load:
test-data-management, ../../backend/backend-authorization.
- Stop when: the endpoint × required-case matrix passes hermetically.
Token Efficiency Guidance
Each cell of the endpoint × required-case matrix is a test; drive from it rather than re-deriving cases in prose.
1---2name: api-integration-testing3description: Use to plan backend API integration tests with Supertest — real HTTP requests through the full middleware stack against a real database, covering happy path, invalid input, error path, and the authorization-denial negative suite (cross-user, non-admin, cross-tenant, untrusted IDs).4---56# API Integration Testing78## Purpose910Prove the HTTP API behaves correctly end-to-end within the backend: real requests through routing, middleware, validation, authorization, services, and a **real database** — using **Supertest**. This is where the API's validation and authorization guarantees get verified. The backend-pack `../../backend/backend-integration-testing` owns the same discipline from the framework side; this testing-pack skill is its Supertest-centered expression.1112## When to Use1314- For backend API endpoint behavior, auth flows, and DB-touching request paths.15- **Not** for pure logic (`unit-testing`), non-HTTP integration (`integration-testing`), or browser/device journeys (`playwright-e2e`/`maestro-e2e`).1617## Inputs1819- Endpoint design + authorization matrix (`../../backend/rest-api-design`, `../../backend/backend-authorization`).20- Real test DB + isolation (`test-environment-management`), personas/factories (`test-data-management`).2122## Discovery Questions2324- How does a test authenticate as each persona (anonymous, user A, user B, admin, other-tenant)?25- How is the database provisioned and isolated per test (containerized, transaction-rollback)?26- Which endpoints are release-critical and covered first?2728## Responsibilities2930- Take the behavior to be covered from its **Gherkin scenarios** (`gherkin-specifications`) — implement one test per scenario rather than inventing a parallel case list.31- Send **real HTTP** via Supertest through the assembled app + real production-engine DB (migrations applied, seeded to known state).32- Cover per endpoint the **required cases**: success (status + contract shape — `../../backend/api-contracts`), **invalid input** (malformed / unknown-field / oversize rejected — `../../backend/backend-validation`), **error path** (mapped status + safe shape — `../../backend/backend-error-handling`), and the **authorization-denial suite**:33 - anonymous → 401 on protected routes;34 - **User A cannot access User B's resource**;35 - **non-admin cannot invoke an admin action**;36 - **client-supplied ownership/tenant IDs are ignored** (scope from session);37 - **cross-tenant access is rejected** with a valid guessed ID.38- Cover **auth flows**: signup/login/refresh/logout, expired/tampered token, single-use reset token.39- Fake only true externals (provider clients, email — `../../backend/third-party-integrations` sinks).40- Keep hermetic, parallelizable, CI-gating (`../../devops/ci-cd`).4142## Required Workflow43441. Build helpers: app bootstrap, persona auth, seed factories.452. Choose DB provisioning + per-test isolation.463. Per endpoint: success + invalid-input + error + authorization-denial cases.474. Add auth-flow and (where present) webhook/job-path tests.485. Wire as a CI gate; keep runtime bounded (parallel, scoped seeds).4950## Decision Rules5152- Production-engine database, always; no SQLite substitution.53- The authorization-denial suite is **mandatory per scoped resource** — omission is a hole in a security control.54- Assert response contracts (status + shape), not implementation artifacts.55- Flaky API tests are fixed or quarantined same-day (`flaky-test-audit`).5657## Rules5859- No real third-party or email sends from tests.60- Tests own their data; no order dependence.61- Every endpoint bug fix lands with its regression test.6263## Anti-Patterns6465- Mocking the data layer — that's a unit test wearing an integration label.66- Happy-path-only coverage; skipping the denial suite.67- Shared mutable seed database.68- Asserting exact error strings over codes/shapes.69- A suite too slow to run locally.7071## Validation Checklist7273- [ ] Supertest over real app + production-engine DB, isolated.74- [ ] Persona auth + factories in place.75- [ ] Per endpoint: success, invalid-input, error, authorization-denial.76- [ ] Cross-user / non-admin / untrusted-ID / cross-tenant present.77- [ ] Auth flows covered; externals faked.78- [ ] Hermetic, parallel, CI-gating.7980## Definition of Done8182A Supertest suite driving real HTTP against the app and a production-engine database — success, invalid-input, error, and the full authorization-denial suite per endpoint — hermetic, CI-gating, reproducible locally.8384## Related Skills8586`../../backend/backend-integration-testing`, `testing-selection`, `integration-testing`, `test-data-management`, `test-environment-management`, `../../backend/backend-authorization`, `../../backend/ownership-authorization`, `contract-testing`, `../../devops/ci-cd`.8788## Related Knowledge8990`../../../knowledge/` (critical endpoints, personas/tenancy).9192## Related References9394`../../../references/testing/` (helper patterns, when populated).9596## Context Loading Guidance9798- **Requires:** endpoint + authorization matrix, DB/seed strategy.99- **Does not require:** service internals (tests go over HTTP), provider docs.100- **May load:** `test-data-management`, `../../backend/backend-authorization`.101- **Stop when:** the endpoint × required-case matrix passes hermetically.102103## Token Efficiency Guidance104105Each cell of the endpoint × required-case matrix is a test; drive from it rather than re-deriving cases in prose.