Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 1 | /** |
| 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 none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 17 | |
Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 18 | import '../../../test/common-test-setup-karma.js'; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 19 | import './gr-group.js'; |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 20 | import {stubRestApi} from '../../../test/test-utils.js'; |
Ben Rohlfs | a76c82f | 2021-01-22 22:22:32 +0100 | [diff] [blame^] | 21 | import {addListenerForTest} from '../../../test/test-utils.js'; |
Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 22 | |
| 23 | const basicFixture = fixtureFromElement('gr-group'); |
| 24 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 25 | suite('gr-group tests', () => { |
| 26 | let element; |
Dmitrii Filippov | 7d4f2f3 | 2020-06-28 23:14:26 +0200 | [diff] [blame] | 27 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 28 | let groupStub; |
| 29 | const group = { |
| 30 | id: '6a1e70e1a88782771a91808c8af9bbb7a9871389', |
| 31 | url: '#/admin/groups/uuid-6a1e70e1a88782771a91808c8af9bbb7a9871389', |
| 32 | options: {}, |
| 33 | description: 'Gerrit Site Administrators', |
| 34 | group_id: 1, |
| 35 | owner: 'Administrators', |
| 36 | owner_id: '6a1e70e1a88782771a91808c8af9bbb7a9871389', |
| 37 | name: 'Administrators', |
| 38 | }; |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 39 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 40 | setup(() => { |
Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 41 | element = basicFixture.instantiate(); |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 42 | groupStub = stubRestApi('getGroupConfig').returns(Promise.resolve(group)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 43 | }); |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 44 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 45 | test('loading displays before group config is loaded', () => { |
| 46 | assert.isTrue(element.$.loading.classList.contains('loading')); |
| 47 | assert.isFalse(getComputedStyle(element.$.loading).display === 'none'); |
| 48 | assert.isTrue(element.$.loadedContent.classList.contains('loading')); |
| 49 | assert.isTrue(getComputedStyle(element.$.loadedContent) |
| 50 | .display === 'none'); |
| 51 | }); |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 52 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 53 | test('default values are populated with internal group', done => { |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 54 | stubRestApi('getIsGroupOwner').returns(Promise.resolve(true)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 55 | element.groupId = 1; |
| 56 | element._loadGroup().then(() => { |
| 57 | assert.isTrue(element._groupIsInternal); |
| 58 | assert.isFalse(element.$.visibleToAll.bindValue); |
| 59 | done(); |
Paladox none | 70cb10c | 2018-02-17 19:12:09 +0000 | [diff] [blame] | 60 | }); |
Paladox none | a4b816b | 2017-06-18 20:21:37 +0000 | [diff] [blame] | 61 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 62 | |
| 63 | test('default values with external group', done => { |
Tao Zhou | 4cd35cb | 2020-07-22 11:28:22 +0200 | [diff] [blame] | 64 | const groupExternal = {...group}; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 65 | groupExternal.id = 'external-group-id'; |
| 66 | groupStub.restore(); |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 67 | groupStub = stubRestApi('getGroupConfig').returns( |
| 68 | Promise.resolve(groupExternal)); |
| 69 | stubRestApi('getIsGroupOwner').returns(Promise.resolve(true)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 70 | element.groupId = 1; |
| 71 | element._loadGroup().then(() => { |
| 72 | assert.isFalse(element._groupIsInternal); |
| 73 | assert.isFalse(element.$.visibleToAll.bindValue); |
| 74 | done(); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | test('rename group', done => { |
| 79 | const groupName = 'test-group'; |
| 80 | const groupName2 = 'test-group2'; |
| 81 | element.groupId = 1; |
| 82 | element._groupConfig = { |
| 83 | name: groupName, |
| 84 | }; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 85 | element._groupName = groupName; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 86 | |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 87 | stubRestApi('getIsGroupOwner').returns(Promise.resolve(true)); |
| 88 | stubRestApi('saveGroupName').returns(Promise.resolve({status: 200})); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 89 | |
| 90 | const button = element.$.inputUpdateNameBtn; |
| 91 | |
| 92 | element._loadGroup().then(() => { |
| 93 | assert.isTrue(button.hasAttribute('disabled')); |
| 94 | assert.isFalse(element.$.Title.classList.contains('edited')); |
| 95 | |
| 96 | element.$.groupNameInput.text = groupName2; |
| 97 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 98 | assert.isFalse(button.hasAttribute('disabled')); |
| 99 | assert.isTrue(element.$.groupName.classList.contains('edited')); |
| 100 | |
| 101 | element._handleSaveName().then(() => { |
| 102 | assert.isTrue(button.hasAttribute('disabled')); |
| 103 | assert.isFalse(element.$.Title.classList.contains('edited')); |
| 104 | assert.equal(element._groupName, groupName2); |
| 105 | done(); |
| 106 | }); |
Tao Zhou | 0a3c986 | 2020-04-08 14:45:37 +0200 | [diff] [blame] | 107 | }); |
| 108 | }); |
| 109 | |
| 110 | test('rename group owner', done => { |
| 111 | const groupName = 'test-group'; |
| 112 | element.groupId = 1; |
| 113 | element._groupConfig = { |
| 114 | name: groupName, |
| 115 | }; |
| 116 | element._groupConfigOwner = 'testId'; |
| 117 | element._groupOwner = true; |
| 118 | |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 119 | stubRestApi('getIsGroupOwner').returns(Promise.resolve({status: 200})); |
Tao Zhou | 0a3c986 | 2020-04-08 14:45:37 +0200 | [diff] [blame] | 120 | |
| 121 | const button = element.$.inputUpdateOwnerBtn; |
| 122 | |
| 123 | element._loadGroup().then(() => { |
| 124 | assert.isTrue(button.hasAttribute('disabled')); |
| 125 | assert.isFalse(element.$.Title.classList.contains('edited')); |
| 126 | |
| 127 | element.$.groupOwnerInput.text = 'testId2'; |
| 128 | |
| 129 | assert.isFalse(button.hasAttribute('disabled')); |
| 130 | assert.isTrue(element.$.groupOwner.classList.contains('edited')); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 131 | |
| 132 | element._handleSaveOwner().then(() => { |
| 133 | assert.isTrue(button.hasAttribute('disabled')); |
| 134 | assert.isFalse(element.$.Title.classList.contains('edited')); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 135 | done(); |
| 136 | }); |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | test('test for undefined group name', done => { |
| 141 | groupStub.restore(); |
| 142 | |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 143 | stubRestApi('getGroupConfig').returns(Promise.resolve({})); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 144 | |
| 145 | assert.isUndefined(element.groupId); |
| 146 | |
| 147 | element.groupId = 1; |
| 148 | |
| 149 | assert.isDefined(element.groupId); |
| 150 | |
| 151 | // Test that loading shows instead of filling |
| 152 | // in group details |
| 153 | element._loadGroup().then(() => { |
| 154 | assert.isTrue(element.$.loading.classList.contains('loading')); |
| 155 | |
| 156 | assert.isTrue(element._loading); |
| 157 | |
| 158 | done(); |
| 159 | }); |
| 160 | }); |
| 161 | |
| 162 | test('test fire event', done => { |
| 163 | element._groupConfig = { |
| 164 | name: 'test-group', |
| 165 | }; |
Dhruv Srivastava | 8769e10 | 2020-08-25 22:23:34 +0200 | [diff] [blame] | 166 | element.groupId = 'gg'; |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 167 | stubRestApi('saveGroupName').returns(Promise.resolve({status: 200})); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 168 | |
Dmitrii Filippov | 7d4f2f3 | 2020-06-28 23:14:26 +0200 | [diff] [blame] | 169 | const showStub = sinon.stub(element, 'dispatchEvent'); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 170 | element._handleSaveName() |
| 171 | .then(() => { |
| 172 | assert.isTrue(showStub.called); |
| 173 | done(); |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | test('_computeGroupDisabled', () => { |
| 178 | let admin = true; |
| 179 | let owner = false; |
| 180 | let groupIsInternal = true; |
| 181 | assert.equal(element._computeGroupDisabled(owner, admin, |
| 182 | groupIsInternal), false); |
| 183 | |
| 184 | admin = false; |
| 185 | assert.equal(element._computeGroupDisabled(owner, admin, |
| 186 | groupIsInternal), true); |
| 187 | |
| 188 | owner = true; |
| 189 | assert.equal(element._computeGroupDisabled(owner, admin, |
| 190 | groupIsInternal), false); |
| 191 | |
| 192 | owner = false; |
| 193 | assert.equal(element._computeGroupDisabled(owner, admin, |
| 194 | groupIsInternal), true); |
| 195 | |
| 196 | groupIsInternal = false; |
| 197 | assert.equal(element._computeGroupDisabled(owner, admin, |
| 198 | groupIsInternal), true); |
| 199 | |
| 200 | admin = true; |
| 201 | assert.equal(element._computeGroupDisabled(owner, admin, |
| 202 | groupIsInternal), true); |
| 203 | }); |
| 204 | |
| 205 | test('_computeLoadingClass', () => { |
| 206 | assert.equal(element._computeLoadingClass(true), 'loading'); |
| 207 | assert.equal(element._computeLoadingClass(false), ''); |
| 208 | }); |
| 209 | |
| 210 | test('fires page-error', done => { |
| 211 | groupStub.restore(); |
| 212 | |
| 213 | element.groupId = 1; |
| 214 | |
| 215 | const response = {status: 404}; |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 216 | stubRestApi('getGroupConfig').callsFake((group, errFn) => { |
Dmitrii Filippov | 7d4f2f3 | 2020-06-28 23:14:26 +0200 | [diff] [blame] | 217 | errFn(response); |
| 218 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 219 | |
Ben Rohlfs | a76c82f | 2021-01-22 22:22:32 +0100 | [diff] [blame^] | 220 | addListenerForTest(document, 'page-error', e => { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 221 | assert.deepEqual(e.detail.response, response); |
| 222 | done(); |
| 223 | }); |
| 224 | |
| 225 | element._loadGroup(); |
| 226 | }); |
Paladox none | 54505fc | 2020-05-11 10:34:08 +0000 | [diff] [blame] | 227 | |
| 228 | test('uuid', () => { |
| 229 | element._groupConfig = { |
| 230 | id: '6a1e70e1a88782771a91808c8af9bbb7a9871389', |
| 231 | }; |
| 232 | |
| 233 | assert.equal(element._groupConfig.id, element.$.uuid.text); |
David Pursehouse | 7d65d0a | 2020-05-14 17:27:10 +0900 | [diff] [blame] | 234 | |
| 235 | element._groupConfig = { |
| 236 | id: 'user%2Fgroup', |
| 237 | }; |
| 238 | |
| 239 | assert.equal('user/group', element.$.uuid.text); |
Paladox none | 54505fc | 2020-05-11 10:34:08 +0000 | [diff] [blame] | 240 | }); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 241 | }); |
Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 242 | |