-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auto login): add setup function for enabling autologin
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* globals before */ | ||
import { LOGIN_ENDPOINT } from '../constants' | ||
|
||
export const enableAutoLogin = () => { | ||
// This will authenticate and set the session cookie | ||
const username = Cypress.env('dhis2_username') | ||
const password = Cypress.env('dhis2_password') | ||
const loginUrl = Cypress.env('dhis2_base_url') | ||
|
||
if (!loginUrl) { | ||
throw new Error( | ||
'No `dhis2_base_url` found. Please make sure to add it to `cypress.env.json`' | ||
) | ||
} | ||
|
||
before(() => { | ||
// Persist this across tests so we don't have to login before each test | ||
Cypress.Cookies.defaults({ | ||
whitelist: 'JSESSIONID', | ||
}) | ||
|
||
cy.request({ | ||
url: `${loginUrl}/${LOGIN_ENDPOINT}`, | ||
method: 'POST', | ||
form: true, | ||
followRedirect: true, | ||
body: { | ||
j_username: username, | ||
j_password: password, | ||
'2fa_code': '', | ||
}, | ||
}) | ||
}) | ||
|
||
beforeEach(() => { | ||
// This ensures the app platform knows which URL to use even if REACT_APP_DHIS2_BASE_URL is undefined | ||
// It also ensures that the value from the cypress env is used instead of REACT_APP_DHIS2_BASE_URL | ||
localStorage.setItem('DHIS2_BASE_URL', loginUrl) | ||
}) | ||
} |