test: cover the x-api-key recognition middleware#169
Merged
mikewheeleer merged 1 commit intoJun 28, 2026
Merged
Conversation
Contributor
|
lgtm — in it goes 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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