filesSharingUtils.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* eslint-disable jsdoc/require-jsdoc */
  2. /**
  3. * @copyright Copyright (c) 2024 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 { triggerActionForFile } from '../files/FilesUtils'
  24. export interface ShareSetting {
  25. read: boolean
  26. update: boolean
  27. delete: boolean
  28. share: boolean
  29. download: boolean
  30. }
  31. export function createShare(fileName: string, username: string, shareSettings: Partial<ShareSetting> = {}) {
  32. openSharingPanel(fileName)
  33. cy.get('#app-sidebar-vue').within(() => {
  34. cy.get('#sharing-search-input').clear()
  35. cy.intercept({ times: 1, method: 'GET', url: '**/apps/files_sharing/api/v1/sharees?*' }).as('userSearch')
  36. cy.get('#sharing-search-input').type(username)
  37. cy.wait('@userSearch')
  38. })
  39. cy.get(`[user="${username}"]`).click()
  40. // HACK: Save the share and then update it, as permissions changes are currently not saved for new share.
  41. cy.get('[data-cy-files-sharing-share-editor-action="save"]').click({ scrollBehavior: 'nearest' })
  42. updateShare(fileName, 0, shareSettings)
  43. }
  44. export function updateShare(fileName: string, index: number, shareSettings: Partial<ShareSetting> = {}) {
  45. openSharingPanel(fileName)
  46. cy.get('#app-sidebar-vue').within(() => {
  47. cy.get('[data-cy-files-sharing-share-actions]').eq(index).click()
  48. cy.get('[data-cy-files-sharing-share-permissions-bundle="custom"]').click()
  49. if (shareSettings.download !== undefined) {
  50. cy.get('[data-cy-files-sharing-share-permissions-checkbox="download"]').find('input').as('downloadCheckbox')
  51. if (shareSettings.download) {
  52. // Force:true because the checkbox is hidden by the pretty UI.
  53. cy.get('@downloadCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  54. } else {
  55. // Force:true because the checkbox is hidden by the pretty UI.
  56. cy.get('@downloadCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  57. }
  58. }
  59. if (shareSettings.read !== undefined) {
  60. cy.get('[data-cy-files-sharing-share-permissions-checkbox="read"]').find('input').as('readCheckbox')
  61. if (shareSettings.read) {
  62. // Force:true because the checkbox is hidden by the pretty UI.
  63. cy.get('@readCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  64. } else {
  65. // Force:true because the checkbox is hidden by the pretty UI.
  66. cy.get('@readCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  67. }
  68. }
  69. if (shareSettings.update !== undefined) {
  70. cy.get('[data-cy-files-sharing-share-permissions-checkbox="update"]').find('input').as('updateCheckbox')
  71. if (shareSettings.update) {
  72. // Force:true because the checkbox is hidden by the pretty UI.
  73. cy.get('@updateCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  74. } else {
  75. // Force:true because the checkbox is hidden by the pretty UI.
  76. cy.get('@updateCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  77. }
  78. }
  79. if (shareSettings.delete !== undefined) {
  80. cy.get('[data-cy-files-sharing-share-permissions-checkbox="delete"]').find('input').as('deleteCheckbox')
  81. if (shareSettings.delete) {
  82. // Force:true because the checkbox is hidden by the pretty UI.
  83. cy.get('@deleteCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  84. } else {
  85. // Force:true because the checkbox is hidden by the pretty UI.
  86. cy.get('@deleteCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  87. }
  88. }
  89. cy.get('[data-cy-files-sharing-share-editor-action="save"]').click({ scrollBehavior: 'nearest' })
  90. })
  91. }
  92. export function openSharingPanel(fileName: string) {
  93. triggerActionForFile(fileName, 'details')
  94. cy.get('#app-sidebar-vue')
  95. .get('[aria-controls="tab-sharing"]')
  96. .click()
  97. }