From 6fcc1fffbf23fd4483982eaee3bd91039a712c85 Mon Sep 17 00:00:00 2001 From: Jan-Gerke Salomon Date: Thu, 19 Nov 2020 12:27:28 +0100 Subject: [PATCH 1/5] refactor(registering commands): auto-register commands --- packages/cli/templates/support.js | 3 +++ packages/cypress-commands/src/commands/find.js | 2 ++ packages/cypress-commands/src/commands/get.js | 2 ++ packages/cypress-commands/src/commands/login.js | 13 +++++++++++-- .../src/commands/registerCommands.js | 13 ------------- .../src/commands/stubWithFixture.js | 2 ++ .../src/commands/visitWhenStubbed.js | 2 ++ packages/cypress-commands/src/constants.js | 1 + packages/cypress-commands/src/index.js | 16 ++++++++++++++-- 9 files changed, 37 insertions(+), 17 deletions(-) delete mode 100644 packages/cypress-commands/src/commands/registerCommands.js create mode 100644 packages/cypress-commands/src/constants.js diff --git a/packages/cli/templates/support.js b/packages/cli/templates/support.js index bf0e95f9..cf2d47cd 100644 --- a/packages/cli/templates/support.js +++ b/packages/cli/templates/support.js @@ -1,3 +1,6 @@ import '@dhis2/cypress-commands' +// You can use helpers of `@dhis2/cypress-commands` directly: +// import { enableAutoLogin } from '@dhis2/cypress-commands' +// enableAutoLogin() // Add additional support functions here diff --git a/packages/cypress-commands/src/commands/find.js b/packages/cypress-commands/src/commands/find.js index 8d17ba6e..673bde12 100644 --- a/packages/cypress-commands/src/commands/find.js +++ b/packages/cypress-commands/src/commands/find.js @@ -14,3 +14,5 @@ export const find = (originalFn, subject, selectors, options = {}) => { const selector = parseSelectorWithDataTest(selectors, prefix) return originalFn(subject, selector, restOptions) } + +Cypress.Commands.overwrite('find', find) diff --git a/packages/cypress-commands/src/commands/get.js b/packages/cypress-commands/src/commands/get.js index 43141dc7..e67885c1 100644 --- a/packages/cypress-commands/src/commands/get.js +++ b/packages/cypress-commands/src/commands/get.js @@ -14,3 +14,5 @@ export const get = (originalFn, selectors, options = {}) => { const selector = parseSelectorWithDataTest(selectors, prefix) return originalFn(selector, restOptions) } + +Cypress.Commands.overwrite('get', get) diff --git a/packages/cypress-commands/src/commands/login.js b/packages/cypress-commands/src/commands/login.js index 91850b29..3f7201d2 100644 --- a/packages/cypress-commands/src/commands/login.js +++ b/packages/cypress-commands/src/commands/login.js @@ -1,4 +1,4 @@ -const LOGIN_END_POINT = 'dhis-web-commons-security/login.action' +import { LOGIN_ENDPOINT } from '../constants' const loginBasicAuth = () => { const username = Cypress.env('dhis2_username') @@ -6,7 +6,7 @@ const loginBasicAuth = () => { const loginUrl = Cypress.env('dhis2_base_url') return cy.request({ - url: `${loginUrl}/${LOGIN_END_POINT}`, + url: `${loginUrl}/${LOGIN_ENDPOINT}`, method: 'POST', form: true, followRedirect: true, @@ -53,5 +53,14 @@ const loginDev = () => { * 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) diff --git a/packages/cypress-commands/src/commands/registerCommands.js b/packages/cypress-commands/src/commands/registerCommands.js deleted file mode 100644 index bf4f8b59..00000000 --- a/packages/cypress-commands/src/commands/registerCommands.js +++ /dev/null @@ -1,13 +0,0 @@ -import { find } from './find' -import { get } from './get' -import { login } from './login' -import { stubWithFixture } from './stubWithFixture' -import { visitWhenStubbed } from './visitWhenStubbed' - -export const registerCommands = () => { - Cypress.Commands.overwrite('find', find) - Cypress.Commands.overwrite('get', get) - Cypress.Commands.add('login', login) - Cypress.Commands.add('stubWithFixture', stubWithFixture) - Cypress.Commands.add('visitWhenStubbed', visitWhenStubbed) -} diff --git a/packages/cypress-commands/src/commands/stubWithFixture.js b/packages/cypress-commands/src/commands/stubWithFixture.js index a0cfbf2d..c541db93 100644 --- a/packages/cypress-commands/src/commands/stubWithFixture.js +++ b/packages/cypress-commands/src/commands/stubWithFixture.js @@ -12,3 +12,5 @@ export const stubWithFixture = ({ method = 'GET', url, fixture }) => { response: `fixture:${fixture}`, }) } + +Cypress.Commands.add('stubWithFixture', stubWithFixture) diff --git a/packages/cypress-commands/src/commands/visitWhenStubbed.js b/packages/cypress-commands/src/commands/visitWhenStubbed.js index 62213b91..a3147efa 100644 --- a/packages/cypress-commands/src/commands/visitWhenStubbed.js +++ b/packages/cypress-commands/src/commands/visitWhenStubbed.js @@ -12,3 +12,5 @@ export const visitWhenStubbed = (url, options = {}) => { }, }) } + +Cypress.Commands.add('visitWhenStubbed', visitWhenStubbed) diff --git a/packages/cypress-commands/src/constants.js b/packages/cypress-commands/src/constants.js new file mode 100644 index 00000000..ff821174 --- /dev/null +++ b/packages/cypress-commands/src/constants.js @@ -0,0 +1 @@ +export const LOGIN_ENDPOINT = 'dhis-web-commons-security/login.action' diff --git a/packages/cypress-commands/src/index.js b/packages/cypress-commands/src/index.js index bd26c9b2..d2b74b52 100644 --- a/packages/cypress-commands/src/index.js +++ b/packages/cypress-commands/src/index.js @@ -1,6 +1,18 @@ +// will automatically create the commands +import './commands/find' +import './commands/get' +import './commands/login' +import './commands/stubWithFixture' +import './commands/visitWhenStubbed' + // helpers export { dataTestNameToSelector } from './helper/dataTestNameToSelector' export { parseSelectorWithDataTest } from './helper/parseSelectorWithDataTest' -// commands -export { registerCommands } from './commands/registerCommands' + +// backward compatibility +export const registerCommands = () => { + cy.log('The usage of `registerCommands` has been depricated') + cy.log('This function does nothing anymore and can be removed') + cy.log('Commands are registered automatically') +} From 03d44db2cdcd6b7b476321c4133760e1bdebc641 Mon Sep 17 00:00:00 2001 From: Jan-Gerke Salomon Date: Thu, 19 Nov 2020 12:28:13 +0100 Subject: [PATCH 2/5] feat(auto login): add setup function for enabling autologin --- packages/cypress-commands/src/index.js | 2 + .../src/setups/enableAutoLogin.js | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 packages/cypress-commands/src/setups/enableAutoLogin.js diff --git a/packages/cypress-commands/src/index.js b/packages/cypress-commands/src/index.js index d2b74b52..b2615628 100644 --- a/packages/cypress-commands/src/index.js +++ b/packages/cypress-commands/src/index.js @@ -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 = () => { diff --git a/packages/cypress-commands/src/setups/enableAutoLogin.js b/packages/cypress-commands/src/setups/enableAutoLogin.js new file mode 100644 index 00000000..2c14e959 --- /dev/null +++ b/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) + }) +} From 555a13e4b548f6bbd74c41795cc8331f6acfa817 Mon Sep 17 00:00:00 2001 From: Jan-Gerke Salomon Date: Wed, 25 Nov 2020 12:31:33 +0100 Subject: [PATCH 3/5] refactor(login command): update login command and use it in setup --- .../cypress-commands/src/commands/login.js | 56 +++---------------- packages/cypress-commands/src/constants.js | 1 - .../src/setups/enableAutoLogin.js | 36 ++---------- 3 files changed, 14 insertions(+), 79 deletions(-) delete mode 100644 packages/cypress-commands/src/constants.js diff --git a/packages/cypress-commands/src/commands/login.js b/packages/cypress-commands/src/commands/login.js index 3f7201d2..0f17daef 100644 --- a/packages/cypress-commands/src/commands/login.js +++ b/packages/cypress-commands/src/commands/login.js @@ -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, @@ -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) diff --git a/packages/cypress-commands/src/constants.js b/packages/cypress-commands/src/constants.js deleted file mode 100644 index ff821174..00000000 --- a/packages/cypress-commands/src/constants.js +++ /dev/null @@ -1 +0,0 @@ -export const LOGIN_ENDPOINT = 'dhis-web-commons-security/login.action' diff --git a/packages/cypress-commands/src/setups/enableAutoLogin.js b/packages/cypress-commands/src/setups/enableAutoLogin.js index 2c14e959..f75e6b51 100644 --- a/packages/cypress-commands/src/setups/enableAutoLogin.js +++ b/packages/cypress-commands/src/setups/enableAutoLogin.js @@ -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) }) } From 32b0f963a474cc176b4c2cb7ac53fa42c1a9a242 Mon Sep 17 00:00:00 2001 From: Jan-Gerke Salomon Date: Wed, 25 Nov 2020 12:31:57 +0100 Subject: [PATCH 4/5] chore(commands index file): update deprication warning content --- packages/cypress-commands/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cypress-commands/src/index.js b/packages/cypress-commands/src/index.js index b2615628..ebd64a3c 100644 --- a/packages/cypress-commands/src/index.js +++ b/packages/cypress-commands/src/index.js @@ -14,7 +14,7 @@ export { enableAutoLogin } from './setups/enableAutoLogin' // backward compatibility export const registerCommands = () => { - cy.log('The usage of `registerCommands` has been depricated') - cy.log('This function does nothing anymore and can be removed') - cy.log('Commands are registered automatically') + cy.log('The usage of `registerCommands` has been deprecated') + cy.log('It is now a no-op') + cy.log('commands are registered automatically.') } From 0bbb8e94b4a52763aecd7f1e051b867664560fb5 Mon Sep 17 00:00:00 2001 From: Jan-Gerke Salomon Date: Wed, 25 Nov 2020 13:10:33 +0100 Subject: [PATCH 5/5] refactor(deprication warning): show in one line --- packages/cypress-commands/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cypress-commands/src/index.js b/packages/cypress-commands/src/index.js index ebd64a3c..4d33623e 100644 --- a/packages/cypress-commands/src/index.js +++ b/packages/cypress-commands/src/index.js @@ -14,7 +14,7 @@ export { enableAutoLogin } from './setups/enableAutoLogin' // backward compatibility export const registerCommands = () => { - cy.log('The usage of `registerCommands` has been deprecated') - cy.log('It is now a no-op') - cy.log('commands are registered automatically.') + cy.log( + 'The usage of `registerCommands` has been deprecated. It is now a no-op. Commands are registered automatically.' + ) }