Skip to content

Commit 9730f86

Browse files
committed
test: Improve stability of live photo e2ee tests
Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent ad5aa6c commit 9730f86

File tree

2 files changed

+130
-68
lines changed

2 files changed

+130
-68
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import type { User } from '@nextcloud/cypress'
7+
8+
type SetupInfo = {
9+
snapshot: string
10+
jpgFileId: number
11+
movFileId: number
12+
fileName: string
13+
user: User
14+
}
15+
16+
/**
17+
*
18+
* @param user
19+
* @param fileName
20+
* @param domain
21+
* @param requesttoken
22+
* @param metadata
23+
*/
24+
function setMetadata(user: User, fileName: string, requesttoken: string, metadata: object) {
25+
cy.url().then(url => {
26+
const hostname = new URL(url).hostname
27+
cy.request({
28+
method: 'PROPPATCH',
29+
url: `http://${hostname}/remote.php/dav/files/${user.userId}/${fileName}`,
30+
auth: { user: user.userId, pass: user.password },
31+
headers: {
32+
requesttoken,
33+
},
34+
body: `<?xml version="1.0"?>
35+
<d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">
36+
<d:set>
37+
<d:prop>
38+
${Object.entries(metadata).map(([key, value]) => `<${key}>${value}</${key}>`).join('\n')}
39+
</d:prop>
40+
</d:set>
41+
</d:propertyupdate>`,
42+
})
43+
})
44+
45+
}
46+
47+
/**
48+
*
49+
* @param enable
50+
*/
51+
export function setShowHiddenFiles(enable: boolean) {
52+
cy.get('[data-cy-files-navigation-settings-button]').click()
53+
// Force:true because the checkbox is hidden by the pretty UI.
54+
if (enable) {
55+
cy.get('[data-cy-files-settings-setting="show_hidden"] input').check({ force: true })
56+
} else {
57+
cy.get('[data-cy-files-settings-setting="show_hidden"] input').uncheck({ force: true })
58+
}
59+
cy.get('[data-cy-files-navigation-settings]').type('{esc}')
60+
}
61+
62+
/**
63+
*
64+
*/
65+
export function setupLivePhotos(): Cypress.Chainable<SetupInfo> {
66+
return cy.task('getVariable', { key: 'live-photos-data' })
67+
.then((_setupInfo) => {
68+
const setupInfo = _setupInfo as SetupInfo || {}
69+
if (setupInfo.snapshot) {
70+
cy.restoreState(setupInfo.snapshot)
71+
} else {
72+
let requesttoken: string
73+
74+
setupInfo.fileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
75+
76+
cy.createRandomUser().then(_user => { setupInfo.user = _user })
77+
78+
cy.then(() => {
79+
cy.uploadContent(setupInfo.user, new Blob(['jpg file'], { type: 'image/jpg' }), 'image/jpg', `/${setupInfo.fileName}.jpg`)
80+
.then(response => { setupInfo.jpgFileId = parseInt(response.headers['oc-fileid']) })
81+
cy.uploadContent(setupInfo.user, new Blob(['mov file'], { type: 'video/mov' }), 'video/mov', `/${setupInfo.fileName}.mov`)
82+
.then(response => { setupInfo.movFileId = parseInt(response.headers['oc-fileid']) })
83+
84+
cy.login(setupInfo.user)
85+
})
86+
87+
cy.visit('/apps/files')
88+
89+
cy.get('head').invoke('attr', 'data-requesttoken').then(_requesttoken => { requesttoken = _requesttoken as string })
90+
91+
cy.then(() => {
92+
setMetadata(setupInfo.user, `${setupInfo.fileName}.jpg`, requesttoken, { 'nc:metadata-files-live-photo': setupInfo.movFileId })
93+
setMetadata(setupInfo.user, `${setupInfo.fileName}.mov`, requesttoken, { 'nc:metadata-files-live-photo': setupInfo.jpgFileId })
94+
})
95+
96+
cy.then(() => {
97+
cy.saveState().then((value) => { setupInfo.snapshot = value })
98+
cy.task('setVariable', { key: 'live-photos-data', value: setupInfo })
99+
})
100+
}
101+
return cy.then(() => {
102+
cy.login(setupInfo.user)
103+
cy.visit('/apps/files')
104+
return cy.wrap(setupInfo)
105+
})
106+
})
107+
}

cypress/e2e/files/live_photos.cy.ts

Lines changed: 23 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,34 @@
2121
*/
2222

2323
import type { User } from '@nextcloud/cypress'
24-
import { clickOnBreadcrumbs, closeSidebar, copyFile, getRowForFile, getRowForFileId, renameFile, triggerActionForFile, triggerInlineActionForFileId } from './FilesUtils'
25-
26-
/**
27-
*
28-
* @param user
29-
* @param fileName
30-
* @param domain
31-
* @param requesttoken
32-
* @param metadata
33-
*/
34-
function setMetadata(user: User, fileName: string, domain: string, requesttoken: string, metadata: object) {
35-
cy.request({
36-
method: 'PROPPATCH',
37-
url: `http://${domain}/remote.php/dav/files/${user.userId}/${fileName}`,
38-
auth: { user: user.userId, pass: user.password },
39-
headers: {
40-
requesttoken,
41-
},
42-
body: `<?xml version="1.0"?>
43-
<d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">
44-
<d:set>
45-
<d:prop>
46-
${Object.entries(metadata).map(([key, value]) => `<${key}>${value}</${key}>`).join('\n')}
47-
</d:prop>
48-
</d:set>
49-
</d:propertyupdate>`,
50-
})
51-
}
24+
import {
25+
clickOnBreadcrumbs,
26+
copyFile,
27+
createFolder,
28+
getRowForFile,
29+
getRowForFileId,
30+
moveFile,
31+
navigateToFolder,
32+
renameFile,
33+
triggerActionForFile,
34+
triggerInlineActionForFileId,
35+
} from './FilesUtils'
36+
import { setShowHiddenFiles, setupLivePhotos } from './LivePhotosUtils'
5237

5338
describe('Files: Live photos', { testIsolation: true }, () => {
54-
let currentUser: User
39+
let user: User
5540
let randomFileName: string
5641
let jpgFileId: number
5742
let movFileId: number
58-
let hostname: string
59-
let requesttoken: string
60-
61-
before(() => {
62-
cy.createRandomUser().then((user) => {
63-
currentUser = user
64-
cy.login(currentUser)
65-
cy.visit('/apps/files')
66-
})
67-
68-
cy.url().then(url => { hostname = new URL(url).hostname })
69-
})
7043

7144
beforeEach(() => {
72-
randomFileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)
73-
74-
cy.uploadContent(currentUser, new Blob(['jpg file'], { type: 'image/jpg' }), 'image/jpg', `/${randomFileName}.jpg`)
75-
.then(response => { jpgFileId = parseInt(response.headers['oc-fileid']) })
76-
cy.uploadContent(currentUser, new Blob(['mov file'], { type: 'video/mov' }), 'video/mov', `/${randomFileName}.mov`)
77-
.then(response => { movFileId = parseInt(response.headers['oc-fileid']) })
78-
79-
cy.login(currentUser)
80-
cy.visit('/apps/files')
81-
82-
cy.get('head').invoke('attr', 'data-requesttoken').then(_requesttoken => { requesttoken = _requesttoken as string })
83-
84-
cy.then(() => {
85-
setMetadata(currentUser, `${randomFileName}.jpg`, hostname, requesttoken, { 'nc:metadata-files-live-photo': movFileId })
86-
setMetadata(currentUser, `${randomFileName}.mov`, hostname, requesttoken, { 'nc:metadata-files-live-photo': jpgFileId })
87-
})
88-
89-
cy.then(() => {
90-
cy.visit(`/apps/files/files/${jpgFileId}`) // Refresh and scroll to the .jpg file.
91-
closeSidebar()
92-
})
45+
setupLivePhotos()
46+
.then((setupInfo) => {
47+
user = setupInfo.user
48+
randomFileName = setupInfo.fileName
49+
jpgFileId = setupInfo.jpgFileId
50+
movFileId = setupInfo.movFileId
51+
})
9352
})
9453

9554
it('Only renders the .jpg file', () => {
@@ -98,12 +57,8 @@ describe('Files: Live photos', { testIsolation: true }, () => {
9857
})
9958

10059
context("'Show hidden files' is enabled", () => {
101-
before(() => {
102-
cy.login(currentUser)
103-
cy.visit('/apps/files')
104-
cy.get('[data-cy-files-navigation-settings-button]').click()
105-
// Force:true because the checkbox is hidden by the pretty UI.
106-
cy.get('[data-cy-files-settings-setting="show_hidden"] input').check({ force: true })
60+
beforeEach(() => {
61+
setShowHiddenFiles(true)
10762
})
10863

10964
it("Shows both files when 'Show hidden files' is enabled", () => {

0 commit comments

Comments
 (0)