Skip to content

Commit

Permalink
Pass custom column as argument and construct value later. Improve e2e…
Browse files Browse the repository at this point in the history
… tests for alert and computed columns

Signed-off-by: Gavin Reynolds <[email protected]>
  • Loading branch information
gsreynolds committed Jul 18, 2024
1 parent 0459b8d commit 38ac483
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 37 deletions.
73 changes: 51 additions & 22 deletions cypress/e2e/Settings/settings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,56 +141,85 @@ describe('Manage Settings', { failFast: { enabled: true } }, () => {
});

it('Add valid custom alert column to incident table', () => {
const customAlertColumnDefinitions = ['Quote:details.quote'];
manageCustomColumnDefinitions(customAlertColumnDefinitions);
customAlertColumnDefinitions.forEach((columnName) => {
const header = columnName.split(':')[0];
cy.get(`[data-column-name="${header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
const customColumnDefinitions = [
{ header: 'Quote', accessorPath: 'details.quote', expression: '' },
];
manageCustomColumnDefinitions(customColumnDefinitions);
customColumnDefinitions.forEach((column) => {
cy.get(`[data-column-name="${column.header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${column.header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
// eslint-disable-next-line no-unused-expressions
expect($el.text()).to.exist;
// Quote exists in the alert body, so it should not be empty
expect($el.text()).to.not.equal('--');
expect($el.text().length).to.be.greaterThan(20);
});
});
});

it('Add valid custom alert column with JSON path containing spaces to incident table', () => {
const customColumnDefinitions = ["Fav Flavour:details.['favorite ice cream flavor']"];
const customColumnDefinitions = [
{ header: 'Fav Flavour', accessorPath: "details.['favorite ice cream flavor']", expression: '' },
];
manageCustomColumnDefinitions(customColumnDefinitions);
customColumnDefinitions.forEach((columnName) => {
const header = columnName.split(':')[0];
cy.get(`[data-column-name="${header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
customColumnDefinitions.forEach((column) => {
cy.get(`[data-column-name="${column.header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${column.header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
// eslint-disable-next-line no-unused-expressions
expect($el.text()).to.exist;
// Fav Flavour doesn't exist in the alert body, so it should be empty
expect($el.text()).to.equal('--');
});
});
});

it('Add valid custom computed column to incident table', () => {
const customColumnDefinitions = ['CI:first_trigger_log_entry.channel.details:(.*.example.com)'];
const customColumnDefinitions = [
{ header: 'CI', accessorPath: 'first_trigger_log_entry.channel.details', expression: '(.*.example.com)' },
];
manageCustomColumnDefinitions(customColumnDefinitions, 'computed');
customColumnDefinitions.forEach((columnName) => {
const header = columnName.split(':')[0];
cy.get(`[data-column-name="${header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
customColumnDefinitions.forEach((column) => {
cy.get(`[data-column-name="${column.header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${column.header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
// eslint-disable-next-line no-unused-expressions
expect($el.text()).to.exist;
// CI doesn't exist in the alert body, so it should be empty
expect($el.text()).to.equal('--');
});
});
});

it('Add two valid custom computed column to incident table with different expressions', () => {
const customColumnDefinitions = [
'CI:first_trigger_log_entry.channel.details:(.*.example.com)',
'Category:first_trigger_log_entry.channel.details:Category(.*)'
{ header: 'CI', accessorPath: 'first_trigger_log_entry.channel.details', expression: '(.*.example.com)' },
{ header: 'Category', accessorPath: 'first_trigger_log_entry.channel.details', expression: 'Category(.*)' },
];
manageCustomColumnDefinitions(customColumnDefinitions, 'computed');
customColumnDefinitions.forEach((column) => {
cy.get(`[data-column-name="${column.header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${column.header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
// eslint-disable-next-line no-unused-expressions
expect($el.text()).to.exist;
// CI or Category don't exist in the alert body, so it should be empty
expect($el.text()).to.equal('--');
});
});
});

it('Add valid quote custom computed column to incident table', () => {
const customColumnDefinitions = [
{ header: 'QuoteRegex', accessorPath: 'first_trigger_log_entry.channel.details', expression: '{"quote":"(.*)"}' },
];
manageCustomColumnDefinitions(customColumnDefinitions, 'computed');
customColumnDefinitions.forEach((columnName) => {
const header = columnName.split(':')[0];
cy.get(`[data-column-name="${header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
customColumnDefinitions.forEach((column) => {
cy.get(`[data-column-name="${column.header}"]`).scrollIntoView().should('be.visible');
cy.get(`[data-incident-header="${column.header}"][data-incident-row-cell-idx="0"]`).then(($el) => {
// eslint-disable-next-line no-unused-expressions
expect($el.text()).to.exist;
// Quote does exist in the alert body, so it should not be empty and also shouldn't contain the custom details JSON with quote key, just the quote value
expect($el.text()).to.not.equal('--');
expect($el.text()).to.not.contain('"quote"');
expect($el.text().length).to.be.greaterThan(20);
});
});
});
Expand Down
14 changes: 10 additions & 4 deletions cypress/support/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,24 @@ export const manageCustomColumnDefinitions = (customColumnDefinitions, type = 'a
cy.wrap($el).click();
});

customColumnDefinitions.forEach((customAlertColumnDefinition) => {
const [header, accessorPath, expression] = customAlertColumnDefinition.split(':');
customColumnDefinitions.forEach((customColumnDefinition) => {
const {
header, accessorPath, expression,
} = customColumnDefinition;
console.error(header, accessorPath, expression);
cy.get('#column-type-select').select(type);
cy.get('input[placeholder="Header"]').clear().type(header);
cy.get('input[placeholder="JSON Path"]').clear().type(accessorPath);
if (type === 'computed') {
cy.get('input[placeholder="Regex"]').clear().type(expression);
cy.get('input[placeholder="Regex"]').clear().type(expression, { parseSpecialCharSequences: false });
}
cy.get('button[aria-label="Add custom column"]').click();
// Need to escape special characters in accessorPath
// https://docs.cypress.io/faq/questions/using-cypress-faq#How-do-I-use-special-characters-with-cyget
cy.get(`#column-${Cypress.$.escapeSelector(customAlertColumnDefinition)}-add-icon`).click();
const columnId = Cypress.$.escapeSelector(
[header, accessorPath, expression.replace(/:/g, '\\:')].filter((value) => value !== '').join(':'),

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.
);
cy.get(`#column-${columnId}-add-icon`).click();
});
cy.get('#save-columns-button').click();
checkActionAlertsModalContent('Incident table columns saved');
Expand Down
24 changes: 13 additions & 11 deletions src/components/ColumnsModal/ColumnsModalComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ const TableColumnsModalComponent = () => {
setSelectedColumns((prev) => [...prev, column]);
};

const addCustomAlertColumn = (value) => {
const [Header, accessorPath] = value.split(':');
const addCustomAlertColumn = (Header, accessorPath) => {
const value = `${Header}:${accessorPath}`;
if (!Header || !accessorPath) {
return;
}
Expand All @@ -138,8 +138,8 @@ const TableColumnsModalComponent = () => {
setAlertCustomDetailColumns(newAlertCustomDetailFields);
};

const addCustomComputedColumn = (value) => {
const [Header, accessorPath, expression] = value.split(':');
const addCustomComputedColumn = (Header, accessorPath, expression) => {
const value = `${Header}:${accessorPath}:${expression.replace(/:/g, '\\:')}`;

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.
if (!Header || !accessorPath || !expression) {
return;
}
Expand Down Expand Up @@ -340,14 +340,16 @@ const TableColumnsModalComponent = () => {
isDisabled={!inputIsValid}
onClick={() => {
if (columnType === 'alert') {
const value = `${headerInputRef.current.value}:`
+ `${accessorPathInputRef.current.value}`;
addCustomAlertColumn(value);
addCustomAlertColumn(
headerInputRef.current.value,
accessorPathInputRef.current.value,
);
} else {
const value = `${headerInputRef.current.value}:`
+ `${accessorPathInputRef.current.value}:`
+ `${regexInputRef.current.value}`;
addCustomComputedColumn(value);
addCustomComputedColumn(
headerInputRef.current.value,
accessorPathInputRef.current.value,
regexInputRef.current.value,
);
}
}}
m={1}
Expand Down

0 comments on commit 38ac483

Please sign in to comment.