-
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.
- Loading branch information
Showing
5 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
x-pack/plugins/canvas/server/routes/shareables/download.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,55 @@ | ||
/* | ||
* 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('fs'); | ||
|
||
import fs from 'fs'; | ||
import { | ||
IRouter, | ||
kibanaResponseFactory, | ||
RequestHandlerContext, | ||
RequestHandler, | ||
} from 'src/core/server'; | ||
import { httpServiceMock, httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; | ||
import { initializeDownloadShareableWorkpadRoute } from './download'; | ||
|
||
const mockRouteContext = {} as RequestHandlerContext; | ||
const path = `api/canvas/workpad/find`; | ||
const mockRuntime = 'Canvas shareable runtime'; | ||
|
||
describe('Download Canvas shareables runtime', () => { | ||
let routeHandler: RequestHandler<any, any, any>; | ||
|
||
beforeEach(() => { | ||
const httpService = httpServiceMock.createSetupContract(); | ||
const router = httpService.createRouter('') as jest.Mocked<IRouter>; | ||
initializeDownloadShareableWorkpadRoute({ | ||
router, | ||
logger: loggingServiceMock.create().get(), | ||
}); | ||
|
||
routeHandler = router.get.mock.calls[0][1]; | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it(`returns 200 with canvas shareables runtime`, async () => { | ||
const request = httpServerMock.createKibanaRequest({ | ||
method: 'get', | ||
path, | ||
}); | ||
|
||
const readFileSyncMock = fs.readFileSync as jest.Mock; | ||
readFileSyncMock.mockReturnValueOnce(mockRuntime); | ||
|
||
const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.payload).toMatchInlineSnapshot(`"Canvas shareable runtime"`); | ||
}); | ||
}); |
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
Empty file.
78 changes: 78 additions & 0 deletions
78
x-pack/plugins/canvas/server/routes/shareables/zip.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,78 @@ | ||
/* | ||
* 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('archiver'); | ||
|
||
const archiver = require('archiver') as jest.Mock; | ||
import { | ||
IRouter, | ||
kibanaResponseFactory, | ||
RequestHandlerContext, | ||
RequestHandler, | ||
} from 'src/core/server'; | ||
import { httpServiceMock, httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; | ||
import { initializeZipShareableWorkpadRoute } from './zip'; | ||
import { API_ROUTE_SHAREABLE_ZIP } from '../../../../../legacy/plugins/canvas/common/lib'; | ||
import { | ||
SHAREABLE_RUNTIME_FILE, | ||
SHAREABLE_RUNTIME_SRC, | ||
SHAREABLE_RUNTIME_NAME, | ||
} from '../../../../../legacy/plugins/canvas/shareable_runtime/constants'; | ||
|
||
const mockRouteContext = {} as RequestHandlerContext; | ||
const mockWorkpad = {}; | ||
const routePath = API_ROUTE_SHAREABLE_ZIP; | ||
|
||
describe('Zips Canvas shareables runtime together with workpad', () => { | ||
let routeHandler: RequestHandler<any, any, any>; | ||
|
||
beforeEach(() => { | ||
const httpService = httpServiceMock.createSetupContract(); | ||
const router = httpService.createRouter('') as jest.Mocked<IRouter>; | ||
initializeZipShareableWorkpadRoute({ | ||
router, | ||
logger: loggingServiceMock.create().get(), | ||
}); | ||
|
||
routeHandler = router.post.mock.calls[0][1]; | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it(`returns 200 with zip file with runtime and workpad`, async () => { | ||
const request = httpServerMock.createKibanaRequest({ | ||
method: 'get', | ||
path: routePath, | ||
body: mockWorkpad, | ||
}); | ||
|
||
const mockArchive = { | ||
append: jest.fn(), | ||
file: jest.fn(), | ||
finalize: jest.fn(), | ||
}; | ||
|
||
archiver.mockReturnValueOnce(mockArchive); | ||
|
||
const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.payload).toBe(mockArchive); | ||
expect(mockArchive.append).toHaveBeenCalledWith(JSON.stringify(mockWorkpad), { | ||
name: 'workpad.json', | ||
}); | ||
expect(mockArchive.file).toHaveBeenCalledTimes(2); | ||
expect(mockArchive.file).nthCalledWith(1, `${SHAREABLE_RUNTIME_SRC}/template.html`, { | ||
name: 'index.html', | ||
}); | ||
expect(mockArchive.file).nthCalledWith(2, SHAREABLE_RUNTIME_FILE, { | ||
name: `${SHAREABLE_RUNTIME_NAME}.js`, | ||
}); | ||
expect(mockArchive.finalize).toBeCalled(); | ||
}); | ||
}); |
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