Skip to content

Commit

Permalink
[Canvas] Fixes the Copy Post Url link (elastic#54831)
Browse files Browse the repository at this point in the history
* Fixes the Copy Post Url link

* Adds tests

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
2 people authored and jkelastic committed Jan 17, 2020
1 parent 5e66b39 commit 415bbcf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ export const WorkpadExport = compose<ComponentProps, {}>(
enabled,
getExportUrl: type => {
if (type === 'pdf') {
const { createPdfUri } = getPdfUrl(
workpad,
{ pageCount },
kibana.services.http.basePath.prepend
);
return getAbsoluteUrl(createPdfUri);
const pdfUrl = getPdfUrl(workpad, { pageCount }, kibana.services.http.basePath.prepend);
return getAbsoluteUrl(pdfUrl);
}

throw new Error(strings.getUnknownExportErrorMessage(type));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

jest.mock('../../../../common/lib/fetch');

import { getPdfUrl, createPdf } from './utils';
import { workpads } from '../../../../__tests__/fixtures/workpads';
import { fetch } from '../../../../common/lib/fetch';

const addBasePath = jest.fn().mockImplementation(s => `basepath/${s}`);
const workpad = workpads[0];

test('getPdfUrl returns the correct url', () => {
const url = getPdfUrl(workpad, { pageCount: 2 }, addBasePath);

expect(url).toMatchInlineSnapshot(
`"basepath//api/reporting/generate/printablePdf?jobParams=(browserTimezone:America%2FPhoenix,layout:(dimensions:(height:0,width:0),id:preserve_layout),objectType:'canvas%20workpad',relativeUrls:!(%2Fapp%2Fcanvas%23%2Fexport%2Fworkpad%2Fpdf%2Fbase-workpad%2Fpage%2F1,%2Fapp%2Fcanvas%23%2Fexport%2Fworkpad%2Fpdf%2Fbase-workpad%2Fpage%2F2),title:'base%20workpad')"`
);
});

test('createPdf posts to create the pdf', () => {
createPdf(workpad, { pageCount: 2 }, addBasePath);

expect(fetch.post).toBeCalled();

const args = (fetch.post as jest.MockedFunction<typeof fetch.post>).mock.calls[0];

expect(args[0]).toMatchInlineSnapshot(`"basepath//api/reporting/generate/printablePdf"`);
expect(args[1]).toMatchInlineSnapshot(`
Object {
"jobParams": "(browserTimezone:America/Phoenix,layout:(dimensions:(height:0,width:0),id:preserve_layout),objectType:'canvas workpad',relativeUrls:!(/app/canvas#/export/workpad/pdf/base-workpad/page/1,/app/canvas#/export/workpad/pdf/base-workpad/page/2),title:'base workpad')",
}
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import rison from 'rison-node';
// @ts-ignore Untyped local.
import { fetch } from '../../../../common/lib/fetch';
import { getStartPlugins } from '../../../legacy';
import { CanvasWorkpad } from '../../../../types';

// type of the desired pdf output (print or preserve_layout)
Expand All @@ -25,7 +26,7 @@ interface PdfUrlData {
createPdfPayload: { jobParams: string };
}

export function getPdfUrl(
function getPdfUrlParts(
{ id, name: title, width, height }: CanvasWorkpad,
{ pageCount }: PageCount,
addBasePath: (path: string) => string
Expand Down Expand Up @@ -68,7 +69,16 @@ export function getPdfUrl(
};
}

export function getPdfUrl(...args: Arguments): string {
const urlParts = getPdfUrlParts(...args);

return `${urlParts.createPdfUri}?${getStartPlugins().__LEGACY.QueryString.param(
'jobParams',
urlParts.createPdfPayload.jobParams
)}`;
}

export function createPdf(...args: Arguments) {
const { createPdfUri, createPdfPayload } = getPdfUrl(...args);
const { createPdfUri, createPdfPayload } = getPdfUrlParts(...args);
return fetch.post(createPdfUri, createPdfPayload);
}
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/canvas/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { absoluteToParsedUrl } from 'ui/url/absolute_to_parsed_url'; // eslint-d
import { Storage } from '../../../../../src/plugins/kibana_utils/public'; // eslint-disable-line import/order
// @ts-ignore Untyped Kibana Lib
import { formatMsg } from 'ui/notify/lib/format_msg'; // eslint-disable-line import/order
// @ts-ignore Untyped Kibana Lib
import { QueryString } from 'ui/utils/query_string'; // eslint-disable-line import/order

const shimCoreSetup = {
...npSetup.core,
Expand All @@ -30,6 +32,7 @@ const shimStartPlugins: CanvasStartDeps = {
absoluteToParsedUrl,
// ToDo: Copy directly into canvas
formatMsg,
QueryString,
// ToDo: Remove in favor of core.application.register
setRootController: chrome.setRootController,
storage: Storage,
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/canvas/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface CanvasStartDeps {
__LEGACY: {
absoluteToParsedUrl: (url: string, basePath: string) => any;
formatMsg: any;
QueryString: any;
setRootController: Chrome['setRootController'];
storage: typeof Storage;
trackSubUrlForApp: Chrome['trackSubUrlForApp'];
Expand Down

0 comments on commit 415bbcf

Please sign in to comment.