From 05f554ecc18b5e5a11f27661e004eb73ebe14961 Mon Sep 17 00:00:00 2001 From: Ricky Ang Date: Thu, 29 Jun 2023 09:34:05 -0700 Subject: [PATCH] more pr comments --- .../apis/cloud_security_posture/helper.ts | 7 ++ .../status/status_unprivileged.ts | 119 +++--------------- 2 files changed, 26 insertions(+), 100 deletions(-) diff --git a/x-pack/test/api_integration/apis/cloud_security_posture/helper.ts b/x-pack/test/api_integration/apis/cloud_security_posture/helper.ts index b659153b1bf69..62ac586495737 100644 --- a/x-pack/test/api_integration/apis/cloud_security_posture/helper.ts +++ b/x-pack/test/api_integration/apis/cloud_security_posture/helper.ts @@ -8,6 +8,8 @@ import type { SuperTest, Test } from 'supertest'; import { Client } from '@elastic/elasticsearch'; import { SecurityService } from '../../../../../test/common/services/security/security'; +import type { IndexDetails } from '../../../../../x-pack/plugins/cloud_security_posture/common/types'; +import expect from '@kbn/expect/expect'; export const deleteIndex = (es: Client, indexToBeDeleted: string[]) => { Promise.all([ @@ -141,3 +143,8 @@ export const deleteRole = async (security: SecurityService, roleName: string) => export const deleteUser = async (security: SecurityService, userName: string) => { await security.user.delete(userName); }; + +export const assertIndexStatus = (indicesDetails: IndexDetails[], indexName: string, expectedStatus: string) => { + const actualValue = indicesDetails.find((idx) => idx.index === indexName)?.status; + expect(actualValue).to.eql(expectedStatus, `expected ${indexName} to be ${expectedStatus} but got ${actualValue} instead`); +} diff --git a/x-pack/test/api_integration/apis/cloud_security_posture/status/status_unprivileged.ts b/x-pack/test/api_integration/apis/cloud_security_posture/status/status_unprivileged.ts index 946f22c27f77d..e0c3242362533 100644 --- a/x-pack/test/api_integration/apis/cloud_security_posture/status/status_unprivileged.ts +++ b/x-pack/test/api_integration/apis/cloud_security_posture/status/status_unprivileged.ts @@ -20,6 +20,7 @@ import { deleteRole, deleteUser, deleteIndex, + assertIndexStatus, } from '../helper'; const UNPRIVILEGED_ROLE = 'unprivileged_test_role'; @@ -154,39 +155,13 @@ export default function (providerContext: FtrProviderContext) { `expected unprivileged but got ${res.vuln_mgmt.status} instead` ); - expect( - res.indicesDetails.find((idx) => idx.index === LATEST_FINDINGS_INDEX_DEFAULT_NS)?.status - ).to.eql( - 'empty', - `expected empty but got ${ - res.indicesDetails.find((idx) => idx.index === LATEST_FINDINGS_INDEX_DEFAULT_NS)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === FINDINGS_INDEX_PATTERN)?.status - ).to.eql( - 'empty', - `expected empty but got ${ - res.indicesDetails.find((idx) => idx.index === FINDINGS_INDEX_PATTERN)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === BENCHMARK_SCORE_INDEX_DEFAULT_NS)?.status - ).to.eql( - 'unprivileged', - `expected unprivileged but got ${ - res.indicesDetails.find((idx) => idx.index === BENCHMARK_SCORE_INDEX_DEFAULT_NS)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === LATEST_VULNERABILITIES_INDEX_DEFAULT_NS) - ?.status - ).to.eql( - 'unprivileged', - `expected unprivileged but got ${ - res.indicesDetails.find((idx) => idx.index === LATEST_VULNERABILITIES_INDEX_DEFAULT_NS) - ?.status - } instead` + assertIndexStatus(res.indicesDetails, LATEST_FINDINGS_INDEX_DEFAULT_NS, 'empty'); + assertIndexStatus(res.indicesDetails, FINDINGS_INDEX_PATTERN, 'empty'); + assertIndexStatus(res.indicesDetails, BENCHMARK_SCORE_INDEX_DEFAULT_NS, 'unprivileged'); + assertIndexStatus( + res.indicesDetails, + LATEST_VULNERABILITIES_INDEX_DEFAULT_NS, + 'unprivileged' ); }); @@ -223,39 +198,13 @@ export default function (providerContext: FtrProviderContext) { `expected unprivileged but got ${res.vuln_mgmt.status} instead` ); - expect( - res.indicesDetails.find((idx) => idx.index === LATEST_FINDINGS_INDEX_DEFAULT_NS)?.status - ).to.eql( - 'unprivileged', - `expected unprivileged but got ${ - res.indicesDetails.find((idx) => idx.index === LATEST_FINDINGS_INDEX_DEFAULT_NS)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === FINDINGS_INDEX_PATTERN)?.status - ).to.eql( - 'empty', - `expected empty but got ${ - res.indicesDetails.find((idx) => idx.index === FINDINGS_INDEX_PATTERN)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === BENCHMARK_SCORE_INDEX_DEFAULT_NS)?.status - ).to.eql( - 'empty', - `expected empty but got ${ - res.indicesDetails.find((idx) => idx.index === BENCHMARK_SCORE_INDEX_DEFAULT_NS)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === LATEST_VULNERABILITIES_INDEX_DEFAULT_NS) - ?.status - ).to.eql( - 'unprivileged', - `expected unprivileged but got ${ - res.indicesDetails.find((idx) => idx.index === LATEST_VULNERABILITIES_INDEX_DEFAULT_NS) - ?.status - } instead` + assertIndexStatus(res.indicesDetails, LATEST_FINDINGS_INDEX_DEFAULT_NS, 'unprivileged'); + assertIndexStatus(res.indicesDetails, FINDINGS_INDEX_PATTERN, 'empty'); + assertIndexStatus(res.indicesDetails, BENCHMARK_SCORE_INDEX_DEFAULT_NS, 'empty'); + assertIndexStatus( + res.indicesDetails, + LATEST_VULNERABILITIES_INDEX_DEFAULT_NS, + 'unprivileged' ); }); @@ -295,40 +244,10 @@ export default function (providerContext: FtrProviderContext) { `expected not-installed but got ${res.vuln_mgmt.status} instead` ); - expect( - res.indicesDetails.find((idx) => idx.index === LATEST_FINDINGS_INDEX_DEFAULT_NS)?.status - ).to.eql( - 'unprivileged', - `expected unprivileged but got ${ - res.indicesDetails.find((idx) => idx.index === LATEST_FINDINGS_INDEX_DEFAULT_NS)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === FINDINGS_INDEX_PATTERN)?.status - ).to.eql( - 'empty', - `expected empty but got ${ - res.indicesDetails.find((idx) => idx.index === FINDINGS_INDEX_PATTERN)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === BENCHMARK_SCORE_INDEX_DEFAULT_NS)?.status - ).to.eql( - 'unprivileged', - `expected unprivileged but got ${ - res.indicesDetails.find((idx) => idx.index === BENCHMARK_SCORE_INDEX_DEFAULT_NS)?.status - } instead` - ); - expect( - res.indicesDetails.find((idx) => idx.index === LATEST_VULNERABILITIES_INDEX_DEFAULT_NS) - ?.status - ).to.eql( - 'empty', - `expected empty but got ${ - res.indicesDetails.find((idx) => idx.index === LATEST_VULNERABILITIES_INDEX_DEFAULT_NS) - ?.status - } instead` - ); + assertIndexStatus(res.indicesDetails, LATEST_FINDINGS_INDEX_DEFAULT_NS, 'unprivileged'); + assertIndexStatus(res.indicesDetails, FINDINGS_INDEX_PATTERN, 'empty'); + assertIndexStatus(res.indicesDetails, BENCHMARK_SCORE_INDEX_DEFAULT_NS, 'unprivileged'); + assertIndexStatus(res.indicesDetails, LATEST_VULNERABILITIES_INDEX_DEFAULT_NS, 'empty'); }); }); });