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

# Backport

This will backport the following commits from `main` to `8.6`:
- [[ML] Add API tests for index exists endpoint
(#146400)](https://github.com/elastic/kibana/pull/146400)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Pete
Harverson","email":"pete@elastic.co"},"sourceCommit":{"committedDate":"2022-12-07T13:12:17Z","message":"[ML]
Add API tests for index exists endpoint (#146400)\n\n##
Summary\r\n\r\nAdds tests to check the response of the index exists
endpoint:\r\n\r\n```\r\n/api/ml/index_exists\r\n```\r\n\r\nAlso fixed
the behavior of the endpoint when passed an index with a\r\nwilcard
expression so that it no longer returns `true` if an index does\r\nnot
exist matching the expression by adding `allow_no_indices: false`
to\r\nthe query parameters.\r\n\r\nPart of
https://github.com/elastic/kibana/issues/142456\r\n\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"4294e3b17f559a0073f5453a47df7e9dc83c58ba","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["review",":ml","release_note:skip","test-api-integration","v8.6.0","v8.7.0"],"number":146400,"url":"https://github.com/elastic/kibana/pull/146400","mergeCommit":{"message":"[ML]
Add API tests for index exists endpoint (#146400)\n\n##
Summary\r\n\r\nAdds tests to check the response of the index exists
endpoint:\r\n\r\n```\r\n/api/ml/index_exists\r\n```\r\n\r\nAlso fixed
the behavior of the endpoint when passed an index with a\r\nwilcard
expression so that it no longer returns `true` if an index does\r\nnot
exist matching the expression by adding `allow_no_indices: false`
to\r\nthe query parameters.\r\n\r\nPart of
https://github.com/elastic/kibana/issues/142456\r\n\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"4294e3b17f559a0073f5453a47df7e9dc83c58ba"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"8.6","label":"v8.6.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/146400","number":146400,"mergeCommit":{"message":"[ML]
Add API tests for index exists endpoint (#146400)\n\n##
Summary\r\n\r\nAdds tests to check the response of the index exists
endpoint:\r\n\r\n```\r\n/api/ml/index_exists\r\n```\r\n\r\nAlso fixed
the behavior of the endpoint when passed an index with a\r\nwilcard
expression so that it no longer returns `true` if an index does\r\nnot
exist matching the expression by adding `allow_no_indices: false`
to\r\nthe query parameters.\r\n\r\nPart of
https://github.com/elastic/kibana/issues/142456\r\n\r\n\r\n###
Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"4294e3b17f559a0073f5453a47df7e9dc83c58ba"}}]}]
BACKPORT-->

Co-authored-by: Pete Harverson <pete@elastic.co>
This commit is contained in:
Kibana Machine 2022-12-07 09:17:44 -05:00 committed by GitHub
parent f329a77595
commit 11cde5b883
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);
});
});
}
});
});
};