From f6f0aedc65a36e103d8aab25987c83dbe8ba6228 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Fri, 27 Dec 2019 21:01:59 +0200 Subject: [PATCH] Fix signin and add more signin e2e tests cases --- packages/app/pages/root/RootPage.tsx | 4 +- .../cypress/integration/auth/register.test.ts | 0 .../cypress/integration/auth/sign-in.test.ts | 124 ++++++++++++++++++ .../integration/invalid-refreshToken.test.ts | 15 ++- tests-e2e/cypress/integration/sign-in.test.ts | 46 ------- 5 files changed, 134 insertions(+), 55 deletions(-) create mode 100644 tests-e2e/cypress/integration/auth/register.test.ts create mode 100644 tests-e2e/cypress/integration/auth/sign-in.test.ts delete mode 100644 tests-e2e/cypress/integration/sign-in.test.ts diff --git a/packages/app/pages/root/RootPage.tsx b/packages/app/pages/root/RootPage.tsx index bd0d9b1..78405f9 100644 --- a/packages/app/pages/root/RootPage.tsx +++ b/packages/app/pages/root/RootPage.tsx @@ -69,7 +69,7 @@ class RootPage extends React.PureComponent<{ [styles.isPopupActive]: isPopupActive, })} > -
+
@@ -90,7 +90,7 @@ class RootPage extends React.PureComponent<{ {!user.isGuest && ( - + )} diff --git a/tests-e2e/cypress/integration/auth/register.test.ts b/tests-e2e/cypress/integration/auth/register.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/tests-e2e/cypress/integration/auth/sign-in.test.ts b/tests-e2e/cypress/integration/auth/sign-in.test.ts new file mode 100644 index 0000000..f93fa1e --- /dev/null +++ b/tests-e2e/cypress/integration/auth/sign-in.test.ts @@ -0,0 +1,124 @@ +import { account1 } from '../../fixtures/accounts.json'; + +it('should sign in', () => { + cy.visit('/'); + + cy.get('[name=login]').type(`${account1.login}{enter}`); + + cy.url().should('include', '/password'); + + cy.get('[name=password]').type(account1.password); + cy.get('[name=rememberMe]').should('be.checked'); + cy.get('[type=submit]').click(); + + cy.location('pathname').should('eq', '/'); + + cy.getByTestId('toolbar') + .contains(account1.username) + .should(() => { + const state = JSON.parse(localStorage.getItem('redux-storage') || ''); + expect(state.accounts.available).to.have.length(1); + + const [account] = state.accounts.available; + expect(account.username).to.be.equal(account1.username); + expect(account.id) + .to.be.a('number') + .and.to.be.gt(0); + expect(account.email) + .to.be.a('string') + .and.have.length.gt(0); + expect(account.token) + .to.be.a('string') + .and.have.length.gt(0); + expect(account.refreshToken) + .to.be.a('string') + .and.have.length.gt(0); + + expect(state.accounts.active).to.be.equal(account.id); + + const { user } = state; + expect(user.id).to.be.equal(account.id); + expect(user.username).to.be.equal(account.username); + expect(user.isGuest).to.be.false; + }); +}); + +it('should sign in without remember me', () => { + cy.visit('/'); + + cy.get('[name=login]').type(`${account1.login}{enter}`); + + cy.url().should('include', '/password'); + + cy.get('[name=password]').type(account1.password); + cy.get('[name=rememberMe]') + .parent() + .click(); + cy.get('[name=rememberMe]').should('not.be.checked'); + cy.get('[type=submit]').click(); + + cy.location('pathname').should('eq', '/'); + + cy.getByTestId('toolbar') + .contains(account1.username) + .should(() => { + const state = JSON.parse(localStorage.getItem('redux-storage') || ''); + expect(state.accounts.available).to.have.length(1); + + const [account] = state.accounts.available; + expect(account.username).to.be.equal(account1.username); + expect(account.id) + .to.be.a('number') + .and.to.be.gt(0); + expect(account.email) + .to.be.a('string') + .and.have.length.gt(0); + expect(account.token) + .to.be.a('string') + .and.have.length.gt(0); + expect(account.refreshToken).eql(null); + + expect(state.accounts.active).to.be.equal(account.id); + + const { user } = state; + expect(user.id).to.be.equal(account.id); + expect(user.username).to.be.equal(account.username); + expect(user.isGuest).to.be.false; + }); +}); + +it('should sign in with totp', () => { + cy.visit('/'); + + cy.get('[name=login]').type(`${account1.login}{enter}`); + + cy.url().should('include', '/password'); + + cy.server(); + cy.route({ + method: 'POST', + url: '/api/authentication/login', + response: { + success: false, + errors: { totp: 'error.totp_required' }, + }, + }); + + cy.get('[name=password]').type(account1.password); + cy.get('[type=submit]').click(); + + cy.url().should('include', '/mfa'); + + cy.route({ + method: 'POST', + url: '/api/authentication/login', + }).as('login'); + + cy.get('[name=totp]').type('123{enter}'); + + cy.wait('@login') + .its('requestBody') + .should('include', 'totp=123'); + + cy.location('pathname').should('eq', '/'); +}); diff --git a/tests-e2e/cypress/integration/invalid-refreshToken.test.ts b/tests-e2e/cypress/integration/invalid-refreshToken.test.ts index 53c34f5..95979df 100644 --- a/tests-e2e/cypress/integration/invalid-refreshToken.test.ts +++ b/tests-e2e/cypress/integration/invalid-refreshToken.test.ts @@ -55,7 +55,8 @@ describe("when user's token and refreshToken are invalid", () => { cy.url().should('include', '/password'); - cy.get('[data-e2e-toolbar] a') + cy.getByTestId('toolbar') + .get('a') .contains('Ely.by') .click(); @@ -96,7 +97,7 @@ describe("when user's token and refreshToken are invalid", () => { cy.url().should('include', '/login'); - cy.get('[data-e2e-toolbar]').contains('Join'); + cy.getByTestId('toolbar').contains('Join'); }); it('should allow logout', () => { @@ -107,10 +108,10 @@ describe("when user's token and refreshToken are invalid", () => { `/api/v1/accounts/${account2.id}`, ); - cy.get('[data-e2e-toolbar]') + cy.getByTestId('toolbar') .contains(account2.username) .click(); - cy.get('[data-e2e-toolbar]') + cy.getByTestId('toolbar') .contains('Log out') .click(); @@ -118,10 +119,10 @@ describe("when user's token and refreshToken are invalid", () => { 'be.calledWith', '/api/authentication/logout', ); - cy.get('[data-e2e-toolbar]') + cy.getByTestId('toolbar') .contains(account2.email) .should('not.exist'); - cy.get('[data-e2e-toolbar]') + cy.getByTestId('toolbar') .contains(account2.username) .should('not.exist'); }); @@ -167,7 +168,7 @@ describe("when user's token and refreshToken are invalid", () => { cy.url().should('include', '/login'); - cy.get('[data-e2e-toolbar]').contains('a', 'Join'); + cy.getByTestId('toolbar').contains('a', 'Join'); }); it('should ask for password if selected account with bad token', () => { diff --git a/tests-e2e/cypress/integration/sign-in.test.ts b/tests-e2e/cypress/integration/sign-in.test.ts deleted file mode 100644 index 99991d9..0000000 --- a/tests-e2e/cypress/integration/sign-in.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { account1 } from '../fixtures/accounts.json'; - -describe('sign in', () => { - it('should sign in', () => { - cy.visit('/'); - - cy.get('[name=login]').type(`${account1.login}{enter}`); - - cy.url().should('include', '/password'); - - cy.get('[name=password]').type(account1.password); - cy.get('[name=rememberMe]').should('be.checked'); - cy.get('[type=submit]').click(); - - cy.location('pathname', { timeout: 15000 }).should('eq', '/'); - - cy.get('[data-e2e-toolbar]') - .contains(account1.username) - .should(() => { - const state = JSON.parse(localStorage.getItem('redux-storage') || ''); - expect(state.accounts.available).to.have.length(1); - - const [account] = state.accounts.available; - expect(account.username).to.be.equal(account1.username); - expect(account.id) - .to.be.a('number') - .and.to.be.gt(0); - expect(account.email) - .to.be.a('string') - .and.have.length.gt(0); - expect(account.token) - .to.be.a('string') - .and.have.length.gt(0); - expect(account.refreshToken) - .to.be.a('string') - .and.have.length.gt(0); - - expect(state.accounts.active).to.be.equal(account.id); - - const { user } = state; - expect(user.id).to.be.equal(account.id); - expect(user.username).to.be.equal(account.username); - expect(user.isGuest).to.be.false; - }); - }); -});