|
| 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 | + |
| 9 | +import { DETECTION_ENGINE_RULES_URL } from '../../../../plugins/siem/common/constants'; |
| 10 | +import { FtrProviderContext } from '../../common/ftr_provider_context'; |
| 11 | +import { |
| 12 | + createSignalsIndex, |
| 13 | + deleteAllAlerts, |
| 14 | + deleteSignalsIndex, |
| 15 | + getSimpleRule, |
| 16 | + getSimpleRuleOutput, |
| 17 | + getSimpleRuleOutputWithoutRuleId, |
| 18 | + getSimpleRuleWithoutRuleId, |
| 19 | + removeServerGeneratedProperties, |
| 20 | + removeServerGeneratedPropertiesIncludingRuleId, |
| 21 | + getSimpleMlRule, |
| 22 | +} from '../../utils'; |
| 23 | + |
| 24 | +// eslint-disable-next-line import/no-default-export |
| 25 | +export default ({ getService }: FtrProviderContext) => { |
| 26 | + const supertest = getService('supertest'); |
| 27 | + const es = getService('legacyEs'); |
| 28 | + |
| 29 | + describe('create_rules', () => { |
| 30 | + describe('validation errors', () => { |
| 31 | + it('should give an error that the index must exist first if it does not exist before creating a rule', async () => { |
| 32 | + const { body } = await supertest |
| 33 | + .post(DETECTION_ENGINE_RULES_URL) |
| 34 | + .set('kbn-xsrf', 'true') |
| 35 | + .send(getSimpleRule()) |
| 36 | + .expect(400); |
| 37 | + |
| 38 | + expect(body).to.eql({ |
| 39 | + message: |
| 40 | + 'To create a rule, the index must exist first. Index .siem-signals-default does not exist', |
| 41 | + status_code: 400, |
| 42 | + }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('creating rules', () => { |
| 47 | + beforeEach(async () => { |
| 48 | + await createSignalsIndex(supertest); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(async () => { |
| 52 | + await deleteSignalsIndex(supertest); |
| 53 | + await deleteAllAlerts(es); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should create a single rule with a rule_id', async () => { |
| 57 | + const { body } = await supertest |
| 58 | + .post(DETECTION_ENGINE_RULES_URL) |
| 59 | + .set('kbn-xsrf', 'true') |
| 60 | + .send(getSimpleRule()) |
| 61 | + .expect(200); |
| 62 | + |
| 63 | + const bodyToCompare = removeServerGeneratedProperties(body); |
| 64 | + expect(bodyToCompare).to.eql(getSimpleRuleOutput()); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should create a single rule without an input index', async () => { |
| 68 | + const { index, ...payload } = getSimpleRule(); |
| 69 | + const { index: _index, ...expected } = getSimpleRuleOutput(); |
| 70 | + |
| 71 | + const { body } = await supertest |
| 72 | + .post(DETECTION_ENGINE_RULES_URL) |
| 73 | + .set('kbn-xsrf', 'true') |
| 74 | + .send(payload) |
| 75 | + .expect(200); |
| 76 | + |
| 77 | + const bodyToCompare = removeServerGeneratedProperties(body); |
| 78 | + expect(bodyToCompare).to.eql(expected); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should create a single rule without a rule_id', async () => { |
| 82 | + const { body } = await supertest |
| 83 | + .post(DETECTION_ENGINE_RULES_URL) |
| 84 | + .set('kbn-xsrf', 'true') |
| 85 | + .send(getSimpleRuleWithoutRuleId()) |
| 86 | + .expect(200); |
| 87 | + |
| 88 | + const bodyToCompare = removeServerGeneratedPropertiesIncludingRuleId(body); |
| 89 | + expect(bodyToCompare).to.eql(getSimpleRuleOutputWithoutRuleId()); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should give a 403 when trying to create a single Machine Learning rule since the license is basic', async () => { |
| 93 | + const { body } = await supertest |
| 94 | + .post(DETECTION_ENGINE_RULES_URL) |
| 95 | + .set('kbn-xsrf', 'true') |
| 96 | + .send(getSimpleMlRule()) |
| 97 | + .expect(403); |
| 98 | + |
| 99 | + const bodyToCompare = removeServerGeneratedProperties(body); |
| 100 | + expect(bodyToCompare).to.eql({ |
| 101 | + message: 'Your license does not support machine learning. Please upgrade your license.', |
| 102 | + status_code: 403, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should cause a 409 conflict if we attempt to create the same rule_id twice', async () => { |
| 107 | + await supertest |
| 108 | + .post(DETECTION_ENGINE_RULES_URL) |
| 109 | + .set('kbn-xsrf', 'true') |
| 110 | + .send(getSimpleRule()) |
| 111 | + .expect(200); |
| 112 | + |
| 113 | + const { body } = await supertest |
| 114 | + .post(DETECTION_ENGINE_RULES_URL) |
| 115 | + .set('kbn-xsrf', 'true') |
| 116 | + .send(getSimpleRule()) |
| 117 | + .expect(409); |
| 118 | + |
| 119 | + expect(body).to.eql({ |
| 120 | + message: 'rule_id: "rule-1" already exists', |
| 121 | + status_code: 409, |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}; |
0 commit comments