filesVersionsUtils.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* eslint-disable jsdoc/require-jsdoc */
  2. /**
  3. * @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
  4. *
  5. * @author Louis Chemineau <louis@chmn.me>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. import type { User } from '@nextcloud/cypress'
  24. import path from 'path'
  25. import { createShare, type ShareSetting } from '../files_sharing/filesSharingUtils'
  26. export const uploadThreeVersions = (user: User, fileName: string) => {
  27. // A new version will not be created if the changes occur
  28. // within less than one second of each other.
  29. // eslint-disable-next-line cypress/no-unnecessary-waiting
  30. cy.uploadContent(user, new Blob(['v1'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
  31. .wait(1100)
  32. .uploadContent(user, new Blob(['v2'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
  33. .wait(1100)
  34. .uploadContent(user, new Blob(['v3'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
  35. cy.login(user)
  36. }
  37. export function openVersionsPanel(fileName: string) {
  38. // Detect the versions list fetch
  39. cy.intercept('PROPFIND', '**/dav/versions/*/versions/**').as('getVersions')
  40. // Open the versions tab
  41. cy.window().then(win => {
  42. win.OCA.Files.Sidebar.setActiveTab('version_vue')
  43. win.OCA.Files.Sidebar.open(`/${fileName}`)
  44. })
  45. // Wait for the versions list to be fetched
  46. cy.wait('@getVersions')
  47. cy.get('#tab-version_vue').should('be.visible', { timeout: 10000 })
  48. }
  49. export function toggleVersionMenu(index: number) {
  50. cy.get('#tab-version_vue [data-files-versions-version]')
  51. .eq(index)
  52. .find('button')
  53. .click()
  54. }
  55. export function triggerVersionAction(index: number, actionName: string) {
  56. toggleVersionMenu(index)
  57. cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).filter(':visible').click()
  58. }
  59. export function nameVersion(index: number, name: string) {
  60. cy.intercept('PROPPATCH', '**/dav/versions/*/versions/**').as('labelVersion')
  61. triggerVersionAction(index, 'label')
  62. cy.get(':focused').type(`${name}{enter}`)
  63. cy.wait('@labelVersion')
  64. }
  65. export function restoreVersion(index: number) {
  66. cy.intercept('MOVE', '**/dav/versions/*/versions/**').as('restoreVersion')
  67. triggerVersionAction(index, 'restore')
  68. cy.wait('@restoreVersion')
  69. }
  70. export function deleteVersion(index: number) {
  71. cy.intercept('DELETE', '**/dav/versions/*/versions/**').as('deleteVersion')
  72. triggerVersionAction(index, 'delete')
  73. cy.wait('@deleteVersion')
  74. }
  75. export function doesNotHaveAction(index: number, actionName: string) {
  76. toggleVersionMenu(index)
  77. cy.get(`[data-cy-files-versions-version-action="${actionName}"]`).should('not.exist')
  78. toggleVersionMenu(index)
  79. }
  80. export function assertVersionContent(index: number, expectedContent: string) {
  81. cy.intercept({ method: 'GET', times: 1, url: 'remote.php/**' }).as('downloadVersion')
  82. triggerVersionAction(index, 'download')
  83. cy.wait('@downloadVersion')
  84. .then(({ response }) => expect(response?.body).to.equal(expectedContent))
  85. }
  86. export function setupTestSharedFileFromUser(owner: User, randomFileName: string, shareOptions: Partial<ShareSetting>) {
  87. return cy.createRandomUser()
  88. .then((recipient) => {
  89. cy.login(owner)
  90. cy.visit('/apps/files')
  91. createShare(randomFileName, recipient.userId, shareOptions)
  92. cy.login(recipient)
  93. cy.visit('/apps/files')
  94. return cy.wrap(recipient)
  95. })
  96. }