summaryrefslogtreecommitdiff
path: root/spec/frontend/profile/add_ssh_key_validation_spec.js
blob: 1fec864599c53424949f79e0bb94111c2f924e13 (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
import AddSshKeyValidation from '../../../app/assets/javascripts/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()).toBeTruthy();
    });

    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')).toBeFalsy();
      expect(originalSubmitElement.classList.contains('hide')).toBeTruthy();
    });

    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')).toBeTruthy();
      expect(originalSubmitElement.classList.contains('hide')).toBeFalsy();
    });
  });

  describe('isPublicKey', () => {
    it('returns false if probably invalid public ssh key', () => {
      expect(AddSshKeyValidation.isPublicKey('nope')).toBeFalsy();
    });

    it('returns true if probably valid public ssh key', () => {
      expect(AddSshKeyValidation.isPublicKey('ssh-')).toBeTruthy();
      expect(AddSshKeyValidation.isPublicKey('ecdsa-sha2-')).toBeTruthy();
    });
  });
});