3041acf5b2cd9e783a4241190862ec27cbdccd2a
[arvados-workbench2.git] / cypress / integration / collection.spec.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 describe('Collection panel tests', function () {
6     let activeUser;
7     let adminUser;
8
9     before(function () {
10         // Only set up common users once. These aren't set up as aliases because
11         // aliases are cleaned up after every test. Also it doesn't make sense
12         // to set the same users on beforeEach() over and over again, so we
13         // separate a little from Cypress' 'Best Practices' here.
14         cy.getUser('admin', 'Admin', 'User', true, true)
15             .as('adminUser').then(function () {
16                 adminUser = this.adminUser;
17             }
18             );
19         cy.getUser('collectionuser1', 'Collection', 'User', false, true)
20             .as('activeUser').then(function () {
21                 activeUser = this.activeUser;
22             }
23             );
24     });
25
26     beforeEach(function () {
27         cy.clearCookies();
28         cy.clearLocalStorage();
29     });
30
31     it('uses the property editor with vocabulary terms', function () {
32         cy.createCollection(adminUser.token, {
33             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
34             owner_uuid: activeUser.user.uuid,
35             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"
36         })
37             .as('testCollection').then(function () {
38                 cy.loginAs(activeUser);
39                 cy.doSearch(`${this.testCollection.uuid}`);
40
41                 // Key: Color (IDTAGCOLORS) - Value: Magenta (IDVALCOLORS3)
42                 cy.get('[data-cy=resource-properties-form]').within(() => {
43                     cy.get('[data-cy=property-field-key]').within(() => {
44                         cy.get('input').type('Color');
45                     });
46                     cy.get('[data-cy=property-field-value]').within(() => {
47                         cy.get('input').type('Magenta');
48                     });
49                     cy.root().submit();
50                 });
51                 // Confirm proper vocabulary labels are displayed on the UI.
52                 cy.get('[data-cy=collection-properties-panel]')
53                     .should('contain', 'Color')
54                     .and('contain', 'Magenta');
55                 // Confirm proper vocabulary IDs were saved on the backend.
56                 cy.doRequest('GET', `/arvados/v1/collections/${this.testCollection.uuid}`)
57                     .its('body').as('collection')
58                     .then(function () {
59                         expect(this.collection.properties).to.deep.equal(
60                             { IDTAGCOLORS: 'IDVALCOLORS3' });
61                     });
62             });
63     });
64
65     it('shows collection by URL', function () {
66         cy.loginAs(activeUser);
67         [true, false].map(function (isWritable) {
68             // Using different file names to avoid test flakyness: the second iteration
69             // on this loop may pass an assertion from the first iteration by looking
70             // for the same file name.
71             const fileName = isWritable ? 'bar' : 'foo';
72             cy.createGroup(adminUser.token, {
73                 name: 'Shared project',
74                 group_class: 'project',
75             }).as('sharedGroup').then(function () {
76                 // Creates the collection using the admin token so we can set up
77                 // a bogus manifest text without block signatures.
78                 cy.createCollection(adminUser.token, {
79                     name: 'Test collection',
80                     owner_uuid: this.sharedGroup.uuid,
81                     properties: { someKey: 'someValue' },
82                     manifest_text: `. 37b51d194a7513e45b56f6524f2d51f2+3 0:3:${fileName}\n`
83                 })
84                     .as('testCollection').then(function () {
85                         // Share the group with active user.
86                         cy.createLink(adminUser.token, {
87                             name: isWritable ? 'can_write' : 'can_read',
88                             link_class: 'permission',
89                             head_uuid: this.sharedGroup.uuid,
90                             tail_uuid: activeUser.user.uuid
91                         })
92                         cy.doSearch(`${this.testCollection.uuid}`);
93
94                         // Check that name & uuid are correct.
95                         cy.get('[data-cy=collection-info-panel]')
96                             .should('contain', this.testCollection.name)
97                             .and('contain', this.testCollection.uuid)
98                             .and('not.contain', 'This is an old version');
99                         // Check for the read-only icon
100                         cy.get('[data-cy=read-only-icon]').should(`${isWritable ? 'not.' : ''}exist`);
101                         // Check that both read and write operations are available on
102                         // the 'More options' menu.
103                         cy.get('[data-cy=collection-panel-options-btn]')
104                             .click()
105                         cy.get('[data-cy=context-menu]')
106                             .should('contain', 'Add to favorites')
107                             .and(`${isWritable ? '' : 'not.'}contain`, 'Edit collection');
108                         cy.get('body').click(); // Collapse the menu avoiding details panel expansion
109                         cy.get('[data-cy=collection-properties-panel]')
110                             .should('contain', 'someKey')
111                             .and('contain', 'someValue')
112                             .and('not.contain', 'anotherKey')
113                             .and('not.contain', 'anotherValue')
114                         if (isWritable === true) {
115                             // Check that properties can be added.
116                             cy.get('[data-cy=resource-properties-form]').within(() => {
117                                 cy.get('[data-cy=property-field-key]').within(() => {
118                                     cy.get('input').type('anotherKey');
119                                 });
120                                 cy.get('[data-cy=property-field-value]').within(() => {
121                                     cy.get('input').type('anotherValue');
122                                 });
123                                 cy.root().submit();
124                             })
125                             cy.get('[data-cy=collection-properties-panel]')
126                                 .should('contain', 'anotherKey')
127                                 .and('contain', 'anotherValue')
128                         } else {
129                             // Properties form shouldn't be displayed.
130                             cy.get('[data-cy=resource-properties-form]').should('not.exist');
131                         }
132                         // Check that the file listing show both read & write operations
133                         cy.get('[data-cy=collection-files-panel]').within(() => {
134                             cy.root().should('contain', fileName);
135                             if (isWritable) {
136                                 cy.get('[data-cy=upload-button]')
137                                     .should(`${isWritable ? '' : 'not.'}contain`, 'Upload data');
138                             }
139                         });
140                         cy.get('[data-cy=collection-files-panel]')
141                             .contains(fileName).rightclick({ force: true });
142                         cy.get('[data-cy=context-menu]')
143                             .should('contain', 'Download')
144                             .and('contain', 'Open in new tab')
145                             .and('contain', 'Copy to clipboard')
146                             .and(`${isWritable ? '' : 'not.'}contain`, 'Rename')
147                             .and(`${isWritable ? '' : 'not.'}contain`, 'Remove');
148                         cy.get('body').click(); // Collapse the menu
149                         // Hamburger 'more options' menu button
150                         cy.get('[data-cy=collection-files-panel-options-btn]')
151                             .click()
152                         cy.get('[data-cy=context-menu]')
153                             .should('contain', 'Select all')
154                             .click()
155                         cy.get('[data-cy=collection-files-panel-options-btn]')
156                             .click()
157                         cy.get('[data-cy=context-menu]')
158                             // .should('contain', 'Download selected')
159                             .should(`${isWritable ? '' : 'not.'}contain`, 'Remove selected')
160                         cy.get('body').click(); // Collapse the menu
161                         // File item 'more options' button
162                         cy.get('[data-cy=file-item-options-btn')
163                             .click()
164                         cy.get('[data-cy=context-menu]')
165                             .should('contain', 'Download')
166                             .and(`${isWritable ? '' : 'not.'}contain`, 'Remove');
167                         cy.get('body').click(); // Collapse the menu
168                     })
169             })
170         })
171     })
172
173     it('renames a file using valid names', function () {
174         function eachPair(lst, func){
175             for(var i=0; i < lst.length - 1; i++){
176                 func(lst[i], lst[i + 1])
177             }
178         }
179         // Creates the collection using the admin token so we can set up
180         // a bogus manifest text without block signatures.
181         cy.createCollection(adminUser.token, {
182             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
183             owner_uuid: activeUser.user.uuid,
184             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"
185         })
186             .as('testCollection').then(function () {
187                 cy.loginAs(activeUser);
188                 cy.doSearch(`${this.testCollection.uuid}`);
189
190                 const names = [
191                     'bar', // initial name already set
192                     '&',
193                     'foo',
194                     '&amp;',
195                     'I ❤️ ⛵️',
196                     '...',
197                     '#..',
198                     'some name with whitespaces',
199                     'some name with #2',
200                     'is this name legal? I hope it is',
201                     'some_file.pdf#',
202                     'some_file.pdf?',
203                     '?some_file.pdf',
204                     'some%file.pdf',
205                     'some%2Ffile.pdf',
206                     'some%22file.pdf',
207                     'some%20file.pdf',
208                     "G%C3%BCnter's%20file.pdf",
209                     'bar' // make sure we can go back to the original name as a last step
210                 ];
211                 eachPair(names, (from, to) => {
212                     cy.get('[data-cy=collection-files-panel]')
213                         .contains(`${from}`).rightclick();
214                     cy.get('[data-cy=context-menu]')
215                         .contains('Rename')
216                         .click();
217                     cy.get('[data-cy=form-dialog]')
218                         .should('contain', 'Rename')
219                         .within(() => {
220                             cy.get('input').type(`{selectall}{backspace}${to}`);
221                         });
222                     cy.get('[data-cy=form-submit-btn]').click();
223                     cy.get('[data-cy=collection-files-panel]')
224                         .should('not.contain', `${from}`)
225                         .and('contain', `${to}`);
226                 })
227             });
228     });
229
230     it('renames a file to a different directory', function () {
231         // Creates the collection using the admin token so we can set up
232         // a bogus manifest text without block signatures.
233         cy.createCollection(adminUser.token, {
234             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
235             owner_uuid: activeUser.user.uuid,
236             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"
237         })
238             .as('testCollection').then(function () {
239                 cy.loginAs(activeUser);
240                 cy.doSearch(`${this.testCollection.uuid}`);
241
242                 // Rename 'bar' to 'subdir/foo'
243                 cy.get('[data-cy=collection-files-panel]')
244                     .contains('bar').rightclick();
245                 cy.get('[data-cy=context-menu]')
246                     .contains('Rename')
247                     .click();
248                 cy.get('[data-cy=form-dialog]')
249                     .should('contain', 'Rename')
250                     .within(() => {
251                         cy.get('input').type(`{selectall}{backspace}subdir/foo`);
252                     });
253                 cy.get('[data-cy=form-submit-btn]').click();
254                 cy.get('[data-cy=collection-files-panel]')
255                     .should('not.contain', 'bar')
256                     .and('contain', 'subdir');
257                 // Look for the "arrow icon" and expand the "subdir" directory.
258                 cy.get('[data-cy=virtual-file-tree] > div > i').click();
259                 // Rename 'subdir/foo' to 'baz'
260                 cy.get('[data-cy=collection-files-panel]')
261                     .contains('foo').rightclick();
262                 cy.get('[data-cy=context-menu]')
263                     .contains('Rename')
264                     .click();
265                 cy.get('[data-cy=form-dialog]')
266                     .should('contain', 'Rename')
267                     .within(() => {
268                         cy.get('input')
269                             .should('have.value', 'subdir/foo')
270                             .type(`{selectall}{backspace}baz`);
271                     });
272                 cy.get('[data-cy=form-submit-btn]').click();
273                 cy.get('[data-cy=collection-files-panel]')
274                     .should('contain', 'subdir') // empty dir kept
275                     .and('contain', 'baz');
276             });
277     });
278
279     it('tries to rename a file with illegal names', function () {
280         // Creates the collection using the admin token so we can set up
281         // a bogus manifest text without block signatures.
282         cy.createCollection(adminUser.token, {
283             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
284             owner_uuid: activeUser.user.uuid,
285             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"
286         })
287             .as('testCollection').then(function () {
288                 cy.loginAs(activeUser);
289                 cy.doSearch(`${this.testCollection.uuid}`);
290
291                 const illegalNamesFromUI = [
292                     ['.', "Name cannot be '.' or '..'"],
293                     ['..', "Name cannot be '.' or '..'"],
294                     ['', 'This field is required'],
295                     [' ', 'Leading/trailing whitespaces not allowed'],
296                     [' foo', 'Leading/trailing whitespaces not allowed'],
297                     ['foo ', 'Leading/trailing whitespaces not allowed'],
298                     ['//foo', 'Empty dir name not allowed']
299                 ]
300                 illegalNamesFromUI.forEach(([name, errMsg]) => {
301                     cy.get('[data-cy=collection-files-panel]')
302                         .contains('bar').rightclick();
303                     cy.get('[data-cy=context-menu]')
304                         .contains('Rename')
305                         .click();
306                     cy.get('[data-cy=form-dialog]')
307                         .should('contain', 'Rename')
308                         .within(() => {
309                             cy.get('input').type(`{selectall}{backspace}${name}`);
310                         });
311                     cy.get('[data-cy=form-dialog]')
312                         .should('contain', 'Rename')
313                         .within(() => {
314                             cy.contains(`${errMsg}`);
315                         });
316                     cy.get('[data-cy=form-cancel-btn]').click();
317                 })
318             });
319     });
320
321     it('can correctly display old versions', function () {
322         const colName = `Versioned Collection ${Math.floor(Math.random() * 999999)}`;
323         let colUuid = '';
324         let oldVersionUuid = '';
325         // Make sure no other collections with this name exist
326         cy.doRequest('GET', '/arvados/v1/collections', null, {
327             filters: `[["name", "=", "${colName}"]]`,
328             include_old_versions: true
329         })
330             .its('body.items').as('collections')
331             .then(function () {
332                 expect(this.collections).to.be.empty;
333             });
334         // Creates the collection using the admin token so we can set up
335         // a bogus manifest text without block signatures.
336         cy.createCollection(adminUser.token, {
337             name: colName,
338             owner_uuid: activeUser.user.uuid,
339             preserve_version: true,
340             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"
341         })
342             .as('originalVersion').then(function () {
343                 // Change the file name to create a new version.
344                 cy.updateCollection(adminUser.token, this.originalVersion.uuid, {
345                     manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:foo\n"
346                 })
347                 colUuid = this.originalVersion.uuid;
348             });
349         // Confirm that there are 2 versions of the collection
350         cy.doRequest('GET', '/arvados/v1/collections', null, {
351             filters: `[["name", "=", "${colName}"]]`,
352             include_old_versions: true
353         })
354             .its('body.items').as('collections')
355             .then(function () {
356                 expect(this.collections).to.have.lengthOf(2);
357                 this.collections.map(function (aCollection) {
358                     expect(aCollection.current_version_uuid).to.equal(colUuid);
359                     if (aCollection.uuid !== aCollection.current_version_uuid) {
360                         oldVersionUuid = aCollection.uuid;
361                     }
362                 });
363                 // Check the old version displays as what it is.
364                 cy.loginAs(activeUser)
365                 cy.doSearch(`${oldVersionUuid}`);
366
367                 cy.get('[data-cy=collection-info-panel]').should('contain', 'This is an old version');
368                 cy.get('[data-cy=read-only-icon]').should('exist');
369                 cy.get('[data-cy=collection-info-panel]').should('contain', colName);
370                 cy.get('[data-cy=collection-files-panel]').should('contain', 'bar');
371             });
372     });
373
374     it('uses the collection version browser to view a previous version', function () {
375         const colName = `Test Collection ${Math.floor(Math.random() * 999999)}`;
376
377         // Creates the collection using the admin token so we can set up
378         // a bogus manifest text without block signatures.
379         cy.createCollection(adminUser.token, {
380             name: colName,
381             owner_uuid: activeUser.user.uuid,
382             preserve_version: true,
383             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:foo 0:3:bar\n"
384         })
385             .as('collection').then(function () {
386                 // Visit collection, check basic information
387                 cy.loginAs(activeUser)
388                 cy.doSearch(`${this.collection.uuid}`);
389
390                 cy.get('[data-cy=collection-info-panel]').should('not.contain', 'This is an old version');
391                 cy.get('[data-cy=read-only-icon]').should('not.exist');
392                 cy.get('[data-cy=collection-version-number]').should('contain', '1');
393                 cy.get('[data-cy=collection-info-panel]').should('contain', colName);
394                 cy.get('[data-cy=collection-files-panel]').should('contain', 'foo').and('contain', 'bar');
395
396                 // Modify collection, expect version number change
397                 cy.get('[data-cy=collection-files-panel]').contains('foo').rightclick();
398                 cy.get('[data-cy=context-menu]').contains('Remove').click();
399                 cy.get('[data-cy=confirmation-dialog]').should('contain', 'Removing file');
400                 cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
401                 cy.get('[data-cy=collection-version-number]').should('contain', '2');
402                 cy.get('[data-cy=collection-files-panel]').should('not.contain', 'foo').and('contain', 'bar');
403
404                 // Click on version number, check version browser. Click on past version.
405                 cy.get('[data-cy=collection-version-browser]').should('not.exist');
406                 cy.get('[data-cy=collection-version-number]').contains('2').click();
407                 cy.get('[data-cy=collection-version-browser]')
408                     .should('contain', 'Nr').and('contain', 'Size').and('contain', 'Date')
409                     .within(() => {
410                         // Version 1: 6 bytes in size
411                         cy.get('[data-cy=collection-version-browser-select-1]')
412                             .should('contain', '1').and('contain', '6 B');
413                         // Version 2: 3 bytes in size (one file removed)
414                         cy.get('[data-cy=collection-version-browser-select-2]')
415                             .should('contain', '2').and('contain', '3 B');
416                         cy.get('[data-cy=collection-version-browser-select-3]')
417                             .should('not.exist');
418                         cy.get('[data-cy=collection-version-browser-select-1]')
419                             .click();
420                     });
421                 cy.get('[data-cy=collection-info-panel]').should('contain', 'This is an old version');
422                 cy.get('[data-cy=read-only-icon]').should('exist');
423                 cy.get('[data-cy=collection-version-number]').should('contain', '1');
424                 cy.get('[data-cy=collection-info-panel]').should('contain', colName);
425                 cy.get('[data-cy=collection-files-panel]')
426                     .should('contain', 'foo').and('contain', 'bar');
427
428                 // Check that only old collection action are available on context menu
429                 cy.get('[data-cy=collection-panel-options-btn]').click();
430                 cy.get('[data-cy=context-menu]')
431                     .should('contain', 'Restore version')
432                     .and('not.contain', 'Add to favorites');
433                 cy.get('body').click(); // Collapse the menu avoiding details panel expansion
434
435                 // Click on "head version" link, confirm that it's the latest version.
436                 cy.get('[data-cy=collection-info-panel]').contains('head version').click();
437                 cy.get('[data-cy=collection-info-panel]')
438                     .should('not.contain', 'This is an old version');
439                 cy.get('[data-cy=read-only-icon]').should('not.exist');
440                 cy.get('[data-cy=collection-version-number]').should('contain', '2');
441                 cy.get('[data-cy=collection-info-panel]').should('contain', colName);
442                 cy.get('[data-cy=collection-files-panel]').
443                     should('not.contain', 'foo').and('contain', 'bar');
444
445                 // Check that old collection action isn't available on context menu
446                 cy.get('[data-cy=collection-panel-options-btn]').click()
447                 cy.get('[data-cy=context-menu]').should('not.contain', 'Restore version')
448                 cy.get('body').click(); // Collapse the menu avoiding details panel expansion
449
450                 // Make another change, confirm new version.
451                 cy.get('[data-cy=collection-panel-options-btn]').click();
452                 cy.get('[data-cy=context-menu]').contains('Edit collection').click();
453                 cy.get('[data-cy=form-dialog]')
454                     .should('contain', 'Edit Collection')
455                     .within(() => {
456                         // appends some text
457                         cy.get('input').first().type(' renamed');
458                     });
459                 cy.get('[data-cy=form-submit-btn]').click();
460                 cy.get('[data-cy=collection-info-panel]')
461                     .should('not.contain', 'This is an old version');
462                 cy.get('[data-cy=read-only-icon]').should('not.exist');
463                 cy.get('[data-cy=collection-version-number]').should('contain', '3');
464                 cy.get('[data-cy=collection-info-panel]').should('contain', colName + ' renamed');
465                 cy.get('[data-cy=collection-files-panel]')
466                     .should('not.contain', 'foo').and('contain', 'bar');
467                 cy.get('[data-cy=collection-version-browser-select-3]')
468                     .should('contain', '3').and('contain', '3 B');
469
470                 // Check context menus on version browser
471                 cy.get('[data-cy=collection-version-browser-select-3]').rightclick()
472                 cy.get('[data-cy=context-menu]')
473                     .should('contain', 'Add to favorites')
474                     .and('contain', 'Make a copy')
475                     .and('contain', 'Edit collection');
476                 cy.get('body').click();
477                 // (and now an old version...)
478                 cy.get('[data-cy=collection-version-browser-select-1]').rightclick()
479                 cy.get('[data-cy=context-menu]')
480                     .should('not.contain', 'Add to favorites')
481                     .and('contain', 'Make a copy')
482                     .and('not.contain', 'Edit collection');
483                 cy.get('body').click();
484
485                 // Restore first version
486                 cy.get('[data-cy=collection-version-browser]').within(() => {
487                     cy.get('[data-cy=collection-version-browser-select-1]').click();
488                 });
489                 cy.get('[data-cy=collection-panel-options-btn]').click()
490                 cy.get('[data-cy=context-menu]').contains('Restore version').click();
491                 cy.get('[data-cy=confirmation-dialog]').should('contain', 'Restore version');
492                 cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
493                 cy.get('[data-cy=collection-info-panel]')
494                     .should('not.contain', 'This is an old version');
495                 cy.get('[data-cy=collection-version-number]').should('contain', '4');
496                 cy.get('[data-cy=collection-info-panel]').should('contain', colName);
497                 cy.get('[data-cy=collection-files-panel]')
498                     .should('contain', 'foo').and('contain', 'bar');
499             });
500     });
501
502     it('creates new collection on home project', function () {
503         cy.loginAs(activeUser);
504         cy.doSearch(`${activeUser.user.uuid}`);
505         cy.get('[data-cy=breadcrumb-first]').should('contain', 'Projects');
506         cy.get('[data-cy=breadcrumb-last]').should('not.exist');
507         // Create new collection
508         cy.get('[data-cy=side-panel-button]').click();
509         cy.get('[data-cy=side-panel-new-collection]').click();
510         const collName = `Test collection (${Math.floor(999999 * Math.random())})`;
511         cy.get('[data-cy=form-dialog]')
512             .should('contain', 'New collection')
513             .within(() => {
514                 cy.get('[data-cy=parent-field]').within(() => {
515                     cy.get('input').should('have.value', 'Home project');
516                 })
517                 cy.get('[data-cy=name-field]').within(() => {
518                     cy.get('input').type(collName);
519                 })
520             })
521         cy.get('[data-cy=form-submit-btn]').click();
522         // Confirm that the user was taken to the newly created thing
523         cy.get('[data-cy=form-dialog]').should('not.exist');
524         cy.get('[data-cy=breadcrumb-first]').should('contain', 'Projects');
525         cy.get('[data-cy=breadcrumb-last]').should('contain', collName);
526     });
527 })