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