Skip to content

Commit 64ae622

Browse files
committed
chore(cypress): allow db snapshot and restore fo faster tests
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 44f8a5a commit 64ae622

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

cypress/dockerNode.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,6 @@ export const configureNextcloud = async function() {
135135
console.log('└─ Nextcloud is now ready to use 🎉')
136136
}
137137

138-
export const createSnapshot = async function(): Promise<string> {
139-
console.log('\nSaving Nextcloud DB...')
140-
const randomString = Math.random().toString(36).substring(7)
141-
const container = docker.getContainer(CONTAINER_NAME)
142-
await runExec(container, ['cp', '/var/www/html/data/owncloud.db', '/var/www/html/data/owncloud.db-' + randomString], true)
143-
console.log('└─ Done')
144-
return randomString
145-
}
146-
147-
export const restoreSnapshot = async function(snapshot: string) {
148-
console.log('\nRestoring Nextcloud DB...')
149-
const container = docker.getContainer(CONTAINER_NAME)
150-
await runExec(container, ['cp', '/var/www/html/data/owncloud.db-' + snapshot, '/var/www/html/data/owncloud.db'], true)
151-
console.log('└─ Done')
152-
}
153-
154138
/**
155139
* Applying local changes to the container
156140
* Only triggered if we're not in CI. Otherwise the

cypress/e2e/settings/personal-info.cy.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,23 @@ const genericProperties = ['Location', 'X (formerly Twitter)', 'Fediverse']
102102
const nonfederatedProperties = ['Organisation', 'Role', 'Headline', 'About']
103103

104104
describe('Settings: Change personal information', { testIsolation: true }, () => {
105+
let snapshot: string = ''
105106

106107
before(() => {
107108
// ensure we can set locale and language
108109
cy.runOccCommand('config:system:delete force_language')
109110
cy.runOccCommand('config:system:delete force_locale')
111+
cy.createRandomUser().then(($user) => {
112+
user = $user
113+
cy.modifyUser(user, 'language', 'en')
114+
cy.modifyUser(user, 'locale', 'en_US')
115+
})
116+
117+
cy.wait(500)
118+
119+
cy.backupDB().then(($snapshot) => {
120+
snapshot = $snapshot
121+
})
110122
})
111123

112124
after(() => {
@@ -115,30 +127,31 @@ describe('Settings: Change personal information', { testIsolation: true }, () =>
115127
})
116128

117129
beforeEach(() => {
118-
cy.createRandomUser().then(($user) => {
119-
user = $user
120-
cy.modifyUser(user, 'language', 'en')
121-
cy.modifyUser(user, 'locale', 'en_US')
122-
cy.login($user)
123-
cy.visit('/settings/user')
124-
})
130+
cy.login(user)
131+
cy.visit('/settings/user')
125132
cy.intercept('PUT', /ocs\/v2.php\/cloud\/users\//).as('submitSetting')
126133
})
127134

135+
afterEach(() => {
136+
cy.restoreDB(snapshot)
137+
})
138+
128139
it('Can dis- and enable the profile', () => {
129140
cy.visit(`/u/${user.userId}`)
130141
cy.contains('h2', user.userId).should('be.visible')
131142

132143
cy.visit('/settings/user')
133144
cy.contains('Enable profile').click()
134145
handlePasswordConfirmation(user.password)
146+
cy.wait('@submitSetting')
135147

136148
cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
137149
cy.contains('h2', 'Profile not found').should('be.visible')
138150

139151
cy.visit('/settings/user')
140152
cy.contains('Enable profile').click()
141153
handlePasswordConfirmation(user.password)
154+
cy.wait('@submitSetting')
142155

143156
cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
144157
cy.contains('h2', user.userId).should('be.visible')

cypress/support/commands.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ declare global {
6565
* Run an occ command in the docker container.
6666
*/
6767
runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>,
68+
69+
/**
70+
* Create a snapshot of the current database
71+
*/
72+
backupDB(): Cypress.Chainable<string>,
73+
74+
/**
75+
* Restore a snapshot of the database
76+
* Default is the post-setup state
77+
*/
78+
restoreDB(snapshot?: string): Cypress.Chainable,
6879
}
6980
}
7081
}
@@ -276,3 +287,15 @@ Cypress.Commands.add('runOccCommand', (command: string, options?: Partial<Cypres
276287
const env = Object.entries(options?.env ?? {}).map(([name, value]) => `-e '${name}=${value}'`).join(' ')
277288
return cy.exec(`docker exec --user www-data ${env} nextcloud-cypress-tests-server php ./occ ${command}`, options)
278289
})
290+
291+
Cypress.Commands.add('backupDB', (): Cypress.Chainable<string> => {
292+
const randomString = Math.random().toString(36).substring(7)
293+
cy.exec(`docker exec nextcloud-cypress-tests-server cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${randomString}`)
294+
cy.log(`Created snapshot ${randomString}`)
295+
return cy.wrap(randomString)
296+
})
297+
298+
Cypress.Commands.add('restoreDB', (snapshot: string = 'init') => {
299+
cy.exec(`docker exec nextcloud-cypress-tests-server cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`)
300+
cy.log(`Restored snapshot ${snapshot}`)
301+
})

0 commit comments

Comments
 (0)