commands.ts 5.4 KB

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