diff options
Diffstat (limited to 'cypress/support/commands.ts')
-rw-r--r-- | cypress/support/commands.ts | 132 |
1 files changed, 44 insertions, 88 deletions
diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 8d78c3db166..ad486a8a8f7 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -1,94 +1,24 @@ /** - * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com> - * - * @author John Molakvoæ <skjnldsv@protonmail.com> - * - * @license AGPL-3.0-or-later - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ -/* eslint-disable n/no-unpublished-import */ -import axios from '@nextcloud/axios' +// eslint-disable-next-line n/no-extraneous-import +import axios from 'axios' import { addCommands, User } from '@nextcloud/cypress' import { basename } from 'path' // Add custom commands +import '@testing-library/cypress/add-commands' import 'cypress-if' import 'cypress-wait-until' addCommands() -// Register this file's custom commands types -declare global { - // eslint-disable-next-line @typescript-eslint/no-namespace - namespace Cypress { - interface Chainable<Subject = any> { - /** - * Enable or disable a given user - */ - enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>, - - /** - * Upload a file from the fixtures folder to a given user storage. - * **Warning**: Using this function will reset the previous session - */ - uploadFile(user: User, fixture?: string, mimeType?: string, target?: string): Cypress.Chainable<void>, - - /** - * Upload a raw content to a given user storage. - * **Warning**: Using this function will reset the previous session - */ - uploadContent(user: User, content: Blob, mimeType: string, target: string, mtime?: number): Cypress.Chainable<AxiosResponse>, - - /** - * Create a new directory - * **Warning**: Using this function will reset the previous session - */ - mkdir(user: User, target: string): Cypress.Chainable<void>, - - /** - * Set a file as favorite (or remove from favorite) - */ - setFileAsFavorite(user: User, target: string, favorite?: boolean): Cypress.Chainable<void>, - - /** - * Reset the admin theming entirely. - * **Warning**: Using this function will reset the previous session - */ - resetAdminTheming(): Cypress.Chainable<void>, - - /** - * Reset the user theming settings. - * If provided, will clear session and login as the given user. - * **Warning**: Providing a user will reset the previous session. - */ - resetUserTheming(user?: User): Cypress.Chainable<void>, - - /** - * Run an occ command in the docker container. - */ - runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>, - } - } -} - const url = (Cypress.config('baseUrl') || '').replace(/\/index.php\/?$/g, '') Cypress.env('baseUrl', url) /** * Enable or disable a user - * TODO: standardise in @nextcloud/cypress + * TODO: standardize in @nextcloud/cypress * * @param {User} user the user to dis- / enable * @param {boolean} enable True if the user should be enable, false to disable @@ -115,7 +45,7 @@ Cypress.Commands.add('enableUser', (user: User, enable = true) => { /** * cy.uploadedFile - uploads a file from the fixtures folder - * TODO: standardise in @nextcloud/cypress + * TODO: standardize in @nextcloud/cypress * * @param {User} user the owner of the file, e.g. admin * @param {string} fixture the fixture file name, e.g. image1.jpg @@ -124,12 +54,12 @@ Cypress.Commands.add('enableUser', (user: User, enable = true) => { */ Cypress.Commands.add('uploadFile', (user, fixture = 'image.jpg', mimeType = 'image/jpeg', target = `/${fixture}`) => { // get fixture - return cy.fixture(fixture, 'base64').then(async file => { - // convert the base64 string to a blob - const blob = Cypress.Blob.base64StringToBlob(file, mimeType) - - cy.uploadContent(user, blob, mimeType, target) - }) + return cy.fixture(fixture, 'base64') + .then((file) => ( + // convert the base64 string to a blob + Cypress.Blob.base64StringToBlob(file, mimeType) + )) + .then((blob) => cy.uploadContent(user, blob, mimeType, target)) }) Cypress.Commands.add('setFileAsFavorite', (user: User, target: string, favorite = true) => { @@ -168,7 +98,7 @@ Cypress.Commands.add('setFileAsFavorite', (user: User, target: string, favorite Cypress.Commands.add('mkdir', (user: User, target: string) => { // eslint-disable-next-line cypress/unsafe-to-chain-command - cy.clearCookies() + return cy.clearCookies() .then(async () => { try { const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}` @@ -182,6 +112,7 @@ Cypress.Commands.add('mkdir', (user: User, target: string) => { }, }) cy.log(`Created directory ${target}`, response) + return response } catch (error) { cy.log('error', error) throw new Error('Unable to create directory') @@ -189,9 +120,32 @@ Cypress.Commands.add('mkdir', (user: User, target: string) => { }) }) +Cypress.Commands.add('rm', (user: User, target: string) => { + // eslint-disable-next-line cypress/unsafe-to-chain-command + cy.clearCookies() + .then(async () => { + try { + const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}` + const filePath = target.split('/').map(encodeURIComponent).join('/') + const response = await axios({ + url: `${rootPath}${filePath}`, + method: 'DELETE', + auth: { + username: user.userId, + password: user.password, + }, + }) + cy.log(`delete file or directory ${target}`, response) + } catch (error) { + cy.log('error', error) + throw new Error('Unable to delete file or directory') + } + }) +}) + /** * cy.uploadedContent - uploads a raw content - * TODO: standardise in @nextcloud/cypress + * TODO: standardize in @nextcloud/cypress * * @param {User} user the owner of the file, e.g. admin * @param {Blob} blob the content to upload @@ -286,7 +240,9 @@ Cypress.Commands.add('resetUserTheming', (user?: User) => { } }) -Cypress.Commands.add('runOccCommand', (command: string, options?: Partial<Cypress.ExecOptions>) => { - const env = Object.entries(options?.env ?? {}).map(([name, value]) => `-e '${name}=${value}'`).join(' ') - return cy.exec(`docker exec --user www-data ${env} nextcloud-cypress-tests-server php ./occ ${command}`, options) +Cypress.Commands.add('userFileExists', (user: string, path: string) => { + user.replaceAll('"', '\\"') + path.replaceAll('"', '\\"').replaceAll(/^\/+/gm, '') + return cy.runCommand(`stat --printf="%s" "data/${user}/files/${path}"`, { failOnNonZeroExit: true }) + .then((exec) => Number.parseInt(exec.stdout || '0')) }) |