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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 n/no-unpublished-import */
  23. import axios from '@nextcloud/axios'
  24. import { addCommands, 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. /**
  35. * Upload a file from the fixtures folder to a given user storage.
  36. * **Warning**: Using this function will reset the previous session
  37. */
  38. uploadFile(user: User, fixture?: string, mimeType?: string, target?: string): Cypress.Chainable<void>,
  39. /**
  40. * Reset the admin theming entirely.
  41. * **Warning**: Using this function will reset the previous session
  42. */
  43. resetAdminTheming(): Cypress.Chainable<void>,
  44. /**
  45. * Reset the user theming settings.
  46. * If provided, will clear session and login as the given user.
  47. * **Warning**: Providing a user will reset the previous session.
  48. */
  49. resetUserTheming(user?: User): Cypress.Chainable<void>,
  50. }
  51. }
  52. }
  53. const url = (Cypress.config('baseUrl') || '').replace(/\/index.php\/?$/g, '')
  54. Cypress.env('baseUrl', url)
  55. /**
  56. * cy.uploadedFile - uploads a file from the fixtures folder
  57. * TODO: standardise in @nextcloud/cypress
  58. *
  59. * @param {User} user the owner of the file, e.g. admin
  60. * @param {string} fixture the fixture file name, e.g. image1.jpg
  61. * @param {string} mimeType e.g. image/png
  62. * @param {string} [target] the target of the file relative to the user root
  63. */
  64. Cypress.Commands.add('uploadFile', (user, fixture = 'image.jpg', mimeType = 'image/jpeg', target = `/${fixture}`) => {
  65. cy.clearCookies()
  66. const fileName = basename(target)
  67. // get fixture
  68. return cy.fixture(fixture, 'base64').then(async file => {
  69. // convert the base64 string to a blob
  70. const blob = Cypress.Blob.base64StringToBlob(file, mimeType)
  71. // Process paths
  72. const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}`
  73. const filePath = target.split('/').map(encodeURIComponent).join('/')
  74. try {
  75. const file = new File([blob], fileName, { type: mimeType })
  76. await axios({
  77. url: `${rootPath}${filePath}`,
  78. method: 'PUT',
  79. data: file,
  80. headers: {
  81. 'Content-Type': mimeType,
  82. },
  83. auth: {
  84. username: user.userId,
  85. password: user.password,
  86. },
  87. }).then(response => {
  88. cy.log(`Uploaded ${fixture} as ${fileName}`, response)
  89. })
  90. } catch (error) {
  91. cy.log('error', error)
  92. throw new Error(`Unable to process fixture ${fixture}`)
  93. }
  94. })
  95. })
  96. /**
  97. * Reset the admin theming entirely
  98. */
  99. Cypress.Commands.add('resetAdminTheming', () => {
  100. const admin = new User('admin', 'admin')
  101. cy.clearCookies()
  102. cy.login(admin)
  103. // Clear all settings
  104. cy.request('/csrftoken').then(({ body }) => {
  105. const requestToken = body.token
  106. axios({
  107. method: 'POST',
  108. url: '/index.php/apps/theming/ajax/undoAllChanges',
  109. headers: {
  110. requesttoken: requestToken,
  111. },
  112. })
  113. })
  114. // Clear admin session
  115. cy.clearCookies()
  116. })
  117. /**
  118. * Reset the current or provided user theming settings
  119. * It does not reset the theme config as it is enforced in the
  120. * server config for cypress testing.
  121. */
  122. Cypress.Commands.add('resetUserTheming', (user?: User) => {
  123. if (user) {
  124. cy.clearCookies()
  125. cy.login(user)
  126. }
  127. // Reset background config
  128. cy.request('/csrftoken').then(({ body }) => {
  129. const requestToken = body.token
  130. cy.request({
  131. method: 'POST',
  132. url: '/apps/theming/background/default',
  133. headers: {
  134. requesttoken: requestToken,
  135. },
  136. })
  137. })
  138. if (user) {
  139. // Clear current session
  140. cy.clearCookies()
  141. }
  142. })