Skip to content

Commit

Permalink
feat: add e2e tests for react native 2fa
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent df4846d commit a3ac253
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
103 changes: 103 additions & 0 deletions test/e2e/cypress/integration/profiles/mobile/mfa/backup.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { gen, MOBILE_URL, website } from '../../../../helpers'

context('Mobile Profile', () => {
describe('TOTP 2FA Flow', () => {
before(() => {
cy.useConfigProfile('mobile')
})

describe('password', () => {
const email = gen.email()
const password = gen.password()

before(() => {
cy.registerApi({
email,
password,
fields: { 'traits.website': website }
})
})

beforeEach(() => {
cy.loginMobile({ email, password })
cy.visit(MOBILE_URL + '/Settings')
})

it('should be able to lifecycle through lookup_secret flows', () => {
cy.get('*[data-testid="field/lookup_secret_codes"]').should('not.exist')
cy.get('*[data-testid="field/lookup_secret_confirm/true"]').should(
'not.exist'
)
cy.get('*[data-testid="field/lookup_secret_reveal/true"]').should(
'not.exist'
)
cy.get('*[data-testid="field/lookup_secret_regenerate/true"]').click()
cy.get('*[data-testid="field/lookup_secret_reveal/true"]').should(
'not.exist'
)
cy.get('*[data-testid="field/lookup_secret_codes"]').should('exist')
let codes
cy.get('*[data-testid="field/lookup_secret_codes/text"]').then(($e) => {
codes = $e.text().trim().split(', ')
})
cy.get('*[data-testid="field/lookup_secret_confirm/true"]').click()
cy.get('*[data-testid="form-messages"]').should(
'contain.text',
'Your changes have been saved!'
)

cy.get('*[data-testid="field/lookup_secret_confirm/true"]').should(
'not.exist'
)
cy.get('*[data-testid="field/lookup_secret_regenerate/true"]').should(
'not.exist'
)
cy.get('*[data-testid="field/lookup_secret_codes/true"]').should(
'not.exist'
)

cy.get('*[data-testid="field/lookup_secret_reveal/true"]').click()
cy.get('*[data-testid="field/lookup_secret_regenerate/true"]').should(
'exist'
)
cy.get('*[data-testid="field/lookup_secret_codes/text"]').then(($e) => {
const actualCodes = $e.text().trim().split(', ')
expect(actualCodes.join(', ')).to.eq(codes.join(', '))
})

let newCodes
cy.get('*[data-testid="field/lookup_secret_regenerate/true"]').click()
cy.get('*[data-testid="field/lookup_secret_codes/text"]').then(($e) => {
newCodes = $e.text().trim().split(', ')
})
cy.get('*[data-testid="field/lookup_secret_confirm/true"]').click()
cy.get('*[data-testid="field/lookup_secret_reveal/true"]').click()
cy.get('*[data-testid="field/lookup_secret_codes/text"]').then(($e) => {
const actualCodes = $e.text().trim().split(', ')
expect(actualCodes.join(', ')).to.eq(newCodes.join(', '))
})

cy.visit(MOBILE_URL + '/Login?aal=aal2')

// First use a wrong code
cy.get('*[data-testid="field/lookup_secret"]').then(($e) => {
cy.wrap($e).type(codes[0])
})
cy.get('*[data-testid="field/method/lookup_secret"]').click()
cy.get('*[data-testid="form-messages"]').should(
'contain.text',
'The backup recovery code is not valid.'
)
cy.get('*[data-testid="field/lookup_secret"]').then(($e) => {
cy.wrap($e).type(newCodes[0])
})
cy.get('*[data-testid="field/method/lookup_secret"]').click()
cy.get('[data-testid="session-content"]').should('contain', 'aal2')
cy.get('[data-testid="session-content"]').should(
'contain',
'lookup_secret'
)
})
})
})
})
74 changes: 74 additions & 0 deletions test/e2e/cypress/integration/profiles/mobile/mfa/mix.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { APP_URL, gen, MOBILE_URL, website } from '../../../../helpers'
import { authenticator } from 'otplib'

context('Mobile Profile', () => {
describe('TOTP 2FA Flow', () => {
before(() => {
cy.useConfigProfile('mobile')
})

describe('password', () => {
const email = gen.email()
const password = gen.password()

before(() => {
cy.registerApi({
email,
password,
fields: { 'traits.website': website }
})
})

beforeEach(() => {
cy.loginMobile({ email, password })
cy.visit(MOBILE_URL + '/Settings')
})

it('should be able to use both TOTP and lookup', () => {
// set up totp
let totpSecret
cy.get('*[data-testid="field/totp_secret_key/text"]').then(($e) => {
totpSecret = $e.text().trim()
})
cy.get('*[data-testid="field/totp_code"]').then(($e) => {
cy.wrap($e).type(authenticator.generate(totpSecret))
})
cy.get('*[data-testid="field/method/totp"]').click()

// Set up backup code
cy.get('*[data-testid="field/lookup_secret_regenerate/true"]').click()
let recoveryCodes
cy.get('*[data-testid="field/lookup_secret_codes/text"]').then(($e) => {
recoveryCodes = $e.text().trim().split(', ')
})
cy.get('*[data-testid="field/lookup_secret_confirm/true"]').click()

// Lets sign in with TOTP
cy.visit(MOBILE_URL + '/Login?aal=aal2')
cy.get('*[data-testid="field/totp_code"]').then(($e) => {
cy.wrap($e).type(authenticator.generate(totpSecret))
})
cy.get('*[data-testid="field/method/totp"]').click()

// We have AAL now
cy.get('[data-testid="session-content"]').should('contain', 'aal2')
cy.get('[data-testid="session-content"]').should('contain', 'totp')

// Lets sign in with lookup secret
cy.visit(MOBILE_URL + '/Login?aal=aal2&refresh=true')
cy.get('*[data-testid="field/lookup_secret"]').then(($e) => {
cy.wrap($e).type(recoveryCodes[0])
})
cy.get('*[data-testid="field/method/lookup_secret"]').click()

// We have AAL now
cy.get('[data-testid="session-content"]').should('contain', 'aal2')
cy.get('[data-testid="session-content"]').should('contain', 'totp')
cy.get('[data-testid="session-content"]').should(
'contain',
'lookup_secret'
)
})
})
})
})

0 comments on commit a3ac253

Please sign in to comment.