From 5a5d41749ecb827215f9fecb77e5faafa03be724 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Sun, 29 Dec 2019 14:27:44 +0200 Subject: [PATCH] Cover change password with e2e tests --- tests-e2e/cypress.json | 1 + .../profile/change-password.test.ts | 49 +++++++++++++++++++ .../profile/change-username.test.ts | 26 ++++------ .../cypress/integration/profile/utils.ts | 18 +++++++ tests-e2e/cypress/plugins/index.js | 4 ++ 5 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 tests-e2e/cypress/integration/profile/change-password.test.ts create mode 100644 tests-e2e/cypress/integration/profile/utils.ts diff --git a/tests-e2e/cypress.json b/tests-e2e/cypress.json index 266ae66..1dba5eb 100644 --- a/tests-e2e/cypress.json +++ b/tests-e2e/cypress.json @@ -1,4 +1,5 @@ { "baseUrl": "http://localhost:8080", + "testFiles": ["**/*.test.ts"], "chromeWebSecurity": false } diff --git a/tests-e2e/cypress/integration/profile/change-password.test.ts b/tests-e2e/cypress/integration/profile/change-password.test.ts new file mode 100644 index 0000000..53305f1 --- /dev/null +++ b/tests-e2e/cypress/integration/profile/change-password.test.ts @@ -0,0 +1,49 @@ +import { openSectionByName, confirmWithPassword } from './utils'; + +describe('Change password', () => { + it('should change password', () => { + cy.login({ accounts: ['default'] }).then(({ accounts: [account] }) => { + cy.server(); + cy.route({ + method: 'POST', + url: `/api/v1/accounts/${account.id}/password`, + }).as('password'); + cy.visit('/'); + + openSectionByName('Password'); + + cy.get('[name=newPassword]').type(account.password); + cy.get('[name=newRePassword]').type(account.password); + cy.get('[name=logoutAll]').should('be.checked'); + cy.get('[type=submit]').click(); + + cy.wait('@password') + .its('requestBody') + .should( + 'eq', + new URLSearchParams({ + password: '', + newPassword: account.password, + newRePassword: account.password, + logoutAll: '1', + }).toString(), + ); + + confirmWithPassword(account.password); + + cy.wait('@password') + .its('requestBody') + .should( + 'eq', + new URLSearchParams({ + password: account.password, + newPassword: account.password, + newRePassword: account.password, + logoutAll: '1', + }).toString(), + ); + + cy.location('pathname').should('eq', '/'); + }); + }); +}); diff --git a/tests-e2e/cypress/integration/profile/change-username.test.ts b/tests-e2e/cypress/integration/profile/change-username.test.ts index 08d05b4..913728a 100644 --- a/tests-e2e/cypress/integration/profile/change-username.test.ts +++ b/tests-e2e/cypress/integration/profile/change-username.test.ts @@ -1,3 +1,5 @@ +import { openSectionByName, confirmWithPassword } from './utils'; + describe('Change username', () => { it('should change username', () => { cy.server(); @@ -29,16 +31,18 @@ describe('Change username', () => { cy.visit('/'); - cy.getByTestId('profile-item') - .contains('Nickname') - .closest('[data-testid="profile-item"]') - .getByTestId('profile-action') - .click(); + openSectionByName('Nickname'); cy.location('pathname').should('eq', '/profile/change-username'); cy.get('[name=username]').type(`{selectall}${account.username}{enter}`); + // unmock accounts route + cy.route({ + method: 'GET', + url: `/api/v1/accounts/${account.id}`, + }); + cy.wait('@username') .its('requestBody') .should( @@ -48,18 +52,8 @@ describe('Change username', () => { password: '', }).toString(), ); - cy.getByTestId('password-request-form').should('be.visible'); - // unmock accounts route - cy.route({ - method: 'GET', - url: `/api/v1/accounts/${account.id}`, - }); - - cy.get('[name=password]').type(account.password); - cy.getByTestId('password-request-form') - .find('[type=submit]') - .click(); + confirmWithPassword(account.password); cy.wait('@username') .its('requestBody') diff --git a/tests-e2e/cypress/integration/profile/utils.ts b/tests-e2e/cypress/integration/profile/utils.ts new file mode 100644 index 0000000..129f546 --- /dev/null +++ b/tests-e2e/cypress/integration/profile/utils.ts @@ -0,0 +1,18 @@ +export function getSectionByName(name: string) { + return cy.getByTestId('profile-item').contains(name); +} + +export function openSectionByName(name: string) { + return getSectionByName(name) + .closest('[data-testid="profile-item"]') + .getByTestId('profile-action') + .click(); +} + +export function confirmWithPassword(password: string) { + cy.getByTestId('password-request-form').should('be.visible'); + cy.get('[name=password]').type(password); + cy.getByTestId('password-request-form') + .find('[type=submit]') + .click(); +} diff --git a/tests-e2e/cypress/plugins/index.js b/tests-e2e/cypress/plugins/index.js index 3aa618b..23aee49 100644 --- a/tests-e2e/cypress/plugins/index.js +++ b/tests-e2e/cypress/plugins/index.js @@ -19,6 +19,9 @@ // }; const wp = require('@cypress/webpack-preprocessor'); +// for some reason loader can not locate babel.config. So we +const config = require('../../../babel.config'); + module.exports = on => { const options = { webpackOptions: { @@ -38,6 +41,7 @@ module.exports = on => { options: { envName: 'webpack', cacheDirectory: true, + ...config, }, }, ],