summaryrefslogtreecommitdiff
path: root/spec/frontend/authentication/webauthn/error_spec.js
blob: 26f1ca5e27dd589232eb8492fb7700ed325ce520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import WebAuthnError from '~/authentication/webauthn/error';

describe('WebAuthnError', () => {
  it.each([
    [
      'NotSupportedError',
      'Your device is not compatible with GitLab. Please try another device',
      'authenticate',
    ],
    ['InvalidStateError', 'This device has not been registered with us.', 'authenticate'],
    ['InvalidStateError', 'This device has already been registered with us.', 'register'],
    ['UnknownError', 'There was a problem communicating with your device.', 'register'],
  ])('exception %s will have message %s, flow type: %s', (exception, expectedMessage, flowType) => {
    expect(new WebAuthnError(new DOMException('', exception), flowType).message()).toEqual(
      expectedMessage,
    );
  });

  describe('SecurityError', () => {
    const { location } = window;

    beforeEach(() => {
      delete window.location;
      window.location = {};
    });

    afterEach(() => {
      window.location = location;
    });

    it('returns a descriptive error if https is disabled', () => {
      window.location.protocol = 'http:';

      const expectedMessage =
        'WebAuthn only works with HTTPS-enabled websites. Contact your administrator for more details.';
      expect(
        new WebAuthnError(new DOMException('', 'SecurityError'), 'authenticate').message(),
      ).toEqual(expectedMessage);
    });

    it('returns a generic error if https is enabled', () => {
      window.location.protocol = 'https:';

      const expectedMessage = 'There was a problem communicating with your device.';
      expect(
        new WebAuthnError(new DOMException('', 'SecurityError'), 'authenticate').message(),
      ).toEqual(expectedMessage);
    });
  });
});