Skip to content

Commit

Permalink
[Reporting/New Platform] Provide async access to server-side
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Feb 12, 2020
1 parent 3d338c1 commit e8ddc7b
Show file tree
Hide file tree
Showing 33 changed files with 731 additions and 542 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { JobDocPayload } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';

let mockServer: any;
beforeEach(() => {
beforeEach(async () => {
mockServer = createMockServer('');
});

Expand Down Expand Up @@ -147,59 +147,6 @@ describe('conditions', () => {
});
});

test('uses basePath from job when creating saved object service', async () => {
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({
job: { basePath: jobBasePath } as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

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

test(`uses basePath from server if job doesn't have a basePath when creating saved object service`, async () => {
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({
job: {} as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

expect(mockServer.savedObjects.getScopedSavedObjectsClient.mock.calls[0][0].getBasePath()).toBe(
'/sbp'
);
});

describe('config formatting', () => {
test(`lowercases server.host`, async () => {
mockServer = createMockServer({ settings: { 'server.host': 'COOL-HOSTNAME' } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { createMockServer } from '../../../test_helpers/create_mock_server';
import { getConditionalHeaders, getCustomLogo } from './index';
import { JobDocPayloadPDF } from '../../printable_pdf/types';

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

Expand All @@ -19,19 +21,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 { ReportingPlugin } from '../../../server/plugin';
import { ConditionalHeaders, ServerFacade } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types'; // Logo is PDF only

export const getCustomLogo = async ({
reporting,
server,
job,
conditionalHeaders,
}: {
reporting: ReportingPlugin;
server: ServerFacade;
job: JobDocPayloadPDF;
conditionalHeaders: ConditionalHeaders;
Expand All @@ -37,9 +40,8 @@ export const getCustomLogo = async ({
},
};

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 @@ -13,10 +13,11 @@ import {
ESQueueCreateJobFn,
} from '../../../types';
import { JobParamsDiscoverCsv } from '../types';
import { ReportingPlugin } from '../../../server/plugin';

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

return async function createJob(
Expand Down
Loading

0 comments on commit e8ddc7b

Please sign in to comment.