Skip to content

Commit 1565243

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

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-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

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @copyright 2024 Louis Chmn <[email protected]>
3+
*
4+
* @author Louis Chmn <[email protected]>
5+
*
6+
* @license AGPL-3.0-or-later
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
export function saveState(): Cypress.Chainable<string> {
24+
const snapshot = Math.random().toString(36).substring(7)
25+
26+
// DB
27+
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}`)
28+
29+
// Data
30+
cy.exec(`docker exec --user www-data rm /var/www/html/data/data-${snapshot}.tar`, { failOnNonZeroExit: false })
31+
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 .`)
32+
33+
cy.log(`Created snapshot ${snapshot}`)
34+
35+
return cy.wrap(snapshot)
36+
}
37+
38+
export function restoreState(snapshot: string = 'init') {
39+
// DB
40+
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`)
41+
42+
// Data
43+
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')`)
44+
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'`)
45+
46+
cy.log(`Restored snapshot ${snapshot}`)
47+
}

lib/index.ts

+24-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,8 @@ 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('saveState', saveState)
142+
Cypress.Commands.add('restoreState', restoreState)
124143
}
125144

126145
export { User }

0 commit comments

Comments
 (0)