Skip to content

Commit

Permalink
feat(files_versions): Support moving versions across storages
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Apr 4, 2024
1 parent 2663966 commit 812cdc3
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
build
vendor
js/
cypress/downloads/
68 changes: 59 additions & 9 deletions cypress/e2e/files/filesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,83 @@ export const triggerActionForFile = (filename: string, actionId: string) => {
cy.get(`[data-cy-files-list-row-action="${CSS.escape(actionId)}"] > button`).should('exist').click()
}

export const moveFile = (fileName: string, dirName: string) => {
export const moveFile = (fileName: string, dirPath: 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')
cy.intercept({ method: 'MOVE', times: 1, url: /\/remote.php\/dav\/files\// }).as('moveFile')

if (dirName === '/') {
if (dirPath === '/') {
// 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 === '.') {
} else if (dirPath === '.') {
// click move
cy.contains('button', 'Copy').should('be.visible').click()
} else {
// select the folder
cy.get(`[data-filename="${dirName}"]`).should('be.visible').click()
const directories = dirPath.split('/')
directories.forEach((directory) => {
// select the folder
cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
})

// click move
cy.contains('button', `Move to ${dirName}`).should('be.visible').click()
cy.contains('button', `Move to ${directories.at(-1)}`).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()
export const copyFile = (fileName: string, dirPath: 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({ method: 'COPY', times: 1, url: /\/remote.php\/dav\/files\// }).as('copyFile')

if (dirPath === '/') {
// select home folder
cy.get('button[title="Home"]').should('be.visible').click()
// click copy
cy.contains('button', 'Copy').should('be.visible').click()
} else if (dirPath === '.') {
// click copy
cy.contains('button', 'Copy').should('be.visible').click()
} else {
const directories = dirPath.split('/')
directories.forEach((directory) => {
// select the folder
cy.get(`[data-filename="${directory}"]`).should('be.visible').click()
})

// click copy
cy.contains('button', `Copy to ${directories.at(-1)}`).should('be.visible').click()
}

cy.wait('@copyFile')
})
}

export const navigateToFolder = (dirPath: string) => {
const directories = dirPath.split('/')
directories.forEach((directory) => {
getRowForFile(directory).should('be.visible').find('[data-cy-files-list-row-name-link]').click()
})

}

export const closeSidebar = () => {
// {force: true} as it might be hidden behind toasts
cy.get('[cy-data-sidebar] .app-sidebar__close').click({ force: true })
}

export const clickOnBreadcumbs = (label: string) => {
cy.intercept({ method: 'PROPFIND', url: /\/remote.php\/dav\// }).as('propfind')
cy.get('[data-cy-files-content-breadcrumbs]').contains(label).click()
cy.wait('@propfind')
}
19 changes: 8 additions & 11 deletions cypress/e2e/files_versions/filesVersionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const uploadThreeVersions = (user: User, fileName: string) => {

export function openVersionsPanel(fileName: string) {
// Detect the versions list fetch
cy.intercept('PROPFIND', '**/dav/versions/*/versions/**').as('getVersions')
cy.intercept({ method: 'PROPFIND', times: 1, url: '**/dav/versions/*/versions/**' }).as('getVersions')

// Open the versions tab
cy.window().then(win => {
Expand All @@ -64,20 +64,20 @@ export function triggerVersionAction(index: number, actionName: string) {
}

export function nameVersion(index: number, name: string) {
cy.intercept('PROPPATCH', '**/dav/versions/*/versions/**').as('labelVersion')
cy.intercept({ method: 'PROPPATCH', times: 1, url: '**/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')
cy.intercept({ method: 'MOVE', times: 1, url: '**/dav/versions/*/versions/**' }).as('restoreVersion')
triggerVersionAction(index, 'restore')
cy.wait('@restoreVersion')
}

export function deleteVersion(index: number) {
cy.intercept('DELETE', '**/dav/versions/*/versions/**').as('deleteVersion')
cy.intercept({ method: 'DELETE', times: 1, url: '**/dav/versions/*/versions/**' }).as('deleteVersion')
triggerVersionAction(index, 'delete')
cy.wait('@deleteVersion')
}
Expand All @@ -88,12 +88,9 @@ export function doesNotHaveAction(index: number, actionName: string) {
toggleVersionMenu(index)
}

export function assertVersionContent(filename: string, index: number, expectedContent: string) {
const downloadsFolder = Cypress.config('downloadsFolder')

export function assertVersionContent(index: number, expectedContent: string) {
cy.intercept({ method: 'GET', times: 1, url: 'remote.php/**' }).as('downloadVersion')
triggerVersionAction(index, 'download')

return cy.readFile(path.join(downloadsFolder, filename))
.then((versionContent) => expect(versionContent).to.equal(expectedContent))
.then(() => cy.exec(`rm ${downloadsFolder}/${filename}`))
cy.wait('@downloadVersion')
.then(({ response }) => expect(response?.body).to.equal(expectedContent))
}
145 changes: 145 additions & 0 deletions cypress/e2e/files_versions/version_cross_storage_move.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
*
* @author Louis Chmn <louis@chmn.me>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

import type { User } from '@nextcloud/cypress'

import { PERMISSION_DELETE, PERMISSION_READ, PERMISSION_WRITE, addUserToGroup, createGroup, createGroupFolder } from '../groupfoldersUtils'
import { assertVersionContent, nameVersion, openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils'
import { clickOnBreadcumbs, closeSidebar, copyFile, moveFile, navigateToFolder } from '../files/filesUtils'

/**
*
* @param filePath
*/
function assertVersionsContent(filePath: string) {
const path = filePath.split('/').slice(0, -1).join('/')

clickOnBreadcumbs('All files')

if (path !== '') {
navigateToFolder(path)
}

closeSidebar()
openVersionsPanel(filePath)

cy.get('[data-files-versions-version]').should('have.length', 3)
cy.get('[data-files-versions-version]').eq(2).contains('v1')
assertVersionContent(0, 'v3')
assertVersionContent(1, 'v2')
assertVersionContent(2, 'v1')
}

describe('Versions cross storage move', () => {
let randomGroupName: string
let randomGroupFolderName: string
let randomFileName: string
let randomCopiedFileName: string
let user: User

before(() => {
randomGroupName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
randomGroupFolderName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)

cy.createRandomUser().then(_user => { user = _user })
createGroup(randomGroupName)

cy.then(() => {
addUserToGroup(randomGroupName, user.userId)
createGroupFolder(randomGroupFolderName, randomGroupName, [PERMISSION_READ, PERMISSION_WRITE, PERMISSION_DELETE])
})
})

beforeEach(() => {
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
randomFileName = randomString + '.txt'
randomCopiedFileName = randomString + ' (copy).txt'
uploadThreeVersions(user, `${randomGroupFolderName}/${randomFileName}`)

cy.login(user)
cy.visit('/apps/files')
navigateToFolder(randomGroupFolderName)
openVersionsPanel(`${randomGroupFolderName}/${randomFileName}`)
nameVersion(2, 'v1')
})

it('Correctly moves versions to the user\'s FS when the user moves the file out of the groupfolder', () => {
moveFile(randomFileName, '/')

assertVersionsContent(randomFileName)

moveFile(randomFileName, randomGroupFolderName)

assertVersionsContent(`${randomGroupFolderName}/${randomFileName}`)
})

it('Correctly copies versions to the user\'s FS when the user copies the file out of the groupfolder', () => {
copyFile(randomFileName, '/')

assertVersionsContent(randomFileName)

copyFile(randomFileName, randomGroupFolderName)

assertVersionsContent(`${randomGroupFolderName}/${randomCopiedFileName}`)
})

context('When a file is in a subfolder', () => {
let randomSubFolderName
let randomCopiedSubFolderName
let randomSubSubFolderName

beforeEach(() => {
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
randomSubFolderName = randomString
randomCopiedSubFolderName = randomString + ' (copy)'

randomSubSubFolderName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
clickOnBreadcumbs('All files')
cy.mkdir(user, `/${randomGroupFolderName}/${randomSubFolderName}`)
cy.mkdir(user, `/${randomGroupFolderName}/${randomSubFolderName}/${randomSubSubFolderName}`)
cy.login(user)
navigateToFolder(randomGroupFolderName)
})

it('Correctly moves versions when user moves the containing folder out of the groupfolder', () => {
moveFile(randomFileName, `${randomSubFolderName}/${randomSubSubFolderName}`)
moveFile(randomSubFolderName, '/')

assertVersionsContent(`${randomSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)

clickOnBreadcumbs('All files')
moveFile(randomSubFolderName, randomGroupFolderName)
assertVersionsContent(`${randomGroupFolderName}/${randomSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)
})

it('Correctly copies versions when user copies the containing folder out of the groupfolder', () => {
moveFile(randomFileName, `${randomSubFolderName}/${randomSubSubFolderName}`)
copyFile(randomSubFolderName, '/')

assertVersionsContent(`${randomSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)

clickOnBreadcumbs('All files')
copyFile(randomSubFolderName, randomGroupFolderName)
assertVersionsContent(`${randomGroupFolderName}/${randomCopiedSubFolderName}/${randomSubSubFolderName}/${randomFileName}`)
})
})
})
6 changes: 3 additions & 3 deletions cypress/e2e/files_versions/version_download.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('Versions download', () => {
})

it('Download versions and assert their content', () => {
assertVersionContent(randomFileName, 0, 'v3')
assertVersionContent(randomFileName, 1, 'v2')
assertVersionContent(randomFileName, 2, 'v1')
assertVersionContent(0, 'v3')
assertVersionContent(1, 'v2')
assertVersionContent(2, 'v1')
})
})
6 changes: 3 additions & 3 deletions cypress/e2e/files_versions/version_restoration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ describe('Versions restoration', () => {
})

it('Downloads versions and assert there content', () => {
assertVersionContent(randomFileName, 0, 'v1')
assertVersionContent(randomFileName, 1, 'v3')
assertVersionContent(randomFileName, 2, 'v2')
assertVersionContent(0, 'v1')
assertVersionContent(1, 'v3')
assertVersionContent(2, 'v2')
})
})
Loading

0 comments on commit 812cdc3

Please sign in to comment.