Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index_spec.js « users_select « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3757e63c4f96274a6a40d175d0fe51865d602721 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { escape } from 'lodash';
import UsersSelect from '~/users_select/index';
import {
  createInputsModelExpectation,
  createUnassignedExpectation,
  createAssignedExpectation,
  createTestContext,
  findDropdownItemsModel,
  findDropdownItem,
  findAssigneesInputsModel,
  getUsersFixtureAt,
  setAssignees,
  toggleDropdown,
  waitForDropdownItems,
} from './test_helper';

describe('~/users_select/index', () => {
  const context = createTestContext({
    fixturePath: 'merge_requests/merge_request_with_single_assignee_feature.html',
  });

  beforeEach(() => {
    context.setup();
  });

  afterEach(() => {
    context.teardown();
  });

  describe('when opened', () => {
    beforeEach(async () => {
      context.createSubject();

      toggleDropdown();
      await waitForDropdownItems();
    });

    it('shows users', () => {
      expect(findDropdownItemsModel()).toEqual(createUnassignedExpectation());
    });

    describe('when users are selected', () => {
      const selectedUsers = [getUsersFixtureAt(2), getUsersFixtureAt(4)];
      const lastSelected = selectedUsers[selectedUsers.length - 1];
      const expectation = createAssignedExpectation({
        header: 'Assignee',
        assigned: [lastSelected],
      });

      beforeEach(() => {
        selectedUsers.forEach((user) => {
          findDropdownItem(user).click();
        });
      });

      it('shows assignee', () => {
        expect(findDropdownItemsModel()).toEqual(expectation);
      });

      it('updates field', () => {
        expect(findAssigneesInputsModel()).toEqual(createInputsModelExpectation([lastSelected]));
      });
    });
  });

  describe('with preselected user and opened', () => {
    const expectation = createAssignedExpectation({
      header: 'Assignee',
      assigned: [getUsersFixtureAt(0)],
    });

    beforeEach(async () => {
      setAssignees(getUsersFixtureAt(0));

      context.createSubject();

      toggleDropdown();
      await waitForDropdownItems();
    });

    it('shows users', () => {
      expect(findDropdownItemsModel()).toEqual(expectation);
    });

    // Regression test for https://gitlab.com/gitlab-org/gitlab/-/issues/325991
    describe('when closed and reopened', () => {
      beforeEach(() => {
        toggleDropdown();
        toggleDropdown();
      });

      it('shows users', () => {
        expect(findDropdownItemsModel()).toEqual(expectation);
      });
    });

    describe('renderApprovalRules', () => {
      const ruleNames = ['simple-name', '"\'<>&', '"><script>alert(1)<script>'];

      it.each(ruleNames)('escapes rule name correctly for %s', (name) => {
        const escapedName = escape(name);

        expect(
          UsersSelect.prototype.renderApprovalRules('reviewer', [{ name }]),
        ).toMatchInterpolatedText(
          `<div class="gl-display-flex gl-font-sm"> <span class="gl-text-truncate" title="${escapedName}">${escapedName}</span> </div>`,
        );
      });
    });
  });

  describe('XSS', () => {
    const escaped = '&gt;&lt;script&gt;alert(1)&lt;/script&gt;';
    const issuableType = 'merge_request';
    const user = {
      availability: 'not_set',
      can_merge: true,
      name: 'name',
    };
    const selected = true;
    const username = 'username';
    const img = '<img user-avatar />';
    const elsClassName = 'elsclass';

    it.each`
      prop          | val                             | element
      ${'username'} | ${'><script>alert(1)</script>'} | ${'.dropdown-menu-user-username'}
      ${'name'}     | ${'><script>alert(1)</script>'} | ${'.dropdown-menu-user-full-name'}
    `('properly escapes the $prop $val', ({ prop, val, element }) => {
      const u = prop === 'username' ? val : username;
      const n = prop === 'name' ? val : user.name;
      const row = UsersSelect.prototype.renderRow(
        issuableType,
        { ...user, name: n },
        selected,
        u,
        img,
        elsClassName,
      );
      const fragment = document.createRange().createContextualFragment(row);
      const output = fragment.querySelector(element).innerHTML.trim();

      expect(output).toBe(escaped);
    });
  });
});