blob: a8600c7d01cfb758a5972a42d8a3c2b4cfad9acc [file] [log] [blame]
Dmitrii Filippov06117e82020-06-25 13:26:55 +02001/**
2 * @license
Ben Rohlfs94fcbbc2022-05-27 10:45:03 +02003 * Copyright 2018 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
Dmitrii Filippov06117e82020-06-25 13:26:55 +02005 */
Frank Bordene1ba8212022-08-29 15:20:01 +02006import {assert} from '@open-wc/testing';
paladox914529e2022-03-02 15:16:35 +00007import {AccountDetailInfo, GroupId, RepoName, Timestamp} from '../api/rest-api';
Frank Bordenbe9451a2022-09-12 15:44:29 +02008import '../test/common-test-setup';
paladox914529e2022-03-02 15:16:35 +00009import {AdminNavLinksOption, getAdminLinks} from './admin-nav-util';
Dmitrii Filippov06117e82020-06-25 13:26:55 +020010
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010011suite('gr-admin-nav-behavior tests', () => {
paladox914529e2022-03-02 15:16:35 +000012 let capabilityStub: sinon.SinonStub;
13 let menuLinkStub: sinon.SinonStub;
Becky Siegelcad4bf82018-04-18 16:43:05 +020014
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010015 setup(() => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010016 capabilityStub = sinon.stub();
17 menuLinkStub = sinon.stub().returns([]);
18 });
19
paladox914529e2022-03-02 15:16:35 +000020 const testAdminLinks = async (
21 account: AccountDetailInfo | undefined,
22 options: AdminNavLinksOption | undefined,
23 expected: any
24 ) => {
25 const res = await getAdminLinks(
26 account,
27 capabilityStub,
28 menuLinkStub,
29 options
30 );
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010031
Chris Pouceta2e173e2021-08-31 01:04:04 +000032 assert.equal(expected.totalLength, res.links.length);
33 assert.equal(res.links[0].name, 'Repositories');
34 // Repos
35 if (expected.groupListShown) {
36 assert.equal(res.links[1].name, 'Groups');
37 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010038
Chris Pouceta2e173e2021-08-31 01:04:04 +000039 if (expected.pluginListShown) {
40 assert.equal(res.links[2].name, 'Plugins');
41 assert.isNotOk(res.links[2].subsection);
42 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010043
Chris Pouceta2e173e2021-08-31 01:04:04 +000044 if (expected.projectPageShown) {
45 assert.isOk(res.links[0].subsection);
paladox914529e2022-03-02 15:16:35 +000046 assert.equal(res.links[0].subsection!.children!.length, 6);
Chris Pouceta2e173e2021-08-31 01:04:04 +000047 } else {
48 assert.isNotOk(res.links[0].subsection);
49 }
50 // Groups
51 if (expected.groupPageShown) {
52 assert.isOk(res.links[1].subsection);
paladox914529e2022-03-02 15:16:35 +000053 assert.equal(
54 res.links[1].subsection!.children!.length,
55 expected.groupSubpageLength
56 );
57 } else if (expected.totalLength > 1) {
Chris Pouceta2e173e2021-08-31 01:04:04 +000058 assert.isNotOk(res.links[1].subsection);
59 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010060
Chris Pouceta2e173e2021-08-31 01:04:04 +000061 if (expected.pluginGeneratedLinks) {
62 for (const link of expected.pluginGeneratedLinks) {
paladox914529e2022-03-02 15:16:35 +000063 const linkMatch = res.links.find(
64 l => l.url === link.url && l.name === link.text
65 );
Chris Pouceta2e173e2021-08-31 01:04:04 +000066 assert.isTrue(!!linkMatch);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010067
Chris Pouceta2e173e2021-08-31 01:04:04 +000068 // External links should open in new tab.
69 if (link.url[0] !== '/') {
paladox914529e2022-03-02 15:16:35 +000070 assert.equal(linkMatch!.target, '_blank');
Chris Pouceta2e173e2021-08-31 01:04:04 +000071 } else {
paladox914529e2022-03-02 15:16:35 +000072 assert.isNotOk(linkMatch!.target);
Chris Pouceta2e173e2021-08-31 01:04:04 +000073 }
74 }
75 }
76
77 // Current section
78 if (expected.projectPageShown || expected.groupPageShown) {
79 assert.isOk(res.expandedSection);
paladox914529e2022-03-02 15:16:35 +000080 assert.isOk(res.expandedSection!.children);
Chris Pouceta2e173e2021-08-31 01:04:04 +000081 } else {
82 assert.isNotOk(res.expandedSection);
83 }
84 if (expected.projectPageShown) {
paladox914529e2022-03-02 15:16:35 +000085 assert.equal(res.expandedSection!.name, 'my-repo');
86 assert.equal(res.expandedSection!.children!.length, 6);
Chris Pouceta2e173e2021-08-31 01:04:04 +000087 } else if (expected.groupPageShown) {
paladox914529e2022-03-02 15:16:35 +000088 assert.equal(res.expandedSection!.name, 'my-group');
89 assert.equal(
90 res.expandedSection!.children!.length,
91 expected.groupSubpageLength
92 );
Chris Pouceta2e173e2021-08-31 01:04:04 +000093 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010094 };
95
96 suite('logged out', () => {
paladox914529e2022-03-02 15:16:35 +000097 let account: AccountDetailInfo;
98 let expected: any;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010099
100 setup(() => {
101 expected = {
102 groupListShown: false,
103 groupPageShown: false,
104 pluginListShown: false,
105 };
106 });
107
Chris Pouceta2e173e2021-08-31 01:04:04 +0000108 test('without a specific repo or group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100109 let options;
110 expected = Object.assign(expected, {
111 totalLength: 1,
112 projectPageShown: false,
113 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000114 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100115 });
116
Chris Pouceta2e173e2021-08-31 01:04:04 +0000117 test('with a repo', async () => {
paladox914529e2022-03-02 15:16:35 +0000118 const options = {repoName: 'my-repo' as RepoName};
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100119 expected = Object.assign(expected, {
120 totalLength: 1,
121 projectPageShown: true,
122 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000123 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100124 });
125
Chris Pouceta2e173e2021-08-31 01:04:04 +0000126 test('with plugin generated links', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100127 let options;
128 const generatedLinks = [
129 {text: 'internal link text', url: '/internal/link/url'},
130 {text: 'external link text', url: 'http://external/link/url'},
131 ];
132 menuLinkStub.returns(generatedLinks);
133 expected = Object.assign(expected, {
134 totalLength: 3,
135 projectPageShown: false,
136 pluginGeneratedLinks: generatedLinks,
137 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000138 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100139 });
140 });
141
142 suite('no plugin capability logged in', () => {
143 const account = {
144 name: 'test-user',
paladox914529e2022-03-02 15:16:35 +0000145 registered_on: '' as Timestamp,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100146 };
paladox914529e2022-03-02 15:16:35 +0000147 let expected: any;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100148
149 setup(() => {
150 expected = {
151 totalLength: 2,
152 pluginListShown: false,
153 };
154 capabilityStub.returns(Promise.resolve({}));
155 });
156
Chris Pouceta2e173e2021-08-31 01:04:04 +0000157 test('without a specific project or group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100158 let options;
159 expected = Object.assign(expected, {
160 projectPageShown: false,
161 groupListShown: true,
162 groupPageShown: false,
163 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000164 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100165 });
166
Chris Pouceta2e173e2021-08-31 01:04:04 +0000167 test('with a repo', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100168 const account = {
169 name: 'test-user',
paladox914529e2022-03-02 15:16:35 +0000170 registered_on: '' as Timestamp,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100171 };
paladox914529e2022-03-02 15:16:35 +0000172 const options = {repoName: 'my-repo' as RepoName};
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100173 expected = Object.assign(expected, {
174 projectPageShown: true,
175 groupListShown: true,
176 groupPageShown: false,
177 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000178 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100179 });
180 });
181
182 suite('view plugin capability logged in', () => {
183 const account = {
184 name: 'test-user',
paladox914529e2022-03-02 15:16:35 +0000185 registered_on: '' as Timestamp,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100186 };
paladox914529e2022-03-02 15:16:35 +0000187 let expected: any;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100188
189 setup(() => {
190 capabilityStub.returns(Promise.resolve({viewPlugins: true}));
191 expected = {
192 totalLength: 3,
193 groupListShown: true,
194 pluginListShown: true,
195 };
196 });
197
Chris Pouceta2e173e2021-08-31 01:04:04 +0000198 test('without a specific repo or group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100199 let options;
200 expected = Object.assign(expected, {
201 projectPageShown: false,
202 groupPageShown: false,
203 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000204 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100205 });
206
Chris Pouceta2e173e2021-08-31 01:04:04 +0000207 test('with a repo', async () => {
paladox914529e2022-03-02 15:16:35 +0000208 const options = {repoName: 'my-repo' as RepoName};
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100209 expected = Object.assign(expected, {
210 projectPageShown: true,
211 groupPageShown: false,
212 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000213 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100214 });
215
Chris Pouceta2e173e2021-08-31 01:04:04 +0000216 test('admin with internal group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100217 const options = {
paladox914529e2022-03-02 15:16:35 +0000218 groupId: 'a15262' as GroupId,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100219 groupName: 'my-group',
220 groupIsInternal: true,
221 isAdmin: true,
222 groupOwner: false,
223 };
224 expected = Object.assign(expected, {
225 projectPageShown: false,
226 groupPageShown: true,
227 groupSubpageLength: 2,
228 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000229 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100230 });
231
Chris Pouceta2e173e2021-08-31 01:04:04 +0000232 test('group owner with internal group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100233 const options = {
paladox914529e2022-03-02 15:16:35 +0000234 groupId: 'a15262' as GroupId,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100235 groupName: 'my-group',
236 groupIsInternal: true,
237 isAdmin: false,
238 groupOwner: true,
239 };
240 expected = Object.assign(expected, {
241 projectPageShown: false,
242 groupPageShown: true,
243 groupSubpageLength: 2,
244 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000245 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100246 });
247
Chris Pouceta2e173e2021-08-31 01:04:04 +0000248 test('non owner or admin with internal group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100249 const options = {
paladox914529e2022-03-02 15:16:35 +0000250 groupId: 'a15262' as GroupId,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100251 groupName: 'my-group',
252 groupIsInternal: true,
253 isAdmin: false,
254 groupOwner: false,
255 };
256 expected = Object.assign(expected, {
257 projectPageShown: false,
258 groupPageShown: true,
259 groupSubpageLength: 1,
260 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000261 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100262 });
263
Chris Pouceta2e173e2021-08-31 01:04:04 +0000264 test('admin with external group', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100265 const options = {
paladox914529e2022-03-02 15:16:35 +0000266 groupId: 'a15262' as GroupId,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100267 groupName: 'my-group',
268 groupIsInternal: false,
269 isAdmin: true,
270 groupOwner: true,
271 };
272 expected = Object.assign(expected, {
273 projectPageShown: false,
274 groupPageShown: true,
275 groupSubpageLength: 0,
276 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000277 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100278 });
279 });
280
281 suite('view plugin screen with plugin capability', () => {
282 const account = {
283 name: 'test-user',
paladox914529e2022-03-02 15:16:35 +0000284 registered_on: '' as Timestamp,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100285 };
paladox914529e2022-03-02 15:16:35 +0000286 let expected: any;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100287
288 setup(() => {
289 capabilityStub.returns(Promise.resolve({pluginCapability: true}));
290 expected = {};
291 });
292
Chris Pouceta2e173e2021-08-31 01:04:04 +0000293 test('with plugin with capabilities', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100294 let options;
295 const generatedLinks = [
296 {text: 'without capability', url: '/without'},
paladox914529e2022-03-02 15:16:35 +0000297 {text: 'with capability', url: '/with', capability: 'pluginCapability'},
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100298 ];
299 menuLinkStub.returns(generatedLinks);
300 expected = Object.assign(expected, {
301 totalLength: 4,
302 pluginGeneratedLinks: generatedLinks,
303 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000304 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100305 });
306 });
307
308 suite('view plugin screen without plugin capability', () => {
309 const account = {
310 name: 'test-user',
paladox914529e2022-03-02 15:16:35 +0000311 registered_on: '' as Timestamp,
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100312 };
paladox914529e2022-03-02 15:16:35 +0000313 let expected: any;
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100314
315 setup(() => {
316 capabilityStub.returns(Promise.resolve({}));
317 expected = {};
318 });
319
Chris Pouceta2e173e2021-08-31 01:04:04 +0000320 test('with plugin with capabilities', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100321 let options;
322 const generatedLinks = [
323 {text: 'without capability', url: '/without'},
paladox914529e2022-03-02 15:16:35 +0000324 {text: 'with capability', url: '/with', capability: 'pluginCapability'},
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100325 ];
326 menuLinkStub.returns(generatedLinks);
327 expected = Object.assign(expected, {
328 totalLength: 3,
329 pluginGeneratedLinks: [generatedLinks[0]],
330 });
Chris Pouceta2e173e2021-08-31 01:04:04 +0000331 await testAdminLinks(account, options, expected);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100332 });
333 });
334});