From bb808fc2df67c2332f0fe2d56e383c0b0f0a2919 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Wed, 13 Mar 2024 15:29:39 +0100 Subject: [PATCH] test(files_versions): Add tests for files versions Signed-off-by: Louis Chemineau --- cypress/e2e/files/filesUtils.ts | 63 ++++++++ .../e2e/files_versions/filesVersionsUtils.ts | 99 ++++++++++++ .../e2e/files_versions/version_creation.cy.ts | 79 ++++++++++ .../e2e/files_versions/version_deletion.cy.ts | 110 ++++++++++++++ .../e2e/files_versions/version_download.cy.ts | 101 +++++++++++++ .../files_versions/version_expiration.cy.ts | 73 +++++++++ .../e2e/files_versions/version_naming.cy.ts | 143 ++++++++++++++++++ .../files_versions/version_restoration.cy.ts | 128 ++++++++++++++++ 8 files changed, 796 insertions(+) create mode 100644 cypress/e2e/files/filesUtils.ts create mode 100644 cypress/e2e/files_versions/filesVersionsUtils.ts create mode 100644 cypress/e2e/files_versions/version_creation.cy.ts create mode 100644 cypress/e2e/files_versions/version_deletion.cy.ts create mode 100644 cypress/e2e/files_versions/version_download.cy.ts create mode 100644 cypress/e2e/files_versions/version_expiration.cy.ts create mode 100644 cypress/e2e/files_versions/version_naming.cy.ts create mode 100644 cypress/e2e/files_versions/version_restoration.cy.ts diff --git a/cypress/e2e/files/filesUtils.ts b/cypress/e2e/files/filesUtils.ts new file mode 100644 index 000000000..96bff2f85 --- /dev/null +++ b/cypress/e2e/files/filesUtils.ts @@ -0,0 +1,63 @@ +/** + * @copyright Copyright (c) 2024 Ferdinand Thiessen + * + * @author Ferdinand Thiessen + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +export const getRowForFile = (filename: string) => cy.get(`[data-cy-files-list-row-name="${CSS.escape(filename)}"]`) + +export const getActionsForFile = (filename: string) => getRowForFile(filename).find('[data-cy-files-list-row-actions]') + +export const getActionButtonForFile = (filename: string) => getActionsForFile(filename).find('button[aria-label="Actions"]') + +export const triggerActionForFile = (filename: string, actionId: string) => { + getActionButtonForFile(filename).click() + cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click() +} + +export const moveFile = (fileName: string, dirName: string) => { + getRowForFile(fileName).should('be.visible') + triggerActionForFile(fileName, 'move-copy') + + cy.get('.file-picker').within(() => { + // intercept the copy so we can wait for it + cy.intercept('MOVE', /\/remote.php\/dav\/files\//).as('moveFile') + + if (dirName === '/') { + // select home folder + cy.get('button[title="Home"]').should('be.visible').click() + // click move + cy.contains('button', 'Move').should('be.visible').click() + } else if (dirName === '.') { + // click move + cy.contains('button', 'Copy').should('be.visible').click() + } else { + // select the folder + cy.get(`[data-filename="${dirName}"]`).should('be.visible').click() + // click move + cy.contains('button', `Move to ${dirName}`).should('be.visible').click() + } + + cy.wait('@moveFile') + }) +} + +export const navigateToFolder = (folderName: string) => { + getRowForFile(folderName).should('be.visible').find('[data-cy-files-list-row-name-link]').click() +} diff --git a/cypress/e2e/files_versions/filesVersionsUtils.ts b/cypress/e2e/files_versions/filesVersionsUtils.ts new file mode 100644 index 000000000..bef0cc28d --- /dev/null +++ b/cypress/e2e/files_versions/filesVersionsUtils.ts @@ -0,0 +1,99 @@ +/* eslint-disable jsdoc/require-jsdoc */ +/** + * @copyright Copyright (c) 2022 Louis Chemineau + * + * @author Louis Chemineau + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import type { User } from '@nextcloud/cypress' +import path from 'path' + +export const uploadThreeVersions = (user: User, fileName: string) => { + // A new version will not be created if the changes occur + // within less than one second of each other. + // eslint-disable-next-line cypress/no-unnecessary-waiting + cy.uploadContent(user, new Blob(['v1'], { type: 'text/plain' }), 'text/plain', `/${fileName}`) + .wait(1100) + .uploadContent(user, new Blob(['v2'], { type: 'text/plain' }), 'text/plain', `/${fileName}`) + .wait(1100) + .uploadContent(user, new Blob(['v3'], { type: 'text/plain' }), 'text/plain', `/${fileName}`) + cy.login(user) +} + +export function openVersionsPanel(fileName: string) { + // Detect the versions list fetch + cy.intercept('PROPFIND', '**/dav/versions/*/versions/**').as('getVersions') + + // Open the versions tab + cy.window().then(win => { + win.OCA.Files.Sidebar.setActiveTab('version_vue') + win.OCA.Files.Sidebar.open(`/${fileName}`) + }) + + // Wait for the versions list to be fetched + cy.wait('@getVersions') + cy.get('#tab-version_vue').should('be.visible', { timeout: 10000 }) +} + +export function toggleVersionMenu(index: number) { + cy.get('#tab-version_vue [data-files-versions-version]') + .eq(index) + .find('button') + .click() +} + +export function triggerVersionAction(index: number, actionName: string) { + toggleVersionMenu(index) + cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).filter(':visible').click() +} + +export function nameVersion(index: number, name: string) { + cy.intercept('PROPPATCH', '**/dav/versions/*/versions/**').as('labelVersion') + triggerVersionAction(index, 'label') + cy.get(':focused').type(`${name}{enter}`) + cy.wait('@labelVersion') +} + +export function restoreVersion(index: number) { + cy.intercept('MOVE', '**/dav/versions/*/versions/**').as('restoreVersion') + triggerVersionAction(index, 'restore') + cy.wait('@restoreVersion') +} + +export function deleteVersion(index: number) { + cy.intercept('DELETE', '**/dav/versions/*/versions/**').as('deleteVersion') + triggerVersionAction(index, 'delete') + cy.wait('@deleteVersion') +} + +export function doesNotHaveAction(index: number, actionName: string) { + toggleVersionMenu(index) + cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).should('not.exist') + toggleVersionMenu(index) +} + +export function assertVersionContent(filename: string, index: number, expectedContent: string) { + const downloadsFolder = Cypress.config('downloadsFolder') + + triggerVersionAction(index, 'download') + + return cy.readFile(path.join(downloadsFolder, filename)) + .then((versionContent) => expect(versionContent).to.equal(expectedContent)) + .then(() => cy.exec(`rm ${downloadsFolder}/${filename}`)) +} diff --git a/cypress/e2e/files_versions/version_creation.cy.ts b/cypress/e2e/files_versions/version_creation.cy.ts new file mode 100644 index 000000000..e2de2f3dd --- /dev/null +++ b/cypress/e2e/files_versions/version_creation.cy.ts @@ -0,0 +1,79 @@ +/** + * @copyright Copyright (c) 2022 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import type { User } from '@nextcloud/cypress' +import { PERMISSION_DELETE, PERMISSION_READ, PERMISSION_WRITE, addACLManagerUser, addUserToGroup, createGroup, createGroupFolder, enableACLPermissions } from '../groupfoldersUtils' +import { openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils' +import { navigateToFolder } from '../files/filesUtils' + +describe('Versions creation', () => { + let randomGroupName: string + let randomGroupFolderName: string + let randomFileName: string + let randomFilePath: string + let groupFolderId: string + let managerUser: User + let user1: User + let user2: User + + before(() => { + randomGroupName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + randomGroupFolderName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + randomFilePath = `${randomGroupFolderName}/${randomFileName}` + + cy.createRandomUser().then(_user => { managerUser = _user }) + cy.createRandomUser().then(_user => { user1 = _user }) + cy.createRandomUser().then(_user => { user2 = _user }) + createGroup(randomGroupName) + + cy.then(() => { + addUserToGroup(randomGroupName, user1.userId) + addUserToGroup(randomGroupName, user2.userId) + addUserToGroup(randomGroupName, managerUser.userId) + createGroupFolder(randomGroupFolderName, randomGroupName, [PERMISSION_READ, PERMISSION_WRITE, PERMISSION_DELETE]) + .then(_groupFolderId => { groupFolderId = _groupFolderId }) + + cy.then(() => { + enableACLPermissions(groupFolderId) + addACLManagerUser(groupFolderId, managerUser.userId) + }) + + uploadThreeVersions(user1, randomFilePath) + cy.login(user1) + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + }) + }) + + it('Opens the versions panel and sees the versions', () => { + cy.visit('/apps/files') + navigateToFolder(randomGroupFolderName) + openVersionsPanel(randomFileName) + + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').should('have.length', 3) + cy.get('[data-files-versions-version]').eq(0).contains('Current version') + cy.get('[data-files-versions-version]').eq(2).contains('Initial version') + }) + }) +}) diff --git a/cypress/e2e/files_versions/version_deletion.cy.ts b/cypress/e2e/files_versions/version_deletion.cy.ts new file mode 100644 index 000000000..1e90c79fa --- /dev/null +++ b/cypress/e2e/files_versions/version_deletion.cy.ts @@ -0,0 +1,110 @@ +/** + * @copyright Copyright (c) 2024 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import type { User } from '@nextcloud/cypress' +import { doesNotHaveAction, openVersionsPanel, setupTestSharedFileFromUser, uploadThreeVersions, deleteVersion } from './filesVersionsUtils' +import { navigateToFolder, getRowForFile } from '../files/FilesUtils' + +describe('Versions restoration', () => { + const folderName = 'shared_folder' + const randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + const randomFilePath = `/${folderName}/${randomFileName}` + let user: User + let versionCount = 0 + + before(() => { + cy.createRandomUser() + .then((_user) => { + user = _user + cy.mkdir(user, `/${folderName}`) + uploadThreeVersions(user, randomFilePath) + uploadThreeVersions(user, randomFilePath) + versionCount = 6 + cy.login(user) + cy.visit('/apps/files') + navigateToFolder(folderName) + openVersionsPanel(randomFilePath) + }) + }) + + it('Delete initial version', () => { + cy.get('[data-files-versions-version]').should('have.length', versionCount) + deleteVersion(2) + versionCount-- + cy.get('[data-files-versions-version]').should('have.length', versionCount) + }) + + context('Delete versions of shared file', () => { + it('Works with delete permission', () => { + setupTestSharedFileFromUser(user, folderName, { delete: true }) + navigateToFolder(folderName) + openVersionsPanel(randomFilePath) + + cy.get('[data-files-versions-version]').should('have.length', versionCount) + deleteVersion(2) + versionCount-- + cy.get('[data-files-versions-version]').should('have.length', versionCount) + }) + + it('Does not work without delete permission', () => { + setupTestSharedFileFromUser(user, folderName, { delete: false }) + navigateToFolder(folderName) + openVersionsPanel(randomFilePath) + + doesNotHaveAction(0, 'delete') + doesNotHaveAction(1, 'delete') + doesNotHaveAction(2, 'delete') + }) + + it('Does not work without delete permission through direct API access', () => { + let hostname: string + let fileId: string|undefined + let versionId: string|undefined + + setupTestSharedFileFromUser(user, folderName, { delete: false }) + .then(recipient => { + navigateToFolder(folderName) + openVersionsPanel(randomFilePath) + + cy.url().then(url => { hostname = new URL(url).hostname }) + getRowForFile(randomFileName).invoke('attr', 'data-cy-files-list-row-fileid').then(_fileId => { fileId = _fileId }) + cy.get('[data-files-versions-version]').eq(1).invoke('attr', 'data-files-versions-version').then(_versionId => { versionId = _versionId }) + + cy.then(() => { + cy.logout() + cy.request({ + method: 'DELETE', + auth: { user: recipient.userId, pass: recipient.password }, + headers: { + cookie: '', + }, + url: `http://${hostname}/remote.php/dav/versions/${recipient.userId}/versions/${fileId}/${versionId}`, + failOnStatusCode: false, + }) + .then(({ status }) => { + expect(status).to.equal(403) + }) + }) + }) + }) + }) +}) diff --git a/cypress/e2e/files_versions/version_download.cy.ts b/cypress/e2e/files_versions/version_download.cy.ts new file mode 100644 index 000000000..f6a994322 --- /dev/null +++ b/cypress/e2e/files_versions/version_download.cy.ts @@ -0,0 +1,101 @@ +/** + * @copyright Copyright (c) 2022 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import { assertVersionContent, doesNotHaveAction, openVersionsPanel, setupTestSharedFileFromUser, uploadThreeVersions } from './filesVersionsUtils' +import type { User } from '@nextcloud/cypress' +import { getRowForFile } from '../files/FilesUtils' + +describe('Versions download', () => { + let randomFileName = '' + let user: User + + before(() => { + randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + + cy.createRandomUser() + .then((_user) => { + user = _user + uploadThreeVersions(user, randomFileName) + cy.login(user) + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + }) + }) + + it('Download versions and assert their content', () => { + assertVersionContent(randomFileName, 0, 'v3') + assertVersionContent(randomFileName, 1, 'v2') + assertVersionContent(randomFileName, 2, 'v1') + }) + + context('Download versions of shared file', () => { + it('Works with download permission', () => { + setupTestSharedFileFromUser(user, randomFileName, { download: true }) + openVersionsPanel(randomFileName) + + assertVersionContent(randomFileName, 0, 'v3') + assertVersionContent(randomFileName, 1, 'v2') + assertVersionContent(randomFileName, 2, 'v1') + }) + + it('Does not show action without download permission', () => { + setupTestSharedFileFromUser(user, randomFileName, { download: false }) + openVersionsPanel(randomFileName) + + cy.get('[data-files-versions-version]').eq(0).find('.action-item__menutoggle').should('not.exist') + cy.get('[data-files-versions-version]').eq(0).get('[data-cy-version-action="download"]').should('not.exist') + + doesNotHaveAction(1, 'download') + doesNotHaveAction(2, 'download') + }) + + it('Does not work without download permission through direct API access', () => { + let hostname: string + let fileId: string|undefined + let versionId: string|undefined + + setupTestSharedFileFromUser(user, randomFileName, { download: false }) + .then(recipient => { + openVersionsPanel(randomFileName) + + cy.url().then(url => { hostname = new URL(url).hostname }) + getRowForFile(randomFileName).invoke('attr', 'data-cy-files-list-row-fileid').then(_fileId => { fileId = _fileId }) + cy.get('[data-files-versions-version]').eq(1).invoke('attr', 'data-files-versions-version').then(_versionId => { versionId = _versionId }) + + cy.then(() => { + cy.logout() + cy.request({ + auth: { user: recipient.userId, pass: recipient.password }, + headers: { + cookie: '', + }, + url: `http://${hostname}/remote.php/dav/versions/${recipient.userId}/versions/${fileId}/${versionId}`, + failOnStatusCode: false, + }) + .then(({ status }) => { + expect(status).to.equal(403) + }) + }) + }) + }) + }) +}) diff --git a/cypress/e2e/files_versions/version_expiration.cy.ts b/cypress/e2e/files_versions/version_expiration.cy.ts new file mode 100644 index 000000000..a3b2eea83 --- /dev/null +++ b/cypress/e2e/files_versions/version_expiration.cy.ts @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2022 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import { assertVersionContent, nameVersion, openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils' + +describe('Versions expiration', () => { + let randomFileName = '' + + beforeEach(() => { + randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + + cy.createRandomUser() + .then((user) => { + uploadThreeVersions(user, randomFileName) + cy.login(user) + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + }) + }) + + it('Expire all versions', () => { + cy.runOccCommand('config:system:set versions_retention_obligation --value "0, 0"') + cy.runOccCommand('versions:expire') + cy.runOccCommand('config:system:set versions_retention_obligation --value auto') + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').should('have.length', 1) + cy.get('[data-files-versions-version]').eq(0).contains('Current version') + }) + + assertVersionContent(randomFileName, 0, 'v3') + }) + + it('Expire versions v2', () => { + nameVersion(2, 'v1') + + cy.runOccCommand('config:system:set versions_retention_obligation --value "0, 0"') + cy.runOccCommand('versions:expire') + cy.runOccCommand('config:system:set versions_retention_obligation --value auto') + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').should('have.length', 2) + cy.get('[data-files-versions-version]').eq(0).contains('Current version') + cy.get('[data-files-versions-version]').eq(1).contains('v1') + }) + + assertVersionContent(randomFileName, 0, 'v3') + assertVersionContent(randomFileName, 1, 'v1') + }) +}) diff --git a/cypress/e2e/files_versions/version_naming.cy.ts b/cypress/e2e/files_versions/version_naming.cy.ts new file mode 100644 index 000000000..a2f0514df --- /dev/null +++ b/cypress/e2e/files_versions/version_naming.cy.ts @@ -0,0 +1,143 @@ +/** + * @copyright Copyright (c) 2022 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import type { User } from '@nextcloud/cypress' +import { nameVersion, openVersionsPanel, uploadThreeVersions, doesNotHaveAction, setupTestSharedFileFromUser } from './filesVersionsUtils' +import { getRowForFile } from '../files/FilesUtils' + +describe('Versions naming', () => { + let randomFileName = '' + let user: User + + before(() => { + randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + + cy.createRandomUser() + .then((_user) => { + user = _user + uploadThreeVersions(user, randomFileName) + cy.login(user) + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + }) + }) + + it('Names the versions', () => { + nameVersion(2, 'v1') + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').eq(2).contains('v1') + cy.get('[data-files-versions-version]').eq(2).contains('Initial version').should('not.exist') + }) + + nameVersion(1, 'v2') + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').eq(1).contains('v2') + }) + + nameVersion(0, 'v3') + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').eq(0).contains('v3 (Current version)') + }) + }) + + context('Name versions of shared file', () => { + context('with edit permission', () => { + before(() => { + setupTestSharedFileFromUser(user, randomFileName, { update: true }) + openVersionsPanel(randomFileName) + }) + + it('Names the versions', () => { + nameVersion(2, 'v1 - shared') + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').eq(2).contains('v1 - shared') + cy.get('[data-files-versions-version]').eq(2).contains('Initial version').should('not.exist') + }) + + nameVersion(1, 'v2 - shared') + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').eq(1).contains('v2 - shared') + }) + + nameVersion(0, 'v3 - shared') + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').eq(0).contains('v3 - shared (Current version)') + }) + }) + }) + + context('without edit permission', () => { + it('Does not show action', () => { + setupTestSharedFileFromUser(user, randomFileName, { update: false }) + openVersionsPanel(randomFileName) + + cy.get('[data-files-versions-version]').eq(0).find('.action-item__menutoggle').should('not.exist') + cy.get('[data-files-versions-version]').eq(0).get('[data-cy-version-action="label"]').should('not.exist') + + doesNotHaveAction(1, 'label') + doesNotHaveAction(2, 'label') + }) + + it('Does not work without update permission through direct API access', () => { + let hostname: string + let fileId: string|undefined + let versionId: string|undefined + + setupTestSharedFileFromUser(user, randomFileName, { update: false }) + .then(recipient => { + openVersionsPanel(randomFileName) + + cy.url().then(url => { hostname = new URL(url).hostname }) + getRowForFile(randomFileName).invoke('attr', 'data-cy-files-list-row-fileid').then(_fileId => { fileId = _fileId }) + cy.get('[data-files-versions-version]').eq(1).invoke('attr', 'data-files-versions-version').then(_versionId => { versionId = _versionId }) + + cy.then(() => { + cy.logout() + cy.request({ + method: 'PROPPATCH', + auth: { user: recipient.userId, pass: recipient.password }, + headers: { + cookie: '', + }, + body: ` + + + + not authorized labeling + + + `, + url: `http://${hostname}/remote.php/dav/versions/${recipient.userId}/versions/${fileId}/${versionId}`, + failOnStatusCode: false, + }) + .then(({ status }) => { + expect(status).to.equal(403) + }) + }) + }) + }) + }) + }) +}) diff --git a/cypress/e2e/files_versions/version_restoration.cy.ts b/cypress/e2e/files_versions/version_restoration.cy.ts new file mode 100644 index 000000000..c5dbaeab9 --- /dev/null +++ b/cypress/e2e/files_versions/version_restoration.cy.ts @@ -0,0 +1,128 @@ +/** + * @copyright Copyright (c) 2022 Louis Chmn + * + * @author Louis Chmn + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import type { User } from '@nextcloud/cypress' +import { assertVersionContent, doesNotHaveAction, openVersionsPanel, setupTestSharedFileFromUser, restoreVersion, uploadThreeVersions } from './filesVersionsUtils' +import { getRowForFile } from '../files/FilesUtils' + +describe('Versions restoration', () => { + let randomFileName = '' + let user: User + + before(() => { + randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt' + + cy.createRandomUser() + .then((_user) => { + user = _user + uploadThreeVersions(user, randomFileName) + cy.login(user) + cy.visit('/apps/files') + openVersionsPanel(randomFileName) + }) + }) + + it('Current version does not have restore action', () => { + doesNotHaveAction(0, 'restore') + }) + + it('Restores initial version', () => { + restoreVersion(2) + + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').should('have.length', 3) + cy.get('[data-files-versions-version]').eq(0).contains('Current version') + cy.get('[data-files-versions-version]').eq(2).contains('Initial version').should('not.exist') + }) + }) + + it('Downloads versions and assert there content', () => { + assertVersionContent(randomFileName, 0, 'v1') + assertVersionContent(randomFileName, 1, 'v3') + assertVersionContent(randomFileName, 2, 'v2') + }) + + context('Restore versions of shared file', () => { + it('Works with update permission', () => { + setupTestSharedFileFromUser(user, randomFileName, { update: true }) + openVersionsPanel(randomFileName) + + it('Restores initial version', () => { + restoreVersion(2) + cy.get('#tab-version_vue').within(() => { + cy.get('[data-files-versions-version]').should('have.length', 3) + cy.get('[data-files-versions-version]').eq(0).contains('Current version') + cy.get('[data-files-versions-version]').eq(2).contains('Initial version').should('not.exist') + }) + }) + + it('Downloads versions and assert there content', () => { + assertVersionContent(randomFileName, 0, 'v1') + assertVersionContent(randomFileName, 1, 'v3') + assertVersionContent(randomFileName, 2, 'v2') + }) + }) + + it('Does not show action without delete permission', () => { + setupTestSharedFileFromUser(user, randomFileName, { update: false }) + openVersionsPanel(randomFileName) + + cy.get('[data-files-versions-version]').eq(0).find('.action-item__menutoggle').should('not.exist') + cy.get('[data-files-versions-version]').eq(0).get('[data-cy-version-action="restore"]').should('not.exist') + + doesNotHaveAction(1, 'restore') + doesNotHaveAction(2, 'restore') + }) + + it('Does not work without update permission through direct API access', () => { + let hostname: string + let fileId: string|undefined + let versionId: string|undefined + + setupTestSharedFileFromUser(user, randomFileName, { update: false }) + .then(recipient => { + openVersionsPanel(randomFileName) + + cy.url().then(url => { hostname = new URL(url).hostname }) + getRowForFile(randomFileName).invoke('attr', 'data-cy-files-list-row-fileid').then(_fileId => { fileId = _fileId }) + cy.get('[data-files-versions-version]').eq(1).invoke('attr', 'data-files-versions-version').then(_versionId => { versionId = _versionId }) + + cy.then(() => { + cy.logout() + cy.request({ + method: 'MOVE', + auth: { user: recipient.userId, pass: recipient.password }, + headers: { + cookie: '', + Destination: 'https://nextcloud_server1.test/remote.php/dav/versions/admin/restore/target', + }, + url: `http://${hostname}/remote.php/dav/versions/${recipient.userId}/versions/${fileId}/${versionId}`, + failOnStatusCode: false, + }) + .then(({ status }) => { + expect(status).to.equal(403) + }) + }) + }) + }) + }) +})