424c2bad07bc7ff878be02cb44e7f9ba6e67f860
[arvados-workbench2.git] / cypress / integration / collection-panel.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.only('shows collection by URL', function() {
32         cy.loginAs(activeUser);
33         [true, false].map(function(isWritable) {
34             cy.createGroup(adminUser.token, {
35                 name: 'Shared project',
36                 group_class: 'project',
37             }).as('sharedGroup').then(function() {
38                 // Creates the collection using the admin token so we can set up
39                 // a bogus manifest text without block signatures.
40                 cy.createCollection(adminUser.token, {
41                     name: 'Test collection',
42                     owner_uuid: this.sharedGroup.uuid,
43                     properties: {someKey: 'someValue'},
44                     manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
45                 .as('testCollection').then(function() {
46                     // Share the group with active user.
47                     cy.createLink(adminUser.token, {
48                         name: isWritable ? 'can_write' : 'can_read',
49                         link_class: 'permission',
50                         head_uuid: this.sharedGroup.uuid,
51                         tail_uuid: activeUser.user.uuid
52                     })
53                     cy.visit(`/collections/${this.testCollection.uuid}`);
54
55                     cy.get('[data-cy=linear-progress]').should('exist');
56                     cy.get('[data-cy=linear-progress]').should('not.exist');
57
58                     // Check that name & uuid are correct.
59                     cy.get('[data-cy=collection-info-panel]')
60                         .should('contain', this.testCollection.name)
61                         .and('contain', this.testCollection.uuid)
62                         .and('not.contain', 'This is an old version');
63                     // Check for the read-only icon
64                     cy.get('[data-cy=read-only-icon]').should(`${isWritable ? 'not.' : ''}exist`);
65                     // Check that both read and write operations are available on
66                     // the 'More options' menu.
67                     cy.get('[data-cy=collection-panel-options-btn]')
68                         .click()
69                     cy.get('[data-cy=context-menu]')
70                         .should('contain', 'Add to favorites')
71                         .and(`${isWritable ? '' : 'not.'}contain`, 'Edit collection');
72                     cy.get('body').click(); // Collapse the menu avoiding details panel expansion
73                     cy.get('[data-cy=collection-properties-panel]')
74                         .should('contain', 'someKey')
75                         .and('contain', 'someValue')
76                         .and('not.contain', 'anotherKey')
77                         .and('not.contain', 'anotherValue')
78                     if (isWritable === true) {
79                         // Check that properties can be added.
80                         cy.get('[data-cy=collection-properties-form]').within(() => {
81                             cy.get('[data-cy=property-field-key]').within(() => {
82                                 cy.get('input').type('anotherKey');
83                             });
84                             cy.get('[data-cy=property-field-value]').within(() => {
85                                 cy.get('input').type('anotherValue');
86                             });
87                             cy.root().submit();
88                         })
89                         cy.get('[data-cy=collection-properties-panel]')
90                             .should('contain', 'anotherKey')
91                             .and('contain', 'anotherValue')
92                     } else {
93                         // Properties form shouldn't be displayed.
94                         cy.get('[data-cy=collection-properties-form]').should('not.exist');
95                     }
96                     // Check that the file listing show both read & write operations
97                     cy.get('[data-cy=collection-files-panel]').within(() => {
98                         cy.root().should('contain', 'bar');
99                         if (isWritable) {
100                             cy.get('[data-cy=upload-button]')
101                                 .should(`${isWritable ? '' : 'not.'}contain`, 'Upload data');
102                         }
103                     });
104                     cy.get('[data-cy=collection-files-panel]')
105                         .contains('bar').rightclick();
106                     cy.get('[data-cy=context-menu]')
107                         .should('contain', 'Download')
108                         .and('contain', 'Open in new tab')
109                         .and('contain', 'Copy to clipboard')
110                         .and(`${isWritable ? '' : 'not.'}contain`, 'Rename')
111                         .and(`${isWritable ? '' : 'not.'}contain`, 'Remove');
112                     cy.get('body').click(); // Collapse the menu
113                     // Hamburger 'more options' menu button
114                     cy.get('[data-cy=collection-files-panel-options-btn]')
115                         .click()
116                     cy.get('[data-cy=context-menu]')
117                         .should('contain', 'Select all')
118                         .click()
119                     cy.get('[data-cy=collection-files-panel-options-btn]')
120                         .click()
121                     cy.get('[data-cy=context-menu]')
122                         // .should('contain', 'Download selected')
123                         .should(`${isWritable ? '' : 'not.'}contain`, 'Remove selected')
124                     cy.get('body').click(); // Collapse the menu
125                     // File item 'more options' button
126                     cy.get('[data-cy=file-item-options-btn')
127                         .click()
128                     cy.get('[data-cy=context-menu]')
129                         .should('contain', 'Download')
130                         .and(`${isWritable ? '' : 'not.'}contain`, 'Remove');
131                     cy.get('body').click(); // Collapse the menu
132                 })
133             })
134         })
135     })
136
137     it('renames a file using valid names', function() {
138         // Creates the collection using the admin token so we can set up
139         // a bogus manifest text without block signatures.
140         cy.createCollection(adminUser.token, {
141             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
142             owner_uuid: activeUser.user.uuid,
143             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
144         .as('testCollection').then(function() {
145             cy.loginAs(activeUser);
146             cy.visit(`/collections/${this.testCollection.uuid}`);
147
148             cy.get('[data-cy=linear-progress]').should('exist');
149             cy.get('[data-cy=linear-progress]').should('not.exist');
150
151             const nameTransitions = [
152                 ['bar', '&'],
153                 ['&', 'foo'],
154                 ['foo', '&'],
155                 ['&', 'I ❤️ ⛵️'],
156                 ['I ❤️ ⛵️', '...']
157             ];
158             nameTransitions.forEach(([from, to]) => {
159                 cy.get('[data-cy=collection-files-panel]')
160                     .contains(`${from}`).rightclick();
161                 cy.get('[data-cy=context-menu]')
162                     .contains('Rename')
163                     .click();
164                 cy.get('[data-cy=form-dialog]')
165                     .should('contain', 'Rename')
166                     .within(() => {
167                         cy.get('input').type(`{selectall}{backspace}${to}`);
168                     });
169                 cy.get('[data-cy=form-submit-btn]').click();
170                 cy.get('[data-cy=collection-files-panel]')
171                     .should('not.contain', `${from}`)
172                     .and('contain', `${to}`);
173             })
174         });
175     });
176
177     it('renames a file to a different directory', function() {
178         // Creates the collection using the admin token so we can set up
179         // a bogus manifest text without block signatures.
180         cy.createCollection(adminUser.token, {
181             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
182             owner_uuid: activeUser.user.uuid,
183             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
184         .as('testCollection').then(function() {
185             cy.loginAs(activeUser);
186             cy.visit(`/collections/${this.testCollection.uuid}`);
187
188             cy.get('[data-cy=linear-progress]').should('exist');
189             cy.get('[data-cy=linear-progress]').should('not.exist');
190
191             // Rename 'bar' to 'subdir/foo'
192             cy.get('[data-cy=collection-files-panel]')
193                 .contains('bar').rightclick();
194             cy.get('[data-cy=context-menu]')
195                 .contains('Rename')
196                 .click();
197             cy.get('[data-cy=form-dialog]')
198                 .should('contain', 'Rename')
199                 .within(() => {
200                     cy.get('input').type(`{selectall}{backspace}subdir/foo`);
201                 });
202             cy.get('[data-cy=form-submit-btn]').click();
203             cy.get('[data-cy=collection-files-panel]')
204                 .should('not.contain', 'bar')
205                 .and('contain', 'subdir');
206             // Look for the "arrow icon" and expand the "subdir" directory.
207             cy.get('[data-cy=virtual-file-tree] > div > i').click();
208             // Rename 'subdir/foo' to 'baz'
209             cy.get('[data-cy=collection-files-panel]')
210                 .contains('foo').rightclick();
211             cy.get('[data-cy=context-menu]')
212                 .contains('Rename')
213                 .click();
214             cy.get('[data-cy=form-dialog]')
215                 .should('contain', 'Rename')
216                 .within(() => {
217                     cy.get('input')
218                         .should('have.value', 'subdir/foo')
219                         .type(`{selectall}{backspace}baz`);
220                 });
221             cy.get('[data-cy=form-submit-btn]').click();
222             cy.get('[data-cy=collection-files-panel]')
223                 .should('contain', 'subdir') // empty dir kept
224                 .and('contain', 'baz');
225         });
226     });
227
228     it('tries to rename a file with illegal names', function() {
229         // Creates the collection using the admin token so we can set up
230         // a bogus manifest text without block signatures.
231         cy.createCollection(adminUser.token, {
232             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
233             owner_uuid: activeUser.user.uuid,
234             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
235         .as('testCollection').then(function() {
236             cy.loginAs(activeUser);
237             cy.visit(`/collections/${this.testCollection.uuid}`);
238
239             cy.get('[data-cy=linear-progress]').should('exist');
240             cy.get('[data-cy=linear-progress]').should('not.exist');
241
242             const illegalNamesFromUI = [
243                 ['.', "Name cannot be '.' or '..'"],
244                 ['..', "Name cannot be '.' or '..'"],
245                 ['', 'This field is required'],
246                 [' ', 'Leading/trailing whitespaces not allowed'],
247                 [' foo', 'Leading/trailing whitespaces not allowed'],
248                 ['foo ', 'Leading/trailing whitespaces not allowed'],
249                 ['//foo', 'Empty dir name not allowed']
250             ]
251             illegalNamesFromUI.forEach(([name, errMsg]) => {
252                 cy.get('[data-cy=collection-files-panel]')
253                     .contains('bar').rightclick();
254                 cy.get('[data-cy=context-menu]')
255                     .contains('Rename')
256                     .click();
257                 cy.get('[data-cy=form-dialog]')
258                     .should('contain', 'Rename')
259                     .within(() => {
260                         cy.get('input').type(`{selectall}{backspace}${name}`);
261                     });
262                 cy.get('[data-cy=form-dialog]')
263                     .should('contain', 'Rename')
264                     .within(() => {
265                         cy.contains(`${errMsg}`);
266                     });
267                 cy.get('[data-cy=form-cancel-btn]').click();
268             })
269         });
270     });
271
272     it('can correctly display old versions', function() {
273         const colName = `Versioned Collection ${Math.floor(Math.random() * 999999)}`;
274         let colUuid = '';
275         let oldVersionUuid = '';
276         // Make sure no other collections with this name exist
277         cy.doRequest('GET', '/arvados/v1/collections', null, {
278             filters: `[["name", "=", "${colName}"]]`,
279             include_old_versions: true
280         })
281         .its('body.items').as('collections')
282         .then(function() {
283             expect(this.collections).to.be.empty;
284         });
285         // Creates the collection using the admin token so we can set up
286         // a bogus manifest text without block signatures.
287         cy.createCollection(adminUser.token, {
288             name: colName,
289             owner_uuid: activeUser.user.uuid,
290             preserve_version: true,
291             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
292         .as('originalVersion').then(function() {
293             // Change the file name to create a new version.
294             cy.updateCollection(adminUser.token, this.originalVersion.uuid, {
295                 manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:foo\n"
296             })
297             colUuid = this.originalVersion.uuid;
298         });
299         // Confirm that there are 2 versions of the collection
300         cy.doRequest('GET', '/arvados/v1/collections', null, {
301             filters: `[["name", "=", "${colName}"]]`,
302             include_old_versions: true
303         })
304         .its('body.items').as('collections')
305         .then(function() {
306             expect(this.collections).to.have.lengthOf(2);
307             this.collections.map(function(aCollection) {
308                 expect(aCollection.current_version_uuid).to.equal(colUuid);
309                 if (aCollection.uuid !== aCollection.current_version_uuid) {
310                     oldVersionUuid = aCollection.uuid;
311                 }
312             });
313             // Check the old version displays as what it is.
314             cy.loginAs(activeUser)
315             cy.visit(`/collections/${oldVersionUuid}`);
316
317             cy.get('[data-cy=linear-progress]').should('exist');
318             cy.get('[data-cy=linear-progress]').should('not.exist');
319
320             cy.get('[data-cy=collection-info-panel]').should('contain', 'This is an old version');
321             cy.get('[data-cy=read-only-icon]').should('exist');
322             cy.get('[data-cy=collection-info-panel]').should('contain', colName);
323             cy.get('[data-cy=collection-files-panel]').should('contain', 'bar');
324         });
325     });
326
327     it('uses the collection version browser to view a previous version', function() {
328         const colName = `Test Collection ${Math.floor(Math.random() * 999999)}`;
329
330         // Creates the collection using the admin token so we can set up
331         // a bogus manifest text without block signatures.
332         cy.createCollection(adminUser.token, {
333             name: colName,
334             owner_uuid: activeUser.user.uuid,
335             preserve_version: true,
336             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:foo 0:3:bar\n"})
337         .as('collection').then(function() {
338             // Visit collection, check basic information
339             cy.loginAs(activeUser)
340             cy.visit(`/collections/${this.collection.uuid}`);
341
342             cy.get('[data-cy=linear-progress]').should('exist');
343             cy.get('[data-cy=linear-progress]').should('not.exist');
344
345             cy.get('[data-cy=collection-info-panel]').should('not.contain', 'This is an old version');
346             cy.get('[data-cy=read-only-icon]').should('not.exist');
347             cy.get('[data-cy=collection-version-number]').should('contain', '1');
348             cy.get('[data-cy=collection-info-panel]').should('contain', colName);
349             cy.get('[data-cy=collection-files-panel]').should('contain', 'foo').and('contain', 'bar');
350
351             // Modify collection, expect version number change
352             cy.get('[data-cy=collection-files-panel]').contains('foo').rightclick();
353             cy.get('[data-cy=context-menu]').contains('Remove').click();
354             cy.get('[data-cy=confirmation-dialog]').should('contain', 'Removing file');
355             cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
356             cy.get('[data-cy=collection-version-number]').should('contain', '2');
357             cy.get('[data-cy=collection-files-panel]').should('not.contain', 'foo').and('contain', 'bar');
358
359             // Click on version number, check version browser. Click on past version.
360             cy.get('[data-cy=collection-version-browser]').should('not.exist');
361             cy.get('[data-cy=collection-version-number]').contains('2').click();
362             cy.get('[data-cy=collection-version-browser]')
363                 .should('contain', 'Nr').and('contain', 'Size').and('contain', 'Date')
364                 .within(() => {
365                     // Version 1: 6 bytes in size
366                     cy.get('[data-cy=collection-version-browser-select-1]')
367                         .should('contain', '1').and('contain', '6 B');
368                     // Version 2: 3 bytes in size (one file removed)
369                     cy.get('[data-cy=collection-version-browser-select-2]')
370                         .should('contain', '2').and('contain', '3 B');
371                     cy.get('[data-cy=collection-version-browser-select-3]')
372                         .should('not.exist');
373                     cy.get('[data-cy=collection-version-browser-select-1]')
374                         .click();
375             });
376             cy.get('[data-cy=collection-info-panel]').should('contain', 'This is an old version');
377             cy.get('[data-cy=read-only-icon]').should('exist');
378             cy.get('[data-cy=collection-version-number]').should('contain', '1');
379             cy.get('[data-cy=collection-info-panel]').should('contain', colName);
380             cy.get('[data-cy=collection-files-panel]')
381                 .should('contain', 'foo').and('contain', 'bar');
382
383             // Check that only old collection action are available on context menu
384             cy.get('[data-cy=collection-panel-options-btn]').click();
385             cy.get('[data-cy=context-menu]')
386                 .should('contain', 'Restore version')
387                 .and('not.contain', 'Add to favorites');
388             cy.get('body').click(); // Collapse the menu avoiding details panel expansion
389
390             // Click on "head version" link, confirm that it's the latest version.
391             cy.get('[data-cy=collection-info-panel]').contains('head version').click();
392             cy.get('[data-cy=collection-info-panel]')
393                 .should('not.contain', 'This is an old version');
394             cy.get('[data-cy=read-only-icon]').should('not.exist');
395             cy.get('[data-cy=collection-version-number]').should('contain', '2');
396             cy.get('[data-cy=collection-info-panel]').should('contain', colName);
397             cy.get('[data-cy=collection-files-panel]').
398                 should('not.contain', 'foo').and('contain', 'bar');
399
400             // Check that old collection action isn't available on context menu
401             cy.get('[data-cy=collection-panel-options-btn]').click()
402             cy.get('[data-cy=context-menu]').should('not.contain', 'Restore version')
403             cy.get('body').click(); // Collapse the menu avoiding details panel expansion
404
405             // Make another change, confirm new version.
406             cy.get('[data-cy=collection-panel-options-btn]').click();
407             cy.get('[data-cy=context-menu]').contains('Edit collection').click();
408             cy.get('[data-cy=form-dialog]')
409                 .should('contain', 'Edit Collection')
410                 .within(() => {
411                     // appends some text
412                     cy.get('input').first().type(' renamed');
413                 });
414             cy.get('[data-cy=form-submit-btn]').click();
415             cy.get('[data-cy=collection-info-panel]')
416                 .should('not.contain', 'This is an old version');
417             cy.get('[data-cy=read-only-icon]').should('not.exist');
418             cy.get('[data-cy=collection-version-number]').should('contain', '3');
419             cy.get('[data-cy=collection-info-panel]').should('contain', colName + ' renamed');
420             cy.get('[data-cy=collection-files-panel]')
421                 .should('not.contain', 'foo').and('contain', 'bar');
422             cy.get('[data-cy=collection-version-browser-select-3]')
423                 .should('contain', '3').and('contain', '3 B');
424
425             // Check context menus on version browser
426             cy.get('[data-cy=collection-version-browser-select-3]').rightclick()
427             cy.get('[data-cy=context-menu]')
428                 .should('contain', 'Add to favorites')
429                 .and('contain', 'Make a copy')
430                 .and('contain', 'Edit collection');
431             cy.get('body').click();
432             // (and now an old version...)
433             cy.get('[data-cy=collection-version-browser-select-1]').rightclick()
434             cy.get('[data-cy=context-menu]')
435                 .should('not.contain', 'Add to favorites')
436                 .and('contain', 'Make a copy')
437                 .and('not.contain', 'Edit collection');
438             cy.get('body').click();
439
440             // Restore first version
441             cy.get('[data-cy=collection-version-browser]').within(() => {
442                 cy.get('[data-cy=collection-version-browser-select-1]').click();
443             });
444             cy.get('[data-cy=collection-panel-options-btn]').click()
445             cy.get('[data-cy=context-menu]').contains('Restore version').click();
446             cy.get('[data-cy=confirmation-dialog]').should('contain', 'Restore version');
447             cy.get('[data-cy=confirmation-dialog-ok-btn]').click();
448             cy.get('[data-cy=collection-info-panel]')
449                 .should('not.contain', 'This is an old version');
450             cy.get('[data-cy=collection-version-number]').should('contain', '4');
451             cy.get('[data-cy=collection-info-panel]').should('contain', colName);
452             cy.get('[data-cy=collection-files-panel]')
453                 .should('contain', 'foo').and('contain', 'bar');
454         });
455     });
456 })