Skip to content

test: cover the x-api-key recognition middleware#169

Merged
mikewheeleer merged 1 commit into
Agentpay-Org:mainfrom
AbuJulaybeeb:test/testing-79-apikey-recognition
Jun 28, 2026
Merged

test: cover the x-api-key recognition middleware#169
mikewheeleer merged 1 commit into
Agentpay-Org:mainfrom
AbuJulaybeeb:test/testing-79-apikey-recognition

Conversation

@AbuJulaybeeb

Copy link
Copy Markdown

This PR adds comprehensive test coverage for the API key recognition middleware in src/index.ts, establishing a baseline for the current (open) behavior before upcoming enforcement and tenant scoping work lands.

What was done:

Created a new test suite src/apikey-recognition.test.ts using supertest.
Instantiated a test-only Express app mounting the middleware stack to assert req.apiKey tagging behavior without modifying production code.
Covered all edge cases: verified key creation, recognition of valid keys, and silent ignoring of unknown, missing, and revoked keys.
Confirmed that recognition is case-insensitive on the header name and exact on the key value.
Added a baseline test verifying that the API remains fully open to writes when no X-API-Key is provided (documenting the current pre-enforcement contract).
Ensured no raw keys are ever echoed via error paths.
Validation:

Passed npm run lint with 0 issues.
Maintained 100% test coverage for the API-key recognition flow.
Test Output:
▶ API Key Recognition Middleware
✔ recognises a created key (case-insensitive header, exact value) (137.482296ms)
✔ silently ignores an unknown key (16.180683ms)
✔ leaves apiKey undefined if no key is provided (22.94905ms)
✔ silently ignores a revoked key (92.595109ms)
✔ ensures the API remains open: a write with no X-API-Key still succeeds (21.385633ms)
✔ API Key Recognition Middleware (298.02458ms)
ℹ tests 5
ℹ suites 1
ℹ pass 5
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 1094.440668

Exit code: 0

closes #153

@mikewheeleer

Copy link
Copy Markdown
Contributor

lgtm — in it goes 👍

@mikewheeleer mikewheeleer merged commit 72edbf6 into Agentpay-Org:main Jun 28, 2026
1 check passed
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.

Add coverage for the X-API-Key recognition middleware and req.apiKey tagging

2 participants