Skip to content

fix: swagger doc/route mismatch, schema drift, security schemes, and 401 auth test failures - #703

Merged
ogazboiz merged 1 commit into
LabsCrypt:mainfrom
oluebubejoy:fix/swagger-docs-auth-tests
Jun 1, 2026
Merged

fix: swagger doc/route mismatch, schema drift, security schemes, and 401 auth test failures#703
ogazboiz merged 1 commit into
LabsCrypt:mainfrom
oluebubejoy:fix/swagger-docs-auth-tests

Conversation

@oluebubejoy

Copy link
Copy Markdown

Fixes #539
Fixes #540
Fixes #541
Fixes #608

What changed

#539 — top-up undocumented; cancel @openapi sat above the wrong route

stream.routes.ts had the /cancel @openapi block sitting directly above the top-up route registration, so the generated spec attributed cancel's documentation to top-up and left POST /v1/streams/{streamId}/top-up entirely undocumented.

  • Replaced the misplaced block with a proper @openapi spec for POST /v1/streams/{streamId}/top-up (requestBody: { amount: string }, 200/400/401/403/404 responses)
  • The canonical cancel spec already lives in controllers/stream/cancel.ts — no duplicate needed

#540StreamEvent.eventType enum and Stream pause fields out of sync

swagger.ts component schemas were drifting from the real models:

  • Extended StreamEvent.eventType enum with PAUSED, RESUMED, FEE_COLLECTED
  • Added isPaused, pausedAt, totalPausedDuration, endTime to the Stream schema

#541bearerAuth / adminAuth security schemes undefined

Routes reference bearerAuth (lowercase) and adminAuth but only BearerAuth (capital) existed. Added both aliases to components.securitySchemes so Swagger UI renders the lock icon on every protected route.

#608 — 22 integration tests failing with blanket 401

Root cause: The async (importOriginal) => { ...actual, requireAuth: stub } factory pattern in vi.mock doesn't reliably apply in Vitest's pool: 'forks' mode. Because importOriginal is async and the forked worker processes module imports before the factory resolves, the real requireAuth can be registered before the mock is applied — causing dummy_token to fail JWT verification and return 401.

Fix:

  • Replaced every async (importOriginal) factory with a plain synchronous factory that exports only requireAuth and requireAdmin stubs. Synchronous factories are guaranteed to be applied before any module imports resolve.
  • Added JWT_SECRET: 'flowfi-test-secret-...' to vitest.config.ts env so verifyJwt is deterministic and the real middleware accepts properly-signed tokens even if a mock is not applied (belt-and-suspenders).

How to verify

cd backend
npm test
# expect: 0 test failures related to 401 in the 5 integration files

For swagger: start the server and visit /api-docs.json — both top-up and cancel appear under streams, StreamEvent.eventType includes all 8 values, Stream includes pause fields, and all three security schemes are listed.

…401 auth test failures

Closes LabsCrypt#539, LabsCrypt#540, LabsCrypt#541, LabsCrypt#608

## LabsCrypt#539 — top-up route was undocumented; cancel @openapi sat above it

`stream.routes.ts` had a `/cancel` @openapi block directly above the top-up
route registration, so swagger attributed cancel docs to top-up and left the
top-up endpoint entirely undocumented. Replaced the misplaced block with a
proper @openapi spec for `POST /v1/streams/{streamId}/top-up` (requestBody:
amount string; 200/400/401/403/404 responses). The canonical cancel spec
already lives in `controllers/stream/cancel.ts`.

## LabsCrypt#540 — StreamEvent.eventType enum and Stream pause fields out of sync

`swagger.ts` component schemas:
- `StreamEvent.eventType` enum extended with `PAUSED`, `RESUMED`, `FEE_COLLECTED`
  to match the values emitted by the API and stored in the Prisma schema.
- `Stream` schema now includes `isPaused`, `pausedAt`, `totalPausedDuration`,
  and `endTime`, all of which exist on the Prisma model and are returned by
  the API.

## LabsCrypt#541 — bearerAuth / adminAuth security schemes were undefined

Routes reference `bearerAuth` (lowercase) and `adminAuth` but only `BearerAuth`
(capital) was defined in `components.securitySchemes`. Added both aliases so
Swagger UI renders the lock icon on every protected route.

## LabsCrypt#608 — 22 integration tests failing with blanket 401

Root cause: the async `vi.mock(path, async (importOriginal) => { ... })` factory
pattern does not reliably apply in Vitest's `pool: 'forks'` mode. When
`importOriginal` is called inside a forked worker, the mock factory may execute
after the module registry has already resolved the real `requireAuth`, so the
real middleware runs against dummy tokens and returns 401.

Fix: replaced every `async (importOriginal) => { ...actual, requireAuth: stub }`
factory with a plain synchronous factory that only exports the functions the
tests need (`requireAuth`, `requireAdmin`). This guarantees the mock is applied
before any module import resolves.

Additionally added a stable `JWT_SECRET` to the vitest env so `verifyJwt` is
deterministic and the real middleware would accept tokens even if a mock is
somehow not applied.

Files changed:
- `tests/integration/streams/cancel.test.ts`
- `tests/integration/streams/withdraw.test.ts`
- `tests/integration/streams.test.ts`
- `tests/integration/top-up.test.ts`
- `tests/stream.test.ts`
- `vitest.config.ts`
@ogazboiz
ogazboiz merged commit 6b1fd3c into LabsCrypt:main Jun 1, 2026
7 of 9 checks passed
dmystical-coder pushed a commit to dmystical-coder/flowfi that referenced this pull request Jun 1, 2026
PR LabsCrypt#703 changed the auth.js mock in the five stream route test files to a
plain factory that returns only requireAuth and requireAdmin. auth.js also
exports issueChallenge, verifyChallenge and verifyJwt, which auth.routes wires
up when the app is constructed. With those exports missing from the mock,
importing src/app.js throws

  [vitest] No "issueChallenge" export is defined on the "auth.js" mock

so all five suites fail to load and the Backend CI job goes red (5 failed
suites, coverage step skipped).

Use vi.mock with importOriginal to spread the module's real exports and
override only requireAuth/requireAdmin. The app loads, the middleware is still
stubbed, and the suites run.

Verified: 36 suites / 184 tests pass; coverage 70.15% stmts, 66.73% branches,
72.34% funcs, 70.15% lines — all above the 60% gate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment