Skip to content

Commit

Permalink
Check for empty document in the finding flyout (opensearch-project#841)
Browse files Browse the repository at this point in the history
* check for empty document

Signed-off-by: Amardeepsingh Siglani <[email protected]>

* added try catch to avoid page crash for invalid document

Signed-off-by: Amardeepsingh Siglani <[email protected]>

* added cypress test

Signed-off-by: Amardeepsingh Siglani <[email protected]>

---------

Signed-off-by: Amardeepsingh Siglani <[email protected]>
  • Loading branch information
amsiglan authored Dec 13, 2023
1 parent 7788423 commit 7837a99
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
24 changes: 21 additions & 3 deletions cypress/integration/4_findings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ describe('Findings', () => {
});
});

// TODO - upon reaching rules page, trigger appropriate rules detail flyout
// see github issue #124 at https://github.com/opensearch-project/security-analytics-dashboards-plugin/issues/124

it('opens rule details flyout when rule name inside accordion drop down is clicked', () => {
// filter table to show only sample_detector findings
cy.get(`input[placeholder="Search findings"]`).ospSearch(indexName);
Expand All @@ -167,5 +164,26 @@ describe('Findings', () => {
});
});

it('shows document not found warning when the document is empty', () => {
cy.deleteIndex(indexName);
cy.reload();

// Wait for page to load
cy.waitForPageLoad('findings', {
contains: 'Findings',
});

// filter table to show only sample_detector findings
cy.get(`input[placeholder="Search findings"]`).ospSearch(indexName);

// open Finding details flyout via finding id link. cy.wait essential, timeout insufficient.
cy.getTableFirstRow('[data-test-subj="view-details-icon"]').then(($el) => {
cy.get($el).click({ force: true });
});

// Flyout should show 'Document not found' warning
cy.contains('Document not found');
});

after(() => cy.cleanUpTests());
});
25 changes: 23 additions & 2 deletions public/pages/Findings/components/FindingDetailsFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
EuiTabs,
EuiTab,
EuiLoadingContent,
EuiEmptyPrompt,
} from '@elastic/eui';
import {
capitalizeFirstLetter,
Expand Down Expand Up @@ -306,9 +307,16 @@ export default class FindingDetailsFlyout extends Component<
const docId = related_doc_ids[0];
const matchedDocuments = documents.filter((doc) => doc.id === docId);
const document = matchedDocuments.length > 0 ? matchedDocuments[0].document : '';
let formattedDocument = '';
try {
formattedDocument = document ? JSON.stringify(JSON.parse(document), null, 2) : '';
} catch {
// no-op
}

const { indexPatternId } = this.state;

return (
return document ? (
<>
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem>
Expand Down Expand Up @@ -367,10 +375,23 @@ export default class FindingDetailsFlyout extends Component<
isCopyable
data-test-subj={'finding-details-flyout-rule-document'}
>
{JSON.stringify(JSON.parse(document), null, 2)}
{formattedDocument}
</EuiCodeBlock>
</EuiFormRow>
</>
) : (
<>
<EuiTitle size={'s'}>
<h3>Documents</h3>
</EuiTitle>
<EuiSpacer />
<EuiEmptyPrompt
iconType="alert"
iconColor="danger"
title={<h2>Document not found</h2>}
body={<p>The document that generated this finding could not be loaded.</p>}
/>
</>
);
}

Expand Down

0 comments on commit 7837a99

Please sign in to comment.