Skip to content

Commit

Permalink
chore(cypress): allow db snapshot and restore fo faster tests
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Aug 21, 2024
1 parent 44f8a5a commit 64ae622
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
16 changes: 0 additions & 16 deletions cypress/dockerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,6 @@ export const configureNextcloud = async function() {
console.log('└─ Nextcloud is now ready to use 🎉')
}

export const createSnapshot = async function(): Promise<string> {
console.log('\nSaving Nextcloud DB...')
const randomString = Math.random().toString(36).substring(7)
const container = docker.getContainer(CONTAINER_NAME)
await runExec(container, ['cp', '/var/www/html/data/owncloud.db', '/var/www/html/data/owncloud.db-' + randomString], true)
console.log('└─ Done')
return randomString
}

export const restoreSnapshot = async function(snapshot: string) {
console.log('\nRestoring Nextcloud DB...')
const container = docker.getContainer(CONTAINER_NAME)
await runExec(container, ['cp', '/var/www/html/data/owncloud.db-' + snapshot, '/var/www/html/data/owncloud.db'], true)
console.log('└─ Done')
}

/**
* Applying local changes to the container
* Only triggered if we're not in CI. Otherwise the
Expand Down
27 changes: 20 additions & 7 deletions cypress/e2e/settings/personal-info.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,23 @@ const genericProperties = ['Location', 'X (formerly Twitter)', 'Fediverse']
const nonfederatedProperties = ['Organisation', 'Role', 'Headline', 'About']

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

before(() => {
// ensure we can set locale and language
cy.runOccCommand('config:system:delete force_language')
cy.runOccCommand('config:system:delete force_locale')
cy.createRandomUser().then(($user) => {
user = $user
cy.modifyUser(user, 'language', 'en')
cy.modifyUser(user, 'locale', 'en_US')
})

cy.wait(500)

cy.backupDB().then(($snapshot) => {
snapshot = $snapshot
})
})

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

beforeEach(() => {
cy.createRandomUser().then(($user) => {
user = $user
cy.modifyUser(user, 'language', 'en')
cy.modifyUser(user, 'locale', 'en_US')
cy.login($user)
cy.visit('/settings/user')
})
cy.login(user)
cy.visit('/settings/user')
cy.intercept('PUT', /ocs\/v2.php\/cloud\/users\//).as('submitSetting')
})

afterEach(() => {
cy.restoreDB(snapshot)
})

it('Can dis- and enable the profile', () => {
cy.visit(`/u/${user.userId}`)
cy.contains('h2', user.userId).should('be.visible')

cy.visit('/settings/user')
cy.contains('Enable profile').click()
handlePasswordConfirmation(user.password)
cy.wait('@submitSetting')

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

cy.visit('/settings/user')
cy.contains('Enable profile').click()
handlePasswordConfirmation(user.password)
cy.wait('@submitSetting')

cy.visit(`/u/${user.userId}`, { failOnStatusCode: false })
cy.contains('h2', user.userId).should('be.visible')
Expand Down
23 changes: 23 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ declare global {
* Run an occ command in the docker container.
*/
runOccCommand(command: string, options?: Partial<Cypress.ExecOptions>): Cypress.Chainable<Cypress.Exec>,

/**
* Create a snapshot of the current database
*/
backupDB(): Cypress.Chainable<string>,

/**
* Restore a snapshot of the database
* Default is the post-setup state
*/
restoreDB(snapshot?: string): Cypress.Chainable,
}
}
}
Expand Down Expand Up @@ -276,3 +287,15 @@ Cypress.Commands.add('runOccCommand', (command: string, options?: Partial<Cypres
const env = Object.entries(options?.env ?? {}).map(([name, value]) => `-e '${name}=${value}'`).join(' ')
return cy.exec(`docker exec --user www-data ${env} nextcloud-cypress-tests-server php ./occ ${command}`, options)
})

Cypress.Commands.add('backupDB', (): Cypress.Chainable<string> => {
const randomString = Math.random().toString(36).substring(7)
cy.exec(`docker exec nextcloud-cypress-tests-server cp /var/www/html/data/owncloud.db /var/www/html/data/owncloud.db-${randomString}`)
cy.log(`Created snapshot ${randomString}`)
return cy.wrap(randomString)
})

Cypress.Commands.add('restoreDB', (snapshot: string = 'init') => {
cy.exec(`docker exec nextcloud-cypress-tests-server cp /var/www/html/data/owncloud.db-${snapshot} /var/www/html/data/owncloud.db`)
cy.log(`Restored snapshot ${snapshot}`)
})

0 comments on commit 64ae622

Please sign in to comment.