kibana/test/api_integration/apis/console/autocomplete_entities.ts
Christiane (Tina) Heiligers 3a68f8b3ae
[http] api_integration tests handle internal route restriction (#192407)
fix https://github.com/elastic/kibana/issues/192052
## Summary

Internal APIs will be
[restricted](https://github.com/elastic/kibana/issues/163654) from
public access as of 9.0.0. In non-serverless environments, this breaking
change will result in a 400 error if an external request is made to an
internal Kibana API (route `access` option as `"internal"` or
`"public"`).
This PR allows API owners of non-xpack plugins to run their `ftr` API
integration tests against the restriction and adds examples of how to
handle it.

### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios


Note to reviewers: The header needed to allow access to internal apis
shouldn't change your test output, with or without the restriction
enabled.

### How to test the changes work:
#### Non x-pack:
1. Set `server.restrictInternalApis: true` in `test/common/config.js`
2. Ensure your tests pass

#### x-pack:
1. Set `server.restrictInternalApis: true` in
`x-pack/test/api_integration/apis/security/config.ts`
2. Ensure the spaces tests pass

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2024-09-12 09:23:10 +02:00

186 lines
6.8 KiB
TypeScript

/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import expect from '@kbn/expect';
import { X_ELASTIC_INTERNAL_ORIGIN_REQUEST } from '@kbn/core-http-common';
import type { FtrProviderContext } from '../../ftr_provider_context';
export default ({ getService }: FtrProviderContext) => {
const console = getService('console');
const supertest = getService('supertest');
const sendRequest = (query: object) =>
supertest
.get('/api/console/autocomplete_entities')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
.query(query);
describe('/api/console/autocomplete_entities', function () {
const indexName = 'test-index-1';
const aliasName = 'test-alias-1';
const indexTemplateName = 'test-index-template-1';
const componentTemplateName = 'test-component-template-1';
const dataStreamName = 'test-data-stream-1';
const legacyTemplateName = 'test-legacy-template-1';
before(async () => {
// Setup indices, aliases, templates, and data streams
await console.createIndex(indexName);
await console.createAlias(indexName, aliasName);
await console.createComponentTemplate(componentTemplateName);
await console.createIndexTemplate(
indexTemplateName,
[dataStreamName],
[componentTemplateName]
);
await console.createDataStream(dataStreamName);
await console.createLegacyTemplate(legacyTemplateName);
});
after(async () => {
// Cleanup indices, aliases, templates, and data streams
await console.deleteAlias(indexName, aliasName);
await console.deleteIndex(indexName);
await console.deleteDataStream(dataStreamName);
await console.deleteIndexTemplate(indexTemplateName);
await console.deleteComponentTemplate(componentTemplateName);
await console.deleteLegacyTemplate(legacyTemplateName);
});
it('should not succeed if no settings are provided in query params', async () => {
const response = await sendRequest({});
const { status } = response;
expect(status).to.be(400);
});
it('should return an object with properties of "mappings", "aliases", "dataStreams", "legacyTemplates", "indexTemplates", "componentTemplates"', async () => {
const response = await sendRequest({
indices: true,
fields: true,
templates: true,
dataStreams: true,
});
const { body, status } = response;
expect(status).to.be(200);
expect(Object.keys(body).sort()).to.eql([
'aliases',
'componentTemplates',
'dataStreams',
'indexTemplates',
'legacyTemplates',
'mappings',
]);
});
it('should return empty payload with all settings are set to false', async () => {
const response = await sendRequest({
indices: false,
fields: false,
templates: false,
dataStreams: false,
});
const { body, status } = response;
expect(status).to.be(200);
expect(body.legacyTemplates).to.eql({});
expect(body.indexTemplates).to.eql({});
expect(body.componentTemplates).to.eql({});
expect(body.aliases).to.eql({});
expect(body.mappings).to.eql({});
expect(body.dataStreams).to.eql({});
});
it('should return empty templates with templates setting is set to false', async () => {
const response = await sendRequest({
templates: false,
});
const { body, status } = response;
expect(status).to.be(200);
expect(body.legacyTemplates).to.eql({});
expect(body.indexTemplates).to.eql({});
expect(body.componentTemplates).to.eql({});
});
it('should return empty data streams with dataStreams setting is set to false', async () => {
const response = await sendRequest({
dataStreams: false,
});
const { body, status } = response;
expect(status).to.be(200);
expect(body.dataStreams).to.eql({});
});
it('should return empty aliases with indices setting is set to false', async () => {
const response = await sendRequest({
indices: false,
});
const { body, status } = response;
expect(status).to.be(200);
expect(body.aliases).to.eql({});
});
it('should return empty mappings with fields setting is set to false', async () => {
const response = await sendRequest({
fields: false,
});
const { body, status } = response;
expect(status).to.be(200);
expect(body.mappings).to.eql({});
});
it('should not return mappings with fields setting is set to true without the list of indices is provided', async () => {
const response = await sendRequest({ fields: true });
const { body, status } = response;
expect(status).to.be(200);
expect(Object.keys(body.mappings)).to.not.contain(indexName);
});
it('should return mappings with fields setting is set to true and the list of indices is provided', async () => {
const response = await sendRequest({ fields: true, fieldsIndices: indexName });
const { body, status } = response;
expect(status).to.be(200);
expect(Object.keys(body.mappings)).to.contain(indexName);
});
it('should return aliases with indices setting is set to true', async () => {
const response = await sendRequest({ indices: true });
const { body, status } = response;
expect(status).to.be(200);
expect(body.aliases[indexName].aliases).to.eql({ [aliasName]: {} });
});
it('should return data streams with dataStreams setting is set to true', async () => {
const response = await sendRequest({ dataStreams: true });
const { body, status } = response;
expect(status).to.be(200);
expect(body.dataStreams.data_streams.map((ds: { name: string }) => ds.name)).to.contain(
dataStreamName
);
});
it('should return all templates with templates setting is set to true', async () => {
const response = await sendRequest({ templates: true });
const { body, status } = response;
expect(status).to.be(200);
expect(Object.keys(body.legacyTemplates)).to.contain(legacyTemplateName);
expect(body.indexTemplates.index_templates.map((it: { name: string }) => it.name)).to.contain(
indexTemplateName
);
expect(
body.componentTemplates.component_templates.map((ct: { name: string }) => ct.name)
).to.contain(componentTemplateName);
});
});
};