Skip to content

Commit

Permalink
fix(auto login): use api to login (#394)
Browse files Browse the repository at this point in the history
Co-authored-by: Ismay <Ismay@users.noreply.github.com>
  • Loading branch information
Mohammer5 and Ismay authored May 1, 2023
1 parent 2ee2e75 commit 8b22e7b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions packages/cypress-commands/src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import './all.js'
import './fillInLoginForm.js'
import './findWithDataTest.js'
import './getWithDataTest.js'
import './loginByApi.js'
import './validateUserIsLoggedIn.js'
17 changes: 17 additions & 0 deletions packages/cypress-commands/src/commands/loginByApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Cypress.Commands.add('loginByApi', ({ username, password, baseUrl }) => {
// Login via API
cy.request({
url: `${baseUrl}/dhis-web-commons-security/login.action`,
method: 'POST',
form: true,
followRedirect: true,
body: {
j_username: username,
j_password: password,
'2fa_code': '',
},
})

// Set base url for the app platform
window.localStorage.setItem('DHIS2_BASE_URL', baseUrl)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Cypress.Commands.add('validateUserIsLoggedIn', ({ baseUrl, username }) => {
cy.request(`${baseUrl}/api/me`).then((response) => {
expect(response.status).to.eq(200)
expect(response.body.username).to.eq(username)
})
})
4 changes: 2 additions & 2 deletions packages/cypress-commands/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// will automatically create the commands
import './commands/index.js'
import './commands/index.js' // will automatically register the commands

export * from './helper/index.js'
export * from './setups/index.js'
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import { isStubMode } from '../enableNetworkShim/index.js'

export const enableAutoLogin = (
{ username: _username, password: _password, baseUrl: _baseUrl } = {},
{ timeout } = {}
) => {
export const enableAutoLogin = ({
username: _username,
password: _password,
baseUrl: _baseUrl,
} = {}) => {
if (isStubMode()) {
return
}

const name = _username || Cypress.env('dhis2Username')
const username = _username || Cypress.env('dhis2Username')
const password = _password || Cypress.env('dhis2Password')
const server = _baseUrl || Cypress.env('dhis2BaseUrl')
const baseUrl = _baseUrl || Cypress.env('dhis2BaseUrl')

const createSession = () =>
cy.session(
'user',
() => {
cy.visit('/')
cy.fillInLoginForm({ name, password, server })
cy.get('#dhis2-app-root > *', { timeout }).should('exist')
// Not using the login form to log in as that's the
// recommendation by cypress:
// * https://docs.cypress.io/guides/end-to-end-testing/testing-your-app#Fully-test-the-login-flow----but-only-once
// * https://docs.cypress.io/api/commands/session#Multiple-login-commands
cy.loginByApi({ username, password, baseUrl })
},
{
cacheAcrossSpecs: true,
validate: () => {
cy.visit('/')
cy.get('h1:contains("Please sign in")', { timeout }).should(
'not.exist'
)
cy.validateUserIsLoggedIn({ baseUrl, username })
},
}
)
Expand Down

0 comments on commit 8b22e7b

Please sign in to comment.