-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Cloud Security] 10973 migrate flaky e2e tests to jest #208345
Merged
+580
−149
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e3b9c89
add vulnerabilities unit tests related to empty state
alexreal1314 69a64af
add retry mechanism for Hyperlink on PostInstallation Modal should ha…
alexreal1314 12c836b
add cleaning up open tabs before each test in cis_integration_aws.ts …
alexreal1314 096a5f5
fix missing test ids
alexreal1314 71b7b27
try to fix 186302 flaky test
alexreal1314 c096ab5
fix conflicts
alexreal1314 82617e7
remove flaky cloud formation modal test and add unit tests instead
alexreal1314 f0aa4d8
fix mock data types
alexreal1314 c1c07e2
fix conflicts
alexreal1314 ca41c56
remove ftr test related to cloud formation confirmation modal
alexreal1314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
...ge_policy_page/single_page_layout/components/cloud_security_posture/__tests__/mockData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { MonitoringType } from '../../../../../../../../../../common/types'; | ||
|
||
export const mockAgentPolicy = { | ||
id: '888c9a80-cae6-4675-82ad-73553cb343f9', | ||
name: 'Agent policy 1', | ||
description: '', | ||
namespace: 'default', | ||
monitoring_enabled: ['logs', 'metrics', 'traces'] as MonitoringType, | ||
inactivity_timeout: 1209600, | ||
is_protected: false, | ||
status: 'active' as const, // or 'Inactive' as const | ||
is_managed: false, | ||
revision: 1, | ||
updated_at: '2025-01-29T14:12:36.256Z', | ||
updated_by: 'elastic', | ||
schema_version: '1.1.1', | ||
}; | ||
|
||
export const mockPackagePolicy = { | ||
id: '5811fb40-82e4-4808-b94e-5e02158936c7', | ||
version: 'WzgyMSwxXQ==', | ||
name: 'cspm-1', | ||
namespace: 'default', | ||
description: '', | ||
package: { | ||
name: 'cloud_security_posture', | ||
title: 'Security Posture Management', | ||
version: '1.12.0', | ||
}, | ||
enabled: true, | ||
policy_id: '888c9a80-cae6-4675-82ad-73553cb343f9', | ||
policy_ids: ['888c9a80-cae6-4675-82ad-73553cb343f9'], | ||
inputs: [ | ||
{ | ||
type: 'cloudbeat/cis_k8s', | ||
policy_template: 'kspm', | ||
enabled: false, | ||
streams: [ | ||
{ | ||
enabled: false, | ||
data_stream: { | ||
type: 'logs', | ||
dataset: 'cloud_security_posture.findings', | ||
}, | ||
vars: { | ||
condition: { | ||
type: 'text', | ||
}, | ||
}, | ||
id: 'cloudbeat/cis_k8s-cloud_security_posture.findings-5811fb40-82e4-4808-b94e-5e02158936c7', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'cloudbeat/cis_eks', | ||
policy_template: 'kspm', | ||
enabled: false, | ||
streams: [ | ||
{ | ||
enabled: false, | ||
data_stream: { | ||
type: 'logs', | ||
dataset: 'cloud_security_posture.findings', | ||
}, | ||
vars: { | ||
condition: { type: 'text' }, | ||
access_key_id: { type: 'text' }, | ||
secret_access_key: { type: 'password' }, | ||
session_token: { type: 'text' }, | ||
shared_credential_file: { type: 'text' }, | ||
credential_profile_name: { type: 'text' }, | ||
role_arn: { type: 'text' }, | ||
aws_credentials_type: { type: 'text' }, | ||
}, | ||
id: 'cloudbeat/cis_eks-cloud_security_posture.findings-5811fb40-82e4-4808-b94e-5e02158936c7', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: 'cloudbeat/cis_aws', | ||
policy_template: 'cspm', | ||
enabled: true, | ||
streams: [ | ||
{ | ||
enabled: true, | ||
data_stream: { | ||
type: 'logs', | ||
dataset: 'cloud_security_posture.findings', | ||
}, | ||
vars: { | ||
condition: { type: 'text' }, | ||
access_key_id: { type: 'text' }, | ||
secret_access_key: { type: 'password' }, | ||
session_token: { type: 'text' }, | ||
shared_credential_file: { type: 'text' }, | ||
credential_profile_name: { type: 'text' }, | ||
role_arn: { type: 'text' }, | ||
aws_credentials_type: { value: 'cloud_formation', type: 'text' }, | ||
aws_account_type: { value: 'organization-account', type: 'text' }, | ||
aws_supports_cloud_connectors: { type: 'bool' }, | ||
}, | ||
id: 'cloudbeat/cis_aws-cloud_security_posture.findings-5811fb40-82e4-4808-b94e-5e02158936c7', | ||
compiled_stream: { | ||
period: '24h', | ||
fetchers: [ | ||
{ name: 'aws-iam' }, | ||
{ name: 'aws-ec2-network' }, | ||
{ name: 'aws-s3' }, | ||
{ name: 'aws-trail' }, | ||
{ name: 'aws-monitoring' }, | ||
{ name: 'aws-rds' }, | ||
], | ||
config: { | ||
v1: { | ||
type: 'cspm', | ||
deployment: 'aws', | ||
benchmark: 'cis_aws', | ||
aws: { | ||
account_type: 'organization-account', | ||
credentials: { | ||
type: 'cloud_formation', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
vars: { | ||
cloud_formation_template: { | ||
value: | ||
'https://console.aws.amazon.com/cloudformation/home#/stacks/quickcreate?templateURL=https://elastic-cspm-cft.s3.eu-central-1.amazonaws.com/cloudformation-cspm-ACCOUNT_TYPE-8.17.0.yml&stackName=Elastic-Cloud-Security-Posture-Management¶m_EnrollmentToken=FLEET_ENROLLMENT_TOKEN¶m_FleetUrl=FLEET_URL¶m_ElasticAgentVersion=KIBANA_VERSION¶m_ElasticArtifactServer=https://artifacts.elastic.co/downloads/beats/elastic-agent', | ||
type: 'text', | ||
}, | ||
}, | ||
config: { | ||
cloud_formation_template_url: { | ||
value: | ||
'https://console.aws.amazon.com/cloudformation/home#/stacks/quickcreate?templateURL=https://elastic-cspm-cft.s3.eu-central-1.amazonaws.com/cloudformation-cspm-ACCOUNT_TYPE-8.17.0.yml&stackName=Elastic-Cloud-Security-Posture-Management¶m_EnrollmentToken=FLEET_ENROLLMENT_TOKEN¶m_FleetUrl=FLEET_URL¶m_ElasticAgentVersion=KIBANA_VERSION¶m_ElasticArtifactServer=https://artifacts.elastic.co/downloads/beats/elastic-agent', | ||
}, | ||
}, | ||
}, | ||
], | ||
vars: { | ||
posture: { value: 'cspm', type: 'text' }, | ||
deployment: { value: 'aws', type: 'text' }, | ||
}, | ||
revision: 1, | ||
created_at: '2025-01-29T14:12:41.393Z', | ||
created_by: 'system', | ||
updated_at: '2025-01-29T14:12:41.393Z', | ||
updated_by: 'system', | ||
}; |
124 changes: 124 additions & 0 deletions
124
...t/components/cloud_security_posture/__tests__/post_install_cloud_formation_modal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react'; | ||
|
||
import { PostInstallCloudFormationModal } from '../post_install_cloud_formation_modal'; | ||
import { useAgentPolicyWithPackagePolicies } from '../../../../../../../../../components/agent_enrollment_flyout/hooks'; | ||
import { useFleetServerHostsForPolicy } from '../../../../../../../../../hooks'; | ||
|
||
import { useCreateCloudFormationUrl } from '../../../../../../../../../components/cloud_security_posture/hooks'; | ||
|
||
import { mockAgentPolicy, mockPackagePolicy } from './mockData'; | ||
|
||
jest.mock('@tanstack/react-query'); | ||
jest.mock('../../../../../../../../../components/agent_enrollment_flyout/hooks'); | ||
jest.mock('../../../../../../../../../hooks'); | ||
jest.mock('../../../../../../../../../components/cloud_security_posture/hooks'); | ||
|
||
describe('PostInstallCloudFormationModal', () => { | ||
const mockOnConfirm = jest.fn(); | ||
const mockOnCancel = jest.fn(); | ||
|
||
beforeAll(() => { | ||
(useAgentPolicyWithPackagePolicies as jest.Mock).mockReturnValue({ | ||
agentPolicyWithPackagePolicies: null, | ||
}); | ||
|
||
(useFleetServerHostsForPolicy as jest.Mock).mockReturnValue({ | ||
fleetServerHost: 'https://any-hostname:8220', | ||
isLoadingInitialRequest: false, | ||
}); | ||
|
||
(useCreateCloudFormationUrl as jest.Mock).mockReturnValue({ | ||
cloudFormationUrl: 'console.aws.amazon.com/cloudformation', | ||
}); | ||
}); | ||
|
||
it('should render the modal with confirm button enabled', () => { | ||
(useQuery as jest.Mock).mockReturnValueOnce({ | ||
data: { | ||
data: { | ||
items: [{ api_key: 'test-api-key' }], | ||
}, | ||
}, | ||
isLoading: true, | ||
}); | ||
|
||
render( | ||
<IntlProvider> | ||
<PostInstallCloudFormationModal | ||
onConfirm={mockOnConfirm} | ||
onCancel={mockOnCancel} | ||
agentPolicy={mockAgentPolicy} | ||
packagePolicy={mockPackagePolicy} | ||
/> | ||
</IntlProvider> | ||
); | ||
|
||
expect(screen.getByTestId('confirmCloudFormationModalConfirmButton')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render the modal with confirm button disabled', () => { | ||
(useQuery as jest.Mock).mockReturnValueOnce({ | ||
data: { | ||
data: { | ||
items: [{ api_key: 'test-api-key' }], | ||
}, | ||
}, | ||
isLoading: true, | ||
}); | ||
|
||
render( | ||
<IntlProvider> | ||
<PostInstallCloudFormationModal | ||
onConfirm={mockOnConfirm} | ||
onCancel={mockOnCancel} | ||
agentPolicy={mockAgentPolicy} | ||
packagePolicy={mockPackagePolicy} | ||
/> | ||
</IntlProvider> | ||
); | ||
|
||
expect(screen.getByTestId('confirmCloudFormationModalConfirmButton')).toBeDisabled(); | ||
}); | ||
|
||
it('should open correct cloudFormation URL', () => { | ||
// Mock window.open | ||
const mockWindowOpen = jest.fn(); | ||
window.open = mockWindowOpen; | ||
|
||
(useQuery as jest.Mock).mockReturnValueOnce({ | ||
data: { | ||
data: { | ||
items: [{ api_key: 'test-api-key' }], | ||
}, | ||
}, | ||
isLoading: false, | ||
}); | ||
|
||
render( | ||
<IntlProvider> | ||
<PostInstallCloudFormationModal | ||
onConfirm={mockOnConfirm} | ||
onCancel={mockOnCancel} | ||
agentPolicy={mockAgentPolicy} | ||
packagePolicy={mockPackagePolicy} | ||
/> | ||
</IntlProvider> | ||
); | ||
|
||
const confirmButton = screen.getByTestId('confirmCloudFormationModalConfirmButton'); | ||
fireEvent.click(confirmButton); | ||
|
||
expect(mockWindowOpen).toHaveBeenCalledTimes(1); | ||
expect(mockWindowOpen).toHaveBeenCalledWith('console.aws.amazon.com/cloudformation'); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
...urity/plugins/cloud_security_posture/public/components/no_vulnerabilities_states.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import React from 'react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { NoVulnerabilitiesStates } from './no_vulnerabilities_states'; | ||
import * as useCspSetupStatusApi from '@kbn/cloud-security-posture/src/hooks/use_csp_setup_status_api'; | ||
import * as useCspIntegrationLink from '../common/navigation/use_csp_integration_link'; | ||
import * as useAdd3PIntegrationRoute from '../common/api/use_wiz_integration_route'; | ||
import { | ||
CNVM_NOT_INSTALLED_ACTION_SUBJ, | ||
THIRD_PARTY_NO_VULNERABILITIES_FINDINGS_PROMPT_WIZ_INTEGRATION_BUTTON, | ||
} from './test_subjects'; | ||
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react'; | ||
import { CLOUD_SECURITY_POSTURE_BASE_PATH } from '@kbn/cloud-security-posture-common'; | ||
|
||
jest.mock('@kbn/cloud-security-posture/src/hooks/use_csp_setup_status_api'); | ||
jest.mock('../common/navigation/use_csp_integration_link'); | ||
jest.mock('../common/api/use_wiz_integration_route'); | ||
|
||
describe('NoVulnerabilitiesStates', () => { | ||
const cnvmintegrationLink = 'fleet/integrations/cloud_security_posture/add-integration'; | ||
const thirdPartyIntegrationLink = 'fleet/integrations/wiz/add-integration'; | ||
const vulnerabilitiesPath = `${CLOUD_SECURITY_POSTURE_BASE_PATH}/findings/vulnerabilities`; | ||
|
||
beforeAll(() => { | ||
(useCspIntegrationLink.useCspIntegrationLink as jest.Mock).mockReturnValue(cnvmintegrationLink); | ||
(useAdd3PIntegrationRoute.useAdd3PIntegrationRoute as jest.Mock).mockReturnValue( | ||
thirdPartyIntegrationLink | ||
); | ||
}); | ||
|
||
beforeEach(() => { | ||
(useCspSetupStatusApi.useCspSetupStatusApi as jest.Mock).mockReturnValue({ | ||
data: { | ||
vuln_mgmt: { status: 'not-installed' }, | ||
indicesDetails: [], | ||
}, | ||
}); | ||
|
||
render( | ||
<MemoryRouter initialEntries={[vulnerabilitiesPath]}> | ||
<IntlProvider> | ||
<NoVulnerabilitiesStates /> | ||
</IntlProvider> | ||
</MemoryRouter> | ||
); | ||
}); | ||
|
||
it('Vulnerabilities - `Add CNVM integration`: should have link element to CNVM integration installation page', async () => { | ||
await waitFor(() => | ||
expect( | ||
screen.getByText(/Elastic’s Cloud Native\s+Vulnerability Management/i) | ||
).toBeInTheDocument() | ||
); | ||
|
||
// Find the button | ||
const button = screen.getByTestId(CNVM_NOT_INSTALLED_ACTION_SUBJ); | ||
expect(button).toBeInTheDocument(); | ||
expect(button).toHaveAttribute('href', expect.stringContaining(cnvmintegrationLink)); | ||
}); | ||
|
||
it('Vulnerabilities - `Add Wiz integration`: should have link element to CNVM integration installation page', async () => { | ||
await waitFor(() => | ||
expect(screen.getByText(/Already using a\s+cloud security product?/i)).toBeInTheDocument() | ||
); | ||
|
||
// Find the button | ||
const button = screen.getByTestId( | ||
THIRD_PARTY_NO_VULNERABILITIES_FINDINGS_PROMPT_WIZ_INTEGRATION_BUTTON | ||
); | ||
expect(button).toBeInTheDocument(); | ||
expect(button).toHaveAttribute('href', expect.stringContaining(thirdPartyIntegrationLink)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexreal1314 checking the exact text makes the test quite brittle. Every change to the wording would mean a failing test. Ideally, we should adda test id for every selector we use in the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxcold agree, I'll convert it to test id in the next PR that should fix some more flaky tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxcold I see your point about avoiding brittle tests due to wording changes, and I agree that test IDs can provide a stable way to locate elements. However, I believe there’s value in also asserting visible text, especially for key user-facing elements.
Relying solely on test IDs can make tests less reflective of the real user experience since test IDs are not part of what the user actually interacts with. By checking for meaningful text in assertions (like in this case with getByText), we ensure that the UI presents the correct messaging, which is a crucial part of user expectations.
A balanced approach could be to use test IDs for structural assertions (e.g., ensuring an element exists or checking attributes like href), while still verifying key text content where appropriate. In this case, the assertion for the message ensures that users see the correct prompt, while the button's testId allows for stable selection.
What do you think about maintaining a mix of both approaches based on what’s being tested rather than applying test IDs universally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@opauloh I think tests should avoid testing implementation details. For me the litmus test of a good test is when I change the implementation and the test is still green if the business logic didn't change. In the case of the wording, you will be updating test with every wording change. Another way to check if it makes sense is to push it to the absurd level: imagine we test for every visible text in Kibana, does it make sense to you? I believe it will make tests maintenance unbearable.
Plus you don't test for what's really important when it comes to texts anyway. Does it have typos? Does it provide outdated information that is not relevant anymore? etc.
Maybe in the future with the help of AI we will be testing texts in the way it makes sense, eg.
screen.prompt('check that text promoting connecting existing 3p product integration is present ')
But we are not there yet :)