Skip to content

Commit

Permalink
Merge pull request #9491 from OEvgeny/fix/e2e-tests
Browse files Browse the repository at this point in the history
fix: ensure CSRF token is passed through cy.request
  • Loading branch information
tracyboehrer authored Jan 20, 2023
2 parents 05a4853 + fa2eba3 commit f2de658
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ context('Luis Deploy', () => {
response: 'fixture:luPublish/success',
});
cy.findByTestId('startBotButton').click();
cy.findByText(/^Starting bot../);
cy.findAllByText(/^Starting bot../);
cy.route('GET', '/api/publish/*/status/default', { endpointURL: 'anything', status: 200 });
cy.route('PUT', '/api/projects/*/files/appsettings.json', { status: 200 });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

declare namespace Cypress {
interface Chainable {
/**
* Get CSRF token
* @example cy.getCSRFToken().then(token => ...)
*/
getCSRFToken(): Chainable<string>;
/**
* Creates a bot based on empty bot.
* @example cy.createBot('EmptySample', ()=> {})
Expand Down
44 changes: 44 additions & 0 deletions Composer/packages/integration-tests/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,49 @@ import '@testing-library/cypress/add-commands';

let TemplateBotProjectId = '';

let csrfToken: string;

Cypress.Commands.add('getCSRFToken', () => {
if (csrfToken) {
return cy.wrap(csrfToken);
}
cy.visit('/');
return cy
.window()
.its('__csrf__')
.then((csrf) => {
csrfToken = csrf;
return csrf;
});
});

Cypress.Commands.overwrite('request', (originalFn, ...options) => {
return cy.getCSRFToken().then((csrf) => {
const headers = {
'X-CSRF-Token': csrf,
};

const optionsObject = options[0];

if (optionsObject === Object(optionsObject)) {
optionsObject.headers = {
...headers,
...optionsObject.headers,
};

return originalFn(optionsObject);
}

const [method, url, body] = options;
return originalFn({
method,
url,
body,
headers,
});
});
});

Cypress.Commands.add('createBot', (botName: string, callback?: (bot: any) => void) => {
const params = {
description: '',
Expand Down Expand Up @@ -60,6 +103,7 @@ Cypress.Commands.add('createTestBot', (botName: string, callback?: (bot: any) =>
storageId: 'default',
};

cy.wrap(TemplateBotProjectId).should('not.be.empty');
cy.request('post', `/api/projects/${TemplateBotProjectId}/project/saveAs`, params).then((res) => {
callback?.(res.body);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './commands';

before(() => {
cy.exec('yarn clean-all');
cy.getCSRFToken();
cy.createTemplateBot('EmptySample');
});

Expand Down

0 comments on commit f2de658

Please sign in to comment.