|
| 1 | +import { Server } from "@hapi/hapi"; |
| 2 | +import Joi from "@hapi/joi"; |
| 3 | +import { Container } from "@arkecosystem/core-kernel"; |
| 4 | + |
| 5 | +import { RateLimitPlugin } from "@arkecosystem/core-p2p/src/socket-server/plugins/rate-limit"; |
| 6 | +import * as utils from "@arkecosystem/core-p2p/src/utils/build-rate-limiter"; |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + jest.clearAllMocks(); |
| 10 | +}); |
| 11 | + |
| 12 | +describe("RateLimitPlugin", () => { |
| 13 | + let rateLimitPlugin: RateLimitPlugin; |
| 14 | + |
| 15 | + const container = new Container.Container(); |
| 16 | + |
| 17 | + const responsePayload = { status: "ok" }; |
| 18 | + const mockRouteByPath = { |
| 19 | + "/p2p/peer/mockroute": { |
| 20 | + id: "p2p.peer.getPeers", |
| 21 | + handler: () => responsePayload, |
| 22 | + validation: Joi.object().max(0), |
| 23 | + }, |
| 24 | + }; |
| 25 | + const mockRoute = { |
| 26 | + method: "POST", |
| 27 | + path: "/p2p/peer/mockroute", |
| 28 | + config: { |
| 29 | + id: mockRouteByPath["/p2p/peer/mockroute"].id, |
| 30 | + handler: mockRouteByPath["/p2p/peer/mockroute"].handler, |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + const app = { |
| 35 | + resolve: jest.fn().mockReturnValue({ getRoutesConfigByPath: () => mockRouteByPath }), |
| 36 | + }; |
| 37 | + const pluginConfiguration = { getOptional: (id, defaultValue) => defaultValue }; |
| 38 | + const rateLimiter = { |
| 39 | + hasExceededRateLimit: jest.fn().mockReturnValue(false), |
| 40 | + }; |
| 41 | + |
| 42 | + beforeAll(() => { |
| 43 | + container.unbindAll(); |
| 44 | + container.bind(Container.Identifiers.Application).toConstantValue(app); |
| 45 | + container.bind(Container.Identifiers.PluginConfiguration).toConstantValue(pluginConfiguration); |
| 46 | + |
| 47 | + jest.spyOn(utils, "buildRateLimiter").mockReturnValue(rateLimiter as any); |
| 48 | + }); |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + rateLimitPlugin = container.resolve<RateLimitPlugin>(RateLimitPlugin); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should register the plugin", async () => { |
| 55 | + const server = new Server({ port: 4100 }); |
| 56 | + server.route(mockRoute); |
| 57 | + |
| 58 | + const spyExt = jest.spyOn(server, "ext"); |
| 59 | + |
| 60 | + rateLimitPlugin.register(server); |
| 61 | + |
| 62 | + expect(spyExt).toBeCalledWith(expect.objectContaining({ type: "onPreAuth" })); |
| 63 | + |
| 64 | + // try the route with a valid payload |
| 65 | + const remoteAddress = "187.166.55.44"; |
| 66 | + const responseValid = await server.inject({ |
| 67 | + method: "POST", |
| 68 | + url: "/p2p/peer/mockroute", |
| 69 | + payload: {}, |
| 70 | + remoteAddress, |
| 71 | + }); |
| 72 | + expect(JSON.parse(responseValid.payload)).toEqual(responsePayload); |
| 73 | + expect(responseValid.statusCode).toBe(200); |
| 74 | + expect(rateLimiter.hasExceededRateLimit).toBeCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should return a tooManyRequests error when exceeded rate limit", async () => { |
| 78 | + const server = new Server({ port: 4100 }); |
| 79 | + server.route(mockRoute); |
| 80 | + rateLimitPlugin.register(server); |
| 81 | + |
| 82 | + rateLimiter.hasExceededRateLimit.mockReturnValueOnce(true); |
| 83 | + |
| 84 | + // try the route with a valid payload |
| 85 | + const remoteAddress = "187.166.55.44"; |
| 86 | + const responseForbidden = await server.inject({ |
| 87 | + method: "POST", |
| 88 | + url: "/p2p/peer/mockroute", |
| 89 | + payload: {}, |
| 90 | + remoteAddress, |
| 91 | + }); |
| 92 | + expect(responseForbidden.statusCode).toBe(429); |
| 93 | + expect(rateLimiter.hasExceededRateLimit).toBeCalledTimes(1); |
| 94 | + }); |
| 95 | +}); |
0 commit comments