Skip to content

test: add gateway auth middleware unit tests#43

Open
dev-khimesh wants to merge 11 commits into
jackjin1997:mainfrom
dev-khimesh:feat/go-auth-tests
Open

test: add gateway auth middleware unit tests#43
dev-khimesh wants to merge 11 commits into
jackjin1997:mainfrom
dev-khimesh:feat/go-auth-tests

Conversation

@dev-khimesh

Copy link
Copy Markdown

Summary

Add comprehensive unit tests for the gateway auth middleware and fix pre-existing compilation issues blocking tests.

Changes

Bug Fixes

  • Removed duplicate functions: getClientIP, writeJSON, generateUUID, generateAPIKey from middleware.go (already defined in api.go, same package)
  • Removed unused imports: fmt from middleware.go, errors and sort from api.go
  • Fixed function name mismatch: generateUUID()generateRequestID() to match api.go

Tests Added (12 total, all pass)

AuthMiddleware tests:

  • Missing bearer token → 401 with unauthorized error
  • Invalid token (stub accepts any token) → 200
  • Valid token sets user_id, session_id, auth_method in response
  • Does not call handler when unauthorized

RateLimitMiddleware tests:

  • First request allowed, rate limit headers set
  • Exhaust budget (burst=3) → 429 with rate_limit_exceeded

Middleware ordering tests:

  • Auth before RateLimit: missing token blocked before reaching rate limiter
  • Authenticated request passes both auth + rate limit

Request context propagation:

  • Full middleware chain propagates user/session/auth context
  • RequestIDMiddleware sets X-Request-ID header

RecoveryMiddleware:

  • Catches panics → 500 with internal_server_error
  • Allows normal requests through

Test Results

go test ./gateway/ -v  # 12/12 pass

Closes #2

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@dev-khimesh, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 24 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d15d2e75-40d4-4ae2-8f7f-f27e29ca7d04

📥 Commits

Reviewing files that changed from the base of the PR and between 1462fe7 and e1de94d.

⛔ Files ignored due to path filters (2)
  • tools/encryptly/windows-arm64/encryptly.exe is excluded by !**/*.exe
  • tools/encryptly/windows-x64/encryptly.exe is excluded by !**/*.exe
📒 Files selected for processing (11)
  • build.py
  • frontend/src/services/api.ts
  • market/gateway/api.go
  • market/gateway/middleware.go
  • market/gateway/middleware_test.go
  • tests/__init__.py
  • tests/test_diagnostic_metadata.py
  • tools/encryptly/linux-arm64/encryptly
  • tools/encryptly/linux-x64/encryptly
  • tools/encryptly/macos-arm64/encryptly
  • tools/encryptly/macos-x64/encryptly
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

[$50 BOUNTY] [Go] Add gateway auth middleware tests

1 participant