Skip to content

Commit

Permalink
test: Cleanup OpenSCAP tests
Browse files Browse the repository at this point in the history
This cleans up OpenSCAP test file, utilising helper functions we already have and updating names of test and describe blocks to be consistent throughout the test files.
  • Loading branch information
regexowl committed Sep 3, 2024
1 parent e92acd5 commit 979198d
Showing 1 changed file with 50 additions and 146 deletions.
196 changes: 50 additions & 146 deletions src/test/Components/CreateImageWizard/steps/Oscap/Oscap.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import React from 'react';

import { screen, waitFor } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';

import CreateImageWizard from '../../../../../Components/CreateImageWizard/CreateImageWizard';
import ShareImageModal from '../../../../../Components/ShareImageModal/ShareImageModal';
import { CREATE_BLUEPRINT, EDIT_BLUEPRINT } from '../../../../../constants';
import { CreateBlueprintRequest } from '../../../../../store/imageBuilderApi';
import { mockBlueprintIds } from '../../../../fixtures/blueprints';
Expand All @@ -17,7 +13,6 @@ import {
expectedServicesCisL2,
oscapCreateBlueprintRequest,
} from '../../../../fixtures/editMode';
import { renderCustomRoutesWithReduxRouter } from '../../../../testUtils';
import {
clickNext,
clickRegisterLater,
Expand All @@ -29,12 +24,36 @@ import {
renderEditMode,
} from '../../wizardTestUtils';

const goToOscapStep = async () => {
const selectRhel8 = async () => {
const user = userEvent.setup();
await waitFor(async () =>
user.click(
screen.getAllByRole('button', {
name: /options menu/i,
})[0]
)
);
const rhel8 = await screen.findByRole('option', {
name: /red hat enterprise linux \(rhel\) 8/i,
});
await waitFor(async () => user.click(rhel8));
};

const selectGuestImageTarget = async () => {
const user = userEvent.setup();
const guestImageCheckBox = await screen.findByRole('checkbox', {
name: /virtualization guest image checkbox/i,
});
await waitFor(() => user.click(guestImageCheckBox));
};

const selectWslTarget = async () => {
const user = userEvent.setup();
const wslCheckbox = await screen.findByTestId('checkbox-wsl');
user.click(wslCheckbox);
};

const goToOscapStep = async () => {
await clickNext(); // Registration
await clickRegisterLater();
await clickNext(); // OpenSCAP
Expand Down Expand Up @@ -87,91 +106,18 @@ const goToReviewStep = async () => {
await clickNext(); // Review
};

const routes = [
{
path: 'insights/image-builder/*',
element: <div />,
},
{
path: 'insights/image-builder/imagewizard/:composeId?',
element: <CreateImageWizard />,
},
{
path: 'insights/image-builder/share/:composeId',
element: <ShareImageModal />,
},
];

const selectRhel8 = async () => {
const user = userEvent.setup();
await waitFor(async () =>
user.click(
screen.getAllByRole('button', {
name: /options menu/i,
})[0]
)
);
const rhel8 = await screen.findByRole('option', {
name: /red hat enterprise linux \(rhel\) 8/i,
});
await waitFor(async () => user.click(rhel8));
};

const clickFromImageOutputToOpenScap = async () => {
const user = userEvent.setup();
await clickNext();
await waitFor(async () =>
user.click(await screen.findByTestId('automatically-register-checkbox'))
);
await clickNext(); // skip registration
};

describe('Step Compliance', () => {
describe('Step OpenSCAP', () => {
beforeEach(() => {
vi.clearAllMocks();
});

const user = userEvent.setup();
const setup = async () => {
renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);
};
test('create an image with None oscap profile', async () => {
await setup();

// select aws as upload destination
const uploadAws = await screen.findByTestId('upload-aws');
user.click(uploadAws);
await clickNext();

// aws step
const manualOption = await screen.findByRole('radio', {
name: /manually enter an account id\./i,
});
await waitFor(() => user.click(manualOption));
const awsAccountId = await screen.findByRole('textbox', {
name: 'aws account id',
});
await waitFor(() => user.type(awsAccountId, '012345678901'));

await clickNext();
// skip registration
const registrationCheckbox = await screen.findByTestId(
'automatically-register-checkbox'
);

user.click(registrationCheckbox);
await clickNext();

// Now we should be in the Compliance step
await screen.findByRole('heading', { name: /OpenSCAP/i });

const selectProfile = await screen.findByRole('textbox', {
name: /select a profile/i,
});

user.click(selectProfile);
const noneProfile = await screen.findByText(/none/i);
user.click(noneProfile);
test('create an image with None oscap profile', async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectNone();

// check that the FSC does not contain a /tmp partition
await clickNext();
Expand All @@ -196,50 +142,11 @@ describe('Step Compliance', () => {
});

test('create an image with an oscap profile', async () => {
await setup();

// select aws as upload destination
const uploadAws = await screen.findByTestId('upload-aws');

user.click(uploadAws);
await clickNext();

// aws step
const manualOption = await screen.findByRole('radio', {
name: /manually enter an account id\./i,
});

await waitFor(() => user.click(manualOption));

await waitFor(async () =>
user.type(
await screen.findByRole('textbox', {
name: 'aws account id',
}),
'012345678901'
)
);
await clickNext();
// skip registration
const registrationCheckbox = await screen.findByTestId(
'automatically-register-checkbox'
);

user.click(registrationCheckbox);
await clickNext();

// Now we should be at the OpenSCAP step
await screen.findByRole('heading', { name: /OpenSCAP/i });

const selectProfile = await screen.findByRole('textbox', {
name: /select a profile/i,
});
user.click(selectProfile);
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();

const cis1Profile = await screen.findByText(
/cis red hat enterprise linux 8 benchmark for level 1 - workstation/i
);
user.click(cis1Profile);
await screen.findByText(/kernel arguments:/i);
await screen.findByText(/audit_backlog_limit=8192 audit=1/i);
await screen.findByText(/disabled services:/i);
Expand All @@ -254,7 +161,7 @@ describe('Step Compliance', () => {
await screen.findByRole('heading', { name: /File system configuration/i });
await screen.findByText(/tmp/i);

await clickNext(); // skip RepositorySnapshots
await clickNext(); // skip Snapshots
await clickNext(); // skip Repositories

// check that the Packages contains correct packages
Expand All @@ -268,12 +175,11 @@ describe('Step Compliance', () => {
await screen.findByText(/neovim/i);
});

test('OpenSCAP dropdown is disabled for WSL targets only', async () => {
await setup();
test('dropdown is disabled for WSL targets only', async () => {
await renderCreateMode();
await selectRhel8();
const wslCheckbox = await screen.findByTestId('checkbox-wsl');
user.click(wslCheckbox);
await clickFromImageOutputToOpenScap();
await selectWslTarget();
await goToOscapStep();
await screen.findByText(
/OpenSCAP profiles are not compatible with WSL images/i
);
Expand All @@ -282,18 +188,12 @@ describe('Step Compliance', () => {
).toBeDisabled();
});

test('Alert displayed and OpenSCAP dropdown enabled when targets include WSL', async () => {
await setup();
test('alert displayed and OpenSCAP dropdown enabled when targets include WSL', async () => {
await renderCreateMode();
await selectRhel8();
const imageInstallerCheckbox = await screen.findByTestId(
'checkbox-image-installer'
);

user.click(imageInstallerCheckbox);
const wslCheckbox = await screen.findByTestId('checkbox-wsl');

user.click(wslCheckbox);
await clickFromImageOutputToOpenScap();
await selectGuestImageTarget();
await selectWslTarget();
await goToOscapStep();
await screen.findByText(
/OpenSCAP profiles are not compatible with WSL images/i
);
Expand All @@ -305,16 +205,18 @@ describe('Step Compliance', () => {
});
});

describe('oscap', () => {
describe('OpenSCAP request generated correctly', () => {
beforeEach(() => {
vi.clearAllMocks();
});

test('add a profile', async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();
await goToReviewStep();

// informational modal pops up in the first test only as it's tied
// to a 'imageBuilder.saveAndBuildModalSeen' variable in localStorage
await openAndDismissSaveAndBuildModal();
Expand All @@ -332,6 +234,7 @@ describe('oscap', () => {

test('remove a profile', { retry: 3, timeout: 20000 }, async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();
await selectNone();
Expand All @@ -350,6 +253,7 @@ describe('oscap', () => {

test('change profile', { retry: 3, timeout: 20000 }, async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();
await selectDifferentProfile();
Expand Down

0 comments on commit 979198d

Please sign in to comment.