blob: 5e96e33f67ab81ebe6fed4d313dd66337ea084a2 [file] [log] [blame]
Dmitrii Filippov06117e82020-06-25 13:26:55 +02001/**
2 * @license
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Paladox nonea4b816b2017-06-18 20:21:37 +000017
Paladox nonea26c3942021-10-29 00:28:01 +000018import '../../../test/common-test-setup-karma';
19import './gr-group';
20import {GrGroup} from './gr-group';
Chris Pouceta2e173e2021-08-31 01:04:04 +000021import {
22 addListenerForTest,
23 mockPromise,
Paladox noneaaf24dc2021-10-29 13:55:14 +000024 queryAndAssert,
Chris Pouceta2e173e2021-08-31 01:04:04 +000025 stubRestApi,
Paladox nonea26c3942021-10-29 00:28:01 +000026} from '../../../test/test-utils';
27import {createGroupInfo} from '../../../test/test-data-generators.js';
28import {GroupId, GroupInfo, GroupName} from '../../../types/common';
Paladox noneaaf24dc2021-10-29 13:55:14 +000029import {GrAutocomplete} from '../../shared/gr-autocomplete/gr-autocomplete';
30import {GrButton} from '../../shared/gr-button/gr-button';
31import {GrCopyClipboard} from '../../shared/gr-copy-clipboard/gr-copy-clipboard';
32import {GrSelect} from '../../shared/gr-select/gr-select';
Dmitrii Filippov06117e82020-06-25 13:26:55 +020033
34const basicFixture = fixtureFromElement('gr-group');
35
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010036suite('gr-group tests', () => {
Paladox nonea26c3942021-10-29 00:28:01 +000037 let element: GrGroup;
38 let groupStub: sinon.SinonStub;
Dmitrii Filippov7d4f2f32020-06-28 23:14:26 +020039
Paladox nonea26c3942021-10-29 00:28:01 +000040 const group: GroupInfo = {
41 ...createGroupInfo('6a1e70e1a88782771a91808c8af9bbb7a9871389'),
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010042 url: '#/admin/groups/uuid-6a1e70e1a88782771a91808c8af9bbb7a9871389',
Paladox nonea26c3942021-10-29 00:28:01 +000043 options: {
44 visible_to_all: false,
45 },
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010046 description: 'Gerrit Site Administrators',
47 group_id: 1,
48 owner: 'Administrators',
49 owner_id: '6a1e70e1a88782771a91808c8af9bbb7a9871389',
Paladox nonea26c3942021-10-29 00:28:01 +000050 name: 'Administrators' as GroupName,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010051 };
Paladox nonea4b816b2017-06-18 20:21:37 +000052
Paladox noneaaf24dc2021-10-29 13:55:14 +000053 setup(async () => {
Dmitrii Filippov06117e82020-06-25 13:26:55 +020054 element = basicFixture.instantiate();
Paladox noneaaf24dc2021-10-29 13:55:14 +000055 await element.updateComplete;
Ben Rohlfs3a6ff7e2021-01-18 14:08:39 +010056 groupStub = stubRestApi('getGroupConfig').returns(Promise.resolve(group));
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010057 });
Paladox nonea4b816b2017-06-18 20:21:37 +000058
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010059 test('loading displays before group config is loaded', () => {
Paladox noneaaf24dc2021-10-29 13:55:14 +000060 assert.isTrue(
61 queryAndAssert<HTMLDivElement>(element, '#loading').classList.contains(
62 'loading'
63 )
64 );
65 assert.isFalse(
66 getComputedStyle(queryAndAssert<HTMLDivElement>(element, '#loading'))
67 .display === 'none'
68 );
69 assert.isTrue(
70 queryAndAssert<HTMLDivElement>(
71 element,
72 '#loadedContent'
73 ).classList.contains('loading')
74 );
75 assert.isTrue(
76 getComputedStyle(
77 queryAndAssert<HTMLDivElement>(element, '#loadedContent')
78 ).display === 'none'
79 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010080 });
Paladox nonea4b816b2017-06-18 20:21:37 +000081
Chris Pouceta2e173e2021-08-31 01:04:04 +000082 test('default values are populated with internal group', async () => {
Ben Rohlfs3a6ff7e2021-01-18 14:08:39 +010083 stubRestApi('getIsGroupOwner').returns(Promise.resolve(true));
Paladox nonea26c3942021-10-29 00:28:01 +000084 element.groupId = '1' as GroupId;
Paladox noneaaf24dc2021-10-29 13:55:14 +000085 await element.loadGroup();
86 assert.isTrue(element.groupIsInternal);
87 assert.isFalse(
88 queryAndAssert<GrSelect>(element, '#visibleToAll').bindValue
89 );
Paladox nonea4b816b2017-06-18 20:21:37 +000090 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010091
Chris Pouceta2e173e2021-08-31 01:04:04 +000092 test('default values with external group', async () => {
Tao Zhou4cd35cb2020-07-22 11:28:22 +020093 const groupExternal = {...group};
Paladox nonea26c3942021-10-29 00:28:01 +000094 groupExternal.id = 'external-group-id' as GroupId;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010095 groupStub.restore();
Ben Rohlfs3a6ff7e2021-01-18 14:08:39 +010096 groupStub = stubRestApi('getGroupConfig').returns(
Paladox nonea26c3942021-10-29 00:28:01 +000097 Promise.resolve(groupExternal)
98 );
Ben Rohlfs3a6ff7e2021-01-18 14:08:39 +010099 stubRestApi('getIsGroupOwner').returns(Promise.resolve(true));
Paladox nonea26c3942021-10-29 00:28:01 +0000100 element.groupId = '1' as GroupId;
Paladox noneaaf24dc2021-10-29 13:55:14 +0000101 await element.loadGroup();
102 assert.isFalse(element.groupIsInternal);
103 assert.isFalse(
104 queryAndAssert<GrSelect>(element, '#visibleToAll').bindValue
105 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100106 });
107
Chris Pouceta2e173e2021-08-31 01:04:04 +0000108 test('rename group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100109 const groupName = 'test-group';
110 const groupName2 = 'test-group2';
Paladox nonea26c3942021-10-29 00:28:01 +0000111 element.groupId = '1' as GroupId;
Paladox noneaaf24dc2021-10-29 13:55:14 +0000112 element.groupConfig = {
Paladox nonea26c3942021-10-29 00:28:01 +0000113 name: groupName as GroupName,
114 id: '1' as GroupId,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100115 };
Paladox noneaaf24dc2021-10-29 13:55:14 +0000116 element.originalName = groupName as GroupName;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100117
Ben Rohlfs3a6ff7e2021-01-18 14:08:39 +0100118 stubRestApi('getIsGroupOwner').returns(Promise.resolve(true));
Paladox nonea26c3942021-10-29 00:28:01 +0000119 stubRestApi('saveGroupName').returns(
120 Promise.resolve({...new Response(), status: 200})
121 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100122
Paladox noneaaf24dc2021-10-29 13:55:14 +0000123 const button = queryAndAssert<GrButton>(element, '#inputUpdateNameBtn');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100124
Paladox noneaaf24dc2021-10-29 13:55:14 +0000125 await element.loadGroup();
Chris Pouceta2e173e2021-08-31 01:04:04 +0000126 assert.isTrue(button.hasAttribute('disabled'));
Paladox noneaaf24dc2021-10-29 13:55:14 +0000127 assert.isFalse(
128 queryAndAssert<HTMLHeadingElement>(element, '#Title').classList.contains(
129 'edited'
130 )
131 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100132
Paladox noneaaf24dc2021-10-29 13:55:14 +0000133 queryAndAssert<GrAutocomplete>(element, '#groupNameInput').text =
134 groupName2;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100135
Paladox noneaaf24dc2021-10-29 13:55:14 +0000136 await element.updateComplete;
137
Chris Pouceta2e173e2021-08-31 01:04:04 +0000138 assert.isFalse(button.hasAttribute('disabled'));
Paladox noneaaf24dc2021-10-29 13:55:14 +0000139 assert.isTrue(
140 queryAndAssert<HTMLHeadingElement>(
141 element,
142 '#groupName'
143 ).classList.contains('edited')
144 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100145
Paladox noneaaf24dc2021-10-29 13:55:14 +0000146 await element.handleSaveName();
147 assert.isTrue(button.disabled);
148 assert.isFalse(
149 queryAndAssert<HTMLHeadingElement>(element, '#Title').classList.contains(
150 'edited'
151 )
152 );
153 assert.equal(element.originalName, groupName2);
Tao Zhou0a3c9862020-04-08 14:45:37 +0200154 });
155
Chris Pouceta2e173e2021-08-31 01:04:04 +0000156 test('rename group owner', async () => {
Tao Zhou0a3c9862020-04-08 14:45:37 +0200157 const groupName = 'test-group';
Paladox nonea26c3942021-10-29 00:28:01 +0000158 element.groupId = '1' as GroupId;
Paladox noneaaf24dc2021-10-29 13:55:14 +0000159 element.groupConfig = {
Paladox nonea26c3942021-10-29 00:28:01 +0000160 name: groupName as GroupName,
161 id: '1' as GroupId,
Tao Zhou0a3c9862020-04-08 14:45:37 +0200162 };
Paladox noneaaf24dc2021-10-29 13:55:14 +0000163 element.groupConfigOwner = 'testId';
164 element.groupOwner = true;
Tao Zhou0a3c9862020-04-08 14:45:37 +0200165
Paladox nonea26c3942021-10-29 00:28:01 +0000166 stubRestApi('getIsGroupOwner').returns(Promise.resolve(true));
Tao Zhou0a3c9862020-04-08 14:45:37 +0200167
Paladox noneaaf24dc2021-10-29 13:55:14 +0000168 const button = queryAndAssert<GrButton>(element, '#inputUpdateOwnerBtn');
Tao Zhou0a3c9862020-04-08 14:45:37 +0200169
Paladox noneaaf24dc2021-10-29 13:55:14 +0000170 await element.loadGroup();
171 assert.isTrue(button.disabled);
172 assert.isFalse(
173 queryAndAssert<HTMLHeadingElement>(element, '#Title').classList.contains(
174 'edited'
175 )
176 );
Tao Zhou0a3c9862020-04-08 14:45:37 +0200177
Paladox noneaaf24dc2021-10-29 13:55:14 +0000178 queryAndAssert<GrAutocomplete>(element, '#groupOwnerInput').text =
179 'testId2';
Tao Zhou0a3c9862020-04-08 14:45:37 +0200180
Paladox noneaaf24dc2021-10-29 13:55:14 +0000181 await element.updateComplete;
182 assert.isFalse(button.disabled);
183 assert.isTrue(
184 queryAndAssert<HTMLHeadingElement>(
185 element,
186 '#groupOwner'
187 ).classList.contains('edited')
188 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100189
Paladox noneaaf24dc2021-10-29 13:55:14 +0000190 await element.handleSaveOwner();
191 assert.isTrue(button.disabled);
192 assert.isFalse(
193 queryAndAssert<HTMLHeadingElement>(element, '#Title').classList.contains(
194 'edited'
195 )
196 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100197 });
198
Chris Pouceta2e173e2021-08-31 01:04:04 +0000199 test('test for undefined group name', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100200 groupStub.restore();
201
Paladox nonea26c3942021-10-29 00:28:01 +0000202 stubRestApi('getGroupConfig').returns(Promise.resolve(undefined));
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100203
204 assert.isUndefined(element.groupId);
205
Paladox nonea26c3942021-10-29 00:28:01 +0000206 element.groupId = '1' as GroupId;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100207
208 assert.isDefined(element.groupId);
209
210 // Test that loading shows instead of filling
211 // in group details
Paladox noneaaf24dc2021-10-29 13:55:14 +0000212 await element.loadGroup();
213 assert.isTrue(
214 queryAndAssert<HTMLDivElement>(element, '#loading').classList.contains(
215 'loading'
216 )
217 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100218
Paladox noneaaf24dc2021-10-29 13:55:14 +0000219 assert.isTrue(element.loading);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100220 });
221
Chris Pouceta2e173e2021-08-31 01:04:04 +0000222 test('test fire event', async () => {
Paladox noneaaf24dc2021-10-29 13:55:14 +0000223 element.groupConfig = {
Paladox nonea26c3942021-10-29 00:28:01 +0000224 name: 'test-group' as GroupName,
225 id: '1' as GroupId,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100226 };
Paladox nonea26c3942021-10-29 00:28:01 +0000227 element.groupId = 'gg' as GroupId;
228 stubRestApi('saveGroupName').returns(
229 Promise.resolve({...new Response(), status: 200})
230 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100231
Dmitrii Filippov7d4f2f32020-06-28 23:14:26 +0200232 const showStub = sinon.stub(element, 'dispatchEvent');
Paladox noneaaf24dc2021-10-29 13:55:14 +0000233 await element.handleSaveName();
Chris Pouceta2e173e2021-08-31 01:04:04 +0000234 assert.isTrue(showStub.called);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100235 });
236
Paladox noneaaf24dc2021-10-29 13:55:14 +0000237 test('computeGroupDisabled', () => {
238 element.isAdmin = true;
239 element.groupOwner = false;
240 element.groupIsInternal = true;
241 assert.equal(element.computeGroupDisabled(), false);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100242
Paladox noneaaf24dc2021-10-29 13:55:14 +0000243 element.isAdmin = false;
244 assert.equal(element.computeGroupDisabled(), true);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100245
Paladox noneaaf24dc2021-10-29 13:55:14 +0000246 element.groupOwner = true;
247 assert.equal(element.computeGroupDisabled(), false);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100248
Paladox noneaaf24dc2021-10-29 13:55:14 +0000249 element.groupOwner = false;
250 assert.equal(element.computeGroupDisabled(), true);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100251
Paladox noneaaf24dc2021-10-29 13:55:14 +0000252 element.groupIsInternal = false;
253 assert.equal(element.computeGroupDisabled(), true);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100254
Paladox noneaaf24dc2021-10-29 13:55:14 +0000255 element.isAdmin = true;
256 assert.equal(element.computeGroupDisabled(), true);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100257 });
258
Paladox noneaaf24dc2021-10-29 13:55:14 +0000259 test('computeLoadingClass', () => {
260 element.loading = true;
261 assert.equal(element.computeLoadingClass(), 'loading');
262 element.loading = false;
263 assert.equal(element.computeLoadingClass(), '');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100264 });
265
Chris Pouceta2e173e2021-08-31 01:04:04 +0000266 test('fires page-error', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100267 groupStub.restore();
268
Paladox nonea26c3942021-10-29 00:28:01 +0000269 element.groupId = '1' as GroupId;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100270
Paladox nonea26c3942021-10-29 00:28:01 +0000271 const response = {...new Response(), status: 404};
272 stubRestApi('getGroupConfig').callsFake((_, errFn) => {
273 if (errFn !== undefined) {
274 errFn(response);
275 } else {
276 assert.fail('errFn is undefined');
277 }
Chris Pouceta2e173e2021-08-31 01:04:04 +0000278 return Promise.resolve(undefined);
Dmitrii Filippov7d4f2f32020-06-28 23:14:26 +0200279 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100280
Chris Pouceta2e173e2021-08-31 01:04:04 +0000281 const promise = mockPromise();
Ben Rohlfsa76c82f2021-01-22 22:22:32 +0100282 addListenerForTest(document, 'page-error', e => {
Paladox nonea26c3942021-10-29 00:28:01 +0000283 assert.deepEqual((e as CustomEvent).detail.response, response);
Chris Pouceta2e173e2021-08-31 01:04:04 +0000284 promise.resolve();
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100285 });
286
Paladox noneaaf24dc2021-10-29 13:55:14 +0000287 await element.loadGroup();
Chris Pouceta2e173e2021-08-31 01:04:04 +0000288 await promise;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100289 });
Paladox none54505fc2020-05-11 10:34:08 +0000290
Paladox noneaaf24dc2021-10-29 13:55:14 +0000291 test('uuid', async () => {
292 element.groupConfig = {
Paladox nonea26c3942021-10-29 00:28:01 +0000293 id: '6a1e70e1a88782771a91808c8af9bbb7a9871389' as GroupId,
Paladox none54505fc2020-05-11 10:34:08 +0000294 };
295
Paladox noneaaf24dc2021-10-29 13:55:14 +0000296 await element.updateComplete;
David Pursehouse7d65d0a2020-05-14 17:27:10 +0900297
Paladox noneaaf24dc2021-10-29 13:55:14 +0000298 assert.equal(
299 element.groupConfig.id,
300 queryAndAssert<GrCopyClipboard>(element, '#uuid').text
301 );
302
303 element.groupConfig = {
Paladox nonea26c3942021-10-29 00:28:01 +0000304 id: 'user%2Fgroup' as GroupId,
David Pursehouse7d65d0a2020-05-14 17:27:10 +0900305 };
306
Paladox noneaaf24dc2021-10-29 13:55:14 +0000307 await element.updateComplete;
308
309 assert.equal(
310 'user/group',
311 queryAndAssert<GrCopyClipboard>(element, '#uuid').text
312 );
Paladox none54505fc2020-05-11 10:34:08 +0000313 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100314});