From 9890898191147e76985de93bbb7fe90170c0b70a Mon Sep 17 00:00:00 2001 From: mahula Date: Thu, 15 Sep 2022 09:14:29 +0200 Subject: [PATCH] add custom command for reusable login via API --- e2e-tests/cypress/tests/cypress.config.ts | 5 +++- .../cypress/tests/cypress/support/e2e.ts | 26 +++++++++++++++++++ .../cypress/tests/cypress/support/index.ts | 11 ++++++++ e2e-tests/cypress/tests/tsconfig.json | 4 +-- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 e2e-tests/cypress/tests/cypress/support/e2e.ts create mode 100644 e2e-tests/cypress/tests/cypress/support/index.ts diff --git a/e2e-tests/cypress/tests/cypress.config.ts b/e2e-tests/cypress/tests/cypress.config.ts index 0f1d6aa01..e4ae6c34d 100644 --- a/e2e-tests/cypress/tests/cypress.config.ts +++ b/e2e-tests/cypress/tests/cypress.config.ts @@ -31,13 +31,16 @@ export default defineConfig({ excludeSpecPattern: "*.js", baseUrl: 'http://localhost:3000', chromeWebSecurity: false, - supportFile: false, + supportFile: 'cypress/support/index.ts', viewportHeight: 720, viewportWidth: 1280, retries: { runMode: 2, openMode: 0 }, + env: { + backendURL: 'http://localhost:4000', + }, setupNodeEvents } }) diff --git a/e2e-tests/cypress/tests/cypress/support/e2e.ts b/e2e-tests/cypress/tests/cypress/support/e2e.ts new file mode 100644 index 000000000..c34292165 --- /dev/null +++ b/e2e-tests/cypress/tests/cypress/support/e2e.ts @@ -0,0 +1,26 @@ +Cypress.Commands.add('login', (email, password) => { + Cypress.LocalStorage.clear + + cy.request({ + method: 'POST', + url: Cypress.env('backendURL'), + body: { + "operationName": null, + "variables": { + "email": email, + "password": password + }, + "query": "query ($email: String!, $password: String!, $publisherId: Int) {\n login(email: $email, password: $password, publisherId: $publisherId) {\n email\n firstName\n lastName\n language\n klickTipp {\n newsletterState\n __typename\n }\n hasElopage\n publisherId\n isAdmin\n creation\n __typename\n }\n}\n" + } + }) + .then((response) => { + let vuexToken = { + token : JSON.parse(JSON.stringify(response.headers))['token'], + // TODO: how to compute the token time from the token im response? + tokenTime : 1663224487 + }; + + cy.visit('/'); + window.localStorage.setItem('vuex', JSON.stringify(vuexToken)); + }) +}) \ No newline at end of file diff --git a/e2e-tests/cypress/tests/cypress/support/index.ts b/e2e-tests/cypress/tests/cypress/support/index.ts new file mode 100644 index 000000000..b5db7da24 --- /dev/null +++ b/e2e-tests/cypress/tests/cypress/support/index.ts @@ -0,0 +1,11 @@ +/// + +import './e2e'; + +declare global { + namespace Cypress { + interface Chainable { + login(email: string, password: string): Chainable + } + } +} \ No newline at end of file diff --git a/e2e-tests/cypress/tests/tsconfig.json b/e2e-tests/cypress/tests/tsconfig.json index e9739ec00..c031a126e 100644 --- a/e2e-tests/cypress/tests/tsconfig.json +++ b/e2e-tests/cypress/tests/tsconfig.json @@ -3,8 +3,8 @@ "target": "es2016", "lib": ["es6", "dom"], "baseUrl": "../node_modules", - "types": ["cypress", "node"], - "strict": true + "types": ["cypress", "node"], + "strict": true }, "include": ["**/*.ts"] }