57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll, beforeEach } from "vitest";
|
|
import type { FastifyInstance } from "fastify";
|
|
import { createTestApp, cleanDb } from "./helpers.js";
|
|
|
|
let app: FastifyInstance;
|
|
|
|
beforeAll(async () => {
|
|
app = await createTestApp();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await cleanDb();
|
|
});
|
|
|
|
describe("POST /login", () => {
|
|
it("sends OTP for a valid email", async () => {
|
|
const res = await app.inject({
|
|
method: "POST",
|
|
url: "/login",
|
|
payload: { email: "test@example.com" },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.json()).toEqual({ message: "OTP sent to your email" });
|
|
});
|
|
|
|
it("returns 400 if email is missing", async () => {
|
|
const res = await app.inject({
|
|
method: "POST",
|
|
url: "/login",
|
|
payload: {},
|
|
});
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
});
|
|
|
|
it("rate limits after 3 requests", async () => {
|
|
const email = "ratelimit@example.com";
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
await app.inject({ method: "POST", url: "/login", payload: { email } });
|
|
}
|
|
|
|
const res = await app.inject({
|
|
method: "POST",
|
|
url: "/login",
|
|
payload: { email },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(429);
|
|
});
|
|
});
|