|
| 1 | +process.env.NODE_ENV = "test"; |
| 2 | +process.env.LOG_LEVEL = "silent"; |
| 3 | +process.env.DATABASE_URL = "postgres://localhost/test"; |
| 4 | +process.env.JWT_SECRET = "a-very-long-test-secret-at-least-32-bytes!!"; |
| 5 | +process.env.JWT_ISSUER = "predictify"; |
| 6 | +process.env.JWT_AUDIENCE = "predictify-app"; |
| 7 | + |
| 8 | +jest.mock("pg", () => { |
| 9 | + const Pool = jest.fn().mockImplementation(() => ({ |
| 10 | + connect: jest.fn(), |
| 11 | + query: jest.fn(), |
| 12 | + end: jest.fn(), |
| 13 | + })); |
| 14 | + return { Pool }; |
| 15 | +}); |
| 16 | + |
| 17 | +const mockValues = jest.fn().mockResolvedValue(undefined); |
| 18 | +const mockInsert = jest.fn(() => ({ values: mockValues })); |
| 19 | +const mockLimit = jest.fn(); |
| 20 | +const mockOffset = jest.fn(); |
| 21 | + |
| 22 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 23 | +const queryBuilder: any = {}; |
| 24 | +queryBuilder.from = jest.fn().mockReturnValue(queryBuilder); |
| 25 | +queryBuilder.where = jest.fn().mockReturnValue(queryBuilder); |
| 26 | +queryBuilder.orderBy = jest.fn().mockReturnValue(queryBuilder); |
| 27 | +queryBuilder.limit = jest.fn().mockImplementation((val) => { |
| 28 | + if (val === 1) { |
| 29 | + return mockLimit(); |
| 30 | + } |
| 31 | + return queryBuilder; |
| 32 | +}); |
| 33 | +queryBuilder.offset = jest.fn().mockImplementation((val) => mockOffset(val)); |
| 34 | + |
| 35 | +const mockSelect = jest.fn().mockReturnValue(queryBuilder); |
| 36 | + |
| 37 | +const mockDb = { |
| 38 | + select: mockSelect, |
| 39 | + insert: mockInsert, |
| 40 | +}; |
| 41 | + |
| 42 | +jest.mock("drizzle-orm/node-postgres", () => ({ |
| 43 | + drizzle: jest.fn(() => mockDb), |
| 44 | +})); |
| 45 | + |
| 46 | +import request from "supertest"; |
| 47 | +import jwt from "jsonwebtoken"; |
| 48 | +import express from "express"; |
| 49 | +import { createExportsRouter } from "../src/routes/exports"; |
| 50 | +import { errorHandler } from "../src/middleware/errorHandler"; |
| 51 | + |
| 52 | +const TEST_SECRET = "a-very-long-test-secret-at-least-32-bytes!!"; |
| 53 | +const TEST_ISSUER = "predictify"; |
| 54 | +const TEST_AUDIENCE = "predictify-app"; |
| 55 | +const TEST_USER_ID = "11111111-1111-1111-1111-111111111111"; |
| 56 | +const TEST_STELLAR = "GABC1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF12"; |
| 57 | + |
| 58 | +function signToken(_userId = TEST_USER_ID, stellarAddress = TEST_STELLAR): string { |
| 59 | + return jwt.sign({ sub: stellarAddress }, TEST_SECRET, { |
| 60 | + algorithm: "HS256", |
| 61 | + issuer: TEST_ISSUER, |
| 62 | + audience: TEST_AUDIENCE, |
| 63 | + expiresIn: 3600, |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +describe("Rate limiting on /api/exports", () => { |
| 68 | + const RATE_LIMIT_CAPACITY = 2; |
| 69 | + |
| 70 | + function makeApp() { |
| 71 | + const app = express(); |
| 72 | + app.use(express.json()); |
| 73 | + app.use( |
| 74 | + "/api/exports", |
| 75 | + createExportsRouter({ rateLimit: { capacity: RATE_LIMIT_CAPACITY, refillWindowMs: 60000 } }), |
| 76 | + ); |
| 77 | + app.use(errorHandler); |
| 78 | + return app; |
| 79 | + } |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + jest.clearAllMocks(); |
| 83 | + mockLimit.mockReset(); |
| 84 | + mockOffset.mockReset(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("allows requests up to the token bucket capacity", async () => { |
| 88 | + mockLimit.mockResolvedValue([{ id: TEST_USER_ID, stellarAddress: TEST_STELLAR }]); |
| 89 | + mockOffset.mockResolvedValue([]); |
| 90 | + const app = makeApp(); |
| 91 | + |
| 92 | + for (let i = 0; i < RATE_LIMIT_CAPACITY; i++) { |
| 93 | + const res = await request(app) |
| 94 | + .get("/api/exports/predictions?format=json") |
| 95 | + .set("Authorization", `Bearer ${signToken()}`); |
| 96 | + |
| 97 | + expect(res.status).toBe(200); |
| 98 | + expect(Number(res.headers["ratelimit-remaining"])).toBeGreaterThanOrEqual(0); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + it("returns 429 with Retry-After when token bucket is exhausted", async () => { |
| 103 | + mockLimit.mockResolvedValue([{ id: TEST_USER_ID, stellarAddress: TEST_STELLAR }]); |
| 104 | + mockOffset.mockResolvedValue([]); |
| 105 | + const app = makeApp(); |
| 106 | + |
| 107 | + for (let i = 0; i < RATE_LIMIT_CAPACITY; i++) { |
| 108 | + await request(app) |
| 109 | + .get("/api/exports/predictions?format=json") |
| 110 | + .set("Authorization", `Bearer ${signToken()}`); |
| 111 | + } |
| 112 | + |
| 113 | + const res = await request(app) |
| 114 | + .get("/api/exports/predictions?format=json") |
| 115 | + .set("Authorization", `Bearer ${signToken()}`); |
| 116 | + |
| 117 | + expect(res.status).toBe(429); |
| 118 | + expect(res.body.error.code).toBe("rate_limit_exceeded"); |
| 119 | + expect(res.body.error.message).toBe("Too many requests"); |
| 120 | + expect(res.body.error.retryAfter).toBeDefined(); |
| 121 | + expect(typeof res.body.error.retryAfter).toBe("number"); |
| 122 | + expect(res.body.error.retryAfter).toBeGreaterThan(0); |
| 123 | + expect(res.body.error.resetAt).toBeDefined(); |
| 124 | + expect(res.headers["retry-after"]).toBeDefined(); |
| 125 | + expect(Number(res.headers["retry-after"])).toBeGreaterThan(0); |
| 126 | + expect(res.headers["ratelimit-remaining"]).toBe("0"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("returns 429 with proper error envelope", async () => { |
| 130 | + mockLimit.mockResolvedValue([{ id: TEST_USER_ID, stellarAddress: TEST_STELLAR }]); |
| 131 | + mockOffset.mockResolvedValue([]); |
| 132 | + const app = makeApp(); |
| 133 | + |
| 134 | + for (let i = 0; i < RATE_LIMIT_CAPACITY; i++) { |
| 135 | + await request(app) |
| 136 | + .get("/api/exports/predictions?format=json") |
| 137 | + .set("Authorization", `Bearer ${signToken()}`); |
| 138 | + } |
| 139 | + |
| 140 | + const res = await request(app) |
| 141 | + .get("/api/exports/predictions?format=json") |
| 142 | + .set("Authorization", `Bearer ${signToken()}`); |
| 143 | + |
| 144 | + expect(res.status).toBe(429); |
| 145 | + expect(res.body).toMatchObject({ |
| 146 | + error: { |
| 147 | + code: "rate_limit_exceeded", |
| 148 | + message: "Too many requests", |
| 149 | + }, |
| 150 | + }); |
| 151 | + expect(typeof res.body.error.retryAfter).toBe("number"); |
| 152 | + expect(typeof res.body.error.resetAt).toBe("string"); |
| 153 | + }); |
| 154 | + |
| 155 | + it("allows requests from a different user after first user is rate limited", async () => { |
| 156 | + mockLimit.mockResolvedValue([{ id: TEST_USER_ID, stellarAddress: TEST_STELLAR }]); |
| 157 | + mockOffset.mockResolvedValue([]); |
| 158 | + const app = makeApp(); |
| 159 | + |
| 160 | + for (let i = 0; i < RATE_LIMIT_CAPACITY; i++) { |
| 161 | + await request(app) |
| 162 | + .get("/api/exports/predictions?format=json") |
| 163 | + .set("Authorization", `Bearer ${signToken()}`); |
| 164 | + } |
| 165 | + |
| 166 | + const firstUserRes = await request(app) |
| 167 | + .get("/api/exports/predictions?format=json") |
| 168 | + .set("Authorization", `Bearer ${signToken()}`); |
| 169 | + expect(firstUserRes.status).toBe(429); |
| 170 | + |
| 171 | + const secondStellar = "GDEF9876543210ABCDEF9876543210ABCDEF9876543210ABCDEF98"; |
| 172 | + const secondUserId = "22222222-2222-2222-2222-222222222222"; |
| 173 | + |
| 174 | + mockLimit.mockResolvedValue([{ id: secondUserId, stellarAddress: secondStellar }]); |
| 175 | + |
| 176 | + const secondUserRes = await request(app) |
| 177 | + .get("/api/exports/predictions?format=json") |
| 178 | + .set("Authorization", `Bearer ${signToken(secondUserId, secondStellar)}`); |
| 179 | + |
| 180 | + expect(secondUserRes.status).toBe(200); |
| 181 | + expect(Number(secondUserRes.headers["ratelimit-remaining"])).toBe(RATE_LIMIT_CAPACITY - 1); |
| 182 | + }); |
| 183 | +}); |
0 commit comments