version_restoration.cy.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
  3. *
  4. * @author Louis Chmn <louis@chmn.me>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import type { User } from '@nextcloud/cypress'
  23. import { assertVersionContent, doesNotHaveAction, openVersionsPanel, setupTestSharedFileFromUser, restoreVersion, uploadThreeVersions } from './filesVersionsUtils'
  24. import { getRowForFile } from '../files/FilesUtils'
  25. describe('Versions restoration', () => {
  26. let randomFileName = ''
  27. let user: User
  28. before(() => {
  29. randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt'
  30. cy.createRandomUser()
  31. .then((_user) => {
  32. user = _user
  33. uploadThreeVersions(user, randomFileName)
  34. cy.login(user)
  35. cy.visit('/apps/files')
  36. openVersionsPanel(randomFileName)
  37. })
  38. })
  39. it('Current version does not have restore action', () => {
  40. doesNotHaveAction(0, 'restore')
  41. })
  42. it('Restores initial version', () => {
  43. restoreVersion(2)
  44. cy.get('#tab-version_vue').within(() => {
  45. cy.get('[data-files-versions-version]').should('have.length', 3)
  46. cy.get('[data-files-versions-version]').eq(0).contains('Current version')
  47. cy.get('[data-files-versions-version]').eq(2).contains('Initial version').should('not.exist')
  48. })
  49. })
  50. it('Downloads versions and assert there content', () => {
  51. assertVersionContent(0, 'v1')
  52. assertVersionContent(1, 'v3')
  53. assertVersionContent(2, 'v2')
  54. })
  55. context('Restore versions of shared file', () => {
  56. it('Works with update permission', () => {
  57. setupTestSharedFileFromUser(user, randomFileName, { update: true })
  58. openVersionsPanel(randomFileName)
  59. it('Restores initial version', () => {
  60. restoreVersion(2)
  61. cy.get('#tab-version_vue').within(() => {
  62. cy.get('[data-files-versions-version]').should('have.length', 3)
  63. cy.get('[data-files-versions-version]').eq(0).contains('Current version')
  64. cy.get('[data-files-versions-version]').eq(2).contains('Initial version').should('not.exist')
  65. })
  66. })
  67. it('Downloads versions and assert there content', () => {
  68. assertVersionContent(0, 'v1')
  69. assertVersionContent(1, 'v3')
  70. assertVersionContent(2, 'v2')
  71. })
  72. })
  73. it('Does not show action without delete permission', () => {
  74. setupTestSharedFileFromUser(user, randomFileName, { update: false })
  75. openVersionsPanel(randomFileName)
  76. cy.get('[data-files-versions-version]').eq(0).find('.action-item__menutoggle').should('not.exist')
  77. cy.get('[data-files-versions-version]').eq(0).get('[data-cy-version-action="restore"]').should('not.exist')
  78. doesNotHaveAction(1, 'restore')
  79. doesNotHaveAction(2, 'restore')
  80. })
  81. it('Does not work without update permission through direct API access', () => {
  82. let hostname: string
  83. let fileId: string|undefined
  84. let versionId: string|undefined
  85. setupTestSharedFileFromUser(user, randomFileName, { update: false })
  86. .then(recipient => {
  87. openVersionsPanel(randomFileName)
  88. cy.url().then(url => { hostname = new URL(url).hostname })
  89. getRowForFile(randomFileName).invoke('attr', 'data-cy-files-list-row-fileid').then(_fileId => { fileId = _fileId })
  90. cy.get('[data-files-versions-version]').eq(1).invoke('attr', 'data-files-versions-version').then(_versionId => { versionId = _versionId })
  91. cy.then(() => {
  92. cy.logout()
  93. cy.request({
  94. method: 'MOVE',
  95. auth: { user: recipient.userId, pass: recipient.password },
  96. headers: {
  97. cookie: '',
  98. Destination: `http://${hostname}/remote.php/dav/versions/${recipient.userId}/restore/target`,
  99. },
  100. url: `http://${hostname}/remote.php/dav/versions/${recipient.userId}/versions/${fileId}/${versionId}`,
  101. failOnStatusCode: false,
  102. })
  103. .then(({ status }) => {
  104. expect(status).to.equal(403)
  105. })
  106. })
  107. })
  108. })
  109. })
  110. })