Skip to content

test(server): add robust coverage for server/middleware/api-guard.ts - #106

Merged
anthuanvasquez merged 4 commits into
masterfrom
feature/82
Sep 10, 2026
Merged

anthuanvasquez merged 4 commits into
masterfrom
feature/82

Conversation

@anthuanvasquez

Copy link
Copy Markdown
Owner

Summary

Adds dedicated unit test coverage for server/middleware/api-guard.ts to prevent regressions in origin validation and internal secret handling.

Changes

  • Created tests/server/api-guard.test.ts covering all critical branches:
    • Route filtering (skips non-/api/ paths)
    • Development bypass (NODE_ENV === 'development')
    • Valid and invalid x-internal-secret handling in production
    • Rejection of missing origin/referer headers (403)
    • Rejection when allowedOrigin config is missing or invalid (500)
    • Rejection when request origin URL is malformed or unauthorized (403)
    • Authorized requests with matching origins or referers, including port handling

Testing

  • Ran full test suite with Vitest (44/44 passing across 10 test files)
  • Verified clean linter and prettier checks (pnpm lint)

Closes #82

Copilot AI lite review requested due to automatic review settings September 9, 2026 20:16
@anthuanvasquez anthuanvasquez self-assigned this Sep 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new test file uses useRuntimeConfig() without mocking/stubbing it locally, making the suite order-dependent and potentially failing when run in isolation.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds a new Vitest unit test suite to cover the critical branches of the server/middleware/api-guard.ts middleware, aiming to prevent regressions in origin validation and internal-secret bypass behavior.

Changes:

  • Added a dedicated server middleware test file covering route filtering, dev bypass, internal secret bypass, origin/referer enforcement, and allowed-origin validation cases.
File summaries
File Description
tests/server/api-guard.test.ts Introduces unit tests for api-guard middleware branch coverage (production/dev, internal secret, origin/referer, allowedOrigin validation).
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/server/api-guard.test.ts
@anthuanvasquez
anthuanvasquez merged commit 6ffe336 into master Sep 10, 2026
2 checks passed
@anthuanvasquez
anthuanvasquez deleted the feature/82 branch September 10, 2026 02:41
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 robust coverage for server/middleware/api-guard.ts

2 participants