Skip to content
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
merged 10 commits into from
Jan 30, 2025
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&param_EnrollmentToken=FLEET_ENROLLMENT_TOKEN&param_FleetUrl=FLEET_URL&param_ElasticAgentVersion=KIBANA_VERSION&param_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&param_EnrollmentToken=FLEET_ENROLLMENT_TOKEN&param_FleetUrl=FLEET_URL&param_ElasticAgentVersion=KIBANA_VERSION&param_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',
};
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');
});
});
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()
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor

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 :)

);

// 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));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import equals from 'fast-deep-equal';
import { AGENTLESS_SECURITY_POSTURE_PACKAGE_VERSION } from '../constants';
import type { FtrProviderContext } from '../ftr_provider_context';
import { setupMockServer } from './mock_agentless_api';
import { testSubjectIds } from '../constants/test_subject_ids';

const { CIS_AWS_OPTION_TEST_ID, AWS_SINGLE_ACCOUNT_TEST_ID } = testSubjectIds;

// eslint-disable-next-line import/no-default-export
export default function ({ getPageObjects, getService }: FtrProviderContext) {
const agentCreationTimeout = 1000 * 60 * 1; // 1 minute
Expand All @@ -24,10 +28,6 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
'cisAddIntegration',
]);

const CIS_AWS_OPTION_TEST_ID = 'cisAwsTestId';

const AWS_SINGLE_ACCOUNT_TEST_ID = 'awsSingleTestId';

// Failing: See https://github.com/elastic/kibana/issues/208495
describe('Agentless cloud', function () {
let cisIntegration: typeof pageObjects.cisAddIntegration;
Expand Down
Loading
Loading