|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { Request, Response, Router } from 'express'; |
| 3 | +import { ineeda } from 'ineeda'; |
| 4 | +import passport from 'passport'; |
| 5 | +import { match, spy } from 'sinon'; |
| 6 | +import { Repository } from 'typeorm'; |
| 7 | + |
| 8 | +import { Bot } from '../../src/Bot'; |
| 9 | +import { INJECT_BOT, INJECT_STORAGE } from '../../src/BotService'; |
| 10 | +import { MetricsEndpoint } from '../../src/endpoint/MetricsEndpoint'; |
| 11 | +import { BotModule } from '../../src/module/BotModule'; |
| 12 | +import { Storage } from '../../src/storage'; |
| 13 | +import { describeLeaks, itLeaks } from '../helpers/async'; |
| 14 | +import { createService, createServiceContainer } from '../helpers/container'; |
| 15 | +import { getTestLogger } from '../helpers/logger'; |
| 16 | + |
| 17 | +async function createEndpoint(botReady: boolean, storageReady: boolean): Promise<MetricsEndpoint> { |
| 18 | + const storage = ineeda<Storage>({ |
| 19 | + getRepository() { |
| 20 | + return ineeda<Repository<{}>>(); |
| 21 | + }, |
| 22 | + }); |
| 23 | + const bot = ineeda<Bot>({ |
| 24 | + getStorage() { |
| 25 | + return storage; |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + const { container, module } = await createServiceContainer(new BotModule({ |
| 30 | + logger: getTestLogger(), |
| 31 | + })); |
| 32 | + module.bind(INJECT_BOT).toInstance(bot); |
| 33 | + module.bind(INJECT_STORAGE).toInstance(storage); |
| 34 | + |
| 35 | + return createService(container, MetricsEndpoint, { |
| 36 | + data: { |
| 37 | + filters: [], |
| 38 | + strict: false, |
| 39 | + }, |
| 40 | + metadata: { |
| 41 | + kind: 'metrics-endpoint', |
| 42 | + name: 'test-endpoint', |
| 43 | + }, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function createRequest() { |
| 48 | + const end = spy(); |
| 49 | + const set = spy(); |
| 50 | + const response = ineeda<Response>({ |
| 51 | + end, |
| 52 | + set, |
| 53 | + }); |
| 54 | + return { end, response, set }; |
| 55 | +} |
| 56 | + |
| 57 | +// tslint:disable:no-identical-functions |
| 58 | +describeLeaks('metrics endpoint', async () => { |
| 59 | + itLeaks('should have paths', async () => { |
| 60 | + const endpoint = await createEndpoint(false, false); |
| 61 | + expect(endpoint.paths.length).to.equal(3); |
| 62 | + expect(endpoint.paths).to.include('/metrics'); |
| 63 | + }); |
| 64 | + |
| 65 | + itLeaks('should configure a router', async () => { |
| 66 | + const endpoint = await createEndpoint(false, false); |
| 67 | + const get = spy(); |
| 68 | + const router = ineeda<Router>({ |
| 69 | + get, |
| 70 | + }); |
| 71 | + const result = await endpoint.createRouter({ |
| 72 | + passport: ineeda<passport.Authenticator>(), |
| 73 | + router, |
| 74 | + }); |
| 75 | + expect(result).to.equal(router, 'must return the passed router'); |
| 76 | + expect(get).to.have.callCount(1); |
| 77 | + }); |
| 78 | + |
| 79 | + describeLeaks('index route', async () => { |
| 80 | + itLeaks('should return metrics', async () => { |
| 81 | + const endpoint = await createEndpoint(true, true); |
| 82 | + const { end, response } = createRequest(); |
| 83 | + await endpoint.getIndex(ineeda<Request>({}), response); |
| 84 | + expect(end).to.have.been.calledOnce.and.calledWithMatch(match.string); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments