Skip to content

Commit

Permalink
[Security Solution] [Bug Fix] Fix flakey cypress tests (#101231)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic authored Jun 2, 2021
1 parent 98527ad commit 71b4c38
Showing 1 changed file with 6 additions and 51 deletions.
57 changes: 6 additions & 51 deletions x-pack/plugins/security_solution/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,17 @@
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

import { findIndex } from 'lodash/fp';

const getFindRequestConfig = (searchStrategyName, factoryQueryType) => {
if (!factoryQueryType) {
return {
options: { strategy: searchStrategyName },
};
}

return {
options: { strategy: searchStrategyName },
request: { factoryQueryType },
};
};

Cypress.Commands.add(
'stubSearchStrategyApi',
function (stubObject, factoryQueryType, searchStrategyName = 'securitySolutionSearchStrategy') {
cy.intercept('POST', '/internal/bsearch', (req) => {
const findRequestConfig = getFindRequestConfig(searchStrategyName, factoryQueryType);
const requestIndex = findIndex(findRequestConfig, req.body.batch);

if (requestIndex > -1) {
return req.reply((res) => {
const responseObjectsArray = res.body.split('\n').map((responseString) => {
try {
return JSON.parse(responseString);
} catch {
return responseString;
}
});
const responseIndex = findIndex({ id: requestIndex }, responseObjectsArray);

const stubbedResponseObjectsArray = [...responseObjectsArray];
stubbedResponseObjectsArray[responseIndex] = {
...stubbedResponseObjectsArray[responseIndex],
result: {
...stubbedResponseObjectsArray[responseIndex].result,
...stubObject,
},
};

const stubbedResponse = stubbedResponseObjectsArray
.map((object) => {
try {
return JSON.stringify(object);
} catch {
return object;
}
})
.join('\n');

res.send(stubbedResponse);
});
if (searchStrategyName === 'securitySolutionIndexFields') {
req.reply(stubObject.rawResponse);
} else if (factoryQueryType === 'overviewHost') {
req.reply(stubObject.overviewHost);
} else if (factoryQueryType === 'overviewNetwork') {
req.reply(stubObject.overviewNetwork);
}

req.reply();
});
}
Expand Down

0 comments on commit 71b4c38

Please sign in to comment.