-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine common pdf png functions (#25152)
* Create common routines for PDF and PNG report processing * REmove Blacklisted headers not needed * Changes for issues with PDF and PNG testing As per Joel Griffith suggestion I added removal of toast notifications after each click download report. And removed setting of image density to 300. * removed uneccesary image compare routine * Moved server from property on job to passed in parameter. * consolidated PDF and PNG testing Moved test code from PDF and PNG index.test.js into a common index.test.js that now tests the common functions. * Typescripted the common functions and broke out tests to individual test files * Create a common create mock server function that all new tests call * Removed unnecessary exception check and removed test include from index.ts
- Loading branch information
1 parent
aca3fe3
commit dc2953a
Showing
25 changed files
with
711 additions
and
800 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/reporting/export_types/common/execute_job/add_force_now_query_string.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { createMockServer } from '../../../test_helpers/create_mock_server'; | ||
import { addForceNowQuerystring } from './index'; | ||
|
||
let mockServer: any; | ||
beforeEach(() => { | ||
mockServer = createMockServer(''); | ||
}); | ||
|
||
test(`fails if no URL is passed`, async () => { | ||
await expect( | ||
addForceNowQuerystring({ | ||
job: {}, | ||
server: mockServer, | ||
}) | ||
).rejects.toBeDefined(); | ||
}); | ||
|
||
test(`adds forceNow to hash's query, if it exists`, async () => { | ||
const forceNow = '2000-01-01T00:00:00.000Z'; | ||
const { urls } = await addForceNowQuerystring({ | ||
job: { relativeUrl: '/app/kibana#/something', forceNow }, | ||
server: mockServer, | ||
}); | ||
|
||
expect(urls[0]).toEqual( | ||
'http://localhost:5601/sbp/app/kibana#/something?forceNow=2000-01-01T00%3A00%3A00.000Z' | ||
); | ||
}); | ||
|
||
test(`appends forceNow to hash's query, if it exists`, async () => { | ||
const forceNow = '2000-01-01T00:00:00.000Z'; | ||
|
||
const { urls } = await addForceNowQuerystring({ | ||
job: { | ||
relativeUrl: '/app/kibana#/something?_g=something', | ||
forceNow, | ||
}, | ||
server: mockServer, | ||
}); | ||
|
||
expect(urls[0]).toEqual( | ||
'http://localhost:5601/sbp/app/kibana#/something?_g=something&forceNow=2000-01-01T00%3A00%3A00.000Z' | ||
); | ||
}); | ||
|
||
test(`doesn't append forceNow query to url, if it doesn't exists`, async () => { | ||
const { urls } = await addForceNowQuerystring({ | ||
job: { | ||
relativeUrl: '/app/kibana#/something', | ||
}, | ||
server: mockServer, | ||
}); | ||
|
||
expect(urls[0]).toEqual('http://localhost:5601/sbp/app/kibana#/something'); | ||
}); |
60 changes: 60 additions & 0 deletions
60
x-pack/plugins/reporting/export_types/common/execute_job/add_force_now_query_string.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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. | ||
*/ | ||
// @ts-ignore | ||
import url from 'url'; | ||
import { ConditionalHeaders, KbnServer, ReportingJob } from '../../../types'; | ||
import { getAbsoluteUrlFactory } from './get_absolute_url'; | ||
|
||
function getSavedObjectAbsoluteUrl(job: ReportingJob, relativeUrl: string, server: KbnServer) { | ||
const getAbsoluteUrl: any = getAbsoluteUrlFactory(server); | ||
|
||
const { pathname: path, hash, search } = url.parse(relativeUrl); | ||
return getAbsoluteUrl({ basePath: job.basePath, path, hash, search }); | ||
} | ||
|
||
export const addForceNowQuerystring = async ({ | ||
job, | ||
conditionalHeaders, | ||
logo, | ||
server, | ||
}: { | ||
job: ReportingJob; | ||
conditionalHeaders?: ConditionalHeaders; | ||
logo?: any; | ||
server: KbnServer; | ||
}) => { | ||
// if no URLS then its from PNG which should only have one so put it in the array and process as PDF does | ||
if (!job.urls) { | ||
if (!job.relativeUrl) { | ||
throw new Error(`Unable to generate report. Url is not defined.`); | ||
} | ||
job.urls = [getSavedObjectAbsoluteUrl(job, job.relativeUrl, server)]; | ||
} | ||
|
||
const urls = job.urls.map(jobUrl => { | ||
if (!job.forceNow) { | ||
return jobUrl; | ||
} | ||
|
||
const parsed: any = url.parse(jobUrl, true); | ||
const hash: any = url.parse(parsed.hash.replace(/^#/, ''), true); | ||
|
||
const transformedHash = url.format({ | ||
pathname: hash.pathname, | ||
query: { | ||
...hash.query, | ||
forceNow: job.forceNow, | ||
}, | ||
}); | ||
|
||
return url.format({ | ||
...parsed, | ||
hash: transformedHash, | ||
}); | ||
}); | ||
|
||
return { job, conditionalHeaders, logo, urls, server }; | ||
}; |
45 changes: 45 additions & 0 deletions
45
x-pack/plugins/reporting/export_types/common/execute_job/decrypt_job_headers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// @ts-ignore | ||
import { cryptoFactory } from '../../../server/lib/crypto'; | ||
import { createMockServer } from '../../../test_helpers/create_mock_server'; | ||
import { decryptJobHeaders } from './index'; | ||
|
||
let mockServer: any; | ||
beforeEach(() => { | ||
mockServer = createMockServer(''); | ||
}); | ||
|
||
const encryptHeaders = async (headers: Record<string, string>) => { | ||
const crypto = cryptoFactory(mockServer); | ||
return await crypto.encrypt(headers); | ||
}; | ||
|
||
describe('headers', () => { | ||
test(`fails if it can't decrypt headers`, async () => { | ||
await expect( | ||
decryptJobHeaders({ | ||
job: { relativeUrl: '/app/kibana#/something', timeRange: {} }, | ||
server: mockServer, | ||
}) | ||
).rejects.toBeDefined(); | ||
}); | ||
|
||
test(`passes back decrypted headers that were passed in`, async () => { | ||
const headers = { | ||
foo: 'bar', | ||
baz: 'quix', | ||
}; | ||
|
||
const encryptedHeaders = await encryptHeaders(headers); | ||
const { decryptedHeaders } = await decryptJobHeaders({ | ||
job: { relativeUrl: '/app/kibana#/something', headers: encryptedHeaders }, | ||
server: mockServer, | ||
}); | ||
expect(decryptedHeaders).toEqual(headers); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/reporting/export_types/common/execute_job/decrypt_job_headers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
// @ts-ignore | ||
import { cryptoFactory } from '../../../server/lib/crypto'; | ||
import { CryptoFactory, KbnServer, ReportingJob } from '../../../types'; | ||
|
||
export const decryptJobHeaders = async ({ | ||
job, | ||
server, | ||
}: { | ||
job: ReportingJob; | ||
server: KbnServer; | ||
}) => { | ||
const crypto: CryptoFactory = cryptoFactory(server); | ||
const decryptedHeaders: string = await crypto.decrypt(job.headers); | ||
return { job, decryptedHeaders, server }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.