commands.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-if'
  28. import 'cypress-wait-until'
  29. addCommands()
  30. // Register this file's custom commands types
  31. declare global {
  32. // eslint-disable-next-line @typescript-eslint/no-namespace
  33. namespace Cypress {
  34. interface Chainable<Subject = any> {
  35. /**
  36. * Enable or disable a given user
  37. */
  38. enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>,
  39. /**
  40. * Upload a file from the fixtures folder to a given user storage.
  41. * **Warning**: Using this function will reset the previous session
  42. */
  43. uploadFile(user: User, fixture?: string, mimeType?: string, target?: string): Cypress.Chainable<void>,
  44. /**
  45. * Upload a raw content to a given user storage.
  46. * **Warning**: Using this function will reset the previous session
  47. */
  48. uploadContent(user: User, content: Blob, mimeType: string, target: string): Cypress.Chainable<void>,
  49. /**
  50. * Reset the admin theming entirely.
  51. * **Warning**: Using this function will reset the previous session
  52. */
  53. resetAdminTheming(): Cypress.Chainable<void>,
  54. /**
  55. * Reset the user theming settings.
  56. * If provided, will clear session and login as the given user.
  57. * **Warning**: Providing a user will reset the previous session.
  58. */
  59. resetUserTheming(user?: User): Cypress.Chainable<void>,
  60. /**
  61. * Run an occ command in the docker container.
  62. */
  63. runOccCommand(command: string): Cypress.Chainable<void>,
  64. }
  65. }
  66. }
  67. const url = (Cypress.config('baseUrl') || '').replace(/\/index.php\/?$/g, '')
  68. Cypress.env('baseUrl', url)
  69. /**
  70. * Enable or disable a user
  71. * TODO: standardise in @nextcloud/cypress
  72. *
  73. * @param {User} user the user to dis- / enable
  74. * @param {boolean} enable True if the user should be enable, false to disable
  75. */
  76. Cypress.Commands.add('enableUser', (user: User, enable = true) => {
  77. const url = `${Cypress.config('baseUrl')}/ocs/v2.php/cloud/users/${user.userId}/${enable ? 'enable' : 'disable'}`.replace('index.php/', '')
  78. return cy.request({
  79. method: 'PUT',
  80. url,
  81. form: true,
  82. auth: {
  83. user: 'admin',
  84. password: 'admin',
  85. },
  86. headers: {
  87. 'OCS-ApiRequest': 'true',
  88. 'Content-Type': 'application/x-www-form-urlencoded',
  89. },
  90. }).then((response) => {
  91. cy.log(`Enabled user ${user}`, response.status)
  92. return cy.wrap(response)
  93. })
  94. })
  95. /**
  96. * cy.uploadedFile - uploads a file from the fixtures folder
  97. * TODO: standardise in @nextcloud/cypress
  98. *
  99. * @param {User} user the owner of the file, e.g. admin
  100. * @param {string} fixture the fixture file name, e.g. image1.jpg
  101. * @param {string} mimeType e.g. image/png
  102. * @param {string} [target] the target of the file relative to the user root
  103. */
  104. Cypress.Commands.add('uploadFile', (user, fixture = 'image.jpg', mimeType = 'image/jpeg', target = `/${fixture}`) => {
  105. // get fixture
  106. return cy.fixture(fixture, 'base64').then(async file => {
  107. // convert the base64 string to a blob
  108. const blob = Cypress.Blob.base64StringToBlob(file, mimeType)
  109. cy.uploadContent(user, blob, mimeType, target)
  110. })
  111. })
  112. /**
  113. * cy.uploadedContent - uploads a raw content
  114. * TODO: standardise in @nextcloud/cypress
  115. *
  116. * @param {User} user the owner of the file, e.g. admin
  117. * @param {Blob} blob the content to upload
  118. * @param {string} mimeType e.g. image/png
  119. * @param {string} target the target of the file relative to the user root
  120. */
  121. Cypress.Commands.add('uploadContent', (user, blob, mimeType, target) => {
  122. cy.clearCookies()
  123. .then(async () => {
  124. const fileName = basename(target)
  125. // Process paths
  126. const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}`
  127. const filePath = target.split('/').map(encodeURIComponent).join('/')
  128. try {
  129. const file = new File([blob], fileName, { type: mimeType })
  130. const response = await axios({
  131. url: `${rootPath}${filePath}`,
  132. method: 'PUT',
  133. data: file,
  134. headers: {
  135. 'Content-Type': mimeType,
  136. },
  137. auth: {
  138. username: user.userId,
  139. password: user.password,
  140. },
  141. })
  142. cy.log(`Uploaded content as ${fileName}`, response)
  143. } catch (error) {
  144. cy.log('error', error)
  145. throw new Error('Unable to process fixture')
  146. }
  147. })
  148. })
  149. /**
  150. * Reset the admin theming entirely
  151. */
  152. Cypress.Commands.add('resetAdminTheming', () => {
  153. const admin = new User('admin', 'admin')
  154. cy.clearCookies()
  155. cy.login(admin)
  156. // Clear all settings
  157. cy.request('/csrftoken').then(({ body }) => {
  158. const requestToken = body.token
  159. axios({
  160. method: 'POST',
  161. url: '/index.php/apps/theming/ajax/undoAllChanges',
  162. headers: {
  163. requesttoken: requestToken,
  164. },
  165. })
  166. })
  167. // Clear admin session
  168. cy.clearCookies()
  169. })
  170. /**
  171. * Reset the current or provided user theming settings
  172. * It does not reset the theme config as it is enforced in the
  173. * server config for cypress testing.
  174. */
  175. Cypress.Commands.add('resetUserTheming', (user?: User) => {
  176. if (user) {
  177. cy.clearCookies()
  178. cy.login(user)
  179. }
  180. // Reset background config
  181. cy.request('/csrftoken').then(({ body }) => {
  182. const requestToken = body.token
  183. cy.request({
  184. method: 'POST',
  185. url: '/apps/theming/background/default',
  186. headers: {
  187. requesttoken: requestToken,
  188. },
  189. })
  190. })
  191. if (user) {
  192. // Clear current session
  193. cy.clearCookies()
  194. }
  195. })
  196. Cypress.Commands.add('runOccCommand', (command: string) => {
  197. cy.exec(`docker exec --user www-data nextcloud-cypress-tests-server php ./occ ${command}`)
  198. })