Skip to content

Commit

Permalink
adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
legrego committed Oct 1, 2020
1 parent 71c28d2 commit c99fd72
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions x-pack/plugins/security/server/routes/indices/get_fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 { httpServerMock, elasticsearchServiceMock } from '../../../../../../src/core/server/mocks';
import { kibanaResponseFactory } from '../../../../../../src/core/server';

import { routeDefinitionParamsMock } from '../index.mock';
import { defineGetFieldsRoutes } from './get_fields';

const createFieldMapping = (field: string, type: string) => ({
[field]: { mapping: { [field]: { type } } },
});

const createEmptyFieldMapping = (field: string) => ({ [field]: { mapping: {} } });

const mockFieldMappingResponse = {
foo: {
mappings: {
...createFieldMapping('fooField', 'keyword'),
...createFieldMapping('commonField', 'keyword'),
...createEmptyFieldMapping('emptyField'),
},
},
bar: {
mappings: {
...createFieldMapping('commonField', 'keyword'),
...createFieldMapping('barField', 'keyword'),
...createFieldMapping('runtimeField', 'runtime'),
},
},
};

describe('GET /internal/security/fields/{query}', () => {
it('returns a list of deduplicated fields, omitting empty and runtime fields', async () => {
const mockRouteDefinitionParams = routeDefinitionParamsMock.create();

const scopedClient = elasticsearchServiceMock.createLegacyScopedClusterClient();
scopedClient.callAsCurrentUser.mockResolvedValue(mockFieldMappingResponse);
mockRouteDefinitionParams.clusterClient.asScoped.mockReturnValue(scopedClient);

defineGetFieldsRoutes(mockRouteDefinitionParams);

const [[, handler]] = mockRouteDefinitionParams.router.get.mock.calls;

const headers = { authorization: 'foo' };
const mockRequest = httpServerMock.createKibanaRequest({
method: 'get',
path: `/internal/security/fields/foo`,
headers,
});
const response = await handler({} as any, mockRequest, kibanaResponseFactory);
expect(response.status).toBe(200);
expect(response.payload).toEqual(['fooField', 'commonField', 'barField']);
});
});

0 comments on commit c99fd72

Please sign in to comment.