Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] Fix cypress tests for files_versions #37589

Merged
24 changes: 23 additions & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
fail-fast: false
matrix:
# run multiple copies of the current job in parallel
containers: ['component', 1, 2]
containers: ["component", 1, 2]

name: runner ${{ matrix.containers }}

Expand Down Expand Up @@ -91,6 +91,28 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}

- name: Extract NC logs
if: failure() && matrix.containers != 'component'
run: docker logs nextcloud-cypress-tests-server > nextcloud.log

- name: Upload NC logs
uses: actions/upload-artifact@v3
if: failure() && matrix.containers != 'component'
with:
name: nc_logs_${{ matrix.containers }}
path: nextcloud.log

- name: Create data dir archive
if: failure() && matrix.containers != 'component'
run: docker exec nextcloud-cypress-tests-server tar -cvjf - data > data.tar

- name: Upload data dir archive
uses: actions/upload-artifact@v3
if: failure() && matrix.containers != 'component'
with:
name: nc_data_${{ matrix.containers }}
path: data.tar

summary:
runs-on: ubuntu-latest
needs: [init, cypress]
Expand Down
4 changes: 3 additions & 1 deletion cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export default defineConfig({

// Remove container after run
on('after:run', () => {
stopNextcloud()
if (!process.env.CI) {
stopNextcloud()
}
})

// Before the browser launches
Expand Down
1 change: 0 additions & 1 deletion cypress/dockerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export const configureNextcloud = async function() {
await runExec(container, ['php', 'occ', 'config:system:set', 'default_locale', '--value', 'en_US'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'force_locale', '--value', 'en_US'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'enforce_theme', '--value', 'light'], true)
await runExec(container, ['php', 'occ', 'config:system:set', 'versions_retention_obligation', '--value', '0, 0'], true)

console.log('└─ Nextcloud is now ready to use 🎉')
}
Expand Down
28 changes: 16 additions & 12 deletions cypress/e2e/files_versions/filesVersionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
*/

import path from "path"
import type { User } from "@nextcloud/cypress"

export function uploadThreeVersions(user) {
cy.uploadContent(user, new Blob(['v1'], { type: 'text/plain' }), 'text/plain', '/test.txt')
cy.wait(1000)
cy.uploadContent(user, new Blob(['v2'], { type: 'text/plain' }), 'text/plain', '/test.txt')
cy.wait(1000)
cy.uploadContent(user, new Blob(['v3'], { type: 'text/plain' }), 'text/plain', '/test.txt')
export function uploadThreeVersions(user: User, fileName: string) {
// A new version will not be created if the changes occur
// within less than one second of each other.
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.uploadContent(user, new Blob(['v1'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
.wait(1100)
.uploadContent(user, new Blob(['v2'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
.wait(1100)
.uploadContent(user, new Blob(['v3'], { type: 'text/plain' }), 'text/plain', `/${fileName}`)
cy.login(user)
}

Expand All @@ -52,7 +56,7 @@ export function openVersionMenu(index: number) {
cy.get('[data-files-versions-version]')
.eq(index).within(() => {
cy.get('.action-item__menutoggle').filter(':visible')
.click()
.click()
})
})
}
Expand All @@ -65,17 +69,17 @@ export function clickPopperAction(actionName: string) {

export function nameVersion(index: number, name: string) {
openVersionMenu(index)
clickPopperAction("Name this version")
clickPopperAction('Name this version')
cy.get(':focused').type(`${name}{enter}`)
}

export function assertVersionContent(index: number, expectedContent: string) {
export function assertVersionContent(filename: string, index: number, expectedContent: string) {
const downloadsFolder = Cypress.config('downloadsFolder')

openVersionMenu(index)
clickPopperAction("Download version")
clickPopperAction('Download version')

return cy.readFile(path.join(downloadsFolder, 'test.txt'))
return cy.readFile(path.join(downloadsFolder, filename))
.then((versionContent) => expect(versionContent).to.equal(expectedContent))
.then(() => cy.exec(`rm ${downloadsFolder}/test.txt`))
.then(() => cy.exec(`rm ${downloadsFolder}/${filename}`))
}
10 changes: 7 additions & 3 deletions cypress/e2e/files_versions/version_creation.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
import { openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils'

describe('Versions creation', () => {
let randomFileName = ''

before(() => {
randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt'

cy.createRandomUser()
.then((user) => {
uploadThreeVersions(user)
uploadThreeVersions(user, randomFileName)
cy.login(user)
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)
})
})

it('Opens the versions panel and sees the versions', () => {
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)

cy.get('#tab-version_vue').within(() => {
cy.get('[data-files-versions-version]').should('have.length', 3)
Expand Down
14 changes: 9 additions & 5 deletions cypress/e2e/files_versions/version_download.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@
import { assertVersionContent, openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils'

describe('Versions download', () => {
let randomFileName = ''

before(() => {
randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt'

cy.createRandomUser()
.then((user) => {
uploadThreeVersions(user)
uploadThreeVersions(user, randomFileName)
cy.login(user)
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)
})
})

it('Download versions and assert there content', () => {
assertVersionContent(0, 'v3')
assertVersionContent(1, 'v2')
assertVersionContent(2, 'v1')
assertVersionContent(randomFileName, 0, 'v3')
assertVersionContent(randomFileName, 1, 'v2')
assertVersionContent(randomFileName, 2, 'v1')
})
})
22 changes: 15 additions & 7 deletions cypress/e2e/files_versions/version_expiration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,51 @@
import { assertVersionContent, nameVersion, openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils'

describe('Versions expiration', () => {
let randomFileName = ''

beforeEach(() => {
randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt'

cy.createRandomUser()
.then((user) => {
uploadThreeVersions(user)
uploadThreeVersions(user, randomFileName)
cy.login(user)
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)
})
})

it('Expire all versions', () => {
cy.runOccCommand('config:system:set versions_retention_obligation --value "0, 0"')
cy.runOccCommand('versions:expire')
cy.runOccCommand('config:system:set versions_retention_obligation --value auto')
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)

cy.get('#tab-version_vue').within(() => {
cy.get('[data-files-versions-version]').should('have.length', 1)
cy.get('[data-files-versions-version]').eq(0).contains('Current version')
})

assertVersionContent(0, 'v3')
assertVersionContent(randomFileName, 0, 'v3')
})

it('Expire versions v2', () => {
nameVersion(2, 'v1')

cy.runOccCommand('config:system:set versions_retention_obligation --value "0, 0"')
cy.runOccCommand('versions:expire')
cy.runOccCommand('config:system:set versions_retention_obligation --value auto')
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)

cy.get('#tab-version_vue').within(() => {
cy.get('[data-files-versions-version]').should('have.length', 2)
cy.get('[data-files-versions-version]').eq(0).contains('Current version')
cy.get('[data-files-versions-version]').eq(1).contains('v1')
})

assertVersionContent(0, 'v3')
assertVersionContent(1, 'v1')
assertVersionContent(randomFileName, 0, 'v3')
assertVersionContent(randomFileName, 1, 'v1')
})
})
8 changes: 6 additions & 2 deletions cypress/e2e/files_versions/version_naming.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import { nameVersion, openVersionsPanel, uploadThreeVersions } from './filesVersionsUtils'

describe('Versions naming', () => {
let randomFileName = ''

before(() => {
randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt'

cy.createRandomUser()
.then((user) => {
uploadThreeVersions(user)
uploadThreeVersions(user, randomFileName)
cy.login(user)
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)
})
})

Expand Down
16 changes: 10 additions & 6 deletions cypress/e2e/files_versions/version_restoration.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ import { assertVersionContent, clickPopperAction, openVersionMenu, openVersionsP

function restoreVersion(index: number) {
openVersionMenu(index)
clickPopperAction("Restore version")
clickPopperAction('Restore version')
}

describe('Versions restoration', () => {
let randomFileName = ''

before(() => {
randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10) + '.txt'

cy.createRandomUser()
.then((user) => {
uploadThreeVersions(user)
uploadThreeVersions(user, randomFileName)
cy.login(user)
cy.visit('/apps/files')
openVersionsPanel('test.txt')
openVersionsPanel(randomFileName)
})
})

Expand All @@ -48,8 +52,8 @@ describe('Versions restoration', () => {
})

it('Downloads versions and assert there content', () => {
assertVersionContent(0, 'v1')
assertVersionContent(1, 'v3')
assertVersionContent(2, 'v2')
assertVersionContent(randomFileName, 0, 'v1')
assertVersionContent(randomFileName, 1, 'v3')
assertVersionContent(randomFileName, 2, 'v2')
})
})
51 changes: 25 additions & 26 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,32 @@ Cypress.Commands.add('uploadFile', (user, fixture = 'image.jpg', mimeType = 'ima
*/
Cypress.Commands.add('uploadContent', (user, blob, mimeType, target) => {
cy.clearCookies()
.then(async () => {
const fileName = basename(target)

// Process paths
const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}`
const filePath = target.split('/').map(encodeURIComponent).join('/')
try {
const file = new File([blob], fileName, { type: mimeType })
await axios({
url: `${rootPath}${filePath}`,
method: 'PUT',
data: file,
headers: {
'Content-Type': mimeType,
},
auth: {
username: user.userId,
password: user.password,
},
}).then(response => {
.then(async () => {
const fileName = basename(target)

// Process paths
const rootPath = `${Cypress.env('baseUrl')}/remote.php/dav/files/${encodeURIComponent(user.userId)}`
const filePath = target.split('/').map(encodeURIComponent).join('/')
try {
const file = new File([blob], fileName, { type: mimeType })
const response = await axios({
url: `${rootPath}${filePath}`,
method: 'PUT',
data: file,
headers: {
'Content-Type': mimeType,
},
auth: {
username: user.userId,
password: user.password,
},
})
cy.log(`Uploaded content as ${fileName}`, response)
})
} catch (error) {
cy.log('error', error)
throw new Error(`Unable to process fixture`)
}
})
} catch (error) {
cy.log('error', error)
throw new Error('Unable to process fixture')
}
})
})

/**
Expand Down