Skip to content

Commit 38f3b6e

Browse files
committed
feat: Add save/restore state commands
Signed-off-by: Louis Chemineau <[email protected]>
1 parent 5194aa1 commit 38f3b6e

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

lib/commands/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
export * from './getNc'
2424
export * from './sessions'
2525
export * from './users'
26+
export * from './state'

lib/commands/state.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function saveState(): Cypress.Chainable<string> {
2+
const snapshot = Math.random().toString(36).substring(7)
3+
4+
// DB
5+
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}`)
6+
7+
// Data
8+
cy.exec(`docker exec --user www-data rm /var/www/html/data/data-${snapshot}.tar`, { failOnNonZeroExit: false })
9+
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 .`)
10+
11+
cy.log(`Created snapshot ${snapshot}`)
12+
13+
return cy.wrap(snapshot)
14+
}
15+
16+
export function restoreState(snapshot: string = 'init') {
17+
// DB
18+
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`)
19+
20+
// Data
21+
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')`)
22+
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'`)
23+
24+
cy.log(`Restored snapshot ${snapshot}`)
25+
}

lib/index.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
*
2121
*/
22-
import { getNc } from "./commands"
22+
import { getNc, restoreState, saveState } from "./commands"
2323
import { login, logout } from "./commands/sessions"
2424
import { User, createRandomUser, createUser, deleteUser, modifyUser, listUsers, getUserData, enableUser } from "./commands/users"
2525
import type { Selector } from "./selectors"
@@ -69,7 +69,7 @@ declare global {
6969
* Query list of users on the Nextcloud instance
7070
*
7171
* **Warning**: Using this function will reset the previous session
72-
*
72+
*
7373
* @param details Set to true to fetch users with detailed information (default false)
7474
* @return List of user IDs or list of Users (if details was set to true)
7575
*/
@@ -87,18 +87,35 @@ declare global {
8787

8888
/**
8989
* Enable or disable a given user
90-
*
90+
*
9191
* @param user user whom to enable or disable
9292
* @param enable True to enable, false to disable (default is enable)
9393
*/
9494
enableUser(user: User, enable?: boolean): Cypress.Chainable<Cypress.Response<any>>
95+
9596
/**
96-
*
97+
*
9798
* Query metadata for, and in behalf, of a given user
9899
*
99100
* @param user User whom metadata to query
100101
*/
101-
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>
102+
getUserData(user: User): Cypress.Chainable<Cypress.Response<any>>
103+
104+
/**
105+
*
106+
* Saves the DB and data folder state.
107+
*
108+
* @return string the ID of the snapshot
109+
*/
110+
saveState(): Cypress.Chainable<string>
111+
112+
/**
113+
*
114+
* Restores the DB and data folder state.
115+
*
116+
* @param snapshot string the ID of the snapshot
117+
*/
118+
restoreState(snapshot: string): Cypress.Chainable<void>
102119
}
103120
}
104121
}
@@ -121,6 +138,9 @@ export const addCommands = function() {
121138
Cypress.Commands.add('modifyUser', modifyUser)
122139
Cypress.Commands.add('enableUser', enableUser)
123140
Cypress.Commands.add('getUserData', getUserData)
141+
Cypress.Commands.add('modifyUser', modifyUser)
142+
Cypress.Commands.add('saveState', saveState)
143+
Cypress.Commands.add('restoreState', restoreState)
124144
}
125145

126146
export { User }

0 commit comments

Comments
 (0)