Is it possible to write automation test for app with google authenticator

Viewed 2385

We are implementing mfa(multi factor auth) for our app and I'm trying to write automation test to test this. I don't see any direct way to get the google authenticator code for the user to login.

Has anyone tried to do this?

1 Answers

There is a package for that...
https://www.npmjs.com/package/totp-generator is a package that can help you with the google auth tokens. Keep in mind you want to request these right when you need them. Javascript executes asynchronously so you'll need to wrap it in a promise. Here is some example code that assumes a UI login followed by the token input.

describe('check the tokens', function() 
{
// First test 
it('cy.window() - get the global window object', () => { cy.viewport(500, 780) 
  cy.visit('https://site.domain',) 
  cy.get('input[name=email]').type('email@server.io') 
  cy.get('input[name=password]').focus().type('qwerty123') 
  cy.get('.Button').click()
  // Now lets wait on an object that appears on the page 
  // when ready to input the token
    cy.get(<someElement>).then(()=>{
        let token = getToken();
        console.log('first token: ' + token);
    })
})
//Second test
it('cy.window() - get the global window object', () => { cy.viewport(500, 780) 
  cy.visit('https://site.domain',) 
  cy.get('input[name=email]').type('email@server.io') 
  cy.get('input[name=password]').focus().type('qwerty123') 
  cy.get('.Button').click()
  // Now lets wait on an object that appears on the page 
  // when ready to input the token
    cy.get(<someOtherElement>).then(()=>{
        let token = getToken();
        console.log('second token: '+ token);
    });
})
})

function getToken () {
    const totp = require('totp-generator');
    const token = totp('2CQQGPPYFE7JPJAX');
    return token;
}

Related