Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-p2p): rate limit plugin #4193

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: rate limit plugin unit tests
  • Loading branch information
air1one committed Nov 24, 2020
commit fafa548e1ae50b52e17423a695df9c942d248eea
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Server } from "@hapi/hapi";
import Joi from "@hapi/joi";
import { Container } from "@arkecosystem/core-kernel";

import { RateLimitPlugin } from "@arkecosystem/core-p2p/src/socket-server/plugins/rate-limit";
import * as utils from "@arkecosystem/core-p2p/src/utils/build-rate-limiter";

afterEach(() => {
jest.clearAllMocks();
});

describe("RateLimitPlugin", () => {
let rateLimitPlugin: RateLimitPlugin;

const container = new Container.Container();

const responsePayload = { status: "ok" };
const mockRouteByPath = {
"/p2p/peer/mockroute": {
id: "p2p.peer.getPeers",
handler: () => responsePayload,
validation: Joi.object().max(0),
},
};
const mockRoute = {
method: "POST",
path: "/p2p/peer/mockroute",
config: {
id: mockRouteByPath["/p2p/peer/mockroute"].id,
handler: mockRouteByPath["/p2p/peer/mockroute"].handler,
},
};

const app = {
resolve: jest.fn().mockReturnValue({ getRoutesConfigByPath: () => mockRouteByPath }),
};
const pluginConfiguration = { getOptional: (id, defaultValue) => defaultValue };
const rateLimiter = {
hasExceededRateLimit: jest.fn().mockReturnValue(false),
};

beforeAll(() => {
container.unbindAll();
container.bind(Container.Identifiers.Application).toConstantValue(app);
container.bind(Container.Identifiers.PluginConfiguration).toConstantValue(pluginConfiguration);

jest.spyOn(utils, "buildRateLimiter").mockReturnValue(rateLimiter as any);
});

beforeEach(() => {
rateLimitPlugin = container.resolve<RateLimitPlugin>(RateLimitPlugin);
});

it("should register the plugin", async () => {
const server = new Server({ port: 4100 });
server.route(mockRoute);

const spyExt = jest.spyOn(server, "ext");

rateLimitPlugin.register(server);

expect(spyExt).toBeCalledWith(expect.objectContaining({ type: "onPreAuth" }));

// try the route with a valid payload
const remoteAddress = "187.166.55.44";
const responseValid = await server.inject({
method: "POST",
url: "/p2p/peer/mockroute",
payload: {},
remoteAddress,
});
expect(JSON.parse(responseValid.payload)).toEqual(responsePayload);
expect(responseValid.statusCode).toBe(200);
expect(rateLimiter.hasExceededRateLimit).toBeCalledTimes(1);
});

it("should return a tooManyRequests error when exceeded rate limit", async () => {
const server = new Server({ port: 4100 });
server.route(mockRoute);
rateLimitPlugin.register(server);

rateLimiter.hasExceededRateLimit.mockReturnValueOnce(true);

// try the route with a valid payload
const remoteAddress = "187.166.55.44";
const responseForbidden = await server.inject({
method: "POST",
url: "/p2p/peer/mockroute",
payload: {},
remoteAddress,
});
expect(responseForbidden.statusCode).toBe(429);
expect(rateLimiter.hasExceededRateLimit).toBeCalledTimes(1);
});
});