Skip to content

Commit

Permalink
[Reporting/New Platform] Provide async access to server-side dependen…
Browse files Browse the repository at this point in the history
…cies (elastic#56824)

* [Reporting/New Platform] Provide async access to server-side

* consistent name for reportingPlugin

* Prettier changes

* simplify reporting usage collector setup

* add more tests

* extract internals access to separate core class

* fix tests

* fix imports for jest and build

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
tsullivan and elasticmachine committed Feb 14, 2020
1 parent 5bcda58 commit f059d31
Show file tree
Hide file tree
Showing 50 changed files with 1,112 additions and 748 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { cryptoFactory } from '../../../server/lib/crypto';
import { createMockServer } from '../../../test_helpers/create_mock_server';
import { createMockServer } from '../../../test_helpers';
import { Logger } from '../../../types';
import { decryptJobHeaders } from './decrypt_job_headers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { createMockServer } from '../../../test_helpers/create_mock_server';
import { getConditionalHeaders, getCustomLogo } from './index';
import { createMockReportingCore, createMockServer } from '../../../test_helpers';
import { ReportingCore } from '../../../server';
import { JobDocPayload } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { getConditionalHeaders, getCustomLogo } from './index';

let mockReportingPlugin: ReportingCore;
let mockServer: any;
beforeEach(() => {
beforeEach(async () => {
mockReportingPlugin = await createMockReportingCore();
mockServer = createMockServer('');
});

Expand Down Expand Up @@ -148,56 +151,76 @@ describe('conditions', () => {
});

test('uses basePath from job when creating saved object service', async () => {
const mockGetSavedObjectsClient = jest.fn();
mockReportingPlugin.getSavedObjectsClient = mockGetSavedObjectsClient;

const permittedHeaders = {
foo: 'bar',
baz: 'quix',
};

const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
filteredHeaders: permittedHeaders,
server: mockServer,
});

const logo = 'custom-logo';
mockServer.uiSettingsServiceFactory().get.mockReturnValue(logo);

const jobBasePath = '/sbp/s/marketing';
await getCustomLogo({
reporting: mockReportingPlugin,
job: { basePath: jobBasePath } as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

expect(mockServer.savedObjects.getScopedSavedObjectsClient.mock.calls[0][0].getBasePath()).toBe(
jobBasePath
);
const getBasePath = mockGetSavedObjectsClient.mock.calls[0][0].getBasePath;
expect(getBasePath()).toBe(jobBasePath);
});

test(`uses basePath from server if job doesn't have a basePath when creating saved object service`, async () => {
const mockGetSavedObjectsClient = jest.fn();
mockReportingPlugin.getSavedObjectsClient = mockGetSavedObjectsClient;

const permittedHeaders = {
foo: 'bar',
baz: 'quix',
};

const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
filteredHeaders: permittedHeaders,
server: mockServer,
});

const logo = 'custom-logo';
mockServer.uiSettingsServiceFactory().get.mockReturnValue(logo);

await getCustomLogo({
reporting: mockReportingPlugin,
job: {} as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

expect(mockServer.savedObjects.getScopedSavedObjectsClient.mock.calls[0][0].getBasePath()).toBe(
'/sbp'
);
const getBasePath = mockGetSavedObjectsClient.mock.calls[0][0].getBasePath;
expect(getBasePath()).toBe(`/sbp`);
expect(mockGetSavedObjectsClient.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"getBasePath": [Function],
"headers": Object {
"baz": "quix",
"foo": "bar",
},
"path": "/",
"raw": Object {
"req": Object {
"url": "/",
},
},
"route": Object {
"settings": Object {},
},
"url": Object {
"href": "/",
},
},
]
`);
});

describe('config formatting', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { createMockServer } from '../../../test_helpers/create_mock_server';
import { getConditionalHeaders, getCustomLogo } from './index';
import { ReportingCore } from '../../../server';
import { createMockReportingCore, createMockServer } from '../../../test_helpers';
import { ServerFacade } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { getConditionalHeaders, getCustomLogo } from './index';

let mockServer: any;
beforeEach(() => {
let mockReportingPlugin: ReportingCore;
let mockServer: ServerFacade;
beforeEach(async () => {
mockReportingPlugin = await createMockReportingCore();
mockServer = createMockServer('');
});

Expand All @@ -19,19 +23,30 @@ test(`gets logo from uiSettings`, async () => {
baz: 'quix',
};

const mockGet = jest.fn();
mockGet.mockImplementationOnce((...args: any[]) => {
if (args[0] === 'xpackReporting:customPdfLogo') {
return 'purple pony';
}
throw new Error('wrong caller args!');
});
mockReportingPlugin.getUiSettingsServiceFactory = jest.fn().mockResolvedValue({
get: mockGet,
});

const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayloadPDF,
filteredHeaders: permittedHeaders,
server: mockServer,
});

const { logo } = await getCustomLogo({
reporting: mockReportingPlugin,
job: {} as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

mockServer.uiSettingsServiceFactory().get.mockReturnValue(logo);

expect(mockServer.uiSettingsServiceFactory().get).toBeCalledWith('xpackReporting:customPdfLogo');
expect(mockGet).toBeCalledWith('xpackReporting:customPdfLogo');
expect(logo).toBe('purple pony');
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
*/

import { UI_SETTINGS_CUSTOM_PDF_LOGO } from '../../../common/constants';
import { ReportingCore } from '../../../server';
import { ConditionalHeaders, ServerFacade } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types'; // Logo is PDF only

export const getCustomLogo = async ({
reporting,
server,
job,
conditionalHeaders,
}: {
reporting: ReportingCore;
server: ServerFacade;
job: JobDocPayloadPDF;
conditionalHeaders: ConditionalHeaders;
Expand All @@ -27,19 +30,12 @@ export const getCustomLogo = async ({
getBasePath: () => job.basePath || serverBasePath,
path: '/',
route: { settings: {} },
url: {
href: '/',
},
raw: {
req: {
url: '/',
},
},
url: { href: '/' },
raw: { req: { url: '/' } },
};

const savedObjects = server.savedObjects;
const savedObjectsClient = savedObjects.getScopedSavedObjectsClient(fakeRequest);
const uiSettings = server.uiSettingsServiceFactory({ savedObjectsClient });
const savedObjectsClient = await reporting.getSavedObjectsClient(fakeRequest);
const uiSettings = await reporting.getUiSettingsServiceFactory(savedObjectsClient);
const logo: string = await uiSettings.get(UI_SETTINGS_CUSTOM_PDF_LOGO);
return { conditionalHeaders, logo };
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { createMockServer } from '../../../test_helpers/create_mock_server';
import { createMockServer } from '../../../test_helpers';
import { ServerFacade } from '../../../types';
import { JobDocPayloadPNG } from '../../png/types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
*/

import * as Rx from 'rxjs';
import { first, concatMap, take, toArray, mergeMap } from 'rxjs/operators';
import { ServerFacade, CaptureConfig, HeadlessChromiumDriverFactory } from '../../../../types';
import { ScreenshotResults, ScreenshotObservableOpts } from './types';
import { injectCustomCss } from './inject_css';
import { openUrl } from './open_url';
import { waitForRenderComplete } from './wait_for_render';
import { getNumberOfItems } from './get_number_of_items';
import { waitForElementsToBeInDOM } from './wait_for_dom_elements';
import { getTimeRange } from './get_time_range';
import { concatMap, first, mergeMap, take, toArray } from 'rxjs/operators';
import { CaptureConfig, HeadlessChromiumDriverFactory, ServerFacade } from '../../../../types';
import { getElementPositionAndAttributes } from './get_element_position_data';
import { getNumberOfItems } from './get_number_of_items';
import { getScreenshots } from './get_screenshots';
import { getTimeRange } from './get_time_range';
import { injectCustomCss } from './inject_css';
import { openUrl } from './open_url';
import { scanPage } from './scan_page';
import { skipTelemetry } from './skip_telemetry';
import { ScreenshotObservableOpts, ScreenshotResults } from './types';
import { waitForElementsToBeInDOM } from './wait_for_dom_elements';
import { waitForRenderComplete } from './wait_for_render';

export function screenshotsObservableFactory(
server: ServerFacade,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ReportingCore } from '../../../server';
import { cryptoFactory } from '../../../server/lib/crypto';
import {
CreateJobFactory,
ConditionalHeaders,
ServerFacade,
RequestFacade,
CreateJobFactory,
ESQueueCreateJobFn,
RequestFacade,
ServerFacade,
} from '../../../types';
import { JobParamsDiscoverCsv } from '../types';

export const createJobFactory: CreateJobFactory<ESQueueCreateJobFn<
JobParamsDiscoverCsv
>> = function createJobFactoryFn(server: ServerFacade) {
>> = function createJobFactoryFn(reporting: ReportingCore, server: ServerFacade) {
const crypto = cryptoFactory(server);

return async function createJob(
Expand Down
Loading

0 comments on commit f059d31

Please sign in to comment.