FilesSharingUtils.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. export interface ShareSetting {
  8. read: boolean
  9. update: boolean
  10. delete: boolean
  11. share: boolean
  12. download: boolean
  13. note: string
  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. if (shareSettings.note !== undefined) {
  75. cy.findByRole('checkbox', { name: /note to recipient/i }).check({ force: true, scrollBehavior: 'nearest' })
  76. cy.findByRole('textbox', { name: /note to recipient/i }).type(shareSettings.note)
  77. }
  78. cy.get('[data-cy-files-sharing-share-editor-action="save"]').click({ scrollBehavior: 'nearest' })
  79. cy.wait('@updateShare')
  80. })
  81. }
  82. export function openSharingPanel(fileName: string) {
  83. triggerActionForFile(fileName, 'details')
  84. cy.get('#app-sidebar-vue')
  85. .get('[aria-controls="tab-sharing"]')
  86. .click()
  87. }
  88. type FileRequestOptions = {
  89. label?: string
  90. note?: string
  91. password?: string
  92. /* YYYY-MM-DD format */
  93. expiration?: string
  94. }
  95. /**
  96. * Create a file request for a folder
  97. * @param path The path of the folder, leading slash is required
  98. * @param options The options for the file request
  99. */
  100. export const createFileRequest = (path: string, options: FileRequestOptions = {}) => {
  101. if (!path.startsWith('/')) {
  102. throw new Error('Path must start with a slash')
  103. }
  104. // Navigate to the folder
  105. cy.visit('/apps/files/files?dir=' + path)
  106. // Open the file request dialog
  107. cy.get('[data-cy-upload-picker] .action-item__menutoggle').first().click()
  108. cy.contains('.upload-picker__menu-entry button', 'Create file request').click()
  109. cy.get('[data-cy-file-request-dialog]').should('be.visible')
  110. // Check and fill the first page options
  111. cy.get('[data-cy-file-request-dialog-fieldset="label"]').should('be.visible')
  112. cy.get('[data-cy-file-request-dialog-fieldset="destination"]').should('be.visible')
  113. cy.get('[data-cy-file-request-dialog-fieldset="note"]').should('be.visible')
  114. cy.get('[data-cy-file-request-dialog-fieldset="destination"] input').should('contain.value', path)
  115. if (options.label) {
  116. cy.get('[data-cy-file-request-dialog-fieldset="label"] input').type(`{selectall}${options.label}`)
  117. }
  118. if (options.note) {
  119. cy.get('[data-cy-file-request-dialog-fieldset="note"] textarea').type(`{selectall}${options.note}`)
  120. }
  121. // Go to the next page
  122. cy.get('[data-cy-file-request-dialog-controls="next"]').click()
  123. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="checkbox"]').should('exist')
  124. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="date"]').should('not.exist')
  125. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="checkbox"]').should('exist')
  126. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="password"]').should('not.exist')
  127. if (options.expiration) {
  128. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="checkbox"]').check({ force: true })
  129. cy.get('[data-cy-file-request-dialog-fieldset="expiration"] input[type="date"]').type(`{selectall}${options.expiration}`)
  130. }
  131. if (options.password) {
  132. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="checkbox"]').check({ force: true })
  133. cy.get('[data-cy-file-request-dialog-fieldset="password"] input[type="password"]').type(`{selectall}${options.password}`)
  134. }
  135. // Create the file request
  136. cy.get('[data-cy-file-request-dialog-controls="next"]').click()
  137. // Get the file request URL
  138. cy.get('[data-cy-file-request-dialog-fieldset="link"]').then(($link) => {
  139. const url = $link.val()
  140. cy.log(`File request URL: ${url}`)
  141. cy.wrap(url).as('fileRequestUrl')
  142. })
  143. // Close
  144. cy.get('[data-cy-file-request-dialog-controls="finish"]').click()
  145. }
  146. export const enterGuestName = (name: string) => {
  147. cy.get('[data-cy-public-auth-prompt-dialog]').should('be.visible')
  148. cy.get('[data-cy-public-auth-prompt-dialog-name]').should('be.visible')
  149. cy.get('[data-cy-public-auth-prompt-dialog-submit]').should('be.visible')
  150. cy.get('[data-cy-public-auth-prompt-dialog-name]').type(`{selectall}${name}`)
  151. cy.get('[data-cy-public-auth-prompt-dialog-submit]').click()
  152. cy.get('[data-cy-public-auth-prompt-dialog]').should('not.exist')
  153. }