From 506730e48e1a9af2ae04541616650e0e25a377d6 Mon Sep 17 00:00:00 2001 From: ismay Date: Mon, 14 Oct 2024 12:48:23 +0200 Subject: [PATCH] test: use local login helper --- cypress/support/e2e.ts | 47 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/cypress/support/e2e.ts b/cypress/support/e2e.ts index 4e84aa42..b4b47964 100644 --- a/cypress/support/e2e.ts +++ b/cypress/support/e2e.ts @@ -1,3 +1,46 @@ -import { enableAutoLogin } from '@dhis2/cypress-commands' +/* global Cypress */ -enableAutoLogin() +/** + * Custom login command, can be used to login or switch between sessions. + * Will cache and restore cookies, localStorage, and sessionStorage. See: + * https://docs.cypress.io/api/commands/session + */ +Cypress.Commands.add('login', (user) => { + cy.session( + user, + () => { + // Login via API + cy.request({ + url: `${user.server}/dhis-web-commons-security/login.action`, + method: 'POST', + form: true, + followRedirect: true, + body: { + j_username: user.name, + j_password: user.password, + '2fa_code': '', + }, + }) + + // Set base url for the app platform + window.localStorage.setItem('DHIS2_BASE_URL', user.server) + }, + { + validate: () => { + cy.request(`${user.server}/api/me`).then((response) => { + expect(response.status).to.eq(200) + expect(response.body.username).to.eq(user.name) + }) + }, + } + ) +}) + +// Log in before each test, if not already logged in +beforeEach(() => { + cy.login({ + name: Cypress.env('LOGIN_NAME'), + password: Cypress.env('LOGIN_PASSWORD'), + server: Cypress.env('LOGIN_SERVER'), + }) +})