Skip to content

Commit

Permalink
functional tests up and running
Browse files Browse the repository at this point in the history
  • Loading branch information
ofiriro3 committed Jan 15, 2023
1 parent 75aac4a commit 38f1f0e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ export const getFindingsQuery = ({ query, sort }: UseFindingsOptions) => {
* By default, ES will sort keyword fields in case-sensitive format, the
* following fields are required to have a case-insensitive sorting.
*/
const fieldsRequiredSortingByPainlessScript = ['rule.section', 'resource.name', 'resource.type'];
const fieldsRequiredSortingByPainlessScript = [
'rule.section',
'resource.name',
'resource.sub_type',
];

/**
* Generates Painless sorting if the given field is matched or returns default sorting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function FindingsPageProvider({ getService, getPageObjects }: FtrProvider
},

getColumnValues: async (columnName: string) => {
const elementsWithNoFilterCell = ['CIS Section'];
const elementsWithNoFilterCell = ['CIS Section', '@timestamp'];
const tableElement = await table.getElement();
const columnIndex = await table.getColumnIndex(columnName);
const selector = elementsWithNoFilterCell.includes(columnName)
Expand Down
85 changes: 60 additions & 25 deletions x-pack/test/cloud_security_posture_functional/pages/findings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,48 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
const filterBar = getService('filterBar');
const retry = getService('retry');
const pageObjects = getPageObjects(['common', 'findings']);
const numberOfFindings = 10;
const chance = new Chance();

const data = Array.from({ length: numberOfFindings }, (_, id) => {
return {
resource: { id, name: `Resource ${id}` },
result: { evaluation: id === 0 ? 'passed' : 'failed' },
// We need to use a dataset for the tests to run
// We intentionally make some fields start with a capital letter to test that the query bar is case-insensitive/case-sensitive
const data = [
{
resource: { id: chance.guid(), name: `kubelet`, sub_type: 'lower case sub type' },
result: { evaluation: chance.integer() % 2 === 0 ? 'passed' : 'failed' },
rule: {
name: `Rule ${id}`,
section: chance.string({ pool: 'abcdefghABCDEFGH', length: 10 }),
tags: ['Kubernetes'],
type: 'process',
name: 'Upper case rule name',
section: 'Upper case section',
},
};
});
cluster_id: 'Upper case cluster id',
},
{
resource: { id: chance.guid(), name: `Pod`, sub_type: 'Upper case sub type' },
result: { evaluation: chance.integer() % 2 === 0 ? 'passed' : 'failed' },
rule: {
name: 'lower case rule name',
section: 'Another upper case section',
},
cluster_id: 'Another Upper case cluster id',
},
{
resource: { id: chance.guid(), name: `process`, sub_type: 'another lower case type' },
result: { evaluation: 'passed' },
rule: {
name: 'Another upper case rule name',
section: 'lower case section',
},
cluster_id: 'lower case cluster id',
},
{
resource: { id: chance.guid(), name: `process`, sub_type: 'Upper case type again' },
result: { evaluation: 'failed' },
rule: {
name: 'some lower case rule name',
section: 'another lower case section',
},
cluster_id: 'another lower case cluster id',
},
];

const ruleName1 = data[0].rule.name;
const ruleName2 = data[1].rule.name;
Expand Down Expand Up @@ -106,28 +133,36 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
});

describe('Table Sort', () => {
type SortingMethod = (string, string) => number;
type SortDirection = 'asc' | 'desc';
// Sort by lexical order will sort by the first character of the string (case-sensitive)
const compareStringByLexicographicOrder = (a: string, b: string) => {
return +(a > b) || -(b > a);
};
const sortByAlphabeticalOrder = (a: string, b: string) => {
return a.localeCompare(b);
};

it('sorts by a column, should be case sensitive/insensitive depending on the column', async () => {
type SortDirection = 'asc' | 'desc';
type TestCase = [string, string, SortDirection, Intl.CollatorOptions];
type TestCase = [string, SortDirection, SortingMethod];
const testCases: TestCase[] = [
['Rule', 'en', 'asc', {}],
['Rule', 'en', 'desc', {}],
['Resource Name', 'en', 'asc', {}],
['Resource Name', 'en', 'desc', {}],
['CIS Section', 'en', 'desc', { sensitivity: 'base' }],
['CIS Section', 'en', 'desc', { sensitivity: 'base' }],
['CIS Section', 'asc', sortByAlphabeticalOrder],
['CIS Section', 'desc', sortByAlphabeticalOrder],
['Cluster ID', 'asc', compareStringByLexicographicOrder],
['Cluster ID', 'desc', compareStringByLexicographicOrder],
['Resource Name', 'asc', sortByAlphabeticalOrder],
['Resource Name', 'desc', sortByAlphabeticalOrder],
['Resource Type', 'asc', sortByAlphabeticalOrder],
['Resource Type', 'desc', sortByAlphabeticalOrder],
];
for (const [columnName, locale, dir, sortingOptions] of testCases) {
for (const [columnName, dir, sortingMethod] of testCases) {
await table.toggleColumnSort(columnName, dir);
const values = (await table.getColumnValues(columnName)).filter(Boolean);
expect(values).to.not.be.empty();

const sorted = values
.slice()
.sort((a, b) =>
dir === 'asc'
? a.localeCompare(b, locale, sortingOptions)
: b.localeCompare(a, locale, sortingOptions)
);
.sort((a, b) => (dir === 'asc' ? sortingMethod(a, b) : sortingMethod(b, a)));
values.forEach((value, i) => expect(value).to.be(sorted[i]));
}
});
Expand Down

0 comments on commit 38f1f0e

Please sign in to comment.