Skip to content

Commit

Permalink
feat: Add save/restore state commands
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Sep 15, 2024
1 parent 5194aa1 commit 38f3b6e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
export * from './getNc'
export * from './sessions'
export * from './users'
export * from './state'
25 changes: 25 additions & 0 deletions lib/commands/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function saveState(): Cypress.Chainable<string> {
const snapshot = Math.random().toString(36).substring(7)

// DB
cy.exec(`docker exec --user www-data nextcloud-cypress-tests_groupfolders cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${snapshot}`)

// Data
cy.exec(`docker exec --user www-data rm /var/www/html/data/data-${snapshot}.tar`, { failOnNonZeroExit: false })
cy.exec(`docker exec --user www-data --workdir /var/www/html/data nextcloud-cypress-tests_groupfolders tar cf /var/www/html/data/data-${snapshot}.tar .`)

cy.log(`Created snapshot ${snapshot}`)

return cy.wrap(snapshot)
}

export function restoreState(snapshot: string = 'init') {
// DB
cy.exec(`docker exec --user www-data nextcloud-cypress-tests_groupfolders cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`)

// Data
cy.exec(`docker exec --user www-data --workdir /var/www/html/data nextcloud-cypress-tests_groupfolders rm -vfr $(tar --exclude='*/*' -tf '/var/www/html/data/data-${snapshot}.tar')`)
cy.exec(`docker exec --user www-data --workdir /var/www/html/data nextcloud-cypress-tests_groupfolders tar -xf '/var/www/html/data/data-${snapshot}.tar'`)

cy.log(`Restored snapshot ${snapshot}`)
}
30 changes: 25 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { getNc } from "./commands"
import { getNc, restoreState, 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"
Expand Down Expand Up @@ -69,7 +69,7 @@ declare global {
* 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)
*/
Expand All @@ -87,18 +87,35 @@ declare global {

/**
* 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>>
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>

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

/**
*
* Restores the DB and data folder state.
*
* @param snapshot string the ID of the snapshot
*/
restoreState(snapshot: string): Cypress.Chainable<void>
}
}
}
Expand All @@ -121,6 +138,9 @@ export const addCommands = function() {
Cypress.Commands.add('modifyUser', modifyUser)
Cypress.Commands.add('enableUser', enableUser)
Cypress.Commands.add('getUserData', getUserData)
Cypress.Commands.add('modifyUser', modifyUser)
Cypress.Commands.add('saveState', saveState)
Cypress.Commands.add('restoreState', restoreState)
}

export { User }

0 comments on commit 38f3b6e

Please sign in to comment.