accounts-frontend/tests-e2e/cypress/integration/profile/mfa.test.ts

179 lines
5.9 KiB
TypeScript
Raw Permalink Normal View History

import { UserResponse } from 'app/services/api/accounts';
2020-05-24 04:38:24 +05:30
import { openSectionByName, getSectionByName, confirmWithPassword } from './utils';
describe('Profile — mfa', () => {
2020-05-24 04:38:24 +05:30
it('should enable mfa', () => {
const totp = 'totp123';
cy.login({ accounts: ['default'] }).then(({ accounts: [account] }) => {
cy.server();
cy.route({
method: 'POST',
url: `/api/v1/accounts/${account.id}/two-factor-auth`,
response: { success: false, errors: { password: 'foo' } },
}).as('mfaSaved');
cy.visit('/');
getSectionByName('Twofactor auth').should('contain', 'Disabled');
openSectionByName('Twofactor auth');
cy.location('pathname').should('eq', '/profile/mfa');
assertOs('Google Play', 'android');
assertOs('App Store', 'ios');
assertOs('Windows Store', 'windows');
cy.contains('App has been installed').click();
cy.location('pathname').should('eq', '/profile/mfa/step2');
cy.findByTestId('secret').should('not.be.empty');
cy.contains('Ready').click();
cy.location('pathname').should('eq', '/profile/mfa/step3');
cy.get('[name=totp]').type(`${totp}{enter}`);
cy.wait('@mfaSaved')
.its('requestBody')
.should(
'eq',
new URLSearchParams({
totp,
password: '',
}).toString(),
);
cy.route({
method: 'POST',
url: `/api/v1/accounts/${account.id}/two-factor-auth`,
response: { success: true },
}).as('mfaSaved');
cy.route({
method: 'GET',
url: `/api/v1/accounts/${account.id}`,
response: {
id: 7,
uuid: '522e8c19-89d8-4a6d-a2ec-72ebb58c2dbe',
username: 'SleepWalker',
isOtpEnabled: true, // fake enabled mfa
registeredAt: 1475568334,
lang: 'en',
elyProfileLink: 'http://ely.by/u7',
email: 'danilenkos@auroraglobal.com',
isActive: true,
isDeleted: false,
2020-05-24 04:38:24 +05:30
passwordChangedAt: 1476075696,
hasMojangUsernameCollision: true,
shouldAcceptRules: false,
} as UserResponse,
2020-05-24 04:38:24 +05:30
});
confirmWithPassword(account.password);
cy.wait('@mfaSaved')
.its('requestBody')
.should(
'eq',
new URLSearchParams({
totp,
password: account.password,
}).toString(),
);
cy.location('pathname').should('eq', '/');
getSectionByName('Twofactor auth').should('contain', 'Enabled');
});
});
2020-05-24 04:38:24 +05:30
it('should disable mfa', () => {
const totp = 'totp123';
cy.login({ accounts: ['default'] }).then(({ accounts: [account] }) => {
cy.server();
cy.route({
method: 'GET',
url: `/api/v1/accounts/${account.id}`,
response: {
id: 7,
uuid: '522e8c19-89d8-4a6d-a2ec-72ebb58c2dbe',
username: 'SleepWalker',
isOtpEnabled: true, // fake enabled mfa
registeredAt: 1475568334,
lang: 'en',
elyProfileLink: 'http://ely.by/u7',
email: 'danilenkos@auroraglobal.com',
isActive: true,
isDeleted: false,
2020-05-24 04:38:24 +05:30
passwordChangedAt: 1476075696,
hasMojangUsernameCollision: true,
shouldAcceptRules: false,
} as UserResponse,
2020-05-24 04:38:24 +05:30
});
cy.route({
method: 'DELETE',
url: `/api/v1/accounts/${account.id}/two-factor-auth`,
response: { success: false, errors: { password: 'foo' } },
}).as('mfaSaved');
cy.visit('/');
getSectionByName('Twofactor auth').should('contain', 'Enabled');
openSectionByName('Twofactor auth');
cy.location('pathname').should('eq', '/profile/mfa');
cy.contains('Disable').click();
cy.get('[name=totp]').type(`${totp}{enter}`);
cy.wait('@mfaSaved')
.its('requestBody')
.should(
'eq',
new URLSearchParams({
totp,
password: '',
}).toString(),
);
cy.route({
method: 'DELETE',
url: `/api/v1/accounts/${account.id}/two-factor-auth`,
response: { success: true },
}).as('mfaSaved');
// unmock accounts route
cy.route({
method: 'GET',
url: `/api/v1/accounts/${account.id}`,
});
confirmWithPassword(account.password);
cy.wait('@mfaSaved')
.its('requestBody')
.should(
'eq',
new URLSearchParams({
totp,
password: account.password,
}).toString(),
);
cy.location('pathname').should('eq', '/');
getSectionByName('Twofactor auth').should('contain', 'Disabled');
});
});
});
function assertOs(name: string, os: string) {
2020-05-24 04:38:24 +05:30
cy.findAllByTestId('os-tile').contains(name).click();
2020-05-24 04:38:24 +05:30
cy.findByTestId('os-instruction').should('have.attr', 'data-os', os);
2020-05-24 04:38:24 +05:30
cy.findAllByTestId('os-tile').contains(name).click();
}