diff --git a/x-pack/plugins/reporting/server/config/create_config.test.ts b/x-pack/plugins/reporting/server/config/create_config.test.ts index a940e62a7225b..c649fff446a22 100644 --- a/x-pack/plugins/reporting/server/config/create_config.test.ts +++ b/x-pack/plugins/reporting/server/config/create_config.test.ts @@ -5,36 +5,11 @@ * 2.0. */ -import * as Rx from 'rxjs'; import { CoreSetup, PluginInitializerContext } from 'src/core/server'; +import { coreMock } from 'src/core/server/mocks'; import { LevelLogger } from '../lib'; +import { createMockConfigSchema } from '../test_helpers'; import { createConfig$ } from './create_config'; -import { ReportingConfigType } from './schema'; - -interface KibanaServer { - hostname?: string; - port?: number; - protocol?: string; -} - -const makeMockInitContext = (config: { - capture?: Partial; - encryptionKey?: string; - kibanaServer: Partial; -}): PluginInitializerContext => - ({ - config: { - create: () => - Rx.of({ - ...config, - capture: config.capture || { browser: { chromium: { disableSandbox: false } } }, - kibanaServer: config.kibanaServer || {}, - }), - }, - } as PluginInitializerContext); - -const makeMockCoreSetup = (serverInfo: KibanaServer): CoreSetup => - ({ http: { getServerInfo: () => serverInfo } } as any); describe('Reporting server createConfig$', () => { let mockCoreSetup: CoreSetup; @@ -42,10 +17,10 @@ describe('Reporting server createConfig$', () => { let mockLogger: LevelLogger; beforeEach(() => { - mockCoreSetup = makeMockCoreSetup({ hostname: 'kibanaHost', port: 5601, protocol: 'http' }); - mockInitContext = makeMockInitContext({ - kibanaServer: {}, - }); + mockCoreSetup = coreMock.createSetup(); + mockInitContext = coreMock.createPluginInitializerContext( + createMockConfigSchema({ kibanaServer: {} }) + ); mockLogger = ({ warn: jest.fn(), debug: jest.fn(), @@ -58,14 +33,18 @@ describe('Reporting server createConfig$', () => { }); it('creates random encryption key and default config using host, protocol, and port from server info', async () => { + mockInitContext = coreMock.createPluginInitializerContext({ + ...createMockConfigSchema({ kibanaServer: {} }), + encryptionKey: undefined, + }); const mockConfig$: any = mockInitContext.config.create(); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); expect(result.encryptionKey).toMatch(/\S{32,}/); // random 32 characters expect(result.kibanaServer).toMatchInlineSnapshot(` Object { - "hostname": "kibanaHost", - "port": 5601, + "hostname": "localhost", + "port": 80, "protocol": "http", } `); @@ -76,10 +55,11 @@ describe('Reporting server createConfig$', () => { }); it('uses the user-provided encryption key', async () => { - mockInitContext = makeMockInitContext({ - encryptionKey: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', - kibanaServer: {}, - }); + mockInitContext = coreMock.createPluginInitializerContext( + createMockConfigSchema({ + encryptionKey: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', + }) + ); const mockConfig$: any = mockInitContext.config.create(); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); expect(result.encryptionKey).toMatch('iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii'); @@ -87,14 +67,16 @@ describe('Reporting server createConfig$', () => { }); it('uses the user-provided encryption key, reporting kibanaServer settings to override server info', async () => { - mockInitContext = makeMockInitContext({ - encryptionKey: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', - kibanaServer: { - hostname: 'reportingHost', - port: 5677, - protocol: 'httpsa', - }, - }); + mockInitContext = coreMock.createPluginInitializerContext( + createMockConfigSchema({ + encryptionKey: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii', + kibanaServer: { + hostname: 'reportingHost', + port: 5677, + protocol: 'httpsa', + }, + }) + ); const mockConfig$: any = mockInitContext.config.create(); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); @@ -103,26 +85,36 @@ describe('Reporting server createConfig$', () => { "capture": Object { "browser": Object { "chromium": Object { - "disableSandbox": false, + "disableSandbox": true, }, }, }, + "csv": Object {}, "encryptionKey": "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "index": ".reporting", "kibanaServer": Object { "hostname": "reportingHost", "port": 5677, "protocol": "httpsa", }, + "queue": Object { + "indexInterval": "week", + "pollEnabled": true, + "pollInterval": 3000, + "timeout": 120000, + }, } `); expect((mockLogger.warn as any).mock.calls.length).toBe(0); }); it('uses user-provided disableSandbox: false', async () => { - mockInitContext = makeMockInitContext({ - encryptionKey: '888888888888888888888888888888888', - capture: { browser: { chromium: { disableSandbox: false } } }, - } as ReportingConfigType); + mockInitContext = coreMock.createPluginInitializerContext( + createMockConfigSchema({ + encryptionKey: '888888888888888888888888888888888', + capture: { browser: { chromium: { disableSandbox: false } } }, + }) + ); const mockConfig$: any = mockInitContext.config.create(); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); @@ -131,10 +123,12 @@ describe('Reporting server createConfig$', () => { }); it('uses user-provided disableSandbox: true', async () => { - mockInitContext = makeMockInitContext({ - encryptionKey: '888888888888888888888888888888888', - capture: { browser: { chromium: { disableSandbox: true } } }, - } as ReportingConfigType); + mockInitContext = coreMock.createPluginInitializerContext( + createMockConfigSchema({ + encryptionKey: '888888888888888888888888888888888', + capture: { browser: { chromium: { disableSandbox: true } } }, + }) + ); const mockConfig$: any = mockInitContext.config.create(); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); @@ -143,9 +137,11 @@ describe('Reporting server createConfig$', () => { }); it('provides a default for disableSandbox', async () => { - mockInitContext = makeMockInitContext({ - encryptionKey: '888888888888888888888888888888888', - } as ReportingConfigType); + mockInitContext = coreMock.createPluginInitializerContext( + createMockConfigSchema({ + encryptionKey: '888888888888888888888888888888888', + }) + ); const mockConfig$: any = mockInitContext.config.create(); const result = await createConfig$(mockCoreSetup, mockConfig$, mockLogger).toPromise(); diff --git a/x-pack/plugins/reporting/server/export_types/png/execute_job/index.ts b/x-pack/plugins/reporting/server/export_types/png/execute_job/index.ts index 4f439f494015d..c65e7bdf7a5ca 100644 --- a/x-pack/plugins/reporting/server/export_types/png/execute_job/index.ts +++ b/x-pack/plugins/reporting/server/export_types/png/execute_job/index.ts @@ -25,7 +25,6 @@ export const runTaskFnFactory: RunTaskFnFactory< > = function executeJobFactoryFn(reporting, parentLogger) { const config = reporting.getConfig(); const encryptionKey = config.get('encryptionKey'); - const logger = parentLogger.clone([PNG_JOB_TYPE, 'execute']); return async function runTask(jobId, job, cancellationToken) { const apmTrans = apm.startTransaction('reporting execute_job png', 'reporting'); @@ -33,9 +32,9 @@ export const runTaskFnFactory: RunTaskFnFactory< let apmGeneratePng: { end: () => void } | null | undefined; const generatePngObservable = await generatePngObservableFactory(reporting); - const jobLogger = logger.clone([jobId]); + const jobLogger = parentLogger.clone([PNG_JOB_TYPE, 'execute', jobId]); const process$: Rx.Observable = Rx.of(1).pipe( - mergeMap(() => decryptJobHeaders(encryptionKey, job.headers, logger)), + mergeMap(() => decryptJobHeaders(encryptionKey, job.headers, jobLogger)), map((decryptedHeaders) => omitBlockedHeaders(decryptedHeaders)), map((filteredHeaders) => getConditionalHeaders(config, filteredHeaders)), mergeMap((conditionalHeaders) => { diff --git a/x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts b/x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts index 2c4ad288e681b..8e215f87b52e0 100644 --- a/x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts +++ b/x-pack/plugins/reporting/server/export_types/printable_pdf/execute_job/index.ts @@ -28,20 +28,19 @@ export const runTaskFnFactory: RunTaskFnFactory< const encryptionKey = config.get('encryptionKey'); return async function runTask(jobId, job, cancellationToken) { - const logger = parentLogger.clone([PDF_JOB_TYPE, 'execute-job', jobId]); + const jobLogger = parentLogger.clone([PDF_JOB_TYPE, 'execute-job', jobId]); const apmTrans = apm.startTransaction('reporting execute_job pdf', 'reporting'); const apmGetAssets = apmTrans?.startSpan('get_assets', 'setup'); let apmGeneratePdf: { end: () => void } | null | undefined; const generatePdfObservable = await generatePdfObservableFactory(reporting); - const jobLogger = logger.clone([jobId]); const process$: Rx.Observable = Rx.of(1).pipe( - mergeMap(() => decryptJobHeaders(encryptionKey, job.headers, logger)), + mergeMap(() => decryptJobHeaders(encryptionKey, job.headers, jobLogger)), map((decryptedHeaders) => omitBlockedHeaders(decryptedHeaders)), map((filteredHeaders) => getConditionalHeaders(config, filteredHeaders)), mergeMap((conditionalHeaders) => - getCustomLogo(reporting, conditionalHeaders, job.spaceId, logger) + getCustomLogo(reporting, conditionalHeaders, job.spaceId, jobLogger) ), mergeMap(({ logo, conditionalHeaders }) => { const urls = getFullUrls(config, job); diff --git a/x-pack/plugins/reporting/server/routes/diagnostic/browser.test.ts b/x-pack/plugins/reporting/server/routes/diagnostic/browser.test.ts index 1267a7a9a69d7..d80be2d7f0f42 100644 --- a/x-pack/plugins/reporting/server/routes/diagnostic/browser.test.ts +++ b/x-pack/plugins/reporting/server/routes/diagnostic/browser.test.ts @@ -11,7 +11,11 @@ import { createInterface } from 'readline'; import { setupServer } from 'src/core/server/test_utils'; import supertest from 'supertest'; import { ReportingCore } from '../..'; -import { createMockLevelLogger, createMockReportingCore } from '../../test_helpers'; +import { + createMockLevelLogger, + createMockPluginSetup, + createMockReportingCore, +} from '../../test_helpers'; import { registerDiagnoseBrowser } from './browser'; import type { ReportingRequestHandlerContext } from '../../types'; @@ -55,12 +59,12 @@ describe('POST /diagnose/browser', () => { () => ({}) ); - const mockSetupDeps = ({ + const mockSetupDeps = createMockPluginSetup({ elasticsearch: { legacy: { client: { callAsInternalUser: jest.fn() } }, }, router: httpSetup.createRouter(''), - } as unknown) as any; + }); core = await createMockReportingCore(config, mockSetupDeps); diff --git a/x-pack/plugins/reporting/server/routes/diagnostic/config.test.ts b/x-pack/plugins/reporting/server/routes/diagnostic/config.test.ts index 3f95dd86d2af5..f35d8f5910da0 100644 --- a/x-pack/plugins/reporting/server/routes/diagnostic/config.test.ts +++ b/x-pack/plugins/reporting/server/routes/diagnostic/config.test.ts @@ -9,7 +9,11 @@ import { UnwrapPromise } from '@kbn/utility-types'; import { setupServer } from 'src/core/server/test_utils'; import supertest from 'supertest'; import { ReportingCore } from '../..'; -import { createMockReportingCore, createMockLevelLogger } from '../../test_helpers'; +import { + createMockReportingCore, + createMockLevelLogger, + createMockPluginSetup, +} from '../../test_helpers'; import { registerDiagnoseConfig } from './config'; import type { ReportingRequestHandlerContext } from '../../types'; @@ -33,7 +37,7 @@ describe('POST /diagnose/config', () => { () => ({}) ); - mockSetupDeps = ({ + mockSetupDeps = createMockPluginSetup({ elasticsearch: { legacy: { client: { callAsInternalUser: jest.fn() } }, }, diff --git a/x-pack/plugins/reporting/server/routes/diagnostic/screenshot.test.ts b/x-pack/plugins/reporting/server/routes/diagnostic/screenshot.test.ts index 754a116af7999..6c723764d9f0a 100644 --- a/x-pack/plugins/reporting/server/routes/diagnostic/screenshot.test.ts +++ b/x-pack/plugins/reporting/server/routes/diagnostic/screenshot.test.ts @@ -9,7 +9,11 @@ import { UnwrapPromise } from '@kbn/utility-types'; import { setupServer } from 'src/core/server/test_utils'; import supertest from 'supertest'; import { ReportingCore } from '../..'; -import { createMockReportingCore, createMockLevelLogger } from '../../test_helpers'; +import { + createMockReportingCore, + createMockLevelLogger, + createMockPluginSetup, +} from '../../test_helpers'; import { registerDiagnoseScreenshot } from './screenshot'; import type { ReportingRequestHandlerContext } from '../../types'; @@ -52,12 +56,12 @@ describe('POST /diagnose/screenshot', () => { () => ({}) ); - const mockSetupDeps = ({ + const mockSetupDeps = createMockPluginSetup({ elasticsearch: { legacy: { client: { callAsInternalUser: jest.fn() } }, }, router: httpSetup.createRouter(''), - } as unknown) as any; + }); core = await createMockReportingCore(config, mockSetupDeps); }); diff --git a/x-pack/plugins/reporting/server/routes/generation.test.ts b/x-pack/plugins/reporting/server/routes/generation.test.ts index 70a5e2475ab92..490b9b7e45664 100644 --- a/x-pack/plugins/reporting/server/routes/generation.test.ts +++ b/x-pack/plugins/reporting/server/routes/generation.test.ts @@ -12,7 +12,8 @@ import { setupServer } from 'src/core/server/test_utils'; import supertest from 'supertest'; import { ReportingCore } from '..'; import { ExportTypesRegistry } from '../lib/export_types_registry'; -import { createMockReportingCore, createMockLevelLogger } from '../test_helpers'; +import { createMockLevelLogger, createMockReportingCore } from '../test_helpers'; +import { createMockPluginSetup } from '../test_helpers/create_mock_reportingplugin'; import { registerJobGenerationRoutes } from './generation'; import type { ReportingRequestHandlerContext } from '../types'; @@ -37,7 +38,7 @@ describe('POST /api/reporting/generate', () => { case 'index': return '.reporting'; case 'queue.pollEnabled': - return false; + return true; default: return; } @@ -56,7 +57,7 @@ describe('POST /api/reporting/generate', () => { callClusterStub = sinon.stub().resolves({}); - const mockSetupDeps = ({ + const mockSetupDeps = createMockPluginSetup({ elasticsearch: { legacy: { client: { callAsInternalUser: callClusterStub } }, }, @@ -68,7 +69,7 @@ describe('POST /api/reporting/generate', () => { }, router: httpSetup.createRouter(''), licensing: { license$: of({ isActive: true, isAvailable: true, type: 'gold' }) }, - } as unknown) as any; + }); core = await createMockReportingCore(config, mockSetupDeps); diff --git a/x-pack/plugins/reporting/server/routes/jobs.test.ts b/x-pack/plugins/reporting/server/routes/jobs.test.ts index 847d27d44ea72..706a8d5dad7dd 100644 --- a/x-pack/plugins/reporting/server/routes/jobs.test.ts +++ b/x-pack/plugins/reporting/server/routes/jobs.test.ts @@ -12,7 +12,12 @@ import supertest from 'supertest'; import { ReportingCore } from '..'; import { ReportingInternalSetup } from '../core'; import { ExportTypesRegistry } from '../lib/export_types_registry'; -import { createMockConfig, createMockConfigSchema, createMockReportingCore } from '../test_helpers'; +import { + createMockConfig, + createMockConfigSchema, + createMockPluginSetup, + createMockReportingCore, +} from '../test_helpers'; import { ExportTypeDefinition, ReportingRequestHandlerContext } from '../types'; import { registerJobInfoRoutes } from './jobs'; @@ -41,7 +46,7 @@ describe('GET /api/reporting/jobs/download', () => { 'reporting', () => ({}) ); - core = await createMockReportingCore(config, ({ + const mockSetupDeps = createMockPluginSetup({ elasticsearch: { legacy: { client: { callAsInternalUser: jest.fn() } }, }, @@ -65,7 +70,9 @@ describe('GET /api/reporting/jobs/download', () => { type: 'gold', }), }, - } as unknown) as ReportingInternalSetup); + }); + + core = await createMockReportingCore(config, mockSetupDeps); // @ts-ignore exportTypesRegistry = new ExportTypesRegistry(); exportTypesRegistry.register({ diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts index e9e057b6d3211..ea8480ef3493d 100644 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts +++ b/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts @@ -12,6 +12,7 @@ jest.mock('../lib/create_queue'); import _ from 'lodash'; import * as Rx from 'rxjs'; +import { coreMock } from 'src/core/server/mocks'; import { ReportingConfig, ReportingCore } from '../'; import { featuresPluginMock } from '../../../features/server/mocks'; import { @@ -22,7 +23,6 @@ import { import { ReportingConfigType } from '../config'; import { ReportingInternalSetup, ReportingInternalStart } from '../core'; import { ReportingStore } from '../lib'; -import { ReportingStartDeps } from '../types'; import { createMockLevelLogger } from './create_mock_levellogger'; (initializeBrowserDriverFactory as jest.Mock< @@ -31,10 +31,7 @@ import { createMockLevelLogger } from './create_mock_levellogger'; (chromium as any).createDriverFactory.mockImplementation(() => ({})); -const createMockPluginSetup = ( - mockReportingCore: ReportingCore, - setupMock?: any -): ReportingInternalSetup => { +export const createMockPluginSetup = (setupMock?: any): ReportingInternalSetup => { return { features: featuresPluginMock.createSetup(), elasticsearch: setupMock.elasticsearch || { legacy: { client: {} } }, @@ -42,6 +39,7 @@ const createMockPluginSetup = ( router: setupMock.router, security: setupMock.security, licensing: { license$: Rx.of({ isAvailable: true, isActive: true, type: 'basic' }) } as any, + ...setupMock, }; }; @@ -58,6 +56,7 @@ const createMockPluginStart = ( savedObjects: startMock.savedObjects || { getScopedClient: jest.fn() }, uiSettings: startMock.uiSettings || { asScopedToClient: () => ({ get: jest.fn() }) }, store, + ...startMock, }; }; @@ -73,7 +72,7 @@ interface ReportingConfigTestType { export const createMockConfigSchema = ( overrides: Partial = {} -): ReportingConfigTestType => { +): ReportingConfigType => { // deeply merge the defaults and the provided partial schema return { index: '.reporting', @@ -93,13 +92,16 @@ export const createMockConfigSchema = ( ...overrides.capture, }, queue: { + indexInterval: 'week', + pollEnabled: true, + pollInterval: 3000, timeout: 120000, ...overrides.queue, }, csv: { ...overrides.csv, }, - }; + } as any; }; export const createMockConfig = ( @@ -114,35 +116,28 @@ export const createMockConfig = ( }; }; -export const createMockStartDeps = (startMock?: any): ReportingStartDeps => ({ - data: startMock.data, -}); - export const createMockReportingCore = async ( config: ReportingConfig, setupDepsMock: ReportingInternalSetup | undefined = undefined, startDepsMock: ReportingInternalStart | undefined = undefined ) => { - const mockReportingCore = { - getConfig: () => config, - getElasticsearchService: () => setupDepsMock?.elasticsearch, - } as ReportingCore; + config = config || {}; if (!setupDepsMock) { - setupDepsMock = createMockPluginSetup(mockReportingCore, {}); - } - if (!startDepsMock) { - startDepsMock = createMockPluginStart(mockReportingCore, {}); + setupDepsMock = createMockPluginSetup({}); } - config = config || {}; + const context = coreMock.createPluginInitializerContext(createMockConfigSchema()); const core = new ReportingCore(logger); + core.setConfig(config); core.pluginSetup(setupDepsMock); - core.setConfig(config); await core.pluginSetsUp(); - core.pluginStart(startDepsMock); + if (!startDepsMock) { + startDepsMock = createMockPluginStart(core, context); + } + await core.pluginStart(startDepsMock); await core.pluginStartsUp(); return core; diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_server.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_server.ts deleted file mode 100644 index 4805bf07a76a2..0000000000000 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_server.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { createHttpServer, createCoreContext } from 'src/core/server/http/test_utils'; -import { coreMock } from 'src/core/server/mocks'; -// eslint-disable-next-line @kbn/eslint/no-restricted-paths -import { ContextService } from 'src/core/server/context/context_service'; - -const coreId = Symbol('reporting'); - -export const createMockServer = async () => { - const coreContext = createCoreContext({ coreId }); - const contextService = new ContextService(coreContext); - - const server = createHttpServer(coreContext); - const httpSetup = await server.setup({ - context: contextService.setup({ pluginDependencies: new Map() }), - }); - const handlerContext = coreMock.createRequestHandlerContext(); - - httpSetup.registerRouteHandlerContext(coreId, 'core', async (ctx, req, res) => { - return handlerContext; - }); - - return { - server, - httpSetup, - handlerContext, - }; -}; diff --git a/x-pack/plugins/reporting/server/test_helpers/index.ts b/x-pack/plugins/reporting/server/test_helpers/index.ts index edf52fe5c2126..fe8c92d928af5 100644 --- a/x-pack/plugins/reporting/server/test_helpers/index.ts +++ b/x-pack/plugins/reporting/server/test_helpers/index.ts @@ -11,6 +11,6 @@ export { createMockLevelLogger } from './create_mock_levellogger'; export { createMockConfig, createMockConfigSchema, + createMockPluginSetup, createMockReportingCore, } from './create_mock_reportingplugin'; -export { createMockServer } from './create_mock_server'; diff --git a/x-pack/plugins/reporting/server/types.ts b/x-pack/plugins/reporting/server/types.ts index 4b7ef68f5e70b..b395738ad4445 100644 --- a/x-pack/plugins/reporting/server/types.ts +++ b/x-pack/plugins/reporting/server/types.ts @@ -14,11 +14,11 @@ import { LicensingPluginSetup } from '../../licensing/server'; import { AuthenticatedUser, SecurityPluginSetup } from '../../security/server'; import { SpacesPluginSetup } from '../../spaces/server'; import { CancellationToken } from '../common'; -import { BaseParams } from '../common/types'; +import { BaseParams, TaskRunResult } from '../common/types'; import { ReportingConfigType } from './config'; import { ReportingCore } from './core'; import { LevelLogger } from './lib'; -import { ReportTaskParams, TaskRunResult } from './lib/tasks'; +import { ReportTaskParams } from './lib/tasks'; /* * Plugin Contract