【问题标题】:Is it possible to write automation test for app with google authenticator是否可以使用谷歌身份验证器为应用程序编写自动化测试
【发布时间】:2018-04-26 17:28:51
【问题描述】:

我们正在为我们的应用程序实施 mfa(多因素身份验证),我正在尝试编写自动化测试来测试这一点。 我没有看到任何直接的方法来获取用户登录的谷歌身份验证器代码。

有人试过吗?

【问题讨论】:

  • 我可能会这样做的方式是让 mfa 模块在构建中可配置,然后在测试期间将 google 模块替换为静态模块。
  • 假设您使用我相信 Google Authenticator 使用的常见 TOTP(基于时间的一次性密码)算法,并且您能够在测试中使用测试用户的密码,您可以要么generate the token by hand,要么使用像Speakeasy 这样的库来生成令牌。
  • 您是专门尝试测试 MFA,还是您不在乎,只是想测试应用程序(因此可以模拟身份验证本身)?
  • @BrandonAnzaldi Speakeasy 要求您扫描二维码。
  • @Shadow 使用 static 很好,但如果没有其他方法,这将是我的选择。

标签: javascript automation google-authentication multi-factor-authentication cypress


【解决方案1】:

有一个包...
https://www.npmjs.com/package/totp-generator 是一个包,可以帮助您使用 google 身份验证令牌。请记住,您想在需要时请求这些权利。 Javascript 是异步执行的,所以你需要将它包装在一个 Promise 中。下面是一些示例代码,假设用户界面登录后跟令牌输入。

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;
}

【讨论】:

    猜你喜欢
    • 2016-07-08
    • 2015-11-01
    • 2019-06-18
    • 2013-02-11
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多