Skip to content

Commit

Permalink
test(files_versions): Add tests for files versions
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Mar 13, 2024
1 parent 2a8062a commit bb808fc
Show file tree
Hide file tree
Showing 8 changed files with 796 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cypress/e2e/files/filesUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
*
* @author Ferdinand Thiessen <opensource@fthiessen.de>
*
* @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/>.
*
*/

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()
}
99 changes: 99 additions & 0 deletions cypress/e2e/files_versions/filesVersionsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint-disable jsdoc/require-jsdoc */
/**
* @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <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 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}`))
}
79 changes: 79 additions & 0 deletions cypress/e2e/files_versions/version_creation.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @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, 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')
})
})
})
110 changes: 110 additions & 0 deletions cypress/e2e/files_versions/version_deletion.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @copyright Copyright (c) 2024 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 { 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)
})
})
})
})
})
})
Loading

0 comments on commit bb808fc

Please sign in to comment.