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