FilesSharingUtils.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. /* eslint-disable jsdoc/require-jsdoc */
  6. import { triggerActionForFile } from '../files/FilesUtils'
  7. import { EntryId as FileRequestEntryID } from '../../../apps/files_sharing/src/new/newFileRequest'
  8. export interface ShareSetting {
  9. read: boolean
  10. update: boolean
  11. delete: boolean
  12. share: boolean
  13. download: boolean
  14. }
  15. export function createShare(fileName: string, username: string, shareSettings: Partial<ShareSetting> = {}) {
  16. openSharingPanel(fileName)
  17. cy.get('#app-sidebar-vue').within(() => {
  18. cy.get('#sharing-search-input').clear()
  19. cy.intercept({ times: 1, method: 'GET', url: '**/apps/files_sharing/api/v1/sharees?*' }).as('userSearch')
  20. cy.get('#sharing-search-input').type(username)
  21. cy.wait('@userSearch')
  22. })
  23. cy.get(`[user="${username}"]`).click()
  24. // HACK: Save the share and then update it, as permissions changes are currently not saved for new share.
  25. cy.get('[data-cy-files-sharing-share-editor-action="save"]').click({ scrollBehavior: 'nearest' })
  26. updateShare(fileName, 0, shareSettings)
  27. }
  28. export function updateShare(fileName: string, index: number, shareSettings: Partial<ShareSetting> = {}) {
  29. openSharingPanel(fileName)
  30. cy.intercept({ times: 1, method: 'PUT', url: '**/apps/files_sharing/api/v1/shares/*' }).as('updateShare')
  31. cy.get('#app-sidebar-vue').within(() => {
  32. cy.get('[data-cy-files-sharing-share-actions]').eq(index).click()
  33. cy.get('[data-cy-files-sharing-share-permissions-bundle="custom"]').click()
  34. if (shareSettings.download !== undefined) {
  35. cy.get('[data-cy-files-sharing-share-permissions-checkbox="download"]').find('input').as('downloadCheckbox')
  36. if (shareSettings.download) {
  37. // Force:true because the checkbox is hidden by the pretty UI.
  38. cy.get('@downloadCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  39. } else {
  40. // Force:true because the checkbox is hidden by the pretty UI.
  41. cy.get('@downloadCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  42. }
  43. }
  44. if (shareSettings.read !== undefined) {
  45. cy.get('[data-cy-files-sharing-share-permissions-checkbox="read"]').find('input').as('readCheckbox')
  46. if (shareSettings.read) {
  47. // Force:true because the checkbox is hidden by the pretty UI.
  48. cy.get('@readCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  49. } else {
  50. // Force:true because the checkbox is hidden by the pretty UI.
  51. cy.get('@readCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  52. }
  53. }
  54. if (shareSettings.update !== undefined) {
  55. cy.get('[data-cy-files-sharing-share-permissions-checkbox="update"]').find('input').as('updateCheckbox')
  56. if (shareSettings.update) {
  57. // Force:true because the checkbox is hidden by the pretty UI.
  58. cy.get('@updateCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  59. } else {
  60. // Force:true because the checkbox is hidden by the pretty UI.
  61. cy.get('@updateCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  62. }
  63. }
  64. if (shareSettings.delete !== undefined) {
  65. cy.get('[data-cy-files-sharing-share-permissions-checkbox="delete"]').find('input').as('deleteCheckbox')
  66. if (shareSettings.delete) {
  67. // Force:true because the checkbox is hidden by the pretty UI.
  68. cy.get('@deleteCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  69. } else {
  70. // Force:true because the checkbox is hidden by the pretty UI.
  71. cy.get('@deleteCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  72. }
  73. }
  74. cy.get('[data-cy-files-sharing-share-editor-action="save"]').click({ scrollBehavior: 'nearest' })
  75. cy.wait('@updateShare')
  76. })
  77. }
  78. export function openSharingPanel(fileName: string) {
  79. triggerActionForFile(fileName, 'details')
  80. cy.get('#app-sidebar-vue')
  81. .get('[aria-controls="tab-sharing"]')
  82. .click()
  83. }
  84. type FileRequestOptions = {
  85. label?: string
  86. note?: string
  87. password?: string
  88. /* YYYY-MM-DD format */
  89. expiration?: string
  90. }
  91. /**
  92. * Create a file request for a folder
  93. * @param path The path of the folder, leading slash is required
  94. * @param options The options for the file request
  95. */
  96. export const createFileRequest = (path: string, options: FileRequestOptions = {}) => {
  97. if (!path.startsWith('/')) {
  98. throw new Error('Path must start with a slash')
  99. }
  100. // Navigate to the folder
  101. cy.visit('/apps/files/files?dir=' + path)
  102. // Open the file request dialog
  103. cy.get('[data-cy-upload-picker] .action-item__menutoggle').first().click()
  104. cy.contains('.upload-picker__menu-entry button', 'Create file request').click()
  105. cy.get('[data-cy-file-request-dialog]').should('be.visible')
  106. // Check and fill the first page options
  107. cy.get('[data-cy-file-request-dialog-fieldset="label"]').should('be.visible')
  108. cy.get('[data-cy-file-request-dialog-fieldset="destination"]').should('be.visible')
  109. cy.get('[data-cy-file-request-dialog-fieldset="note"]').should('be.visible')
  110. cy.get('[data-cy-file-request-dialog-fieldset="destination"] input').should('contain.value', path)
  111. if (options.label) {
  112. cy.get('[data-cy-file-request-dialog-fieldset="label"] input').type(`{selectall}${options.label}`)
  113. }
  114. if (options.note) {
  115. cy.get('[data-cy-file-request-dialog-fieldset="note"] textarea').type(`{selectall}${options.note}`)
  116. }
  117. // Go to the next page
  118. cy.get('[data-cy-file-request-dialog-controls="next"]').click()
  119. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="checkbox"]').should('exist')
  120. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="date"]').should('not.exist')
  121. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="checkbox"]').should('exist')
  122. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="password"]').should('not.exist')
  123. if (options.expiration) {
  124. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="checkbox"]').check({ force: true })
  125. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="date"]').type(`{selectall}${options.expiration}`)
  126. }
  127. if (options.password) {
  128. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="checkbox"]').check({ force: true })
  129. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="password"]').type(`{selectall}${options.password}`)
  130. }
  131. // Create the file request
  132. cy.get('[data-cy-file-request-dialog-controls="next"]').click()
  133. // Get the file request URL
  134. cy.get('[data-cy-file-request-dialog-fieldset="link"]').then(($link) => {
  135. const url = $link.val()
  136. cy.log(`File request URL: ${url}`)
  137. cy.wrap(url).as('fileRequestUrl')
  138. })
  139. // Close
  140. cy.get('[data-cy-file-request-dialog-controls="finish"]').click()
  141. }
  142. export const enterGuestName = (name: string) => {
  143. cy.get('[data-cy-public-auth-prompt-dialog]').should('be.visible')
  144. cy.get('[data-cy-public-auth-prompt-dialog-name]').should('be.visible')
  145. cy.get('[data-cy-public-auth-prompt-dialog-submit]').should('be.visible')
  146. cy.get('[data-cy-public-auth-prompt-dialog-name]').type(`{selectall}${name}`)
  147. cy.get('[data-cy-public-auth-prompt-dialog-submit]').click()
  148. cy.get('[data-cy-public-auth-prompt-dialog]').should('not.exist')
  149. }