blob: 179d221fd9656588fffc525b604301a402f5d52c [file] [log] [blame]
Becky Siegel8db12402017-08-17 16:12:53 -07001<!DOCTYPE html>
2<!--
3Copyright (C) 2017 The Android Open Source Project
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16-->
17
18<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
19<title>gr-permission</title>
20
21<script src="../../../bower_components/page/page.js"></script>
22<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
23<script src="../../../bower_components/web-component-tester/browser.js"></script>
24<link rel="import" href="../../../test/common-test-setup.html"/>
25<link rel="import" href="gr-permission.html">
26
27<script>void(0);</script>
28
29<test-fixture id="basic">
30 <template>
31 <gr-permission></gr-permission>
32 </template>
33</test-fixture>
34
35<script>
36 suite('gr-permission tests', () => {
37 let element;
38 let sandbox;
39
40 setup(() => {
41 sandbox = sinon.sandbox.create();
42 element = fixture('basic');
43 sandbox.stub(element.$.restAPI, 'getSuggestedGroups').returns(
44 Promise.resolve({
45 'Administrators': {
46 id: '4c97682e6ce61b7247f3381b6f1789356666de7f',
47 },
48 'Anonymous Users': {
49 id: 'global%3AAnonymous-Users',
50 },
51 }));
52 });
53
54 teardown(() => {
55 sandbox.restore();
56 });
57
58 suite('unit tests', () => {
59 test('_sortPermission', () => {
60 const permission = {
61 id: 'submit',
62 value: {
63 rules: {
64 'global:Project-Owners': {
65 action: 'ALLOW',
66 force: false,
67 },
68 '4c97682e6ce6b7247f3381b6f1789356666de7f': {
69 action: 'ALLOW',
70 force: false,
71 },
72 },
73 },
74 };
75
76 const expectedRules = [
77 {
78 id: '4c97682e6ce6b7247f3381b6f1789356666de7f',
79 value: {action: 'ALLOW', force: false},
80 },
81 {
82 id: 'global:Project-Owners',
83 value: {action: 'ALLOW', force: false},
84 },
85 ];
86
87 element._sortPermission(permission);
88 assert.deepEqual(element._rules, expectedRules);
89 });
90
91 test('_computeLabel and _computeLabelValues', () => {
92 const labels = {
93 'Code-Review': {
94 default_value: 0,
95 values: {
96 ' 0': 'No score',
97 '-1': 'I would prefer this is not merged as is',
98 '-2': 'This shall not be merged',
99 '+1': 'Looks good to me, but someone else must approve',
100 '+2': 'Looks good to me, approved',
101 },
102 },
103 };
Becky Siegel57df2232017-11-07 10:56:55 -0800104 let permission = {
Becky Siegel8db12402017-08-17 16:12:53 -0700105 id: 'label-Code-Review',
106 value: {
107 label: 'Code-Review',
108 rules: {
109 'global:Project-Owners': {
110 action: 'ALLOW',
111 force: false,
112 min: -2,
113 max: 2,
114 },
115 '4c97682e6ce6b7247f3381b6f1789356666de7f': {
116 action: 'ALLOW',
117 force: false,
118 min: -2,
119 max: 2,
120 },
121 },
122 },
123 };
124
125 const expectedLabelValues = [
126 {value: -2, text: 'This shall not be merged'},
127 {value: -1, text: 'I would prefer this is not merged as is'},
128 {value: 0, text: 'No score'},
129 {value: 1, text: 'Looks good to me, but someone else must approve'},
130 {value: 2, text: 'Looks good to me, approved'},
131 ];
132
133 const expectedLabel = {
134 name: 'Code-Review',
135 values: expectedLabelValues,
136 };
137
138 assert.deepEqual(element._computeLabelValues(
139 labels['Code-Review'].values), expectedLabelValues);
140
141 assert.deepEqual(element._computeLabel(permission, labels),
142 expectedLabel);
Becky Siegel57df2232017-11-07 10:56:55 -0800143
144 permission = {
145 id: 'label-reviewDB',
146 value: {
147 label: 'reviewDB',
148 rules: {
149 'global:Project-Owners': {
150 action: 'ALLOW',
151 force: false,
152 },
153 '4c97682e6ce6b7247f3381b6f1789356666de7f': {
154 action: 'ALLOW',
155 force: false,
156 },
157 },
158 },
159 };
160
161 assert.isNotOk(element._computeLabel(permission, labels));
Becky Siegel8db12402017-08-17 16:12:53 -0700162 });
163
Becky Siegeld2c7be32017-08-23 14:07:20 -0700164 test('_computeSectionClass', () => {
Becky Siegel8db12402017-08-17 16:12:53 -0700165 let deleted = true;
Becky Siegeld2c7be32017-08-23 14:07:20 -0700166 let editing = false;
167 assert.equal(element._computeSectionClass(editing, deleted), 'deleted');
168
Becky Siegel8db12402017-08-17 16:12:53 -0700169 deleted = false;
Becky Siegeld2c7be32017-08-23 14:07:20 -0700170 assert.equal(element._computeSectionClass(editing, deleted), '');
171
172 editing = true;
173 assert.equal(element._computeSectionClass(editing, deleted), 'editing');
174
175 deleted = true;
176 assert.equal(element._computeSectionClass(editing, deleted),
177 'editing deleted');
Becky Siegel8db12402017-08-17 16:12:53 -0700178 });
179
Becky Siegeld9cc72b2017-09-07 08:35:35 -0700180 test('_computeGroupName', () => {
181 const groups = {
182 abc123: {name: 'test group'},
183 bcd234: {},
184 };
185 assert.equal(element._computeGroupName(groups, 'abc123'), 'test group');
186 assert.equal(element._computeGroupName(groups, 'bcd234'), 'bcd234');
187 });
Becky Siegeld2c7be32017-08-23 14:07:20 -0700188
Becky Siegel8db12402017-08-17 16:12:53 -0700189 test('_computeGroupsWithRules', () => {
190 const rules = [
191 {
192 id: '4c97682e6ce6b7247f3381b6f1789356666de7f',
193 value: {action: 'ALLOW', force: false},
194 },
195 {
196 id: 'global:Project-Owners',
197 value: {action: 'ALLOW', force: false},
198 },
199 ];
200 const groupsWithRules = {
201 '4c97682e6ce6b7247f3381b6f1789356666de7f': true,
202 'global:Project-Owners': true,
203 };
204 assert.deepEqual(element._computeGroupsWithRules(rules),
205 groupsWithRules);
206 });
207
208 test('_getGroupSuggestions without existing rules', done => {
209 element._groupsWithRules = {};
210
211 element._getGroupSuggestions().then(groups => {
212 assert.deepEqual(groups, [
213 {
214 name: 'Administrators',
215 value: {id: '4c97682e6ce61b7247f3381b6f1789356666de7f'},
216 }, {
217 name: 'Anonymous Users',
218 value: {id: 'global%3AAnonymous-Users'},
219 },
220 ]);
221 done();
222 });
223 });
224
225 test('_getGroupSuggestions with existing rules filters them', done => {
226 element._groupsWithRules = {
227 '4c97682e6ce61b7247f3381b6f1789356666de7f': true,
228 };
229
230 element._getGroupSuggestions().then(groups => {
231 assert.deepEqual(groups, [{
232 name: 'Anonymous Users',
233 value: {id: 'global%3AAnonymous-Users'},
234 }]);
235 done();
236 });
237 });
238
239 test('_handleRemovePermission', () => {
240 element.permission = {value: {rules: {}}};
241 element._handleRemovePermission();
242 assert.isTrue(element._deleted);
243 assert.isTrue(element.permission.value.deleted);
244 });
245
246 test('_handleUndoRemove', () => {
247 element.permission = {value: {deleted: true, rules: {}}};
248 element._handleUndoRemove();
249 assert.isFalse(element._deleted);
250 assert.isNotOk(element.permission.value.deleted);
251 });
252 });
253
254 suite('interactions', () => {
255 setup(() => {
256 sandbox.spy(element, '_computeLabel');
257 element.name = 'Priority';
258 element.section = 'refs/*';
259 element.labels = {
260 'Code-Review': {
261 values: {
262 ' 0': 'No score',
263 '-1': 'I would prefer this is not merged as is',
264 '-2': 'This shall not be merged',
265 '+1': 'Looks good to me, but someone else must approve',
266 '+2': 'Looks good to me, approved',
267 },
268 default_value: 0,
269 },
270 };
271 element.permission = {
272 id: 'label-Code-Review',
273 value: {
274 label: 'Code-Review',
275 rules: {
276 'global:Project-Owners': {
277 action: 'ALLOW',
278 force: false,
279 min: -2,
280 max: 2,
281 },
282 '4c97682e6ce6b7247f3381b6f1789356666de7f': {
283 action: 'ALLOW',
284 force: false,
285 min: -2,
286 max: 2,
287 },
288 },
289 },
290 };
291 flushAsynchronousOperations();
292 });
293
294 test('adding a rule', () => {
295 element.name = 'Priority';
296 element.section = 'refs/*';
297 const e = {
298 detail: {
299 value: {
300 id: 'newUserGroupId',
301 },
302 },
303 };
304
305 assert.equal(element._rules.length, 2);
306 assert.equal(Object.keys(element._groupsWithRules).length, 2);
307 element._handleAddRuleItem(e);
308 flushAsynchronousOperations();
309 assert.equal(element._rules.length, 3);
310 assert.equal(Object.keys(element._groupsWithRules).length, 3);
311 assert.deepEqual(element.permission.value.rules['newUserGroupId'],
312 {action: 'ALLOW', min: -2, max: 2});
313 });
314
315 test('removing the permission', () => {
Becky Siegeld2c7be32017-08-23 14:07:20 -0700316 element.editing = true;
Becky Siegel8db12402017-08-17 16:12:53 -0700317 element.name = 'Priority';
318 element.section = 'refs/*';
319
320 assert.isFalse(element.$.permission.classList.contains('deleted'));
321 assert.isFalse(element._deleted);
322 MockInteractions.tap(element.$.removeBtn);
323 assert.isTrue(element.$.permission.classList.contains('deleted'));
324 assert.isTrue(element._deleted);
325 MockInteractions.tap(element.$.undoRemoveBtn);
326 assert.isFalse(element.$.permission.classList.contains('deleted'));
327 assert.isFalse(element._deleted);
328 });
329 });
330 });
331</script>