Skip to content

Commit

Permalink
Add playwright rate limiter test
Browse files Browse the repository at this point in the history
  • Loading branch information
anticorrelator committed Sep 20, 2024
1 parent b3ae7a4 commit dd1e9be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export default defineConfig({
name: "webkit",
use: { ...devices["Desktop Safari"] },
},
{
name: "rate limit",
use: { ...devices["Desktop Chrome"] },
dependencies: ["chromium", "firefox", "webkit"],
testMatch: "**/*.rate-limit.spec.ts",
},
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
Expand Down
5 changes: 4 additions & 1 deletion app/src/pages/auth/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function LoginForm(props: LoginFormProps) {
body: JSON.stringify(params),
});
if (!response.ok) {
setError("Invalid login");
const errorMessage = response.status === 429
? "Too many requests. Please try again later."
: "Invalid login";
setError(errorMessage);
return;
}
} catch (error) {
Expand Down
17 changes: 17 additions & 0 deletions app/tests/login.rate-limit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from "@playwright/test";

test("that login gets rate limited after too many attempts", async ({ page }) => {
await page.goto("/login");
await page.waitForURL("**/login");

const email = `[email protected]`;
// Add the user
await page.getByLabel("Email").fill(email);
await page.getByLabel("Password *", { exact: true }).fill("not-a-password");

const numberOfAttempts = 10;
for (let i = 0; i < numberOfAttempts; i++) {
await page.getByRole("button", { name: "Login" }).click();
}
await expect(page.getByText("Too many requests. Please try again later.")).toBeVisible();
});
2 changes: 1 addition & 1 deletion src/phoenix/server/api/routers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

rate_limiter = ServerRateLimiter(
per_second_rate_limit=0.2,
enforcement_window_seconds=30,
enforcement_window_seconds=60,
partition_seconds=60,
active_partitions=2,
)
Expand Down

0 comments on commit dd1e9be

Please sign in to comment.