|
| 1 | +import { IncomingMessage, ServerResponse } from 'http'; |
| 2 | +import { Socket } from 'net'; |
| 3 | + |
| 4 | +jest.mock('@/api-app', () => ({ __esModule: true, default: jest.fn() })); |
| 5 | +jest.mock('@/controllers/team', () => ({ |
| 6 | + LOCAL_APP_TEAM: { _id: { toString: () => 'local-team' } }, |
| 7 | +})); |
| 8 | +jest.mock('@/migrations', () => ({ runStartupMigrations: jest.fn() })); |
| 9 | +jest.mock('@/models', () => ({ |
| 10 | + connectDB: jest.fn().mockResolvedValue(undefined), |
| 11 | + connectDBWithRetry: jest.fn().mockResolvedValue(undefined), |
| 12 | +})); |
| 13 | +jest.mock('@/opamp/app', () => ({ __esModule: true, default: jest.fn() })); |
| 14 | +jest.mock('@/setupDefaults', () => ({ setupTeamDefaults: jest.fn() })); |
| 15 | +jest.mock('@/utils/tokenEncryption', () => ({ |
| 16 | + verifyTokenEncryption: jest.fn(), |
| 17 | +})); |
| 18 | +jest.mock('@/utils/logger', () => ({ |
| 19 | + __esModule: true, |
| 20 | + default: { error: jest.fn(), info: jest.fn() }, |
| 21 | +})); |
| 22 | +jest.mock('@/utils/instrumentation', () => ({ |
| 23 | + getCounter: jest.fn(() => ({ add: jest.fn() })), |
| 24 | +})); |
| 25 | +jest.mock('http-graceful-shutdown', () => jest.fn()); |
| 26 | +jest.mock('http', () => ({ |
| 27 | + ...jest.requireActual('http'), |
| 28 | + createServer: jest.fn(() => ({ listen: jest.fn() })), |
| 29 | +})); |
| 30 | + |
| 31 | +describe.each(['server', 'serverless'])( |
| 32 | + 'session secret startup diagnostics (%s first)', |
| 33 | + first => { |
| 34 | + const originalEnv = { ...process.env }; |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + process.env = { ...originalEnv }; |
| 38 | + jest.resetModules(); |
| 39 | + }); |
| 40 | + |
| 41 | + it.each([ |
| 42 | + [false, undefined, 1], |
| 43 | + [false, '', 1], |
| 44 | + [false, 'hyperdx is cool 👋', 1], |
| 45 | + [false, 'startup-secret-sentinel', 0], |
| 46 | + [true, undefined, 0], |
| 47 | + [true, 'hyperdx is cool 👋', 0], |
| 48 | + [true, 'startup-secret-sentinel', 0], |
| 49 | + ])( |
| 50 | + 'local=%s, secret=%s logs %s times across both entrypoints', |
| 51 | + async (local, secret, count) => { |
| 52 | + process.env.APP_TYPE = 'api'; |
| 53 | + if (local) { |
| 54 | + process.env.IS_LOCAL_APP_MODE = 'DANGEROUSLY_is_local_app_mode💀'; |
| 55 | + } else { |
| 56 | + delete process.env.IS_LOCAL_APP_MODE; |
| 57 | + } |
| 58 | + if (typeof secret === 'undefined') |
| 59 | + delete process.env.EXPRESS_SESSION_SECRET; |
| 60 | + else process.env.EXPRESS_SESSION_SECRET = secret; |
| 61 | + |
| 62 | + const { default: logger } = |
| 63 | + jest.requireMock<typeof import('@/utils/logger')>('@/utils/logger'); |
| 64 | + const { getCounter } = jest.requireMock< |
| 65 | + typeof import('@/utils/instrumentation') |
| 66 | + >('@/utils/instrumentation'); |
| 67 | + const { default: Server } = |
| 68 | + jest.requireActual<typeof import('@/server')>('@/server'); |
| 69 | + const { serverlessHandler } = |
| 70 | + jest.requireActual<typeof import('@/serverless')>('@/serverless'); |
| 71 | + const config = |
| 72 | + jest.requireActual<typeof import('@/config')>('@/config'); |
| 73 | + expect(logger.error).not.toHaveBeenCalled(); |
| 74 | + |
| 75 | + const req = new IncomingMessage(new Socket()); |
| 76 | + req.url = '/api/health'; |
| 77 | + const res = new ServerResponse(req); |
| 78 | + if (first === 'server') await new Server().start(); |
| 79 | + await serverlessHandler(req, res); |
| 80 | + await new Server().start(); |
| 81 | + await serverlessHandler(req, res); |
| 82 | + |
| 83 | + expect(logger.error).toHaveBeenCalledTimes(count); |
| 84 | + const counter = jest.mocked(getCounter).mock.results[0].value; |
| 85 | + expect(counter.add).toHaveBeenCalledTimes(count); |
| 86 | + expect( |
| 87 | + JSON.stringify(jest.mocked(logger.error).mock.calls), |
| 88 | + ).not.toContain(config.EXPRESS_SESSION_SECRET); |
| 89 | + }, |
| 90 | + ); |
| 91 | + }, |
| 92 | +); |
0 commit comments