Skip to content

Commit

Permalink
[ML] Functional tests - refactor response code checks (elastic#127875)
Browse files Browse the repository at this point in the history
This PR refactors the way how we assert response codes from API requests in our functional tests.

(cherry picked from commit f4c4fd6)

# Conflicts:
#	x-pack/test/api_integration/apis/ml/data_frame_analytics/jobs_exist_spaces.ts
#	x-pack/test/api_integration/apis/ml/data_frame_analytics/new_job_caps.ts
#	x-pack/test/functional/services/ml/api.ts
  • Loading branch information
pheyos committed Mar 18, 2022
1 parent 55aa9f4 commit 3eaccfd
Show file tree
Hide file tree
Showing 100 changed files with 1,061 additions and 956 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should successfully create annotations for anomaly job', async () => {
const { body } = await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(annotationRequestBody)
.expect(200);
.send(annotationRequestBody);
ml.api.assertResponseStatusCode(200, status, body);
const annotationId = body._id;

const fetchedAnnotation = await ml.api.getAnnotationById(annotationId);
Expand All @@ -56,12 +56,12 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should successfully create annotation for user with ML read permissions', async () => {
const { body } = await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(annotationRequestBody)
.expect(200);
.send(annotationRequestBody);
ml.api.assertResponseStatusCode(200, status, body);

const annotationId = body._id;
const fetchedAnnotation = await ml.api.getAnnotationById(annotationId);
Expand All @@ -76,12 +76,12 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should not allow to create annotation for unauthorized user', async () => {
const { body } = await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.send(annotationRequestBody)
.expect(403);
.send(annotationRequestBody);
ml.api.assertResponseStatusCode(403, status, body);

expect(body.error).to.eql('Forbidden');
expect(body.message).to.eql('Forbidden');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export default ({ getService }: FtrProviderContext) => {

const annotationIdToDelete = annotationsForJob[0]._id;

const { body } = await supertest
const { body, status } = await supertest
.delete(`/api/ml/annotations/delete/${annotationIdToDelete}`)
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.expect(200);
.set(COMMON_REQUEST_HEADERS);
ml.api.assertResponseStatusCode(200, status, body);

expect(body._id).to.eql(annotationIdToDelete);
expect(body.result).to.eql('deleted');
Expand All @@ -59,11 +59,11 @@ export default ({ getService }: FtrProviderContext) => {

const annotationIdToDelete = annotationsForJob[0]._id;

const { body } = await supertest
const { body, status } = await supertest
.delete(`/api/ml/annotations/delete/${annotationIdToDelete}`)
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.expect(200);
.set(COMMON_REQUEST_HEADERS);
ml.api.assertResponseStatusCode(200, status, body);

expect(body._id).to.eql(annotationIdToDelete);
expect(body.result).to.eql('deleted');
Expand All @@ -77,11 +77,11 @@ export default ({ getService }: FtrProviderContext) => {

const annotationIdToDelete = annotationsForJob[0]._id;

const { body } = await supertest
const { body, status } = await supertest
.delete(`/api/ml/annotations/delete/${annotationIdToDelete}`)
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.expect(403);
.set(COMMON_REQUEST_HEADERS);
ml.api.assertResponseStatusCode(403, status, body);

expect(body.error).to.eql('Forbidden');
expect(body.message).to.eql('Forbidden');
Expand Down
25 changes: 13 additions & 12 deletions x-pack/test/api_integration/apis/ml/annotations/get_annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ export default ({ getService }: FtrProviderContext) => {
latestMs: Date.now(),
maxAnnotations: 500,
};
const { body } = await supertest
const { body, status } = await supertest
.post('/api/ml/annotations')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(requestBody)
.expect(200);
.send(requestBody);
ml.api.assertResponseStatusCode(200, status, body);

expect(body.success).to.eql(true);
expect(body.annotations).not.to.be(undefined);
Expand All @@ -68,12 +68,12 @@ export default ({ getService }: FtrProviderContext) => {
latestMs: Date.now(),
maxAnnotations: 500,
};
const { body } = await supertest
const { body, status } = await supertest
.post('/api/ml/annotations')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(requestBody)
.expect(200);
.send(requestBody);
ml.api.assertResponseStatusCode(200, status, body);

expect(body.success).to.eql(true);
expect(body.annotations).not.to.be(undefined);
Expand All @@ -93,12 +93,13 @@ export default ({ getService }: FtrProviderContext) => {
latestMs: Date.now(),
maxAnnotations: 500,
};
const { body } = await supertest
const { body, status } = await supertest
.post('/api/ml/annotations')
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(requestBody)
.expect(200);
.send(requestBody);
ml.api.assertResponseStatusCode(200, status, body);

expect(body.success).to.eql(true);
expect(body.annotations).not.to.be(undefined);
jobIds.forEach((jobId, idx) => {
Expand All @@ -117,12 +118,12 @@ export default ({ getService }: FtrProviderContext) => {
latestMs: Date.now(),
maxAnnotations: 500,
};
const { body } = await supertest
const { body, status } = await supertest
.post('/api/ml/annotations')
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.send(requestBody)
.expect(403);
.send(requestBody);
ml.api.assertResponseStatusCode(403, status, body);

expect(body.error).to.eql('Forbidden');
expect(body.message).to.eql('Forbidden');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export default ({ getService }: FtrProviderContext) => {
_id: originalAnnotation._id,
};

const { body } = await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(annotationUpdateRequestBody)
.expect(200);
.send(annotationUpdateRequestBody);
ml.api.assertResponseStatusCode(200, status, body);

expect(body._id).to.eql(originalAnnotation._id);
expect(body.result).to.eql('updated');
Expand All @@ -90,12 +90,12 @@ export default ({ getService }: FtrProviderContext) => {
_id: originalAnnotation._id,
};

const { body } = await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER))
.set(COMMON_REQUEST_HEADERS)
.send(annotationUpdateRequestBody)
.expect(200);
.send(annotationUpdateRequestBody);
ml.api.assertResponseStatusCode(200, status, body);

expect(body._id).to.eql(originalAnnotation._id);
expect(body.result).to.eql('updated');
Expand All @@ -121,12 +121,12 @@ export default ({ getService }: FtrProviderContext) => {
_id: originalAnnotation._id,
};

const { body } = await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
.set(COMMON_REQUEST_HEADERS)
.send(annotationUpdateRequestBody)
.expect(403);
.send(annotationUpdateRequestBody);
ml.api.assertResponseStatusCode(403, status, body);

expect(body.error).to.eql('Forbidden');
expect(body.message).to.eql('Forbidden');
Expand All @@ -150,12 +150,12 @@ export default ({ getService }: FtrProviderContext) => {
detector_index: 2,
_id: originalAnnotation._id,
};
await supertest
const { body, status } = await supertest
.put('/api/ml/annotations/index')
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
.set(COMMON_REQUEST_HEADERS)
.send(annotationUpdateRequestBodyWithMissingFields)
.expect(200);
.send(annotationUpdateRequestBodyWithMissingFields);
ml.api.assertResponseStatusCode(200, status, body);

const updatedAnnotation = await ml.api.getAnnotationById(originalAnnotation._id);
if (updatedAnnotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export default ({ getService }: FtrProviderContext) => {
const idSpace2 = 'space2';

async function runRequest(jobId: string, expectedStatusCode: number, space?: string) {
const { body } = await supertest
const { body, status } = await supertest
.post(`${space ? `/s/${space}` : ''}/api/ml/anomaly_detectors/${jobId}/_close`)
.auth(
USER.ML_POWERUSER_ALL_SPACES,
ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER_ALL_SPACES)
)
.set(COMMON_REQUEST_HEADERS)
.expect(expectedStatusCode);
.set(COMMON_REQUEST_HEADERS);
ml.api.assertResponseStatusCode(expectedStatusCode, status, body);

return body;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ export default ({ getService }: FtrProviderContext) => {

for (const testData of testDataList) {
it(`${testData.testTitle}`, async () => {
const { body } = await supertest
const { body, status } = await supertest
.put(`/api/ml/anomaly_detectors/${testData.jobId}`)
.auth(testData.user, ml.securityCommon.getPasswordForUser(testData.user))
.set(COMMON_REQUEST_HEADERS)
.send(testData.requestBody)
.expect(testData.expected.responseCode);
.send(testData.requestBody);
ml.api.assertResponseStatusCode(testData.expected.responseCode, status, body);

if (body.error === undefined) {
// Validate the important parts of the response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export default ({ getService }: FtrProviderContext) => {
const jobConfig = ml.commonConfig.getADFqSingleMetricJobConfig(jobIdSpace1);

await ml.testExecution.logTestStep('should create job');
await supertest
const { body, status } = await supertest
.put(`/s/${idSpace1}/api/ml/anomaly_detectors/${jobIdSpace1}`)
.auth(
USER.ML_POWERUSER_ALL_SPACES,
ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER_ALL_SPACES)
)
.set(COMMON_REQUEST_HEADERS)
.send(jobConfig)
.expect(200);
.send(jobConfig);
ml.api.assertResponseStatusCode(200, status, body);

await ml.testExecution.logTestStep(`job should be in space '${idSpace1}' only`);
await ml.api.assertJobSpaces(jobIdSpace1, 'anomaly-detector', [idSpace1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ export default ({ getService }: FtrProviderContext) => {
const idSpace2 = 'space2';

async function runRequest(jobId: string, expectedStatusCode: number, space?: string) {
const { body } = await supertest
const { body, status } = await supertest
.delete(`${space ? `/s/${space}` : ''}/api/ml/anomaly_detectors/${jobId}`)
.auth(
USER.ML_POWERUSER_ALL_SPACES,
ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER_ALL_SPACES)
)
.set(COMMON_REQUEST_HEADERS)
.expect(expectedStatusCode);
.set(COMMON_REQUEST_HEADERS);
ml.api.assertResponseStatusCode(expectedStatusCode, status, body);

return body;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export default ({ getService }: FtrProviderContext) => {
user: USER,
expectedStatusCode: number
) {
const { body } = await supertest
const { body, status } = await supertest
.post(`${space ? `/s/${space}` : ''}/api/ml/anomaly_detectors/${jobId}/_forecast`)
.auth(user, ml.securityCommon.getPasswordForUser(user))
.set(COMMON_REQUEST_HEADERS)
.send({ duration })
.expect(expectedStatusCode);
.send({ duration });
ml.api.assertResponseStatusCode(expectedStatusCode, status, body);

return body;
}
Expand Down
Loading

0 comments on commit 3eaccfd

Please sign in to comment.