From ec1fa1cc95b266c5c47d746ae5eeb463f81bc127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Ko=C5=82odziejczyk?= Date: Sun, 29 Sep 2024 21:25:44 +0200 Subject: [PATCH] wip --- .../cypress/e2e/Direct-kibana-request.cy.ts | 1 - e2e-tests/cypress/support/commands.ts | 80 +++++++++++-------- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/e2e-tests/cypress/e2e/Direct-kibana-request.cy.ts b/e2e-tests/cypress/e2e/Direct-kibana-request.cy.ts index 5827b3c..c565dab 100644 --- a/e2e-tests/cypress/e2e/Direct-kibana-request.cy.ts +++ b/e2e-tests/cypress/e2e/Direct-kibana-request.cy.ts @@ -64,7 +64,6 @@ describe('Direct kibana request', () => { cy.log('Get imported saved objects for user1 infosec group'); kbnApiAdvancedClient.getSavedObjects(user1, "infosec_group") .then(result => { - debugger; const actual = result.saved_objects.some( saved_object => saved_object.id === 'my-pattern' || saved_object.id === 'my-dashboard' ); diff --git a/e2e-tests/cypress/support/commands.ts b/e2e-tests/cypress/support/commands.ts index 4875135..09cf652 100644 --- a/e2e-tests/cypress/support/commands.ts +++ b/e2e-tests/cypress/support/commands.ts @@ -109,43 +109,59 @@ Cypress.Commands.add( } ); -function call(method: string, url: string, credentials: string, payload?: Cypress.RequestBody, headers?: { [key: string]: string }) { - cy.request({ - method: method, - url: url, - headers: { - authorization: `Basic ${btoa(credentials)}`, - ...headers - }, - body: payload || null, - }).then((response) => { - console.log(`RR: ${method} ${url} ${credentials} ${JSON.stringify(headers)} = ${JSON.stringify(response)}`) - // expect(response.status).to.be.within(200, 299); +function call(method: string, url: string, credentials: string, payload?: Cypress.RequestBody, headers?: { [key: string]: string }): Cypress.Chainable { + return withIgnoredCookies(() => + cy.request({ + method: method, + url: url, + headers: { + authorization: `Basic ${btoa(credentials)}`, + ...headers + }, + body: payload || null, + }) + ).then((response) => { + expect(response.status).to.be.within(200, 299); return isJsonString(response.body) ? JSON.parse(response.body) : response.body; }) } -function uploadFile(url: string, credentials: string, fixtureFilename: string, headers?: { [key: string]: string }) { - cy.fixture(fixtureFilename, 'base64').then((fileContent) => { - const formData = new FormData(); - formData.append('file', Cypress.Blob.base64StringToBlob(fileContent, 'application/octet-stream'), fixtureFilename); - - const requestHeaders = { - authorization: `Basic ${btoa(credentials)}`, - ...(headers || {}) - }; +function uploadFile(url: string, credentials: string, fixtureFilename: string, headers?: { [key: string]: string }): Cypress.Chainable { + return withIgnoredCookies(() => { + return cy.fixture(fixtureFilename, 'base64').then((fileContent) => { + const formData = new FormData(); + formData.append('file', Cypress.Blob.base64StringToBlob(fileContent, 'application/octet-stream'), fixtureFilename); + + const requestHeaders = { + authorization: `Basic ${btoa(credentials)}`, + ...(headers || {}) + }; + + cy.request({ + method: "POST", + url: url, + headers: requestHeaders, + body: formData, + }).then((response) => { + expect(response.status).to.be.within(200, 299); + return isJsonString(response.body) ? JSON.parse(response.body) : response.body; + }); + }); + }); +} - cy.request({ - method: "POST", - url: url, - headers: requestHeaders, - body: formData, - // You might want to comment this out unless you're sure it should be sent - // contentType: false, // This tells Cypress not to set the content-type, allowing FormData to set it - // failOnStatusCode: false // Uncomment if you want to ignore 4xx/5xx responses temporarily - }).then((response) => { - expect(response.status).to.be.within(200, 299); - return isJsonString(response.body) ? JSON.parse(response.body) : response.body; +// it's a workaround for this: https://github.com/cypress-io/cypress/issues/8909 +function withIgnoredCookies(callback: () => Cypress.Chainable>): Cypress.Chainable> { + return cy.getCookies().then((cookies) => { + return cy.clearAllCookies().then(() => { + return callback().then((result) => { + const setCookiePromises = cookies.map(({ name, value, ...rest }) => { + return cy.setCookie(name, value, rest); + }); + return Cypress.Promise.all(setCookiePromises).then(() => { + return result; + }); + }) }); }); }