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