From 51a85fb20b3c80faa687d9f2759d6eab70fa68ce Mon Sep 17 00:00:00 2001 From: Nancy Butler <42977925+mantis-toboggan-md@users.noreply.github.com> Date: Thu, 7 Oct 2021 08:34:42 -0700 Subject: [PATCH] fix login tests w/ sessions --- cypress/integration/login.spec.ts | 40 ++++++++++++++++--------------- cypress/support/commands.ts | 12 +++++++--- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/cypress/integration/login.spec.ts b/cypress/integration/login.spec.ts index be6755f66d..ed2109d88a 100644 --- a/cypress/integration/login.spec.ts +++ b/cypress/integration/login.spec.ts @@ -1,24 +1,26 @@ -// describe('Local authentication', () => { -// it('Log in with valid creds', () => { -// cy.visit('/auth/login'); -// cy.intercept('POST', '/v3-public/localProviders/local*').as('loginReq'); +describe('Local authentication', () => { + it('Log in with valid creds', () => { + cy.visit('/auth/login'); + cy.intercept('POST', '/v3-public/localProviders/local*').as('loginReq'); -// cy.login(Cypress.env('username'), Cypress.env('password')); + cy.login(Cypress.env('username'), Cypress.env('password'), false); -// cy.wait('@loginReq').then((login) => { -// expect(login.response?.statusCode).to.equal(200); -// cy.url().should('not.equal', `${ Cypress.config().baseUrl }/auth/login`); -// }); -// }); + cy.wait('@loginReq').then((login) => { + expect(login.response?.statusCode).to.equal(200); + cy.url().should('not.equal', `${ Cypress.config().baseUrl }/auth/login`); + }); + }); -// it('Cannot login with invalid creds', () => { -// cy.intercept('POST', '/v3-public/localProviders/local*').as('loginReq'); + it('Cannot login with invalid creds', () => { + cy.visit('/auth/login'); -// cy.login(Cypress.env('username'), `${ Cypress.env('password') }abc`); + cy.intercept('POST', '/v3-public/localProviders/local*').as('loginReq'); -// cy.wait('@loginReq').then((login) => { -// expect(login.response?.statusCode).to.not.equal(200); -// cy.url().should('equal', `${ Cypress.config().baseUrl }/auth/login`); -// }); -// }); -// }); + cy.login(Cypress.env('username'), `${ Cypress.env('password') }abc`, false); + + cy.wait('@loginReq').then((login) => { + expect(login.response?.statusCode).to.not.equal(200); + cy.url().should('equal', `${ Cypress.config().baseUrl }/auth/login`); + }); + }); +}); diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index e7f303fbfb..c0a52a4a1c 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -1,5 +1,5 @@ -Cypress.Commands.add('login', (username = Cypress.env('username'), password = Cypress.env('password')) => { - cy.session([username, password], () => { +Cypress.Commands.add('login', (username = Cypress.env('username'), password = Cypress.env('password'), cacheSession = true) => { + const login = () => { cy.intercept('POST', '/v3-public/localProviders/local*').as('loginReq'); cy.visit('/auth/login'); @@ -13,7 +13,13 @@ Cypress.Commands.add('login', (username = Cypress.env('username'), password = Cy cy.get('button').click(); cy.wait('@loginReq'); - }); + }; + + if (cacheSession) { + cy.session([username, password], login); + } else { + login(); + } }); Cypress.Commands.add('byLabel', (label) => {