From ea2cd5fe96e5e8c40b762a088d77cee85858e1f8 Mon Sep 17 00:00:00 2001 From: Karolina Rakoczy Date: Mon, 28 Nov 2022 12:36:53 +0100 Subject: [PATCH] clear session data when needed (#2711) --- .../e2e/checkout/purchaseWithProductTypes.js | 1 - cypress/support/api/requests/utils/index.js | 60 ++++++++----------- cypress/support/customCommands/user/index.js | 2 +- cypress/support/e2e.js | 1 + 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/cypress/e2e/checkout/purchaseWithProductTypes.js b/cypress/e2e/checkout/purchaseWithProductTypes.js index 52285e6f2..170f880ce 100644 --- a/cypress/e2e/checkout/purchaseWithProductTypes.js +++ b/cypress/e2e/checkout/purchaseWithProductTypes.js @@ -43,7 +43,6 @@ describe("As an unlogged customer I want to order physical and digital products" shippingMethod = resp.shippingMethod; digitalVariants = resp.digitalVariants; physicalVariants = resp.physicalVariants; - cy.clearSessionData(); }); }); diff --git a/cypress/support/api/requests/utils/index.js b/cypress/support/api/requests/utils/index.js index a4a69c9ed..5a011ac18 100644 --- a/cypress/support/api/requests/utils/index.js +++ b/cypress/support/api/requests/utils/index.js @@ -5,42 +5,32 @@ import { urlList } from "../../../../fixtures/urlList"; Cypress.Commands.add( "sendRequestWithQuery", (query, authorization = "auth", variables = "") => { - if (authorization.length !== 30) { - cy.request({ - body: { - variables, - query, - }, - headers: { - Authorization: `JWT ${window.sessionStorage.getItem(authorization)}`, - }, - method: "POST", - url: urlList.apiUri, - log: true, - }).then(response => { - const respInSting = JSON.stringify(response.body); - if (respInSting.includes(`"errors":[{`)) { - cy.log(query).log(JSON.stringify(response.body)); - } - }); + let headers; + + if (authorization && authorization.length !== 30) { + headers = { + Authorization: `JWT ${window.sessionStorage.getItem(authorization)}`, + }; + } else if (authorization) { + headers = { Authorization: `Bearer ${authorization}` }; } else { - cy.request({ - body: { - variables, - query, - }, - headers: { - Authorization: `Bearer ${authorization}`, - }, - method: "POST", - url: urlList.apiUri, - log: true, - }).then(response => { - const respInSting = JSON.stringify(response.body); - if (respInSting.includes(`"errors":[{`)) { - cy.log(query).log(JSON.stringify(response.body)); - } - }); + headers = {}; } + + cy.request({ + body: { + variables, + query, + }, + headers, + method: "POST", + url: urlList.apiUri, + log: true, + }).then(response => { + const respInSting = JSON.stringify(response.body); + if (respInSting.includes(`"errors":[{`)) { + cy.log(query).log(JSON.stringify(response.body)); + } + }); }, ); diff --git a/cypress/support/customCommands/user/index.js b/cypress/support/customCommands/user/index.js index e1a948c16..a90957938 100644 --- a/cypress/support/customCommands/user/index.js +++ b/cypress/support/customCommands/user/index.js @@ -35,7 +35,7 @@ Cypress.Commands.add( } } }`; - return cy.sendRequestWithQuery(mutation, authorization).then(resp => { + return cy.sendRequestWithQuery(mutation, null).then(resp => { window.localStorage.setItem( "_saleorCSRFToken", resp.body.data.tokenCreate.csrfToken, diff --git a/cypress/support/e2e.js b/cypress/support/e2e.js index 78fa5143b..e531ac845 100644 --- a/cypress/support/e2e.js +++ b/cypress/support/e2e.js @@ -27,6 +27,7 @@ cypressGrep(); Cypress.Commands.add("clearSessionData", () => { cy.clearCookies(); cy.clearLocalStorage(); + window.sessionStorage.clear(); }); Cypress.Commands.add("addAliasToGraphRequest", operationName => {