summaryrefslogtreecommitdiff
path: root/spec/frontend/profile/add_ssh_key_validation_spec.js
blob: 730a94592a7678ee4a45a00288fe93f173cd54c3 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import AddSshKeyValidation from '~/profile/add_ssh_key_validation';

describe('AddSshKeyValidation', () => {
  describe('submit', () => {
    it('returns true if isValid is true', () => {
      const addSshKeyValidation = new AddSshKeyValidation([], {});
      jest.spyOn(addSshKeyValidation, 'isPublicKey').mockReturnValue(true);

      expect(addSshKeyValidation.submit()).toBe(true);
    });

    it('calls preventDefault and toggleWarning if isValid is false', () => {
      const addSshKeyValidation = new AddSshKeyValidation([], {});
      const event = {
        preventDefault: jest.fn(),
      };
      jest.spyOn(addSshKeyValidation, 'isPublicKey').mockReturnValue(false);
      jest.spyOn(addSshKeyValidation, 'toggleWarning').mockImplementation(() => {});

      addSshKeyValidation.submit(event);

      expect(event.preventDefault).toHaveBeenCalled();
      expect(addSshKeyValidation.toggleWarning).toHaveBeenCalledWith(true);
    });
  });

  describe('toggleWarning', () => {
    it('shows warningElement and hides originalSubmitElement if isVisible is true', () => {
      const warningElement = document.createElement('div');
      const originalSubmitElement = document.createElement('div');
      warningElement.classList.add('hide');

      const addSshKeyValidation = new AddSshKeyValidation(
        [],
        {},
        warningElement,
        originalSubmitElement,
      );
      addSshKeyValidation.toggleWarning(true);

      expect(warningElement.classList.contains('hide')).toBe(false);
      expect(originalSubmitElement.classList.contains('hide')).toBe(true);
    });

    it('hides warningElement and shows originalSubmitElement if isVisible is false', () => {
      const warningElement = document.createElement('div');
      const originalSubmitElement = document.createElement('div');
      originalSubmitElement.classList.add('hide');

      const addSshKeyValidation = new AddSshKeyValidation(
        [],
        {},
        warningElement,
        originalSubmitElement,
      );
      addSshKeyValidation.toggleWarning(false);

      expect(warningElement.classList.contains('hide')).toBe(true);
      expect(originalSubmitElement.classList.contains('hide')).toBe(false);
    });
  });

  describe('isPublicKey', () => {
    it('returns false if value begins with an algorithm name that is unsupported', () => {
      const addSshKeyValidation = new AddSshKeyValidation(['ssh-rsa', 'ssh-algorithm'], {});

      expect(addSshKeyValidation.isPublicKey('nope key')).toBe(false);
      expect(addSshKeyValidation.isPublicKey('ssh- key')).toBe(false);
      expect(addSshKeyValidation.isPublicKey('unsupported-ssh-rsa key')).toBe(false);
    });

    it('returns true if value begins with an algorithm name that is supported', () => {
      const addSshKeyValidation = new AddSshKeyValidation(['ssh-rsa', 'ssh-algorithm'], {});

      expect(addSshKeyValidation.isPublicKey('ssh-rsa key')).toBe(true);
      expect(addSshKeyValidation.isPublicKey('ssh-algorithm key')).toBe(true);
    });
  });
});