Skip to content

Commit c6d5dbe

Browse files
authored
Cypress checking on rule YAML content (#248)
* check yaml rule content Signed-off-by: Aleksandar Djindjic <[email protected]> * update snapshot Signed-off-by: Aleksandar Djindjic <[email protected]> * fix typo Signed-off-by: Aleksandar Djindjic <[email protected]> Signed-off-by: Aleksandar Djindjic <[email protected]>
1 parent 30a5657 commit c6d5dbe

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

cypress/integration/2_rules.spec.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const uniqueId = Cypress._.random(0, 1e6);
99
const SAMPLE_RULE = {
1010
name: `Cypress test rule ${uniqueId}`,
1111
logType: 'windows',
12-
description: 'This is a rule used to test the rule creation workflow. Not for production use.',
12+
description: 'This is a rule used to test the rule creation workflow.',
1313
detection:
1414
'selection:\n Provider_Name: Service Control Manager\nEventID: 7045\nServiceName: ZzNetSvc\n{backspace}{backspace}condition: selection',
1515
detectionLine: [
@@ -28,22 +28,23 @@ const SAMPLE_RULE = {
2828
};
2929

3030
const YAML_RULE_LINES = [
31+
`id:`,
32+
`logsource:`,
33+
`product: ${SAMPLE_RULE.logType}`,
3134
`title: ${SAMPLE_RULE.name}`,
32-
`description:`,
33-
`${SAMPLE_RULE.description}`,
34-
`level: ${SAMPLE_RULE.severity}`,
35+
`description: ${SAMPLE_RULE.description}`,
3536
`tags:`,
3637
`- ${SAMPLE_RULE.tags[0]}`,
3738
`- ${SAMPLE_RULE.tags[1]}`,
3839
`- ${SAMPLE_RULE.tags[2]}`,
39-
`references:`,
40-
`- '${SAMPLE_RULE.references}'`,
4140
`falsepositives:`,
4241
`- ${SAMPLE_RULE.falsePositive}`,
43-
`author: ${SAMPLE_RULE.author}`,
42+
`level: ${SAMPLE_RULE.severity}`,
4443
`status: ${SAMPLE_RULE.status}`,
45-
`logsource:`,
46-
`product: ${SAMPLE_RULE.logType}`,
44+
`references:`,
45+
`- '${SAMPLE_RULE.references}'`,
46+
`author: ${SAMPLE_RULE.author}`,
47+
`detection:`,
4748
...SAMPLE_RULE.detection.replaceAll(' ', '').replaceAll('{backspace}', '').split('\n'),
4849
];
4950

@@ -222,11 +223,24 @@ describe('Rules', () => {
222223
force: true,
223224
});
224225

225-
YAML_RULE_LINES.forEach((line) =>
226-
cy
227-
.get('[data-test-subj="rule_flyout_yaml_rule"]', TWENTY_SECONDS_TIMEOUT)
228-
.contains(line, TWENTY_SECONDS_TIMEOUT)
229-
);
226+
cy.get('[data-test-subj="rule_flyout_yaml_rule"]')
227+
.get('[class="euiCodeBlock__line"]')
228+
.each((lineElement, lineIndex) => {
229+
if (lineIndex >= YAML_RULE_LINES.length) {
230+
return;
231+
}
232+
let line = lineElement.text().replaceAll('\n', '').trim();
233+
let expectedLine = YAML_RULE_LINES[lineIndex];
234+
235+
// The document ID field is generated when the document is added to the index,
236+
// so this test just checks that the line starts with the ID key.
237+
if (expectedLine.startsWith('id:')) {
238+
expectedLine = 'id:';
239+
expect(line, `Sigma rule line ${lineIndex}`).to.contain(expectedLine);
240+
} else {
241+
expect(line, `Sigma rule line ${lineIndex}`).to.equal(expectedLine);
242+
}
243+
});
230244

231245
// Close the flyout
232246
cy.get('[data-test-subj="close-rule-details-flyout"]', TWENTY_SECONDS_TIMEOUT).click({

public/pages/Rules/components/RuleContentViewer/RuleContentYamlViewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const RuleContentYamlViewer: React.FC<RuleContentYamlViewerProps> = ({ ru
1717
const ruleYaml = mapYamlObjectToYamlString(yamlObject);
1818

1919
return (
20-
<EuiCodeBlock language="yaml" data-test-subj={'rule_flyout_yaml_rule'}>
20+
<EuiCodeBlock language="yaml" data-test-subj={'rule_flyout_yaml_rule'} isCopyable={true}>
2121
{ruleYaml}
2222
</EuiCodeBlock>
2323
);

public/pages/Rules/components/RuleContentViewer/__snapshots__/RuleContentYamlViewer.test.tsx.snap

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`<RuleContentYamlViewer /> spec renders the component 1`] = `
44
<div
5-
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge prismjs language-yaml"
5+
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--hasControls prismjs language-yaml"
66
>
77
<pre
88
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
@@ -403,5 +403,24 @@ exports[`<RuleContentYamlViewer /> spec renders the component 1`] = `
403403
</span>
404404
</code>
405405
</pre>
406+
<div
407+
class="euiCodeBlock__controls"
408+
>
409+
<div
410+
class="euiCodeBlock__copyButton"
411+
>
412+
<span
413+
class="euiToolTipAnchor"
414+
>
415+
<button
416+
aria-label="Copy"
417+
class="euiButtonIcon euiButtonIcon--text euiButtonIcon--empty euiButtonIcon--xSmall"
418+
type="button"
419+
>
420+
EuiIconMock
421+
</button>
422+
</span>
423+
</div>
424+
</div>
406425
</div>
407426
`;

0 commit comments

Comments
 (0)