commands.ts 6.3 KB

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