-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into issue#14884
- Loading branch information
Showing
367 changed files
with
12,348 additions
and
2,841 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: PR Reviewed | ||
|
||
on: | ||
pull_request_review: | ||
types: [submitted] | ||
|
||
jobs: | ||
label-pr: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Label PR as ready for E2E | ||
if: github.event.review.state == 'approved' | ||
uses: actions-ecosystem/action-add-labels@v1 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
labels: 'ready-for-e2e' |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import type { Request, Response } from "express"; | ||
import type { NextApiResponse, NextApiRequest } from "next"; | ||
import { createMocks } from "node-mocks-http"; | ||
import { describe, it, expect, vi } from "vitest"; | ||
|
||
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError"; | ||
|
||
import { rateLimitApiKey } from "~/lib/helpers/rateLimitApiKey"; | ||
|
||
type CustomNextApiRequest = NextApiRequest & Request; | ||
type CustomNextApiResponse = NextApiResponse & Response; | ||
|
||
vi.mock("@calcom/lib/checkRateLimitAndThrowError", () => ({ | ||
checkRateLimitAndThrowError: vi.fn(), | ||
})); | ||
|
||
describe("rateLimitApiKey middleware", () => { | ||
it("should return 401 if no apiKey is provided", async () => { | ||
const { req, res } = createMocks<CustomNextApiRequest, CustomNextApiResponse>({ | ||
method: "GET", | ||
query: {}, | ||
}); | ||
|
||
await rateLimitApiKey(req, res, vi.fn() as any); | ||
|
||
expect(res._getStatusCode()).toBe(401); | ||
expect(res._getJSONData()).toEqual({ message: "No apiKey provided" }); | ||
}); | ||
|
||
it("should call checkRateLimitAndThrowError with correct parameters", async () => { | ||
const { req, res } = createMocks({ | ||
method: "GET", | ||
query: { apiKey: "test-key" }, | ||
}); | ||
|
||
(checkRateLimitAndThrowError as any).mockResolvedValueOnce({ | ||
limit: 100, | ||
remaining: 99, | ||
reset: Date.now(), | ||
}); | ||
|
||
// @ts-expect-error weird typing between middleware and createMocks | ||
await rateLimitApiKey(req, res, vi.fn() as any); | ||
|
||
expect(checkRateLimitAndThrowError).toHaveBeenCalledWith({ | ||
identifier: "test-key", | ||
rateLimitingType: "api", | ||
onRateLimiterResponse: expect.any(Function), | ||
}); | ||
}); | ||
|
||
it("should set rate limit headers correctly", async () => { | ||
const { req, res } = createMocks({ | ||
method: "GET", | ||
query: { apiKey: "test-key" }, | ||
}); | ||
|
||
const rateLimiterResponse = { | ||
limit: 100, | ||
remaining: 99, | ||
reset: Date.now(), | ||
}; | ||
|
||
(checkRateLimitAndThrowError as any).mockImplementationOnce(({ onRateLimiterResponse }) => { | ||
onRateLimiterResponse(rateLimiterResponse); | ||
}); | ||
|
||
// @ts-expect-error weird typing between middleware and createMocks | ||
await rateLimitApiKey(req, res, vi.fn() as any); | ||
|
||
expect(res.getHeader("X-RateLimit-Limit")).toBe(rateLimiterResponse.limit); | ||
expect(res.getHeader("X-RateLimit-Remaining")).toBe(rateLimiterResponse.remaining); | ||
expect(res.getHeader("X-RateLimit-Reset")).toBe(rateLimiterResponse.reset); | ||
}); | ||
|
||
it("should return 429 if rate limit is exceeded", async () => { | ||
const { req, res } = createMocks({ | ||
method: "GET", | ||
query: { apiKey: "test-key" }, | ||
}); | ||
|
||
(checkRateLimitAndThrowError as any).mockRejectedValue(new Error("Rate limit exceeded")); | ||
|
||
// @ts-expect-error weird typing between middleware and createMocks | ||
await rateLimitApiKey(req, res, vi.fn() as any); | ||
|
||
expect(res._getStatusCode()).toBe(429); | ||
expect(res._getJSONData()).toEqual({ message: "Rate limit exceeded" }); | ||
}); | ||
}); |
Oops, something went wrong.