15685: Adds integration tests, fixes rename dialog error reporting.
[arvados.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 ❤️ ⛵️', ' baz'],
147                 [' baz', ' '] // Must be the last case as cy.contains(' ') doesn't work
148             ];
149             nameTransitions.forEach(([from, to]) => {
150                 cy.get('[data-cy=collection-files-panel]')
151                     .contains(`${from}`).rightclick();
152                 cy.get('[data-cy=context-menu]')
153                     .contains('Rename')
154                     .click();
155                 cy.get('[data-cy=form-dialog]')
156                     .should('contain', 'Rename')
157                     .within(() => {
158                         cy.get('input').type(`{selectall}{backspace}${to}`);
159                     });
160                 cy.get('[data-cy=form-submit-btn]').click();
161                 cy.get('[data-cy=collection-files-panel]')
162                     .should('not.contain', `${from}`)
163                     .and('contain', `${to}`);
164             })
165         });
166     });
167
168     it('renames a file to a different directory', function() {
169         // Creates the collection using the admin token so we can set up
170         // a bogus manifest text without block signatures.
171         cy.createCollection(adminUser.token, {
172             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
173             owner_uuid: activeUser.user.uuid,
174             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
175         .as('testCollection').then(function() {
176             cy.loginAs(activeUser);
177             cy.visit(`/collections/${this.testCollection.uuid}`);
178             // Rename 'bar' to 'subdir/foo'
179             cy.get('[data-cy=collection-files-panel]')
180                 .contains('bar').rightclick();
181             cy.get('[data-cy=context-menu]')
182                 .contains('Rename')
183                 .click();
184             cy.get('[data-cy=form-dialog]')
185                 .should('contain', 'Rename')
186                 .within(() => {
187                     cy.get('input').type(`{selectall}{backspace}subdir/foo`);
188                 });
189             cy.get('[data-cy=form-submit-btn]').click();
190             cy.get('[data-cy=collection-files-panel]')
191                 .should('not.contain', 'bar')
192                 .and('contain', 'subdir');
193             // Look for the "arrow icon" and expand the "subdir" directory.
194             cy.get('[data-cy=virtual-file-tree] > div > i').click();
195             // Rename 'subdir/foo' to 'baz'
196             cy.get('[data-cy=collection-files-panel]')
197                 .contains('foo').rightclick();
198             cy.get('[data-cy=context-menu]')
199                 .contains('Rename')
200                 .click();
201             cy.get('[data-cy=form-dialog]')
202                 .should('contain', 'Rename')
203                 .within(() => {
204                     cy.get('input')
205                         .should('have.value', 'subdir/foo')
206                         .type(`{selectall}{backspace}baz`);
207                 });
208             cy.get('[data-cy=form-submit-btn]').click();
209             cy.get('[data-cy=collection-files-panel]')
210                 .should('contain', 'subdir') // empty dir kept
211                 .and('contain', 'baz');
212         });
213     });
214
215     it('tries to rename a file with an illegal names', function() {
216         // Creates the collection using the admin token so we can set up
217         // a bogus manifest text without block signatures.
218         cy.createCollection(adminUser.token, {
219             name: `Test collection ${Math.floor(Math.random() * 999999)}`,
220             owner_uuid: activeUser.user.uuid,
221             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
222         .as('testCollection').then(function() {
223             cy.loginAs(activeUser);
224             cy.visit(`/collections/${this.testCollection.uuid}`);
225             const illegalNamesFromBackend = ['.', '..'];
226             illegalNamesFromBackend.forEach((name) => {
227                 cy.get('[data-cy=collection-files-panel]')
228                     .contains('bar').rightclick();
229                 cy.get('[data-cy=context-menu]')
230                     .contains('Rename')
231                     .click();
232                 cy.get('[data-cy=form-dialog]')
233                     .should('contain', 'Rename')
234                     .within(() => {
235                         cy.get('input').type(`{selectall}{backspace}${name}`);
236                     });
237                 cy.get('[data-cy=form-submit-btn]').click();
238                 cy.get('[data-cy=form-dialog]')
239                     .should('contain', 'Rename')
240                     .within(() => {
241                         cy.contains('Could not rename');
242                     });
243                 cy.get('[data-cy=form-cancel-btn]').click();
244             });
245             const illegalNamesFromUI = [
246                 ['', 'This field is required'],
247                 [' ', 'Leading/trailing whitespaces not allowed'],
248                 [' foo', 'Leading/trailing whitespaces not allowed'],
249                 ['foo ', 'Leading/trailing whitespaces not allowed'],
250                 ['//foo', 'Empty dir name not allowed']
251             ]
252             illegalNamesFromUI.forEach(([name, errMsg]) => {
253                 cy.get('[data-cy=collection-files-panel]')
254                     .contains('bar').rightclick();
255                 cy.get('[data-cy=context-menu]')
256                     .contains('Rename')
257                     .click();
258                 cy.get('[data-cy=form-dialog]')
259                     .should('contain', 'Rename')
260                     .within(() => {
261                         cy.get('input').type(`{selectall}{backspace}${name}`);
262                     });
263                 cy.get('[data-cy=form-cancel-btn]').focus();
264                 cy.get('[data-cy=form-dialog]')
265                     .should('contain', 'Rename')
266                     .within(() => {
267                         cy.contains(`${errMsg}`);
268                     });
269                 cy.get('[data-cy=form-cancel-btn]').click();
270             })
271         });
272     });
273
274     it('can correctly display old versions', function() {
275         const colName = `Versioned Collection ${Math.floor(Math.random() * 999999)}`;
276         let colUuid = '';
277         let oldVersionUuid = '';
278         // Make sure no other collections with this name exist
279         cy.doRequest('GET', '/arvados/v1/collections', null, {
280             filters: `[["name", "=", "${colName}"]]`,
281             include_old_versions: true
282         })
283         .its('body.items').as('collections')
284         .then(function() {
285             expect(this.collections).to.be.empty;
286         });
287         // Creates the collection using the admin token so we can set up
288         // a bogus manifest text without block signatures.
289         cy.createCollection(adminUser.token, {
290             name: colName,
291             owner_uuid: activeUser.user.uuid,
292             manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:bar\n"})
293         .as('originalVersion').then(function() {
294             // Change the file name to create a new version.
295             cy.updateCollection(adminUser.token, this.originalVersion.uuid, {
296                 manifest_text: ". 37b51d194a7513e45b56f6524f2d51f2+3 0:3:foo\n"
297             })
298             colUuid = this.originalVersion.uuid;
299         });
300         // Confirm that there are 2 versions of the collection
301         cy.doRequest('GET', '/arvados/v1/collections', null, {
302             filters: `[["name", "=", "${colName}"]]`,
303             include_old_versions: true
304         })
305         .its('body.items').as('collections')
306         .then(function() {
307             expect(this.collections).to.have.lengthOf(2);
308             this.collections.map(function(aCollection) {
309                 expect(aCollection.current_version_uuid).to.equal(colUuid);
310                 if (aCollection.uuid !== aCollection.current_version_uuid) {
311                     oldVersionUuid = aCollection.uuid;
312                 }
313             });
314             // Check the old version displays as what it is.
315             cy.loginAs(activeUser)
316             cy.visit(`/collections/${oldVersionUuid}`);
317             cy.get('[data-cy=collection-info-panel]').should('contain', 'This is an old version');
318             cy.get('[data-cy=read-only-icon]').should('exist');
319             cy.get('[data-cy=collection-info-panel]').should('contain', colName);
320             cy.get('[data-cy=collection-files-panel]').should('contain', 'bar');
321         });
322     });
323 })