You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commands.ts 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /* eslint-disable node/no-unpublished-import */
  23. import axios from '@nextcloud/axios'
  24. import { addCommands, type User} from '@nextcloud/cypress'
  25. import { basename } from 'path'
  26. // Add custom commands
  27. import 'cypress-wait-until'
  28. addCommands()
  29. // Register this file's custom commands types
  30. declare global {
  31. // eslint-disable-next-line @typescript-eslint/no-namespace
  32. namespace Cypress {
  33. interface Chainable<Subject = any> {
  34. uploadFile(user: User, fixture: string, mimeType: string, target ?: string): Cypress.Chainable<void>
  35. }
  36. }
  37. }
  38. const url = (Cypress.config('baseUrl') || '').replace(/\/index.php\/?$/g, '')
  39. Cypress.env('baseUrl', url)
  40. /**
  41. * cy.uploadedFile - uploads a file from the fixtures folder
  42. * TODO: standardise in @nextcloud/cypress
  43. *
  44. * @param {User} user the owner of the file, e.g. admin
  45. * @param {string} fixture the fixture file name, e.g. image1.jpg
  46. * @param {string} mimeType e.g. image/png
  47. * @param {string} [target] the target of the file relative to the user root
  48. */
  49. Cypress.Commands.add('uploadFile', (user, fixture, mimeType, target = `/${fixture}`) => {
  50. cy.clearCookies()
  51. const fileName = basename(target)
  52. // get fixture
  53. return cy.fixture(fixture, 'base64').then(async file => {
  54. // convert the base64 string to a blob
  55. const blob = Cypress.Blob.base64StringToBlob(file, mimeType)
  56. // Process paths
  57. const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}`
  58. const filePath = target.split('/').map(encodeURIComponent).join('/')
  59. try {
  60. const file = new File([blob], fileName, { type: mimeType })
  61. await axios({
  62. url: `${rootPath}${filePath}`,
  63. method: 'PUT',
  64. data: file,
  65. headers: {
  66. 'Content-Type': mimeType,
  67. },
  68. auth: {
  69. username: user.userId,
  70. password: user.password,
  71. },
  72. }).then(response => {
  73. cy.log(`Uploaded ${fixture} as ${fileName}`, response)
  74. })
  75. } catch (error) {
  76. cy.log('error', error)
  77. throw new Error(`Unable to process fixture ${fixture}`)
  78. }
  79. })
  80. })