Skip to content

Commit

Permalink
feat!: Export docker functions from main entry
Browse files Browse the repository at this point in the history
Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Feb 11, 2025
1 parent ffe8fe0 commit 2bd6693
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 155 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Please take a look at the [forms app](/~https://github.com/nextcloud/forms) for an
You can use the `cypress` folder and the `cypress.config.ts` in this repository as starting points or adjust your `cypress.config.ts` (or `.js`):

```js
import { configureNextcloud, startNextcloud, stopNextcloud, waitOnNextcloud } from '@nextcloud/cypress/docker'
import { configureNextcloud, startNextcloud, stopNextcloud, waitOnNextcloud } from '@nextcloud/e2e-test-server'

export default defineConfig({
// ...
Expand Down Expand Up @@ -66,16 +66,16 @@ You can find [the list of all available commands here](https://nextcloud.github.

```js
// cypress/support/commands.js
import { addCommands } from '@nextcloud/cypress'
import { addCommands } from '@nextcloud/e2e-test-server/cypress'

addCommands()
```

```js
// cypress/support/commands.js
import { getNc } from '@nextcloud/cypress/commands'
import { login } from '@nextcloud/e2e-test-server/commands'

Cypress.Commands.add('getNc', getNc)
Cypress.Commands.add('login', login)
```

## Selectors (:warn: deprecated)
Expand Down
2 changes: 1 addition & 1 deletion cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
// ***********************************************
//

import { addCommands } from '../../dist/'
import { addCommands } from '../../dist/cypress'
addCommands()
17 changes: 17 additions & 0 deletions lib/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

export class User {
userId: string
password: string
language: string

constructor(user: string, password: string = user, language = 'en') {
this.userId = user
this.password = password
this.language = language
}
}

2 changes: 1 addition & 1 deletion lib/commands/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { User } from "./users"
import type { User } from "../User"

/**
* You should always upload files and/or create users
Expand Down
18 changes: 4 additions & 14 deletions lib/commands/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

export class User {
userId: string
password: string
language: string

constructor(user: string, password: string = user, language = 'en') {
this.userId = user
this.password = password
this.language = language
}
}
import { User } from "../User"

export const randHash = () => Math.random().toString(36).replace(/[^a-z]+/g, '').slice(0, 10)

Expand All @@ -40,8 +30,8 @@ export const createUser = function(user: User): Cypress.Chainable<Cypress.Respon
return cy.request({
method: 'POST',
url,
body: {
userid: user.userId,
body: {
userid: user.userId,
password: user.password
},
auth: {
Expand Down Expand Up @@ -168,7 +158,7 @@ export const modifyUser = function(user: User, key: string, value: any): Cypress

/**
* Query metadata for and in behalf of a given user
*
*
* @param user User to change
*/
export const getUserData = function(user: User): Cypress.Chainable<Cypress.Response<any>> {
Expand Down
141 changes: 141 additions & 0 deletions lib/cypress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { User } from "./User"
import { getNc, restoreState, runCommand, runOccCommand, saveState } from "./commands"
import { login, logout } from "./commands/sessions"
import { createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users"
import type { Selector } from "./selectors"

declare global {
namespace Cypress {
interface Chainable {
/**
* Get an element from the Nextcloud selector set.
* @example cy.getNc(FileList)
* cy.getNc(FileRow, { id: fileInfo.id })
*/
getNc(selector: Selector, args?: Object): Cypress.Chainable<JQuery<HTMLElement>>

/**
* Login on a Nextcloud instance
*/
login(user: User): void

/**
* Logout from a Nextcloud instance
*/
logout(): void

/**
* Create a random user on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*/
createRandomUser(): Cypress.Chainable<User>

/**
* Create a user on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*/
createUser(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Delete a user on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*/
deleteUser(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Query list of users on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*
* @param details Set to true to fetch users with detailed information (default false)
* @return List of user IDs or list of Users (if details was set to true)
*/
listUsers<b extends boolean>(details?: b): Cypress.Chainable<b extends true ? Record<string, string>[] : string[]>
listUsers(details?: boolean): Cypress.Chainable<Record<string,string>[] | string[]>

/**
* Modify an attribute of a given user on the Nextcloud instance
*
* @param user User who modifies their metadata
* @param key Attribute name
* @param value New attribute value
*/
modifyUser(user: User, key: string, value: any): Cypress.Chainable<Cypress.Response<any>>

/**
* Enable or disable a given user
*
* @param user user whom to enable or disable
* @param enable True to enable, false to disable (default is enable)
*/
enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>

/**
*
* Query metadata for, and in behalf, of a given user
*
* @param user User whom metadata to query
*/
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Create a snapshot of the current DB and data folder state.
*
* @return string the ID of the snapshot
*/
saveState(): Cypress.Chainable<string>

/**
* Restore a snapshot of the database
* Default is the post-setup state
*
* @param snapshot string the ID of the snapshot
*/
restoreState(snapshot?: string): Cypress.Chainable<void>

/**
* Run a command in the docker container
*
*/
runCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>

/**
* Run an occ command
*/
runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>
}
}
}

/**
* Register all existing commands provided by this library
*
* You can also manually register those commands by importing them
* @example import { login } from '@nextcloud/e2e-test-server/commands'
* Cypress.Commands.add('login', login)
*/
export const addCommands = function() {
Cypress.Commands.add('getNc', getNc)
Cypress.Commands.add('login', login)
Cypress.Commands.add('logout', logout)
Cypress.Commands.add('createRandomUser', createRandomUser)
Cypress.Commands.add('createUser', createUser)
Cypress.Commands.add('deleteUser', deleteUser)
Cypress.Commands.add('listUsers', listUsers)
Cypress.Commands.add('modifyUser', modifyUser)
Cypress.Commands.add('enableUser', enableUser)
Cypress.Commands.add('getUserData', getUserData)
Cypress.Commands.add('saveState', saveState)
Cypress.Commands.add('restoreState', restoreState)
Cypress.Commands.add('runCommand', runCommand)
Cypress.Commands.add('runOccCommand', runOccCommand)
}

export { User }
137 changes: 2 additions & 135 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,139 +2,6 @@
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { getNc, restoreState, runCommand, runOccCommand, saveState } from "./commands"
import { login, logout } from "./commands/sessions"
import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users"
import type { Selector } from "./selectors"

declare global {
namespace Cypress {
interface Chainable {
/**
* Get an element from the Nextcloud selector set.
* @example cy.getNc(FileList)
* cy.getNc(FileRow, { id: fileInfo.id })
*/
getNc(selector: Selector, args?: Object): Cypress.Chainable<JQuery<HTMLElement>>

/**
* Login on a Nextcloud instance
*/
login(user: User): void

/**
* Logout from a Nextcloud instance
*/
logout(): void

/**
* Create a random user on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*/
createRandomUser(): Cypress.Chainable<User>

/**
* Create a user on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*/
createUser(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Delete a user on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*/
deleteUser(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Query list of users on the Nextcloud instance
*
* **Warning**: Using this function will reset the previous session
*
* @param details Set to true to fetch users with detailed information (default false)
* @return List of user IDs or list of Users (if details was set to true)
*/
listUsers<b extends boolean>(details?: b): Cypress.Chainable<b extends true ? Record<string, string>[] : string[]>
listUsers(details?: boolean): Cypress.Chainable<Record<string,string>[] | string[]>

/**
* Modify an attribute of a given user on the Nextcloud instance
*
* @param user User who modifies their metadata
* @param key Attribute name
* @param value New attribute value
*/
modifyUser(user: User, key: string, value: any): Cypress.Chainable<Cypress.Response<any>>

/**
* Enable or disable a given user
*
* @param user user whom to enable or disable
* @param enable True to enable, false to disable (default is enable)
*/
enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>

/**
*
* Query metadata for, and in behalf, of a given user
*
* @param user User whom metadata to query
*/
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>

/**
* Create a snapshot of the current DB and data folder state.
*
* @return string the ID of the snapshot
*/
saveState(): Cypress.Chainable<string>

/**
* Restore a snapshot of the database
* Default is the post-setup state
*
* @param snapshot string the ID of the snapshot
*/
restoreState(snapshot?: string): Cypress.Chainable<void>

/**
* Run a command in the docker container
*
*/
runCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>

/**
* Run an occ command
*/
runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>
}
}
}

/**
* Register all existing commands provided by this library
*
* You can also manually register those commands by importing them
* @example import { login } from '@nextcloud/e2e-test-server/commands'
* Cypress.Commands.add('login', login)
*/
export const addCommands = function() {
Cypress.Commands.add('getNc', getNc)
Cypress.Commands.add('login', login)
Cypress.Commands.add('logout', logout)
Cypress.Commands.add('createRandomUser', createRandomUser)
Cypress.Commands.add('createUser', createUser)
Cypress.Commands.add('deleteUser', deleteUser)
Cypress.Commands.add('listUsers', listUsers)
Cypress.Commands.add('modifyUser', modifyUser)
Cypress.Commands.add('enableUser', enableUser)
Cypress.Commands.add('getUserData', getUserData)
Cypress.Commands.add('saveState', saveState)
Cypress.Commands.add('restoreState', restoreState)
Cypress.Commands.add('runCommand', runCommand)
Cypress.Commands.add('runOccCommand', runOccCommand)
}

export { User }
export * from "./docker"
export * from "./User"
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"import": "./dist/docker.mjs",
"require": "./dist/docker.js"
},
"./cypress": {
"types": "./dist/cypress.d.ts",
"import": "./dist/cypress.mjs",
"require": "./dist/cypress.js"
},
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
Expand Down
Loading

0 comments on commit 2bd6693

Please sign in to comment.