-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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 app-install-flow-followup
- Loading branch information
Showing
138 changed files
with
3,038 additions
and
1,361 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as Sentry from "@sentry/nextjs"; | ||
|
||
export function register() { | ||
if (process.env.NEXT_RUNTIME === "nodejs") { | ||
Sentry.init({ | ||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN, | ||
// reduce sample rate to 10% on production | ||
tracesSampleRate: process.env.NODE_ENV !== "production" ? 1.0 : 0.1, | ||
}); | ||
} | ||
|
||
if (process.env.NEXT_RUNTIME === "edge") { | ||
Sentry.init({ | ||
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN, | ||
}); | ||
} | ||
} |
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" }); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.