-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Canvas] Migrate Shareable Routes to NP #56053
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
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"`); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 { readFileSync } from 'fs'; | ||
import { SHAREABLE_RUNTIME_FILE } from '../../../../../legacy/plugins/canvas/shareable_runtime/constants'; | ||
import { RouteInitializerDeps } from '../'; | ||
import { API_ROUTE_SHAREABLE_RUNTIME_DOWNLOAD } from '../../../../../legacy/plugins/canvas/common/lib/constants'; | ||
|
||
export function initializeDownloadShareableWorkpadRoute(deps: RouteInitializerDeps) { | ||
const { router } = deps; | ||
router.get( | ||
{ | ||
path: API_ROUTE_SHAREABLE_RUNTIME_DOWNLOAD, | ||
validate: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why no validation on this route? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not required for this route. We don't expected anything to be passed in as part of the request. |
||
}, | ||
async (_context, _request, response) => { | ||
// TODO: check if this is still an issue on cloud after migrating to NP | ||
// | ||
// The option setting is not for typical use. We're using it here to avoid | ||
// problems in Cloud environments. See elastic/kibana#47405. | ||
// @ts-ignore No type for inert Hapi handler | ||
// const file = handler.file(SHAREABLE_RUNTIME_FILE, { confine: false }); | ||
const file = readFileSync(SHAREABLE_RUNTIME_FILE); | ||
return response.ok({ | ||
headers: { 'content-type': 'application/octet-stream' }, | ||
body: file, | ||
}); | ||
} | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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 { RouteInitializerDeps } from '../'; | ||
import { initializeZipShareableWorkpadRoute } from './zip'; | ||
import { initializeDownloadShareableWorkpadRoute } from './download'; | ||
|
||
export function initShareablesRoutes(deps: RouteInitializerDeps) { | ||
initializeDownloadShareableWorkpadRoute(deps); | ||
initializeZipShareableWorkpadRoute(deps); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 { schema } from '@kbn/config-schema'; | ||
|
||
export const PositionSchema = schema.object({ | ||
angle: schema.number(), | ||
height: schema.number(), | ||
left: schema.number(), | ||
parent: schema.nullable(schema.string()), | ||
top: schema.number(), | ||
width: schema.number(), | ||
}); | ||
|
||
export const ContainerStyleSchema = schema.object({ | ||
type: schema.maybe(schema.string()), | ||
border: schema.maybe(schema.string()), | ||
borderRadius: schema.maybe(schema.string()), | ||
padding: schema.maybe(schema.string()), | ||
backgroundColor: schema.maybe(schema.string()), | ||
backgroundImage: schema.maybe(schema.string()), | ||
backgroundSize: schema.maybe(schema.string()), | ||
backgroundRepeat: schema.maybe(schema.string()), | ||
opacity: schema.maybe(schema.number()), | ||
overflow: schema.maybe(schema.string()), | ||
}); | ||
|
||
export const RenderableSchema = schema.object({ | ||
error: schema.nullable(schema.string()), | ||
state: schema.string(), | ||
value: schema.object({ | ||
as: schema.string(), | ||
containerStyle: ContainerStyleSchema, | ||
css: schema.maybe(schema.string()), | ||
type: schema.string(), | ||
value: schema.any(), | ||
}), | ||
}); | ||
|
||
export const RenderedWorkpadElementSchema = schema.object({ | ||
expressionRenderable: RenderableSchema, | ||
id: schema.string(), | ||
position: PositionSchema, | ||
}); | ||
|
||
export const RenderedWorkpadPageSchema = schema.object({ | ||
id: schema.string(), | ||
elements: schema.arrayOf(RenderedWorkpadElementSchema), | ||
groups: schema.maybe(schema.arrayOf(schema.arrayOf(RenderedWorkpadElementSchema))), | ||
style: schema.recordOf(schema.string(), schema.string()), | ||
transition: schema.maybe( | ||
schema.oneOf([ | ||
schema.object({}), | ||
schema.object({ | ||
name: schema.string(), | ||
}), | ||
]) | ||
), | ||
}); | ||
|
||
export const RenderedWorkpadSchema = schema.object({ | ||
'@created': schema.maybe(schema.string()), | ||
'@timestamp': schema.maybe(schema.string()), | ||
assets: schema.maybe(schema.recordOf(schema.string(), RenderedWorkpadPageSchema)), | ||
colors: schema.arrayOf(schema.string()), | ||
css: schema.string(), | ||
height: schema.number(), | ||
id: schema.string(), | ||
isWriteable: schema.maybe(schema.boolean()), | ||
name: schema.string(), | ||
page: schema.number(), | ||
pages: schema.arrayOf(RenderedWorkpadPageSchema), | ||
width: schema.number(), | ||
}); |
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(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌