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 20, 2024
1 parent 486fc27 commit dd4bda8
Showing 1 changed file with 53 additions and 148 deletions.
201 changes: 53 additions & 148 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, within } 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 {
clickRegisterLater,
enterBlueprintName,
Expand All @@ -29,12 +24,27 @@ import {
} from '../../wizardTestUtils';
import { clickNext, clickReviewAndFinish } from '../../wizardTestUtils';

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

const selectImageInstallerTarget = async () => {
const user = userEvent.setup();
const imageInstallerCheckBox = await screen.findByTestId(
'checkbox-image-installer'
);
await waitFor(() => user.click(imageInstallerCheckBox));
};

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

const goToOscapStep = async () => {
await clickNext(); // Registration
await clickRegisterLater();
await clickNext(); // OpenSCAP
Expand Down Expand Up @@ -96,21 +106,6 @@ const clickRevisitButton = async () => {
await waitFor(() => user.click(revisitButton));
};

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 () =>
Expand All @@ -126,61 +121,18 @@ const selectRhel8 = async () => {
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 @@ -205,50 +157,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 Down Expand Up @@ -277,12 +190,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 @@ -291,18 +203,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 selectImageInstallerTarget();
await selectWslTarget();
await goToOscapStep();
await screen.findByText(
/OpenSCAP profiles are not compatible with WSL images/i
);
Expand All @@ -312,30 +218,36 @@ describe('Step Compliance', () => {
).toBeEnabled();
});
});
});

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

test('clicking Review and finish leads to Details', async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await clickReviewAndFinish();
await screen.findByRole('heading', {
name: 'Details',
});
});

test('revisit step button on Review works', async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();
await goToReviewStep();
await clickRevisitButton();
await screen.findByRole('heading', { name: /OpenSCAP/ });
});
});

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

test('add a profile', async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();
await goToReviewStep();
Expand All @@ -356,6 +268,7 @@ describe('OpenSCAP', () => {

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

test('change profile', { retry: 3, timeout: 20000 }, async () => {
await renderCreateMode();
await selectGuestImageTarget();
await goToOscapStep();
await selectProfile();
await selectDifferentProfile();
Expand All @@ -397,15 +311,6 @@ describe('OpenSCAP', () => {
expect(receivedRequest).toEqual(expectedRequest);
});
});

test('revisit step button on Review works', async () => {
await renderCreateMode();
await goToOscapStep();
await selectProfile();
await goToReviewStep();
await clickRevisitButton();
await screen.findByRole('heading', { name: /OpenSCAP/ });
});
});

describe('OpenSCAP edit mode', () => {
Expand Down

0 comments on commit dd4bda8

Please sign in to comment.