LivePhotosUtils.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. type SetupInfo = {
  7. snapshot: string
  8. jpgFileId: number
  9. movFileId: number
  10. fileName: string
  11. user: User
  12. }
  13. /**
  14. *
  15. * @param user
  16. * @param fileName
  17. * @param domain
  18. * @param requesttoken
  19. * @param metadata
  20. */
  21. function setMetadata(user: User, fileName: string, requesttoken: string, metadata: object) {
  22. cy.url().then(url => {
  23. const hostname = new URL(url).hostname
  24. cy.request({
  25. method: 'PROPPATCH',
  26. url: `http://${hostname}/remote.php/dav/files/${user.userId}/${fileName}`,
  27. auth: { user: user.userId, pass: user.password },
  28. headers: {
  29. requesttoken,
  30. },
  31. body: `<?xml version="1.0"?>
  32. <d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">
  33. <d:set>
  34. <d:prop>
  35. ${Object.entries(metadata).map(([key, value]) => `<${key}>${value}</${key}>`).join('\n')}
  36. </d:prop>
  37. </d:set>
  38. </d:propertyupdate>`,
  39. })
  40. })
  41. }
  42. /**
  43. *
  44. * @param enable
  45. */
  46. export function setShowHiddenFiles(enable: boolean) {
  47. cy.get('[data-cy-files-navigation-settings-button]').click()
  48. // Force:true because the checkbox is hidden by the pretty UI.
  49. if (enable) {
  50. cy.get('[data-cy-files-settings-setting="show_hidden"] input').check({ force: true })
  51. } else {
  52. cy.get('[data-cy-files-settings-setting="show_hidden"] input').uncheck({ force: true })
  53. }
  54. cy.get('[data-cy-files-navigation-settings]').type('{esc}')
  55. }
  56. /**
  57. *
  58. */
  59. export function setupLivePhotos(): Cypress.Chainable<SetupInfo> {
  60. return cy.task('getVariable', { key: 'live-photos-data' })
  61. .then((_setupInfo) => {
  62. const setupInfo = _setupInfo as SetupInfo || {}
  63. if (setupInfo.snapshot) {
  64. cy.restoreState(setupInfo.snapshot)
  65. } else {
  66. let requesttoken: string
  67. setupInfo.fileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
  68. cy.createRandomUser().then(_user => { setupInfo.user = _user })
  69. cy.then(() => {
  70. cy.uploadContent(setupInfo.user, new Blob(['jpg file'], { type: 'image/jpg' }), 'image/jpg', `/${setupInfo.fileName}.jpg`)
  71. .then(response => { setupInfo.jpgFileId = parseInt(response.headers['oc-fileid']) })
  72. cy.uploadContent(setupInfo.user, new Blob(['mov file'], { type: 'video/mov' }), 'video/mov', `/${setupInfo.fileName}.mov`)
  73. .then(response => { setupInfo.movFileId = parseInt(response.headers['oc-fileid']) })
  74. cy.login(setupInfo.user)
  75. })
  76. cy.visit('/apps/files')
  77. cy.get('head').invoke('attr', 'data-requesttoken').then(_requesttoken => { requesttoken = _requesttoken as string })
  78. cy.then(() => {
  79. setMetadata(setupInfo.user, `${setupInfo.fileName}.jpg`, requesttoken, { 'nc:metadata-files-live-photo': setupInfo.movFileId })
  80. setMetadata(setupInfo.user, `${setupInfo.fileName}.mov`, requesttoken, { 'nc:metadata-files-live-photo': setupInfo.jpgFileId })
  81. })
  82. cy.then(() => {
  83. cy.saveState().then((value) => { setupInfo.snapshot = value })
  84. cy.task('setVariable', { key: 'live-photos-data', value: setupInfo })
  85. })
  86. }
  87. return cy.then(() => {
  88. cy.login(setupInfo.user)
  89. cy.visit('/apps/files')
  90. return cy.wrap(setupInfo)
  91. })
  92. })
  93. }