import * as Rx from 'rxjs' ; import { Writable } from 'stream' ; import { ReportingCore } from '../../../' ; import { CancellationToken } from '../../../../common/cancellation_token' ; import { cryptoFactory , LevelLogger } from '../../../lib' ; import { createMockConfigSchema , createMockReportingCore } from '../../../test_helpers' ; import { generatePdfObservable } from '../lib/generate_pdf' ; import { TaskPayloadPDF } from '../types' ; import { runTaskFnFactory } from './' ; jest . mock ( '../lib/generate_pdf' ) ; let content ; let mockReporting ; let stream ; const cancellationToken = { on : jest . fn ( ) , } as unknown as CancellationToken ; const mockLoggerFactory = { get : jest . fn ( ) . mockImplementation ( ( ) => ( { error : jest . fn ( ) , debug : jest . fn ( ) , warn : jest . fn ( ) , } ) ) , } ; const getMockLogger = ( ) => new LevelLogger ( mockLoggerFactory ) ; const mockEncryptionKey = 'testencryptionkey' ; const encryptHeaders = async ( headers ) => { const crypto = cryptoFactory ( mockEncryptionKey ) ; return await crypto . encrypt ( headers ) ; } ; const getBasePayload = ( baseObj ) => baseObj as TaskPayloadPDF ; beforeEach ( async ( ) => { content = '' ; stream = { write : jest . fn ( ( chunk ) => ( content += chunk ) ) } as unknown as typeof stream ; const reportingConfig = { 'server.basePath' : '/sbp' , index : '.reports-test' , encryptionKey : mockEncryptionKey , 'kibanaServer.hostname' : 'localhost' , 'kibanaServer.port' : 5601 , 'kibanaServer.protocol' : 'http' , } ; const mockSchema = createMockConfigSchema ( reportingConfig ) ; mockReporting = await createMockReportingCore ( mockSchema ) ; } ) ; afterEach ( ( ) => ( generatePdfObservable as jest . Mock ) . mockReset ( ) ) ; test ( `passes browserTimezone to generatePdf` , async ( ) => { const encryptedHeaders = await encryptHeaders ( { } ) ; ( generatePdfObservable as jest . Mock ) . mockReturnValue ( Rx . of ( { buffer : Buffer . from ( '' ) } ) ) ; const runTask = runTaskFnFactory ( mockReporting , getMockLogger ( ) ) ; const browserTimezone = 'UTC' ; await runTask ( 'pdfJobId' , getBasePayload ( { title : 'PDF Params Timezone Test' , relativeUrl : '/app/kibana#/something' , browserTimezone , headers : encryptedHeaders , } ) , cancellationToken , stream ) ; expect ( generatePdfObservable ) . toHaveBeenCalledWith ( expect . anything ( ) , expect . anything ( ) , expect . anything ( ) , expect . objectContaining ( { browserTimezone : 'UTC' } ) , undefined ) ; } ) ; test ( `returns content_type of application/pdf` , async ( ) => { const logger = getMockLogger ( ) ; const runTask = runTaskFnFactory ( mockReporting , logger ) ; const encryptedHeaders = await encryptHeaders ( { } ) ; ( generatePdfObservable as jest . Mock ) . mockReturnValue ( Rx . of ( { buffer : Buffer . from ( '' ) } ) ) ; const { content_type : contentType } = await runTask ( 'pdfJobId' , getBasePayload ( { objects : [ ] , headers : encryptedHeaders } ) , cancellationToken , stream ) ; expect ( contentType ) . toBe ( 'application/pdf' ) ; } ) ; test ( `returns content of generatePdf getBuffer base64 encoded` , async ( ) => { const testContent = 'test content' ; ( generatePdfObservable as jest . Mock ) . mockReturnValue ( Rx . of ( { buffer : Buffer . from ( testContent ) } ) ) ; const runTask = runTaskFnFactory ( mockReporting , getMockLogger ( ) ) ; const encryptedHeaders = await encryptHeaders ( { } ) ; await runTask ( 'pdfJobId' , getBasePayload ( { objects : [ ] , headers : encryptedHeaders } ) , cancellationToken , stream ) ; expect ( content ) . toEqual ( testContent ) ; } ) ;