|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import expect from '@kbn/expect'; |
| 8 | +import { omit } from 'lodash'; |
| 9 | +import { FtrProviderContext } from '../../../ftr_provider_context'; |
| 10 | +import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common'; |
| 11 | +import { USER } from '../../../../functional/services/ml/security_common'; |
| 12 | +import { testSetupJobConfigs, jobIds, testSetupAnnotations } from './common_jobs'; |
| 13 | + |
| 14 | +// eslint-disable-next-line import/no-default-export |
| 15 | +export default ({ getService }: FtrProviderContext) => { |
| 16 | + const esArchiver = getService('esArchiver'); |
| 17 | + const supertest = getService('supertestWithoutAuth'); |
| 18 | + const ml = getService('ml'); |
| 19 | + |
| 20 | + describe('get_annotations', function () { |
| 21 | + before(async () => { |
| 22 | + await esArchiver.loadIfNeeded('ml/farequote'); |
| 23 | + await ml.testResources.setKibanaTimeZoneToUTC(); |
| 24 | + |
| 25 | + // generate one annotation for each job |
| 26 | + for (let i = 0; i < testSetupJobConfigs.length; i++) { |
| 27 | + const job = testSetupJobConfigs[i]; |
| 28 | + const annotationToIndex = testSetupAnnotations[i]; |
| 29 | + await ml.api.createAnomalyDetectionJob(job); |
| 30 | + await ml.api.indexAnnotation(annotationToIndex); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + after(async () => { |
| 35 | + await ml.api.cleanMlIndices(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should fetch all annotations for jobId', async () => { |
| 39 | + const requestBody = { |
| 40 | + jobIds: [jobIds[0]], |
| 41 | + earliestMs: 1454804100000, |
| 42 | + latestMs: Date.now(), |
| 43 | + maxAnnotations: 500, |
| 44 | + }; |
| 45 | + const { body } = await supertest |
| 46 | + .post('/api/ml/annotations') |
| 47 | + .auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) |
| 48 | + .set(COMMON_REQUEST_HEADERS) |
| 49 | + .send(requestBody) |
| 50 | + .expect(200); |
| 51 | + |
| 52 | + expect(body.success).to.eql(true); |
| 53 | + expect(body.annotations).not.to.be(undefined); |
| 54 | + [jobIds[0]].forEach((jobId, idx) => { |
| 55 | + expect(body.annotations).to.have.property(jobId); |
| 56 | + expect(body.annotations[jobId]).to.have.length(1); |
| 57 | + |
| 58 | + const indexedAnnotation = omit(body.annotations[jobId][0], '_id'); |
| 59 | + expect(indexedAnnotation).to.eql(testSetupAnnotations[idx]); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should fetch all annotations for multiple jobs', async () => { |
| 64 | + const requestBody = { |
| 65 | + jobIds, |
| 66 | + earliestMs: 1454804100000, |
| 67 | + latestMs: Date.now(), |
| 68 | + maxAnnotations: 500, |
| 69 | + }; |
| 70 | + const { body } = await supertest |
| 71 | + .post('/api/ml/annotations') |
| 72 | + .auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER)) |
| 73 | + .set(COMMON_REQUEST_HEADERS) |
| 74 | + .send(requestBody) |
| 75 | + .expect(200); |
| 76 | + |
| 77 | + expect(body.success).to.eql(true); |
| 78 | + expect(body.annotations).not.to.be(undefined); |
| 79 | + jobIds.forEach((jobId, idx) => { |
| 80 | + expect(body.annotations).to.have.property(jobId); |
| 81 | + expect(body.annotations[jobId]).to.have.length(1); |
| 82 | + |
| 83 | + const indexedAnnotation = omit(body.annotations[jobId][0], '_id'); |
| 84 | + expect(indexedAnnotation).to.eql(testSetupAnnotations[idx]); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should fetch all annotations for user with ML read permissions', async () => { |
| 89 | + const requestBody = { |
| 90 | + jobIds: testSetupJobConfigs.map((j) => j.job_id), |
| 91 | + earliestMs: 1454804100000, |
| 92 | + latestMs: Date.now(), |
| 93 | + maxAnnotations: 500, |
| 94 | + }; |
| 95 | + const { body } = await supertest |
| 96 | + .post('/api/ml/annotations') |
| 97 | + .auth(USER.ML_VIEWER, ml.securityCommon.getPasswordForUser(USER.ML_VIEWER)) |
| 98 | + .set(COMMON_REQUEST_HEADERS) |
| 99 | + .send(requestBody) |
| 100 | + .expect(200); |
| 101 | + expect(body.success).to.eql(true); |
| 102 | + expect(body.annotations).not.to.be(undefined); |
| 103 | + jobIds.forEach((jobId, idx) => { |
| 104 | + expect(body.annotations).to.have.property(jobId); |
| 105 | + expect(body.annotations[jobId]).to.have.length(1); |
| 106 | + |
| 107 | + const indexedAnnotation = omit(body.annotations[jobId][0], '_id'); |
| 108 | + expect(indexedAnnotation).to.eql(testSetupAnnotations[idx]); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should not allow to fetch annotation for unauthorized user', async () => { |
| 113 | + const requestBody = { |
| 114 | + jobIds: testSetupJobConfigs.map((j) => j.job_id), |
| 115 | + earliestMs: 1454804100000, |
| 116 | + latestMs: Date.now(), |
| 117 | + maxAnnotations: 500, |
| 118 | + }; |
| 119 | + const { body } = await supertest |
| 120 | + .post('/api/ml/annotations') |
| 121 | + .auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED)) |
| 122 | + .set(COMMON_REQUEST_HEADERS) |
| 123 | + .send(requestBody) |
| 124 | + .expect(404); |
| 125 | + |
| 126 | + expect(body.error).to.eql('Not Found'); |
| 127 | + expect(body.message).to.eql('Not Found'); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}; |
0 commit comments