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