-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core-p2p): respond with error when blockchain is not ready (#4109)
- Loading branch information
Showing
6 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
__tests__/unit/core-p2p/socket-server/plugins/is-app-ready.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { Server } from "@hapi/hapi"; | ||
import Joi from "@hapi/joi"; | ||
import { Container } from "@arkecosystem/core-kernel"; | ||
|
||
import { IsAppReadyPlugin } from "@arkecosystem/core-p2p/src/socket-server/plugins/is-app-ready"; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("IsAppReadyPlugin", () => { | ||
let isAppReadyPlugin: IsAppReadyPlugin; | ||
|
||
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 blockchainService = { isBooted: jest.fn().mockReturnValue(true) }; | ||
const app = { | ||
isBound: jest.fn().mockReturnValue(true), | ||
get: jest.fn().mockReturnValue(blockchainService), | ||
resolve: jest.fn().mockReturnValue({ getRoutesConfigByPath: () => mockRouteByPath }), | ||
}; | ||
|
||
beforeAll(() => { | ||
container.unbindAll(); | ||
container.bind(Container.Identifiers.Application).toConstantValue(app); | ||
}); | ||
|
||
beforeEach(() => { | ||
isAppReadyPlugin = container.resolve<IsAppReadyPlugin>(IsAppReadyPlugin); | ||
}); | ||
|
||
it("should register the plugin", async () => { | ||
const server = new Server({ port: 4100 }); | ||
server.route(mockRoute); | ||
|
||
const spyExt = jest.spyOn(server, "ext"); | ||
|
||
isAppReadyPlugin.register(server); | ||
|
||
expect(spyExt).toBeCalledWith(expect.objectContaining({ type: "onPreHandler" })); | ||
|
||
// 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(app.isBound).toBeCalledTimes(1); | ||
expect(blockchainService.isBooted).toBeCalledTimes(1); | ||
}); | ||
|
||
it("should return a forbidden error when blockchain service is not bound", async () => { | ||
const server = new Server({ port: 4100 }); | ||
server.route(mockRoute); | ||
isAppReadyPlugin.register(server); | ||
|
||
app.isBound.mockReturnValueOnce(false); | ||
|
||
// 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(403); | ||
expect(app.isBound).toBeCalledTimes(1); | ||
expect(blockchainService.isBooted).toBeCalledTimes(0); | ||
}); | ||
|
||
it("should return a forbidden error when blockchain service is not booted", async () => { | ||
const server = new Server({ port: 4100 }); | ||
server.route(mockRoute); | ||
isAppReadyPlugin.register(server); | ||
|
||
blockchainService.isBooted.mockReturnValueOnce(false); | ||
|
||
// 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(403); | ||
expect(app.isBound).toBeCalledTimes(1); | ||
expect(blockchainService.isBooted).toBeCalledTimes(1); | ||
}); | ||
|
||
it("should not be called on another route", async () => { | ||
const testRoute = { | ||
method: "POST", | ||
path: "/p2p/peer/testroute", | ||
config: { | ||
handler: () => { | ||
return { status: "ok" }; | ||
}, | ||
}, | ||
}; | ||
|
||
const server = new Server({ port: 4100 }); | ||
server.route(testRoute); | ||
server.route(mockRoute); | ||
|
||
const spyExt = jest.spyOn(server, "ext"); | ||
|
||
isAppReadyPlugin.register(server); | ||
|
||
expect(spyExt).toBeCalledWith(expect.objectContaining({ type: "onPreHandler" })); | ||
|
||
// try the route with a valid payload | ||
const remoteAddress = "187.166.55.44"; | ||
const responseValid = await server.inject({ | ||
method: "POST", | ||
url: "/p2p/peer/testroute", | ||
payload: {}, | ||
remoteAddress, | ||
}); | ||
expect(JSON.parse(responseValid.payload)).toEqual(responsePayload); | ||
expect(responseValid.statusCode).toBe(200); | ||
expect(app.isBound).toBeCalledTimes(1); | ||
expect(blockchainService.isBooted).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/core-p2p/src/socket-server/plugins/is-app-ready.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Container, Contracts } from "@arkecosystem/core-kernel"; | ||
import Boom from "@hapi/boom"; | ||
|
||
@Container.injectable() | ||
export class IsAppReadyPlugin { | ||
@Container.inject(Container.Identifiers.Application) | ||
protected readonly app!: Contracts.Kernel.Application; | ||
|
||
public register(server) { | ||
server.ext({ | ||
type: "onPreHandler", | ||
method: async (request, h) => { | ||
if ( | ||
this.app.isBound(Container.Identifiers.BlockchainService) && | ||
this.app.get<Contracts.Blockchain.Blockchain>(Container.Identifiers.BlockchainService).isBooted() | ||
) { | ||
return h.continue; | ||
} | ||
|
||
return Boom.forbidden("App is not ready"); | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters