commands.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. 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. }).then(response => {
  111. cy.log(`Uploaded content as ${fileName}`, response)
  112. })
  113. } catch (error) {
  114. cy.log('error', error)
  115. throw new Error(`Unable to process fixture`)
  116. }
  117. })
  118. })
  119. /**
  120. * Reset the admin theming entirely
  121. */
  122. Cypress.Commands.add('resetAdminTheming', () => {
  123. const admin = new User('admin', 'admin')
  124. cy.clearCookies()
  125. cy.login(admin)
  126. // Clear all settings
  127. cy.request('/csrftoken').then(({ body }) => {
  128. const requestToken = body.token
  129. axios({
  130. method: 'POST',
  131. url: '/index.php/apps/theming/ajax/undoAllChanges',
  132. headers: {
  133. requesttoken: requestToken,
  134. },
  135. })
  136. })
  137. // Clear admin session
  138. cy.clearCookies()
  139. })
  140. /**
  141. * Reset the current or provided user theming settings
  142. * It does not reset the theme config as it is enforced in the
  143. * server config for cypress testing.
  144. */
  145. Cypress.Commands.add('resetUserTheming', (user?: User) => {
  146. if (user) {
  147. cy.clearCookies()
  148. cy.login(user)
  149. }
  150. // Reset background config
  151. cy.request('/csrftoken').then(({ body }) => {
  152. const requestToken = body.token
  153. cy.request({
  154. method: 'POST',
  155. url: '/apps/theming/background/default',
  156. headers: {
  157. requesttoken: requestToken,
  158. },
  159. })
  160. })
  161. if (user) {
  162. // Clear current session
  163. cy.clearCookies()
  164. }
  165. })
  166. Cypress.Commands.add('runOccCommand', (command: string) => {
  167. cy.exec(`docker exec --user www-data nextcloud-cypress-tests-server php ./occ ${command}`)
  168. })