Skip to content

Commit

Permalink
Merge pull request #14 from tsullivan/reporting-packages
Browse files Browse the repository at this point in the history
Reporting packages
  • Loading branch information
rshen91 authored Nov 14, 2023
2 parents 7a194e7 + b1ac942 commit f3d4bc2
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import { coreMock, elasticsearchServiceMock, loggingSystemMock } from '@kbn/core
import { Writable } from 'stream';
import { CancellationToken } from '@kbn/reporting-common';
import { discoverPluginMock } from '@kbn/discover-plugin/server/mocks';
import { createFieldFormatsStartMock } from '@kbn/field-formats-plugin/server/mocks';
import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import { setFieldFormats } from '@kbn/reporting-server';
import { createMockConfigSchema } from '@kbn/reporting-mocks-server';

import { CsvSearchSourceExportType } from '.';
Expand All @@ -35,6 +37,10 @@ let stream: jest.Mocked<Writable>;
let mockCsvSearchSourceExportType: CsvSearchSourceExportType;

beforeAll(async () => {
// use fieldFormats plugin for csv formats
// normally, this is done in the Reporting plugin
setFieldFormats(createFieldFormatsStartMock());

const crypto = nodeCrypto({ encryptionKey });

encryptedHeaders = await crypto.encrypt(headers);
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-reporting/export_types/csv/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"@kbn/reporting-export-types-csv-common",
"@kbn/reporting-mocks-server",
"@kbn/core-http-request-handler-context-server",
"@kbn/field-formats-plugin",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

import type { ReportingServerInfo } from '@kbn/reporting-common/types';
import type { TaskPayloadPDF } from '@kbn/reporting-export-types-pdf-common';
import { createMockConfigSchema } from '@kbn/reporting-mocks-server';

import type { ReportingConfigType } from '.';
import { getFullUrls } from './get_full_urls';

const getMockJob = (base: object) => base as TaskPayloadPDF;
const mockConfig = { kibanaServer: {} } as unknown as ReportingConfigType;
const mockConfig = createMockConfigSchema();
const mockServerInfo: ReportingServerInfo = {
hostname: 'localhost',
protocol: 'http',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {

import type { ReportingServerInfo } from '@kbn/reporting-common/types';
import type { TaskPayloadPDF } from '@kbn/reporting-export-types-pdf-common';
import type { ReportingConfigType } from '@kbn/reporting-server';

import type { ReportingConfigType } from '.';
import { getAbsoluteUrlFactory } from './get_absolute_url';
import { validateUrls } from './validate_urls';

Expand Down
11 changes: 4 additions & 7 deletions packages/kbn-reporting/export_types/pdf/printable_pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ import {
PDF_JOB_TYPE,
TaskPayloadPDF,
} from '@kbn/reporting-export-types-pdf-common';
import {
ExportType,
decryptJobHeaders,
getCustomLogo,
getFullUrls,
validateUrls,
} from '@kbn/reporting-server';
import { ExportType, decryptJobHeaders } from '@kbn/reporting-server';

import { generatePdfObservable } from './generate_pdf';
import { validateUrls } from './validate_urls';
import { getCustomLogo } from './get_custom_logo';
import { getFullUrls } from './get_full_urls';

/**
* @deprecated
Expand Down
8 changes: 2 additions & 6 deletions packages/kbn-reporting/export_types/pdf/printable_pdf_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ import {
PDF_REPORT_TYPE_V2,
TaskPayloadPDFV2,
} from '@kbn/reporting-export-types-pdf-common';
import {
decryptJobHeaders,
getFullRedirectAppUrl,
ExportType,
getCustomLogo,
} from '@kbn/reporting-server';
import { decryptJobHeaders, getFullRedirectAppUrl, ExportType } from '@kbn/reporting-server';

import { generatePdfObservableV2 } from './generate_pdf_v2';
import { getCustomLogo } from './get_custom_logo';

export class PdfExportType extends ExportType<JobParamsPDFV2, TaskPayloadPDFV2> {
id = PDF_REPORT_TYPE_V2;
Expand Down
9 changes: 2 additions & 7 deletions packages/kbn-reporting/export_types/png/png_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test(`passes browserTimezone to generatePng`, async () => {
'pngJobId',
getBasePayload({
forceNow: 'test',
locatorParams: [{ version: 'test', id: 'test', params: {} }] as LocatorParams[],
locatorParams: [],
browserTimezone,
headers: encryptedHeaders,
}),
Expand All @@ -92,14 +92,9 @@ test(`passes browserTimezone to generatePng`, async () => {
expect.anything(),
expect.anything(),
expect.objectContaining({
urls: [
[
'http://localhost:80/mock-server-basepath/app/reportingRedirect?forceNow=test',
{ id: 'test', params: {}, version: 'test' },
],
],
browserTimezone: 'UTC',
headers: {},
layout: { id: 'preserve_layout' },
})
);
});
Expand Down
122 changes: 122 additions & 0 deletions packages/kbn-reporting/server/__snapshots__/config_schema.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions packages/kbn-reporting/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
export { cryptoFactory } from './crypto';
export { decryptJobHeaders } from './decrypt_job_headers';
export { ExportType } from './export_type';
export { getAbsoluteUrlFactory } from './get_absolute_url';
export { getCustomLogo } from './get_custom_logo';
export { getFullRedirectAppUrl } from './get_full_redirect_app_url';
export { getFullUrls } from './get_full_urls';
export { getFieldFormats, setFieldFormats } from './services';
export { validateUrls } from './validate_urls';

export type { BaseExportTypeSetupDeps, BaseExportTypeStartDeps } from './export_type';
export { ConfigSchema } from './config_schema';
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-reporting/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@kbn/core-elasticsearch-server",
"@kbn/core-http-router-server-internal",
"@kbn/core-http-request-handler-context-server",
"@kbn/reporting-export-types-pdf-common",
"@kbn/config-schema",
]
}
99 changes: 1 addition & 98 deletions x-pack/plugins/reporting/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@
* 2.0.
*/

import { Writable } from 'stream';

import nodeCrypto from '@elastic/node-crypto';
import type { CoreSetup, CoreStart, Logger } from '@kbn/core/server';
import { coreMock, elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks';
import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import { discoverPluginMock } from '@kbn/discover-plugin/server/mocks';
import { CancellationToken } from '@kbn/reporting-common';
import { setFieldFormats } from '@kbn/reporting-server';
import { coreMock, loggingSystemMock } from '@kbn/core/server/mocks';
import { createMockConfigSchema } from '@kbn/reporting-mocks-server';

import { CSV_REPORT_TYPE, CSV_REPORT_TYPE_V2 } from '@kbn/reporting-export-types-csv-common';
import { PDF_REPORT_TYPE, PDF_REPORT_TYPE_V2 } from '@kbn/reporting-export-types-pdf-common';
import { PNG_REPORT_TYPE_V2 } from '@kbn/reporting-export-types-png-common';

import { CsvSearchSourceExportType } from '@kbn/reporting-export-types-csv';
import { createMockScreenshottingStart } from '@kbn/screenshotting-plugin/server/mock';
import type { ReportingCore, ReportingInternalStart } from './core';
import { ExportTypesRegistry } from './lib/export_types_registry';
import { ReportingPlugin } from './plugin';
Expand Down Expand Up @@ -152,92 +143,4 @@ describe('Reporting Plugin', () => {
);
});
});
// Requires field formats which is set on the plugin level to render for csv types
describe('CsvSearchSource export type functionality', () => {
// create the mock csv search source export type
jest.mock('@kbn/generate-csv', () => ({
CsvGenerator: class CsvGeneratorMock {
generateData() {
return {
size: 123,
content_type: 'text/csv',
};
}
},
}));

let mockCsvSearchSourceExportType: CsvSearchSourceExportType;

const mockLogger = loggingSystemMock.createLogger();
const encryptionKey = 'tetkey';
const headers = { sid: 'cooltestheaders' };
let encryptedHeaders: string;
let stream: jest.Mocked<Writable>;

beforeAll(async () => {
const crypto = nodeCrypto({ encryptionKey });

encryptedHeaders = await crypto.encrypt(headers);
const configType = createMockConfigSchema({
encryptionKey,
csv: {
checkForFormulas: true,
escapeFormulaValues: true,
maxSizeBytes: 180000,
scroll: { size: 500, duration: '30s' },
},
});
const mockCoreSetup = coreMock.createSetup();
const mockCoreStart = coreMock.createStart();
const context = coreMock.createPluginInitializerContext(configType);

mockCsvSearchSourceExportType = new CsvSearchSourceExportType(
mockCoreSetup,
configType,
mockLogger,
context
);

mockCsvSearchSourceExportType.setup({
basePath: { set: jest.fn() },
});

mockCsvSearchSourceExportType.start({
esClient: elasticsearchServiceMock.createClusterClient(),
savedObjects: mockCoreStart.savedObjects,
uiSettings: mockCoreStart.uiSettings,
discover: discoverPluginMock.createStartContract(),
data: dataPluginMock.createStartContract(),
screenshotting: createMockScreenshottingStart(),
});
});

beforeEach(async () => {
stream = {} as typeof stream;
setFieldFormats(pluginStart.fieldFormats);
});

xtest('gets the csv content from job parameters', async () => {
const payload = await mockCsvSearchSourceExportType.runTask(
'cool-job-id',
{
headers: encryptedHeaders,
browserTimezone: 'US/Alaska',
searchSource: {},
objectType: 'search',
title: 'Test Search',
version: '7.13.0',
},
new CancellationToken(),
stream
);

expect(payload).toMatchInlineSnapshot(`
Object {
"content_type": "text/csv",
"size": 123,
}
`);
});
});
});

0 comments on commit f3d4bc2

Please sign in to comment.