aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/files/router-query.cy.ts
blob: 9c6564c8ecf361729087702fcd7b41954c19017c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*!
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { User } from '@nextcloud/cypress'
import { join } from 'path'
import { getRowForFileId } from './FilesUtils.ts'

/**
 * Check that the sidebar is opened for a specific file
 * @param name The name of the file
 */
function sidebarIsOpen(name: string): void {
	cy.get('[data-cy-sidebar]')
		.should('be.visible')
		.findByRole('heading', { name })
		.should('be.visible')
}

/**
 * Skip a test without viewer installed
 */
function skipIfViewerDisabled(this: Mocha.Context): void {
	cy.runOccCommand('app:list --enabled --output json')
		.then((exec) => exec.stdout)
		.then((output) => JSON.parse(output))
		.then((obj) => 'viewer' in obj.enabled)
		.then((enabled) => {
			if (!enabled) {
				this.skip()
			}
		})
}

/**
 * Check a file was not downloaded
 * @param filename The expected filename
 */
function fileNotDownloaded(filename: string): void {
	const downloadsFolder = Cypress.config('downloadsFolder')
	cy.readFile(join(downloadsFolder, filename)).should('not.exist')
}

describe('Check router query flags:', function() {
	let user: User
	let imageId: number
	let archiveId: number
	let folderId: number

	before(() => {
		cy.createRandomUser().then(($user) => {
			user = $user
			cy.uploadFile(user, 'image.jpg')
				.then((response) => { imageId = Number.parseInt(response.headers['oc-fileid']) })
			cy.mkdir(user, '/folder')
				.then((response) => { folderId = Number.parseInt(response.headers['oc-fileid']) })
			cy.uploadContent(user, new Blob([]), 'application/zstd', '/archive.zst')
				.then((response) => { archiveId = Number.parseInt(response.headers['oc-fileid']) })
			cy.login(user)
		})
	})

	describe('"opendetails"', () => {
		it('open details for known file type', () => {
			cy.visit(`/apps/files/files/${imageId}?opendetails`)

			// see sidebar
			sidebarIsOpen('image.jpg')

			// but no viewer
			cy.findByRole('dialog', { name: 'image.jpg' })
				.should('not.exist')

			// and no download
			fileNotDownloaded('image.jpg')
		})

		it('open details for unknown file type', () => {
			cy.visit(`/apps/files/files/${archiveId}?opendetails`)

			// see sidebar
			sidebarIsOpen('archive.zst')

			// but no viewer
			cy.findByRole('dialog', { name: 'archive.zst' })
				.should('not.exist')

			// and no download
			fileNotDownloaded('archive.zst')
		})

		it('open details for folder', () => {
			cy.visit(`/apps/files/files/${folderId}?opendetails`)

			// see sidebar
			sidebarIsOpen('folder')

			// but no viewer
			cy.findByRole('dialog', { name: 'folder' })
				.should('not.exist')

			// and no download
			fileNotDownloaded('folder')
		})
	})

	describe('"openfile"', function() {
		/** Check the viewer is open and shows the image */
		function viewerShowsImage(): void {
			cy.findByRole('dialog', { name: 'image.jpg' })
				.should('be.visible')
				.find(`img[src*="fileId=${imageId}"]`)
				.should('be.visible')
		}

		it('opens files with default action', function() {
			skipIfViewerDisabled.call(this)

			cy.visit(`/apps/files/files/${imageId}?openfile`)
			viewerShowsImage()
		})

		it('opens files with default action using explicit query state', function() {
			skipIfViewerDisabled.call(this)

			cy.visit(`/apps/files/files/${imageId}?openfile=true`)
			viewerShowsImage()
		})

		it('does not open files with default action when using explicitly query value `false`', function() {
			skipIfViewerDisabled.call(this)

			cy.visit(`/apps/files/files/${imageId}?openfile=false`)
			getRowForFileId(imageId)
				.should('be.visible')
				.and('have.class', 'files-list__row--active')

			cy.findByRole('dialog', { name: 'image.jpg' })
				.should('not.exist')
		})

		it('does not open folders but shows details', () => {
			cy.visit(`/apps/files/files/${folderId}?openfile`)

			// See the URL was replaced
			cy.url()
				.should('match', /[?&]opendetails(&|=|$)/)
				.and('not.match', /openfile/)

			// See the sidebar is correctly opened
			cy.get('[data-cy-sidebar]')
				.should('be.visible')
				.findByRole('heading', { name: 'folder' })
				.should('be.visible')

			// see the folder was not changed
			getRowForFileId(imageId).should('exist')
		})

		it('does not open unknown file types but shows details', () => {
			cy.visit(`/apps/files/files/${archiveId}?openfile`)

			// See the URL was replaced
			cy.url()
				.should('match', /[?&]opendetails(&|=|$)/)
				.and('not.match', /openfile/)

			// See the sidebar is correctly opened
			cy.get('[data-cy-sidebar]')
				.should('be.visible')
				.findByRole('heading', { name: 'archive.zst' })
				.should('be.visible')

			// See no file was downloaded
			const downloadsFolder = Cypress.config('downloadsFolder')
			cy.readFile(join(downloadsFolder, 'archive.zst')).should('not.exist')
		})
	})
})