commonUtils.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import { basename } from 'path'
  6. /**
  7. * Get the header navigation bar
  8. */
  9. export function getNextcloudHeader() {
  10. return cy.get('#header')
  11. }
  12. /**
  13. * Get user menu in the header navigation bar
  14. */
  15. export function getNextcloudUserMenu() {
  16. return getNextcloudHeader().find('#user-menu')
  17. }
  18. /**
  19. * Get the user menu toggle in the header navigation bar
  20. */
  21. export function getNextcloudUserMenuToggle() {
  22. return getNextcloudUserMenu().find('.header-menu__trigger').should('have.length', 1)
  23. }
  24. /**
  25. * Helper function ensure users and groups in this tests have a clean state
  26. * Deletes all users (except admin) and groups
  27. */
  28. export function clearState() {
  29. // cleanup ignoring any failures
  30. cy.runOccCommand('group:list --output=json').then(($result) => {
  31. const groups = Object.keys(JSON.parse($result.stdout)).filter((name) => name !== 'admin')
  32. groups.forEach((groupID) => cy.runOccCommand(`group:delete '${groupID}'`))
  33. })
  34. cy.runOccCommand('user:list --output=json').then(($result) => {
  35. const users = Object.keys(JSON.parse($result.stdout)).filter((name) => name !== 'admin')
  36. users.forEach((userID) => cy.runOccCommand(`user:delete '${userID}'`))
  37. })
  38. }
  39. /**
  40. * Install the test app
  41. */
  42. export function installTestApp() {
  43. const testAppPath = 'cypress/fixtures/testapp'
  44. cy.runOccCommand('-V').then((output) => {
  45. const version = output.stdout.match(/(\d\d+)\.\d+\.\d+/)?.[1]
  46. cy.wrap(version).should('not.be.undefined')
  47. getContainerName()
  48. .then(containerName => {
  49. cy.exec(`docker cp '${testAppPath}' ${containerName}:/var/www/html/apps`, { log: true })
  50. cy.exec(`docker exec --workdir /var/www/html ${containerName} chown -R www-data:www-data /var/www/html/apps/testapp`)
  51. })
  52. cy.runCommand(`sed -i -e 's|-version=\\"[0-9]\\+|-version=\\"${version}|g' apps/testapp/appinfo/info.xml`)
  53. cy.runOccCommand('app:enable --force testapp')
  54. })
  55. }
  56. /**
  57. * Remove the test app
  58. */
  59. export function uninstallTestApp() {
  60. cy.runOccCommand('app:remove testapp', { failOnNonZeroExit: false })
  61. cy.runCommand('rm -fr apps/testapp/appinfo/info.xml')
  62. }
  63. /**
  64. *
  65. */
  66. export function getContainerName(): Cypress.Chainable<string> {
  67. return cy.exec('pwd')
  68. .then(({ stdout }) => {
  69. return cy.wrap(`nextcloud-cypress-tests_${basename(stdout).replace(' ', '')}`)
  70. })
  71. }