|
| 1 | +import { TOTP } from 'totp-generator'; |
| 2 | + |
| 3 | +afterEach(() => cy.db_getUserId().then((uid) => cy.task('queryDB', `DELETE FROM #__user_mfa WHERE user_id = ${uid}`))); |
| 4 | + |
| 5 | +describe('Test in backend that the user', () => { |
| 6 | + it('can login with Multi-factor Authentication (email)', () => { |
| 7 | + cy.doAdministratorLogin(); |
| 8 | + cy.visit('/administrator/index.php?option=com_users&view=users'); |
| 9 | + cy.get('.header-profile:visible').click(); |
| 10 | + cy.get('.header-profile a.dropdown-item').contains('Edit Account').click(); |
| 11 | + cy.get('#myTab div[role="tablist"] button[aria-controls="multifactorauth"]').click(); |
| 12 | + cy.task('clearEmails'); |
| 13 | + cy.get('.com-users-methods-list-method-name-email a.com-users-methods-list-method-addnew').click(); |
| 14 | + cy.get('#com-users-method-edit-title').clear().type('Test Code'); |
| 15 | + cy.task('getMails').then((mails) => { |
| 16 | + cy.wrap(mails).should('have.lengthOf', 1); |
| 17 | + cy.wrap(mails[0].headers.subject).should('match', /code is -\d{6}-$/); |
| 18 | + cy.wrap(/code is -(\d{6})-$/.exec(mails[0].headers.subject)[1]).as('code') |
| 19 | + .then((code) => cy.wrap(mails[0].body).should('have.string', `Your authentication code is ${code}.`)); |
| 20 | + cy.wrap(mails[0].html).should('be.false'); |
| 21 | + }); |
| 22 | + cy.get('@code').then((code) => cy.get('#com-users-method-code').clear().type(code)); |
| 23 | + cy.get('#com-users-method-edit').submit(); |
| 24 | + cy.get('.com-users-methods-list-method-name-email .com-users-methods-list-method-record').contains('Test Code'); |
| 25 | + cy.clickToolbarButton('Cancel'); |
| 26 | + cy.doAdministratorLogout(); |
| 27 | + cy.get('#mod-login-username').type(Cypress.env('username')); |
| 28 | + cy.get('#mod-login-password').type(Cypress.env('password')); |
| 29 | + cy.get('#form-login').submit(); |
| 30 | + cy.get('#users-mfa-title').contains('Test Code'); |
| 31 | + cy.task('getMails').then((mails) => { |
| 32 | + cy.wrap(mails).should('have.lengthOf', 2); |
| 33 | + cy.wrap(mails[1].headers.subject).should('match', /code is -\d{6}-$/); |
| 34 | + cy.wrap(/code is -(\d{6})-$/.exec(mails[1].headers.subject)[1]).as('code'); |
| 35 | + }); |
| 36 | + cy.get('@code').then((code) => cy.get('#users-mfa-code').clear().type(code)); |
| 37 | + cy.get('#users-mfa-captive-form').submit(); |
| 38 | + cy.visit('/administrator/index.php?option=com_users&view=users'); |
| 39 | + cy.get('.header-profile:visible').click(); |
| 40 | + cy.get('.header-profile a.dropdown-item').contains('Edit Account').click(); |
| 41 | + cy.get('#myTab div[role="tablist"] button[aria-controls="multifactorauth"]').click(); |
| 42 | + cy.get('#com-users-methods-reset-message').contains('is enabled'); |
| 43 | + cy.get('.com-users-methods-list-method-name-email a.com-users-methods-list-method-record-delete').click(); |
| 44 | + cy.on('window:confirm', (text) => expect(text).to.contains('Are you sure you want to delete?')); |
| 45 | + cy.get('#com-users-methods-reset-message').contains('not enabled'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('can login with Multi-factor Authentication (totp)', () => { |
| 49 | + cy.doAdministratorLogin(); |
| 50 | + cy.visit('/administrator/index.php?option=com_users&view=users'); |
| 51 | + cy.get('.header-profile:visible').click(); |
| 52 | + cy.get('.header-profile a.dropdown-item').contains('Edit Account').click(); |
| 53 | + cy.get('#myTab div[role="tablist"] button[aria-controls="multifactorauth"]').click(); |
| 54 | + cy.get('.com-users-methods-list-method-name-totp a.com-users-methods-list-method-addnew').click(); |
| 55 | + cy.get('#com-users-method-edit-title').clear().type('Test Code'); |
| 56 | + cy.get('.com-users-method-edit-tabular-container table tr td') |
| 57 | + .contains('Enter this key') |
| 58 | + .next() |
| 59 | + .invoke('text') |
| 60 | + .then((key) => key.trim()) |
| 61 | + .as('secret'); |
| 62 | + cy.get('@secret').then((secret) => cy.get('#com-users-method-code').clear().type(TOTP.generate(secret).otp)); |
| 63 | + cy.get('#com-users-method-edit').submit(); |
| 64 | + cy.get('.com-users-methods-list-method-name-totp .com-users-methods-list-method-record').contains('Test Code'); |
| 65 | + cy.clickToolbarButton('Cancel'); |
| 66 | + cy.doAdministratorLogout(); |
| 67 | + cy.get('#mod-login-username').type(Cypress.env('username')); |
| 68 | + cy.get('#mod-login-password').type(Cypress.env('password')); |
| 69 | + cy.get('#form-login').submit(); |
| 70 | + cy.get('#users-mfa-title').contains('Verification code'); |
| 71 | + cy.get('@secret').then((secret) => cy.get('#users-mfa-code').clear().type(TOTP.generate(secret).otp)); |
| 72 | + cy.get('#users-mfa-captive-form').submit(); |
| 73 | + cy.visit('/administrator/index.php?option=com_users&view=users'); |
| 74 | + cy.get('.header-profile:visible').click(); |
| 75 | + cy.get('.header-profile a.dropdown-item').contains('Edit Account').click(); |
| 76 | + cy.get('#myTab div[role="tablist"] button[aria-controls="multifactorauth"]').click(); |
| 77 | + cy.get('#com-users-methods-reset-message').contains('is enabled'); |
| 78 | + cy.get('.com-users-methods-list-method-name-totp a.com-users-methods-list-method-record-delete').click(); |
| 79 | + cy.on('window:confirm', (text) => expect(text).to.contains('Are you sure you want to delete?')); |
| 80 | + cy.get('#com-users-methods-reset-message').contains('not enabled'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('can login with Multi-factor Authentication (backup codes)', () => { |
| 84 | + cy.doAdministratorLogin(); |
| 85 | + cy.visit('/administrator/index.php?option=com_users&view=users'); |
| 86 | + cy.get('.header-profile:visible').click(); |
| 87 | + cy.get('.header-profile a.dropdown-item').contains('Edit Account').click(); |
| 88 | + cy.get('#myTab div[role="tablist"] button[aria-controls="multifactorauth"]').click(); |
| 89 | + cy.get('.com-users-methods-list-method-name-totp a.com-users-methods-list-method-addnew').click(); |
| 90 | + cy.get('#com-users-method-edit-title').clear().type('Test Code'); |
| 91 | + cy.get('.com-users-method-edit-tabular-container table tr td') |
| 92 | + .contains('Enter this key') |
| 93 | + .next() |
| 94 | + .invoke('text') |
| 95 | + .then((key) => key.trim()) |
| 96 | + .as('secret'); |
| 97 | + cy.get('@secret').then((secret) => cy.get('#com-users-method-code').clear().type(TOTP.generate(secret).otp)); |
| 98 | + cy.get('#com-users-method-edit').submit(); |
| 99 | + cy.get('.com-users-methods-list-method-name-totp .com-users-methods-list-method-record').contains('Test Code'); |
| 100 | + cy.get('.com-users-methods-list-method-name-backupcodes .com-users-methods-list-method-record-info a') |
| 101 | + .should('have.text', 'Print these codes') |
| 102 | + .click(); |
| 103 | + cy.get('table > tbody > tr > td').first().invoke('text').then((code) => cy.wrap(/\d{8}/.exec(code)[0]).as('code')); |
| 104 | + cy.get('#toolbar-user-mfa-edit-cancel').contains('Back').click(); |
| 105 | + cy.clickToolbarButton('Cancel'); |
| 106 | + cy.doAdministratorLogout(); |
| 107 | + cy.get('#mod-login-username').type(Cypress.env('username')); |
| 108 | + cy.get('#mod-login-password').type(Cypress.env('password')); |
| 109 | + cy.get('#form-login').submit(); |
| 110 | + cy.get('#users-mfa-title').contains('Verification code'); |
| 111 | + cy.get('#toolbar-user-mfa-choose-another').click(); |
| 112 | + cy.get('a.com-users-method').contains('Backup Codes').click(); |
| 113 | + cy.get('#users-mfa-title').contains('Backup Codes'); |
| 114 | + cy.get('@code').then((code) => cy.get('#users-mfa-code').clear().type(code)); |
| 115 | + cy.get('#users-mfa-captive-form').submit(); |
| 116 | + cy.visit('/administrator/index.php?option=com_users&view=users'); |
| 117 | + cy.get('.header-profile:visible').click(); |
| 118 | + cy.get('.header-profile a.dropdown-item').contains('Edit Account').click(); |
| 119 | + cy.get('#myTab div[role="tablist"] button[aria-controls="multifactorauth"]').click(); |
| 120 | + cy.get('#com-users-methods-reset-message').contains('is enabled'); |
| 121 | + cy.get('.com-users-methods-list-method-name-totp a.com-users-methods-list-method-record-delete').click(); |
| 122 | + cy.on('window:confirm', (text) => expect(text).to.contains('Are you sure you want to delete?')); |
| 123 | + cy.get('#com-users-methods-reset-message').contains('not enabled'); |
| 124 | + }); |
| 125 | +}); |
0 commit comments