Skip to content

Commit

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

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* added try catch to avoid page crash for invalid document

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* added cypress test

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

---------

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
  • Loading branch information
amsiglan committed Mar 13, 2024
1 parent 8163803 commit ba69d57
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 @@ -142,9 +142,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('sample_detector');
Expand Down Expand Up @@ -205,5 +202,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 @@ -32,6 +32,7 @@ import {
EuiTab,
EuiInMemoryTable,
EuiBasicTableColumn,
EuiEmptyPrompt,
} from '@elastic/eui';
import { capitalizeFirstLetter, renderTime } from '../../../utils/helpers';
import { DEFAULT_EMPTY_DATA, ROUTES } from '../../../utils/constants';
Expand Down Expand Up @@ -254,9 +255,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 @@ -314,10 +322,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 ba69d57

Please sign in to comment.