Skip to content

Commit

Permalink
feat(auto login): add setup function for enabling autologin
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Nov 19, 2020
1 parent 71b5a35 commit bdc2757
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/cypress-commands/src/index.js
Expand Up @@ -9,6 +9,8 @@ import './commands/visitWhenStubbed'
export { dataTestNameToSelector } from './helper/dataTestNameToSelector'
export { parseSelectorWithDataTest } from './helper/parseSelectorWithDataTest'

// setup helpers
export { enableAutoLogin } from './setups/enableAutoLogin'

// backward compatibility
export const registerCommands = () => {
Expand Down
40 changes: 40 additions & 0 deletions packages/cypress-commands/src/setups/enableAutoLogin.js
@@ -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)
})
}

0 comments on commit bdc2757

Please sign in to comment.