debian-mirror-gitlab/spec/frontend/authentication/u2f/authenticate_spec.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-06-23 00:09:42 +05:30
import U2FAuthenticate from '~/authentication/u2f/authenticate';
2017-09-10 17:25:29 +05:30
import 'vendor/u2f';
2018-03-17 18:26:18 +05:30
import MockU2FDevice from './mock_u2f_device';
2020-04-22 19:07:51 +05:30
describe('U2FAuthenticate', () => {
let u2fDevice;
let container;
let component;
2019-07-07 11:18:12 +05:30
preloadFixtures('u2f/authenticate.html');
2016-09-13 17:45:13 +05:30
2018-11-08 19:23:39 +05:30
beforeEach(() => {
2019-07-07 11:18:12 +05:30
loadFixtures('u2f/authenticate.html');
2020-04-22 19:07:51 +05:30
u2fDevice = new MockU2FDevice();
2020-06-23 00:09:42 +05:30
container = $('#js-authenticate-token-2fa');
2020-04-22 19:07:51 +05:30
component = new U2FAuthenticate(
container,
2020-06-23 00:09:42 +05:30
'#js-login-token-2fa-form',
2018-03-17 18:26:18 +05:30
{
sign_requests: [],
},
document.querySelector('#js-login-2fa-device'),
document.querySelector('.js-2fa-form'),
);
2018-11-08 19:23:39 +05:30
});
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
describe('with u2f unavailable', () => {
2020-04-22 19:07:51 +05:30
let oldu2f;
2018-11-08 19:23:39 +05:30
beforeEach(() => {
2020-04-22 19:07:51 +05:30
jest.spyOn(component, 'switchToFallbackUI').mockImplementation(() => {});
oldu2f = window.u2f;
2018-11-08 19:23:39 +05:30
window.u2f = null;
});
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
afterEach(() => {
2020-04-22 19:07:51 +05:30
window.u2f = oldu2f;
2018-11-08 19:23:39 +05:30
});
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
it('falls back to normal 2fa', (done) => {
2020-04-22 19:07:51 +05:30
component
2018-12-13 13:39:08 +05:30
.start()
.then(() => {
2020-04-22 19:07:51 +05:30
expect(component.switchToFallbackUI).toHaveBeenCalled();
2018-12-13 13:39:08 +05:30
done();
})
.catch(done.fail);
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});
2018-11-08 19:23:39 +05:30
describe('with u2f available', () => {
2021-03-08 18:12:59 +05:30
beforeEach((done) => {
2018-11-08 19:23:39 +05:30
// bypass automatic form submission within renderAuthenticated
2020-04-22 19:07:51 +05:30
jest.spyOn(component, 'renderAuthenticated').mockReturnValue(true);
u2fDevice = new MockU2FDevice();
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
component.start().then(done).catch(done.fail);
2016-09-13 17:45:13 +05:30
});
2018-11-08 19:23:39 +05:30
it('allows authenticating via a U2F device', () => {
2020-04-22 19:07:51 +05:30
const inProgressMessage = container.find('p');
2018-12-13 13:39:08 +05:30
2018-11-08 19:23:39 +05:30
expect(inProgressMessage.text()).toContain('Trying to communicate with your device');
2020-04-22 19:07:51 +05:30
u2fDevice.respondToAuthenticateRequest({
2018-03-17 18:26:18 +05:30
deviceData: 'this is data from the device',
2016-09-13 17:45:13 +05:30
});
2018-12-13 13:39:08 +05:30
2020-04-22 19:07:51 +05:30
expect(component.renderAuthenticated).toHaveBeenCalledWith(
2018-12-13 13:39:08 +05:30
'{"deviceData":"this is data from the device"}',
);
2016-09-13 17:45:13 +05:30
});
2018-11-08 19:23:39 +05:30
describe('errors', () => {
it('displays an error message', () => {
2020-11-24 15:15:51 +05:30
const setupButton = container.find('#js-login-2fa-device');
2018-11-08 19:23:39 +05:30
setupButton.trigger('click');
2020-04-22 19:07:51 +05:30
u2fDevice.respondToAuthenticateRequest({
2018-11-08 19:23:39 +05:30
errorCode: 'error!',
});
2020-04-22 19:07:51 +05:30
const errorMessage = container.find('p');
2018-12-13 13:39:08 +05:30
expect(errorMessage.text()).toContain('There was a problem communicating with your device');
2018-11-08 19:23:39 +05:30
});
2020-04-22 19:07:51 +05:30
it('allows retrying authentication after an error', () => {
2020-11-24 15:15:51 +05:30
let setupButton = container.find('#js-login-2fa-device');
2018-11-08 19:23:39 +05:30
setupButton.trigger('click');
2020-04-22 19:07:51 +05:30
u2fDevice.respondToAuthenticateRequest({
2018-11-08 19:23:39 +05:30
errorCode: 'error!',
});
2020-06-23 00:09:42 +05:30
const retryButton = container.find('#js-token-2fa-try-again');
2018-11-08 19:23:39 +05:30
retryButton.trigger('click');
2020-11-24 15:15:51 +05:30
setupButton = container.find('#js-login-2fa-device');
2018-11-08 19:23:39 +05:30
setupButton.trigger('click');
2020-04-22 19:07:51 +05:30
u2fDevice.respondToAuthenticateRequest({
2018-11-08 19:23:39 +05:30
deviceData: 'this is data from the device',
});
2018-12-13 13:39:08 +05:30
2020-04-22 19:07:51 +05:30
expect(component.renderAuthenticated).toHaveBeenCalledWith(
2018-12-13 13:39:08 +05:30
'{"deviceData":"this is data from the device"}',
);
2018-11-08 19:23:39 +05:30
});
});
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
});