filesSharingUtils.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. cy.get('@downloadCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  53. } else {
  54. cy.get('@downloadCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  55. }
  56. }
  57. if (shareSettings.read !== undefined) {
  58. cy.get('[data-cy-files-sharing-share-permissions-checkbox="read"]').find('input').as('readCheckbox')
  59. if (shareSettings.read) {
  60. cy.get('@readCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  61. } else {
  62. cy.get('@readCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  63. }
  64. }
  65. if (shareSettings.update !== undefined) {
  66. cy.get('[data-cy-files-sharing-share-permissions-checkbox="update"]').find('input').as('updateCheckbox')
  67. if (shareSettings.update) {
  68. cy.get('@updateCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  69. } else {
  70. cy.get('@updateCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  71. }
  72. }
  73. if (shareSettings.delete !== undefined) {
  74. cy.get('[data-cy-files-sharing-share-permissions-checkbox="delete"]').find('input').as('deleteCheckbox')
  75. if (shareSettings.delete) {
  76. cy.get('@deleteCheckbox').check({ force: true, scrollBehavior: 'nearest' })
  77. } else {
  78. cy.get('@deleteCheckbox').uncheck({ force: true, scrollBehavior: 'nearest' })
  79. }
  80. }
  81. cy.get('[data-cy-files-sharing-share-editor-action="save"]').click({ scrollBehavior: 'nearest' })
  82. })
  83. }
  84. export function openSharingPanel(fileName: string) {
  85. triggerActionForFile(fileName, 'details')
  86. cy.get('#app-sidebar-vue')
  87. .get('[aria-controls="tab-sharing"]')
  88. .click()
  89. }