Skip to content

Commit

Permalink
refactor(login command): update login command and use it in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Nov 25, 2020
1 parent ac373b0 commit aed4b3a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 79 deletions.
56 changes: 8 additions & 48 deletions packages/cypress-commands/src/commands/login.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { LOGIN_ENDPOINT } from '../constants'
export const LOGIN_ENDPOINT = 'dhis-web-commons-security/login.action'

const loginBasicAuth = () => {
/**
* This is done through cy.request(...)
* because Cypress doesn't allow multiple domains per test:
* https://docs.cypress.io/guides/guides/web-security.html#One-Superdomain-per-Test
*/
export const login = () => {
const username = Cypress.env('dhis2_username')
const password = Cypress.env('dhis2_password')
const loginUrl = Cypress.env('dhis2_base_url')

return cy.request({
cy.request({
url: `${loginUrl}/${LOGIN_ENDPOINT}`,
method: 'POST',
form: true,
Expand All @@ -18,49 +23,4 @@ const loginBasicAuth = () => {
})
}

const loginDev = () => {
cy.visit('/')
cy.get('body')
.then($body => {
if (!$body.find('input#server').length) return cy.wrap(false)

const loginUrl = Cypress.env('dhis2_base_url')
const username = Cypress.env('dhis2_username')
const password = Cypress.env('dhis2_password')

cy.get('#server').type(loginUrl)
cy.get('#j_username').type(username)
cy.get('#j_password').type(password)
cy.get('{loginsubmit}', { prefix: 'dhis2-adapter' }).click()

return cy.wrap(true)
})
.then(found => {
if (found) return cy.wrap(true)
loginBasicAuth()
.its('body')
.should('not.include', 'class="loginPage"')
return cy.wrap(true)
})
.then(() => {
return cy
})
}

/**
* This is done through cy.request(...)
* because Cypress doesn't allow multiple domains per test:
* https://docs.cypress.io/guides/guides/web-security.html#One-Superdomain-per-Test
*/
export const login = () => {
;[
'DEPRICATION WARNING:',
'The `login` function will is an older version.',
'It is left in place to prevent breakage.',
'Please use either the `loginThroughForm` command or `autoLogin` setup.',
].forEach(message => cy.log(message))

return loginDev()
}

Cypress.Commands.add('login', login)
1 change: 0 additions & 1 deletion packages/cypress-commands/src/constants.js

This file was deleted.

36 changes: 6 additions & 30 deletions packages/cypress-commands/src/setups/enableAutoLogin.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
/* 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': '',
},
})
Cypress.Cookies.defaults({ whitelist: 'JSESSIONID' })
cy.login()
})

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
// 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
const loginUrl = Cypress.env('dhis2_base_url')
localStorage.setItem('DHIS2_BASE_URL', loginUrl)
})
}

0 comments on commit aed4b3a

Please sign in to comment.