aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/files/LivePhotosUtils.ts
blob: 6b0015affce38d015aa312690b300f13fddd9d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { User } from '@nextcloud/cypress'

type SetupInfo = {
	snapshot: string
	jpgFileId: number
	movFileId: number
	fileName: string
	user: User
}

/**
 *
 * @param user
 * @param fileName
 * @param domain
 * @param requesttoken
 * @param metadata
 */
function setMetadata(user: User, fileName: string, requesttoken: string, metadata: object) {
	cy.url().then(url => {
		const hostname = new URL(url).hostname
		cy.request({
			method: 'PROPPATCH',
			url: `http://${hostname}/remote.php/dav/files/${user.userId}/${fileName}`,
			auth: { user: user.userId, pass: user.password },
			headers: {
				requesttoken,
			},
			body: `<?xml version="1.0"?>
				<d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">
					<d:set>
						<d:prop>
							${Object.entries(metadata).map(([key, value]) => `<${key}>${value}</${key}>`).join('\n')}
						</d:prop>
					</d:set>
				</d:propertyupdate>`,
		})
	})

}

/**
 *
 * @param enable
 */
export function setShowHiddenFiles(enable: boolean) {
	cy.get('[data-cy-files-navigation-settings-button]').click()
	// Force:true because the checkbox is hidden by the pretty UI.
	if (enable) {
		cy.get('[data-cy-files-settings-setting="show_hidden"] input').check({ force: true })
	} else {
		cy.get('[data-cy-files-settings-setting="show_hidden"] input').uncheck({ force: true })
	}
	cy.get('[data-cy-files-navigation-settings]').type('{esc}')
}

/**
 *
 */
export function setupLivePhotos(): Cypress.Chainable<SetupInfo> {
	return cy.task('getVariable', { key: 'live-photos-data' })
		.then((_setupInfo) => {
			const setupInfo = _setupInfo as SetupInfo || {}
			if (setupInfo.snapshot) {
				cy.restoreState(setupInfo.snapshot)
			} else {
				let requesttoken: string

				setupInfo.fileName = Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0, 10)

				cy.createRandomUser().then(_user => { setupInfo.user = _user })

				cy.then(() => {
					cy.uploadContent(setupInfo.user, new Blob(['jpg file'], { type: 'image/jpg' }), 'image/jpg', `/${setupInfo.fileName}.jpg`)
						.then(response => { setupInfo.jpgFileId = parseInt(response.headers['oc-fileid']) })
					cy.uploadContent(setupInfo.user, new Blob(['mov file'], { type: 'video/mov' }), 'video/mov', `/${setupInfo.fileName}.mov`)
						.then(response => { setupInfo.movFileId = parseInt(response.headers['oc-fileid']) })

					cy.login(setupInfo.user)
				})

				cy.visit('/apps/files')

				cy.get('head').invoke('attr', 'data-requesttoken').then(_requesttoken => { requesttoken = _requesttoken as string })

				cy.then(() => {
					setMetadata(setupInfo.user, `${setupInfo.fileName}.jpg`, requesttoken, { 'nc:metadata-files-live-photo': setupInfo.movFileId })
					setMetadata(setupInfo.user, `${setupInfo.fileName}.mov`, requesttoken, { 'nc:metadata-files-live-photo': setupInfo.jpgFileId })
				})

				cy.then(() => {
					cy.saveState().then((value) => { setupInfo.snapshot = value })
					cy.task('setVariable', { key: 'live-photos-data', value: setupInfo })
				})
			}
			return cy.then(() => {
				cy.login(setupInfo.user)
				cy.visit('/apps/files')
				return cy.wrap(setupInfo)
			})
		})
}