Skip to content

Commit

Permalink
Cypress checking on rule YAML content (opensearch-project#248)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
djindjic authored Dec 21, 2022
1 parent 30a5657 commit c6d5dbe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
42 changes: 28 additions & 14 deletions cypress/integration/2_rules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const uniqueId = Cypress._.random(0, 1e6);
const SAMPLE_RULE = {
name: `Cypress test rule ${uniqueId}`,
logType: 'windows',
description: 'This is a rule used to test the rule creation workflow. Not for production use.',
description: 'This is a rule used to test the rule creation workflow.',
detection:
'selection:\n Provider_Name: Service Control Manager\nEventID: 7045\nServiceName: ZzNetSvc\n{backspace}{backspace}condition: selection',
detectionLine: [
Expand All @@ -28,22 +28,23 @@ const SAMPLE_RULE = {
};

const YAML_RULE_LINES = [
`id:`,
`logsource:`,
`product: ${SAMPLE_RULE.logType}`,
`title: ${SAMPLE_RULE.name}`,
`description:`,
`${SAMPLE_RULE.description}`,
`level: ${SAMPLE_RULE.severity}`,
`description: ${SAMPLE_RULE.description}`,
`tags:`,
`- ${SAMPLE_RULE.tags[0]}`,
`- ${SAMPLE_RULE.tags[1]}`,
`- ${SAMPLE_RULE.tags[2]}`,
`references:`,
`- '${SAMPLE_RULE.references}'`,
`falsepositives:`,
`- ${SAMPLE_RULE.falsePositive}`,
`author: ${SAMPLE_RULE.author}`,
`level: ${SAMPLE_RULE.severity}`,
`status: ${SAMPLE_RULE.status}`,
`logsource:`,
`product: ${SAMPLE_RULE.logType}`,
`references:`,
`- '${SAMPLE_RULE.references}'`,
`author: ${SAMPLE_RULE.author}`,
`detection:`,
...SAMPLE_RULE.detection.replaceAll(' ', '').replaceAll('{backspace}', '').split('\n'),
];

Expand Down Expand Up @@ -222,11 +223,24 @@ describe('Rules', () => {
force: true,
});

YAML_RULE_LINES.forEach((line) =>
cy
.get('[data-test-subj="rule_flyout_yaml_rule"]', TWENTY_SECONDS_TIMEOUT)
.contains(line, TWENTY_SECONDS_TIMEOUT)
);
cy.get('[data-test-subj="rule_flyout_yaml_rule"]')
.get('[class="euiCodeBlock__line"]')
.each((lineElement, lineIndex) => {
if (lineIndex >= YAML_RULE_LINES.length) {
return;
}
let line = lineElement.text().replaceAll('\n', '').trim();
let expectedLine = YAML_RULE_LINES[lineIndex];

// The document ID field is generated when the document is added to the index,
// so this test just checks that the line starts with the ID key.
if (expectedLine.startsWith('id:')) {
expectedLine = 'id:';
expect(line, `Sigma rule line ${lineIndex}`).to.contain(expectedLine);
} else {
expect(line, `Sigma rule line ${lineIndex}`).to.equal(expectedLine);
}
});

// Close the flyout
cy.get('[data-test-subj="close-rule-details-flyout"]', TWENTY_SECONDS_TIMEOUT).click({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const RuleContentYamlViewer: React.FC<RuleContentYamlViewerProps> = ({ ru
const ruleYaml = mapYamlObjectToYamlString(yamlObject);

return (
<EuiCodeBlock language="yaml" data-test-subj={'rule_flyout_yaml_rule'}>
<EuiCodeBlock language="yaml" data-test-subj={'rule_flyout_yaml_rule'} isCopyable={true}>
{ruleYaml}
</EuiCodeBlock>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`<RuleContentYamlViewer /> spec renders the component 1`] = `
<div
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge prismjs language-yaml"
class="euiCodeBlock euiCodeBlock--fontSmall euiCodeBlock--paddingLarge euiCodeBlock--hasControls prismjs language-yaml"
>
<pre
class="euiCodeBlock__pre euiCodeBlock__pre--whiteSpacePreWrap"
Expand Down Expand Up @@ -403,5 +403,24 @@ exports[`<RuleContentYamlViewer /> spec renders the component 1`] = `
</span>
</code>
</pre>
<div
class="euiCodeBlock__controls"
>
<div
class="euiCodeBlock__copyButton"
>
<span
class="euiToolTipAnchor"
>
<button
aria-label="Copy"
class="euiButtonIcon euiButtonIcon--text euiButtonIcon--empty euiButtonIcon--xSmall"
type="button"
>
EuiIconMock
</button>
</span>
</div>
</div>
</div>
`;

0 comments on commit c6d5dbe

Please sign in to comment.