authenticate_spec.js 3.11 KB
Newer Older
1
import $ from 'jquery';
2
import U2FAuthenticate from '~/u2f/authenticate';
3
import 'vendor/u2f';
4 5
import MockU2FDevice from './mock_u2f_device';

Mike Greiling's avatar
Mike Greiling committed
6
describe('U2FAuthenticate', function() {
7
  preloadFixtures('u2f/authenticate.html.raw');
Fatih Acet's avatar
Fatih Acet committed
8

9
  beforeEach(() => {
10 11 12 13 14 15 16 17 18 19 20 21
    loadFixtures('u2f/authenticate.html.raw');
    this.u2fDevice = new MockU2FDevice();
    this.container = $('#js-authenticate-u2f');
    this.component = new U2FAuthenticate(
      this.container,
      '#js-login-u2f-form',
      {
        sign_requests: [],
      },
      document.querySelector('#js-login-2fa-device'),
      document.querySelector('.js-2fa-form'),
    );
22
  });
23

24 25 26 27 28 29
  describe('with u2f unavailable', () => {
    beforeEach(() => {
      spyOn(this.component, 'switchToFallbackUI');
      this.oldu2f = window.u2f;
      window.u2f = null;
    });
30

31 32 33
    afterEach(() => {
      window.u2f = this.oldu2f;
    });
34

Mike Greiling's avatar
Mike Greiling committed
35 36 37 38 39 40 41 42
    it('falls back to normal 2fa', done => {
      this.component
        .start()
        .then(() => {
          expect(this.component.switchToFallbackUI).toHaveBeenCalled();
          done();
        })
        .catch(done.fail);
Fatih Acet's avatar
Fatih Acet committed
43
    });
44 45
  });

46
  describe('with u2f available', () => {
Mike Greiling's avatar
Mike Greiling committed
47
    beforeEach(done => {
48 49 50 51
      // bypass automatic form submission within renderAuthenticated
      spyOn(this.component, 'renderAuthenticated').and.returnValue(true);
      this.u2fDevice = new MockU2FDevice();

Mike Greiling's avatar
Mike Greiling committed
52 53 54 55
      this.component
        .start()
        .then(done)
        .catch(done.fail);
Fatih Acet's avatar
Fatih Acet committed
56
    });
57 58 59

    it('allows authenticating via a U2F device', () => {
      const inProgressMessage = this.container.find('p');
60

61
      expect(inProgressMessage.text()).toContain('Trying to communicate with your device');
62 63
      this.u2fDevice.respondToAuthenticateRequest({
        deviceData: 'this is data from the device',
Fatih Acet's avatar
Fatih Acet committed
64
      });
65

Mike Greiling's avatar
Mike Greiling committed
66 67 68
      expect(this.component.renderAuthenticated).toHaveBeenCalledWith(
        '{"deviceData":"this is data from the device"}',
      );
Fatih Acet's avatar
Fatih Acet committed
69
    });
70 71 72 73 74 75 76 77 78

    describe('errors', () => {
      it('displays an error message', () => {
        const setupButton = this.container.find('#js-login-u2f-device');
        setupButton.trigger('click');
        this.u2fDevice.respondToAuthenticateRequest({
          errorCode: 'error!',
        });
        const errorMessage = this.container.find('p');
79 80

        expect(errorMessage.text()).toContain('There was a problem communicating with your device');
81 82 83 84 85 86 87 88 89 90 91 92 93 94
      });
      return it('allows retrying authentication after an error', () => {
        let setupButton = this.container.find('#js-login-u2f-device');
        setupButton.trigger('click');
        this.u2fDevice.respondToAuthenticateRequest({
          errorCode: 'error!',
        });
        const retryButton = this.container.find('#js-u2f-try-again');
        retryButton.trigger('click');
        setupButton = this.container.find('#js-login-u2f-device');
        setupButton.trigger('click');
        this.u2fDevice.respondToAuthenticateRequest({
          deviceData: 'this is data from the device',
        });
95

Mike Greiling's avatar
Mike Greiling committed
96 97 98
        expect(this.component.renderAuthenticated).toHaveBeenCalledWith(
          '{"deviceData":"this is data from the device"}',
        );
99 100
      });
    });
Fatih Acet's avatar
Fatih Acet committed
101
  });
102
});