-
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.
[Fleet] using @kbn/config-schema part 2 (outputs and other apis) (#19…
…3326) ## Summary Relates #184685 Added tests to verify response schemas. To verify, go to Fleet settings and try create/update different output types, fleet server hosts, outputs. ``` GET kbn:/api/oas?pathStartsWith=/api/fleet/outputs GET kbn:/api/oas?pathStartsWith=/api/fleet/health_check GET kbn:/api/oas?pathStartsWith=/api/fleet/message_signing_service/rotate_key_pair GET kbn:/api/oas?pathStartsWith=/api/fleet/fleet_server_hosts GET kbn:/api/oas?pathStartsWith=/api/fleet/data_streams # test APIs POST kbn:/api/fleet/health_check { "id": "default-fleet-server" } POST kbn:/api/fleet/message_signing_service/rotate_key_pair GET kbn:/api/fleet/data_streams GET kbn:/api/fleet/check-permissions POST kbn:/api/fleet/service_tokens ``` --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
773e747
commit 46dbf51
Showing
27 changed files
with
1,508 additions
and
216 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { httpServerMock } from '@kbn/core-http-server-mocks'; | ||
|
||
import type { FleetRequestHandlerContext } from '../../types'; | ||
import { CheckPermissionsResponseSchema } from '../../types'; | ||
|
||
import { xpackMocks } from '../../mocks'; | ||
|
||
import { | ||
GenerateServiceTokenResponseSchema, | ||
generateServiceTokenHandler, | ||
getCheckPermissionsHandler, | ||
} from '.'; | ||
|
||
jest.mock('../../services', () => ({ | ||
appContextService: { | ||
getSecurityLicense: jest.fn().mockReturnValue({ isEnabled: jest.fn().mockReturnValue(false) }), | ||
getCloud: jest.fn().mockReturnValue({ isServerlessEnabled: false } as any), | ||
getExperimentalFeatures: jest.fn().mockReturnValue({ subfeaturePrivileges: false }), | ||
getLogger: jest.fn().mockReturnValue({ debug: jest.fn(), error: jest.fn() } as any), | ||
}, | ||
})); | ||
|
||
describe('schema validation', () => { | ||
let context: FleetRequestHandlerContext; | ||
let response: ReturnType<typeof httpServerMock.createResponseFactory>; | ||
|
||
beforeEach(() => { | ||
context = xpackMocks.createRequestHandlerContext() as unknown as FleetRequestHandlerContext; | ||
response = httpServerMock.createResponseFactory(); | ||
}); | ||
|
||
it('check permissions should return valid response', async () => { | ||
const expectedResponse = { | ||
success: false, | ||
error: 'MISSING_SECURITY', | ||
}; | ||
await getCheckPermissionsHandler(context, {} as any, response); | ||
|
||
expect(response.ok).toHaveBeenCalledWith({ | ||
body: expectedResponse, | ||
}); | ||
const validationResp = CheckPermissionsResponseSchema.validate(expectedResponse); | ||
expect(validationResp).toEqual(expectedResponse); | ||
}); | ||
|
||
it('generate service token should return valid response', async () => { | ||
( | ||
(await context.core).elasticsearch.client.asCurrentUser.transport.request as jest.Mock | ||
).mockResolvedValue({ | ||
created: true, | ||
token: { | ||
name: 'token', | ||
value: 'value', | ||
}, | ||
}); | ||
const expectedResponse = { | ||
name: 'token', | ||
value: 'value', | ||
}; | ||
await generateServiceTokenHandler( | ||
context, | ||
{ | ||
body: {}, | ||
} as any, | ||
response | ||
); | ||
|
||
expect(response.ok).toHaveBeenCalledWith({ | ||
body: expectedResponse, | ||
}); | ||
const validationResp = GenerateServiceTokenResponseSchema.validate(expectedResponse); | ||
expect(validationResp).toEqual(expectedResponse); | ||
}); | ||
}); |
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
69 changes: 69 additions & 0 deletions
69
x-pack/plugins/fleet/server/routes/data_streams/index.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,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { httpServerMock } from '@kbn/core-http-server-mocks'; | ||
|
||
import type { FleetRequestHandlerContext } from '../..'; | ||
|
||
import { xpackMocks } from '../../mocks'; | ||
|
||
import { ListDataStreamsResponseSchema } from '.'; | ||
import { getListHandler } from './handlers'; | ||
|
||
jest.mock('./handlers', () => ({ | ||
getListHandler: jest.fn(), | ||
})); | ||
|
||
const getListHandlerMock = getListHandler as jest.Mock; | ||
|
||
describe('schema validation', () => { | ||
let context: FleetRequestHandlerContext; | ||
let response: ReturnType<typeof httpServerMock.createResponseFactory>; | ||
|
||
beforeEach(() => { | ||
context = xpackMocks.createRequestHandlerContext() as unknown as FleetRequestHandlerContext; | ||
response = httpServerMock.createResponseFactory(); | ||
}); | ||
|
||
it('list data streams should return valid response', async () => { | ||
const expectedResponse = { | ||
data_streams: [ | ||
{ | ||
index: 'index', | ||
dataset: 'dataset', | ||
namespace: 'namespace', | ||
type: 'type', | ||
package: 'package', | ||
package_version: '1.0.0', | ||
last_activity_ms: 123, | ||
size_in_bytes: 123, | ||
size_in_bytes_formatted: 123, | ||
dashboards: [ | ||
{ | ||
id: 'id', | ||
title: 'title', | ||
}, | ||
], | ||
serviceDetails: { | ||
environment: 'environment', | ||
serviceName: 'serviceName', | ||
}, | ||
}, | ||
], | ||
}; | ||
getListHandlerMock.mockImplementation((ctx, request, res) => { | ||
return res.ok({ body: expectedResponse }); | ||
}); | ||
await getListHandler(context, {} as any, response); | ||
expect(response.ok).toHaveBeenCalledWith({ | ||
body: expectedResponse, | ||
}); | ||
|
||
const validateResp = ListDataStreamsResponseSchema.validate(expectedResponse); | ||
expect(validateResp).toEqual(expectedResponse); | ||
}); | ||
}); |
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.