1
0

setup-public-share.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*!
  2. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import type { User } from '@nextcloud/cypress'
  6. import { openSharingPanel } from '../FilesSharingUtils.ts'
  7. let user: User
  8. let url: string
  9. /**
  10. * URL of the share
  11. */
  12. export function getShareUrl() {
  13. if (url === undefined) {
  14. throw new Error('You need to setup the share first!')
  15. }
  16. return url
  17. }
  18. /**
  19. * Setup the available data
  20. * @param shareName The name of the shared folder
  21. */
  22. function setupData(shareName: string) {
  23. cy.mkdir(user, `/${shareName}`)
  24. cy.mkdir(user, `/${shareName}/subfolder`)
  25. cy.uploadContent(user, new Blob(['<content>foo</content>']), 'text/plain', `/${shareName}/foo.txt`)
  26. cy.uploadContent(user, new Blob(['<content>bar</content>']), 'text/plain', `/${shareName}/subfolder/bar.txt`)
  27. }
  28. /**
  29. * Create a public link share
  30. * @param shareName The name of the shared folder
  31. */
  32. function createShare(shareName: string) {
  33. cy.login(user)
  34. // open the files app
  35. cy.visit('/apps/files')
  36. // open the sidebar
  37. openSharingPanel(shareName)
  38. // create the share
  39. cy.intercept('POST', '**/ocs/v2.php/apps/files_sharing/api/v1/shares').as('createShare')
  40. cy.findByRole('button', { name: 'Create a new share link' })
  41. .click()
  42. // extract the link
  43. return cy.wait('@createShare')
  44. .should(({ response }) => {
  45. const { ocs } = response!.body
  46. url = ocs?.data.url
  47. expect(url).to.match(/^http:\/\//)
  48. })
  49. .then(() => cy.wrap(url))
  50. }
  51. /**
  52. * Adjust share permissions to be editable
  53. */
  54. function adjustSharePermission() {
  55. // Update the share to be a file drop
  56. cy.findByRole('list', { name: 'Link shares' })
  57. .findAllByRole('listitem')
  58. .first()
  59. .findByRole('button', { name: /Actions/i })
  60. .click()
  61. cy.findByRole('menuitem', { name: /Customize link/i })
  62. .should('be.visible')
  63. .click()
  64. // Enable upload-edit
  65. cy.get('[data-cy-files-sharing-share-permissions-bundle]')
  66. .should('be.visible')
  67. cy.get('[data-cy-files-sharing-share-permissions-bundle="upload-edit"]')
  68. .click()
  69. // save changes
  70. cy.intercept('PUT', '**/ocs/v2.php/apps/files_sharing/api/v1/shares/*').as('updateShare')
  71. cy.findByRole('button', { name: 'Update share' })
  72. .click()
  73. cy.wait('@updateShare')
  74. }
  75. /**
  76. * Setup a public share and backup the state.
  77. * If the setup was already done in another run, the state will be restored.
  78. *
  79. * @return The URL of the share
  80. */
  81. export function setupPublicShare(): Cypress.Chainable<string> {
  82. const shareName = 'shared'
  83. return cy.task('getVariable', { key: 'public-share-data' })
  84. .then((data) => {
  85. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  86. const { dataSnapshot, dbSnapshot, shareUrl } = data as any || {}
  87. if (dataSnapshot && dbSnapshot) {
  88. cy.restoreDB(dbSnapshot)
  89. cy.restoreData(dataSnapshot)
  90. url = shareUrl
  91. return cy.wrap(shareUrl as string)
  92. } else {
  93. cy.restoreData()
  94. cy.restoreDB()
  95. const shareData: Record<string, unknown> = {}
  96. return cy.createRandomUser()
  97. .then(($user) => { user = $user })
  98. .then(() => setupData(shareName))
  99. .then(() => createShare(shareName))
  100. .then((value) => { shareData.shareUrl = value })
  101. .then(() => adjustSharePermission())
  102. .then(() => cy.backupDB().then((value) => { shareData.dbSnapshot = value }))
  103. .then(() => cy.backupData([user.userId]).then((value) => { shareData.dataSnapshot = value }))
  104. .then(() => cy.task('setVariable', { key: 'public-share-data', value: shareData }))
  105. .then(() => cy.log(`Public share setup, URL: ${shareData.shareUrl}`))
  106. .then(() => cy.wrap(url))
  107. }
  108. })
  109. }