From 066985dd5772e4721665bfb8c4bc17b13a981322 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 27 Aug 2024 17:42:31 +0200 Subject: [PATCH] fix(files): Correctly check for already used names when creating new folder Also add Cypress tests for the "new"-menu. Signed-off-by: Ferdinand Thiessen --- apps/files/src/views/FilesList.vue | 2 +- cypress/e2e/files/FilesUtils.ts | 16 ++++ cypress/e2e/files/files-renaming.cy.ts | 9 +- cypress/e2e/files/new-menu.cy.ts | 123 +++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 cypress/e2e/files/new-menu.cy.ts diff --git a/apps/files/src/views/FilesList.vue b/apps/files/src/views/FilesList.vue index 03cc60e30c090..70c9a38607e36 100644 --- a/apps/files/src/views/FilesList.vue +++ b/apps/files/src/views/FilesList.vue @@ -243,7 +243,7 @@ export default defineComponent({ // as the path is allowed to be undefined we need to normalize the path ('//' to '/') const normalizedPath = normalize(`${this.currentFolder?.path ?? ''}/${path ?? ''}`) // Try cache first - const nodes = this.filesStore.getNodesByPath(view.id, path) + const nodes = this.filesStore.getNodesByPath(view.id, normalizedPath) if (nodes.length > 0) { return nodes } diff --git a/cypress/e2e/files/FilesUtils.ts b/cypress/e2e/files/FilesUtils.ts index b94ad3941ec11..345b6402f1f04 100644 --- a/cypress/e2e/files/FilesUtils.ts +++ b/cypress/e2e/files/FilesUtils.ts @@ -136,3 +136,19 @@ export const createFolder = (folderName: string) => { getRowForFile(folderName).should('be.visible') } + +/** + * Check validity of an input element + * @param validity The expected validity message (empty string means it is valid) + * @example + * ```js + * cy.findByRole('textbox') + * .should(haveValidity(/must not be empty/i)) + * ``` + */ +export const haveValidity = (validity: string | RegExp) => { + if (typeof validity === 'string') { + return (el: JQuery) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity) + } + return (el: JQuery) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity) +} diff --git a/cypress/e2e/files/files-renaming.cy.ts b/cypress/e2e/files/files-renaming.cy.ts index 9404c9544b075..deb257b655b63 100644 --- a/cypress/e2e/files/files-renaming.cy.ts +++ b/cypress/e2e/files/files-renaming.cy.ts @@ -4,14 +4,7 @@ */ import type { User } from '@nextcloud/cypress' -import { getRowForFile, triggerActionForFile } from './FilesUtils' - -const haveValidity = (validity: string | RegExp) => { - if (typeof validity === 'string') { - return (el: JQuery) => expect((el.get(0) as HTMLInputElement).validationMessage).to.equal(validity) - } - return (el: JQuery) => expect((el.get(0) as HTMLInputElement).validationMessage).to.match(validity) -} +import { getRowForFile, haveValidity, triggerActionForFile } from './FilesUtils' describe('files: Rename nodes', { testIsolation: true }, () => { let user: User diff --git a/cypress/e2e/files/new-menu.cy.ts b/cypress/e2e/files/new-menu.cy.ts new file mode 100644 index 0000000000000..dfe586fa073b6 --- /dev/null +++ b/cypress/e2e/files/new-menu.cy.ts @@ -0,0 +1,123 @@ +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { createFolder, getRowForFile, haveValidity, navigateToFolder } from './FilesUtils' + +describe('"New"-menu', { testIsolation: true }, () => { + + beforeEach(() => { + cy.createRandomUser().then(($user) => { + cy.login($user) + cy.visit('/apps/files') + }) + }) + + it('Create new folder', () => { + // Click the "new" button + cy.get('[data-cy-upload-picker]') + .findByRole('button', { name: 'New' }) + .should('be.visible') + .click() + // Click the "new folder" menu entry + cy.findByRole('menuitem', { name: 'New folder' }) + .should('be.visible') + .click() + // Create a folder + cy.intercept('MKCOL', '**/remote.php/dav/files/**').as('mkdir') + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .type('A new folder{enter}') + cy.wait('@mkdir') + // See the folder is visible + getRowForFile('A new folder') + .should('be.visible') + }) + + it('Does not allow creating forbidden folder names', () => { + // Click the "new" button + cy.get('[data-cy-upload-picker]') + .findByRole('button', { name: 'New' }) + .should('be.visible') + .click() + // Click the "new folder" menu entry + cy.findByRole('menuitem', { name: 'New folder' }) + .should('be.visible') + .click() + // enter folder name + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .type('.htaccess') + // See that input has invalid state set + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .should(haveValidity(/reserved name/i)) + // See that it can not create + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('button', { name: 'Create' }) + .should('be.disabled') + }) + + it('Does not allow creating folders with already existing names', () => { + createFolder('already exists') + // Click the "new" button + cy.get('[data-cy-upload-picker]') + .findByRole('button', { name: 'New' }) + .should('be.visible') + .click() + // Click the "new folder" menu entry + cy.findByRole('menuitem', { name: 'New folder' }) + .should('be.visible') + .click() + // enter folder name + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .type('already exists') + // See that input has invalid state set + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .should(haveValidity(/already in use/i)) + // See that it can not create + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('button', { name: 'Create' }) + .should('be.disabled') + }) + + /** + * Regression test of https://github.com/nextcloud/server/issues/47530 + */ + it('Create same folder in child folder', () => { + // setup other folders + createFolder('folder') + createFolder('other folder') + navigateToFolder('folder') + + // Click the "new" button + cy.get('[data-cy-upload-picker]') + .findByRole('button', { name: 'New' }) + .should('be.visible') + .click() + // Click the "new folder" menu entry + cy.findByRole('menuitem', { name: 'New folder' }) + .should('be.visible') + .click() + // enter folder name + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .type('other folder') + // See that creating is allowed + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('textbox', { name: 'Folder name' }) + .should(haveValidity('')) + // can create + cy.intercept('MKCOL', '**/remote.php/dav/files/**').as('mkdir') + cy.findByRole('dialog', { name: /create new folder/i }) + .findByRole('button', { name: 'Create' }) + .click() + cy.wait('@mkdir') + // see it is created + getRowForFile('other folder') + .should('be.visible') + }) +})