summaryrefslogtreecommitdiff
path: root/spec/frontend/projects/settings/access_dropdown_spec.js
blob: 65b01172e7eec038f2b92013f7fa2e51391143ff (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import $ from 'jquery';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import AccessDropdown from '~/projects/settings/access_dropdown';
import { LEVEL_TYPES } from '~/projects/settings/constants';

describe('AccessDropdown', () => {
  const defaultLabel = 'dummy default label';
  let dropdown;

  beforeEach(() => {
    setHTMLFixture(`
      <div id="dummy-dropdown">
        <span class="dropdown-toggle-text"></span>
      </div>
    `);
    const $dropdown = $('#dummy-dropdown');
    $dropdown.data('defaultLabel', defaultLabel);
    const options = {
      $dropdown,
      accessLevelsData: {
        roles: [
          {
            id: 42,
            text: 'Dummy Role',
          },
        ],
      },
    };
    dropdown = new AccessDropdown(options);
  });

  afterEach(() => {
    resetHTMLFixture();
  });

  describe('toggleLabel', () => {
    let $dropdownToggleText;
    const dummyItems = [
      { type: LEVEL_TYPES.ROLE, access_level: 42 },
      { type: LEVEL_TYPES.USER },
      { type: LEVEL_TYPES.USER },
      { type: LEVEL_TYPES.GROUP },
      { type: LEVEL_TYPES.GROUP },
      { type: LEVEL_TYPES.GROUP },
      { type: LEVEL_TYPES.DEPLOY_KEY },
      { type: LEVEL_TYPES.DEPLOY_KEY },
      { type: LEVEL_TYPES.DEPLOY_KEY },
    ];

    beforeEach(() => {
      $dropdownToggleText = $('.dropdown-toggle-text');
    });

    it('displays number of items', () => {
      dropdown.setSelectedItems(dummyItems);
      $dropdownToggleText.addClass('is-default');

      const label = dropdown.toggleLabel();

      expect(label).toBe('1 role, 2 users, 3 deploy keys, 3 groups');
      expect($dropdownToggleText).not.toHaveClass('is-default');
    });

    describe('without selected items', () => {
      beforeEach(() => {
        dropdown.setSelectedItems([]);
      });

      it('falls back to default label', () => {
        const label = dropdown.toggleLabel();

        expect(label).toBe(defaultLabel);
        expect($dropdownToggleText).toHaveClass('is-default');
      });
    });

    describe('with only role', () => {
      beforeEach(() => {
        dropdown.setSelectedItems(dummyItems.filter((item) => item.type === LEVEL_TYPES.ROLE));
        $dropdownToggleText.addClass('is-default');
      });

      it('displays the role name', () => {
        const label = dropdown.toggleLabel();

        expect(label).toBe('Dummy Role');
        expect($dropdownToggleText).not.toHaveClass('is-default');
      });
    });

    describe('with only users', () => {
      beforeEach(() => {
        dropdown.setSelectedItems(dummyItems.filter((item) => item.type === LEVEL_TYPES.USER));
        $dropdownToggleText.addClass('is-default');
      });

      it('displays number of users', () => {
        const label = dropdown.toggleLabel();

        expect(label).toBe('2 users');
        expect($dropdownToggleText).not.toHaveClass('is-default');
      });
    });

    describe('with only groups', () => {
      beforeEach(() => {
        dropdown.setSelectedItems(dummyItems.filter((item) => item.type === LEVEL_TYPES.GROUP));
        $dropdownToggleText.addClass('is-default');
      });

      it('displays number of groups', () => {
        const label = dropdown.toggleLabel();

        expect(label).toBe('3 groups');
        expect($dropdownToggleText).not.toHaveClass('is-default');
      });
    });

    describe('with users and groups', () => {
      beforeEach(() => {
        const selectedTypes = [LEVEL_TYPES.GROUP, LEVEL_TYPES.USER];
        dropdown.setSelectedItems(dummyItems.filter((item) => selectedTypes.includes(item.type)));
        $dropdownToggleText.addClass('is-default');
      });

      it('displays number of groups', () => {
        const label = dropdown.toggleLabel();

        expect(label).toBe('2 users, 3 groups');
        expect($dropdownToggleText).not.toHaveClass('is-default');
      });
    });

    describe('with users and deploy keys', () => {
      beforeEach(() => {
        const selectedTypes = [LEVEL_TYPES.DEPLOY_KEY, LEVEL_TYPES.USER];
        dropdown.setSelectedItems(dummyItems.filter((item) => selectedTypes.includes(item.type)));
        $dropdownToggleText.addClass('is-default');
      });

      it('displays number of deploy keys', () => {
        const label = dropdown.toggleLabel();

        expect(label).toBe('2 users, 3 deploy keys');
        expect($dropdownToggleText).not.toHaveClass('is-default');
      });
    });
  });

  describe('userRowHtml', () => {
    it('escapes users name', () => {
      const user = {
        avatar_url: '',
        name: '<img src=x onerror=alert(document.domain)>',
        username: 'test',
      };
      const template = dropdown.userRowHtml(user);

      expect(template).not.toContain(user.name);
    });
  });
});