[ML] Add API tests for index exists endpoint (#146400)

## Summary

Adds tests to check the response of the index exists endpoint:

```
/api/ml/index_exists
```

Also fixed the behavior of the endpoint when passed an index with a
wilcard expression so that it no longer returns `true` if an index does
not exist matching the expression by adding `allow_no_indices: false` to
the query parameters.

Part of https://github.com/elastic/kibana/issues/142456


### 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

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Pete Harverson 2022-12-07 13:12:17 +00:00 committed by GitHub
parent 9209d50562
commit 4294e3b17f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 0 deletions

View file

@ -231,6 +231,7 @@ export function systemRoutes(
indices.map(async (index) =>
client.asCurrentUser.indices.exists({
index,
allow_no_indices: false,
})
)
);

View file

@ -11,5 +11,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
describe('system', function () {
loadTestFile(require.resolve('./capabilities'));
loadTestFile(require.resolve('./space_capabilities'));
loadTestFile(require.resolve('./index_exists'));
});
}

View file

@ -0,0 +1,111 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { USER } from '../../../../functional/services/ml/security_common';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common_api';
export default ({ getService }: FtrProviderContext) => {
const esArchiver = getService('esArchiver');
const supertest = getService('supertestWithoutAuth');
const ml = getService('ml');
const responseBody = {
ft_farequote_small: { exists: true },
'ft_farequote_*': { exists: true }, // wildcard
ft_farequote_fail: { exists: false },
'ft_farequote_fail_*': { exists: false }, // wildcard
};
const testDataList = [
{
testTitle: 'as ML Poweruser',
user: USER.ML_POWERUSER,
requestBody: {
indices: Object.keys(responseBody),
},
expected: {
responseCode: 200,
responseBody,
},
},
];
const testDataListUnauthorized = [
{
testTitle: 'as ML Viewer',
user: USER.ML_VIEWER,
requestBody: {
indices: Object.keys(responseBody),
},
expected: {
responseCode: 403,
error: 'Forbidden',
},
},
{
testTitle: 'as ML Unauthorized user',
user: USER.ML_UNAUTHORIZED,
requestBody: {
jobIds: Object.keys(responseBody),
},
expected: {
responseCode: 403,
error: 'Forbidden',
},
},
];
async function runRequest(user: USER, requestBody: object, expectedStatusCode: number) {
const { body, status } = await supertest
.post('/api/ml/index_exists')
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS)
.send(requestBody);
ml.api.assertResponseStatusCode(expectedStatusCode, status, body);
return body;
}
describe('POST ml/index_exists', function () {
before(async () => {
await esArchiver.loadIfNeeded('x-pack/test/functional/es_archives/ml/farequote_small');
});
describe('should correctly check if indices exist ', function () {
for (const testData of testDataList) {
it(`${testData.testTitle}`, async () => {
const body = await runRequest(
testData.user,
testData.requestBody,
testData.expected.responseCode
);
const expectedResponse = testData.expected.responseBody;
expect(body).to.eql(expectedResponse);
});
}
});
describe('rejects request', function () {
for (const testData of testDataListUnauthorized) {
describe('fails to check if indices exist', function () {
it(`${testData.testTitle}`, async () => {
const body = await runRequest(
testData.user,
testData.requestBody,
testData.expected.responseCode
);
expect(body).to.have.property('error').eql(testData.expected.error);
});
});
}
});
});
};