|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { User } from '@nextcloud/cypress' |
| 7 | +import { join } from 'path' |
| 8 | +import { getRowForFileId } from './FilesUtils.ts' |
| 9 | + |
| 10 | +/** |
| 11 | + * Check that the sidebar is opened for a specific file |
| 12 | + * @param name The name of the file |
| 13 | + */ |
| 14 | +function sidebarIsOpen(name: string): void { |
| 15 | + cy.get('[data-cy-sidebar]') |
| 16 | + .should('be.visible') |
| 17 | + .findByRole('heading', { name }) |
| 18 | + .should('be.visible') |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Skip a test without viewer installed |
| 23 | + */ |
| 24 | +function skipIfViewerDisabled(this: Mocha.Context): void { |
| 25 | + cy.runOccCommand('app:list --enabled --output json') |
| 26 | + .then((exec) => exec.stdout) |
| 27 | + .then((output) => JSON.parse(output)) |
| 28 | + .then((obj) => 'viewer' in obj.enabled) |
| 29 | + .then((enabled) => { |
| 30 | + if (!enabled) { |
| 31 | + this.skip() |
| 32 | + } |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Check a file was not downloaded |
| 38 | + * @param filename The expected filename |
| 39 | + */ |
| 40 | +function fileNotDownloaded(filename: string): void { |
| 41 | + const downloadsFolder = Cypress.config('downloadsFolder') |
| 42 | + cy.readFile(join(downloadsFolder, filename)).should('not.exist') |
| 43 | +} |
| 44 | + |
| 45 | +describe('Check router query flags:', function() { |
| 46 | + let user: User |
| 47 | + let imageId: number |
| 48 | + let archiveId: number |
| 49 | + let folderId: number |
| 50 | + |
| 51 | + before(() => { |
| 52 | + cy.createRandomUser().then(($user) => { |
| 53 | + user = $user |
| 54 | + cy.uploadFile(user, 'image.jpg') |
| 55 | + .then((response) => { imageId = Number.parseInt(response.headers['oc-fileid']) }) |
| 56 | + cy.mkdir(user, '/folder') |
| 57 | + .then((response) => { folderId = Number.parseInt(response.headers['oc-fileid']) }) |
| 58 | + cy.uploadContent(user, new Blob([]), 'application/zstd', '/archive.zst') |
| 59 | + .then((response) => { archiveId = Number.parseInt(response.headers['oc-fileid']) }) |
| 60 | + cy.login(user) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('"opendetails"', () => { |
| 65 | + it('open details for known file type', () => { |
| 66 | + cy.visit(`/apps/files/files/${imageId}?opendetails`) |
| 67 | + |
| 68 | + // see sidebar |
| 69 | + sidebarIsOpen('image.jpg') |
| 70 | + |
| 71 | + // but no viewer |
| 72 | + cy.findByRole('dialog', { name: 'image.jpg' }) |
| 73 | + .should('not.exist') |
| 74 | + |
| 75 | + // and no download |
| 76 | + fileNotDownloaded('image.jpg') |
| 77 | + }) |
| 78 | + |
| 79 | + it('open details for unknown file type', () => { |
| 80 | + cy.visit(`/apps/files/files/${archiveId}?opendetails`) |
| 81 | + |
| 82 | + // see sidebar |
| 83 | + sidebarIsOpen('archive.zst') |
| 84 | + |
| 85 | + // but no viewer |
| 86 | + cy.findByRole('dialog', { name: 'archive.zst' }) |
| 87 | + .should('not.exist') |
| 88 | + |
| 89 | + // and no download |
| 90 | + fileNotDownloaded('archive.zst') |
| 91 | + }) |
| 92 | + |
| 93 | + it('open details for folder', () => { |
| 94 | + cy.visit(`/apps/files/files/${folderId}?opendetails`) |
| 95 | + |
| 96 | + // see sidebar |
| 97 | + sidebarIsOpen('folder') |
| 98 | + |
| 99 | + // but no viewer |
| 100 | + cy.findByRole('dialog', { name: 'folder' }) |
| 101 | + .should('not.exist') |
| 102 | + |
| 103 | + // and no download |
| 104 | + fileNotDownloaded('folder') |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('"openfile"', function() { |
| 109 | + /** Check the viewer is open and shows the image */ |
| 110 | + function viewerShowsImage(): void { |
| 111 | + cy.findByRole('dialog', { name: 'image.jpg' }) |
| 112 | + .should('be.visible') |
| 113 | + .find(`img[src*="fileId=${imageId}"]`) |
| 114 | + .should('be.visible') |
| 115 | + } |
| 116 | + |
| 117 | + it('opens files with default action', function() { |
| 118 | + skipIfViewerDisabled.call(this) |
| 119 | + |
| 120 | + cy.visit(`/apps/files/files/${imageId}?openfile`) |
| 121 | + viewerShowsImage() |
| 122 | + }) |
| 123 | + |
| 124 | + it('opens files with default action using explicit query state', function() { |
| 125 | + skipIfViewerDisabled.call(this) |
| 126 | + |
| 127 | + cy.visit(`/apps/files/files/${imageId}?openfile=true`) |
| 128 | + viewerShowsImage() |
| 129 | + }) |
| 130 | + |
| 131 | + it('does not open files with default action when using explicitly query value `false`', function() { |
| 132 | + skipIfViewerDisabled.call(this) |
| 133 | + |
| 134 | + cy.visit(`/apps/files/files/${imageId}?openfile=false`) |
| 135 | + getRowForFileId(imageId) |
| 136 | + .should('be.visible') |
| 137 | + .and('have.class', 'files-list__row--active') |
| 138 | + |
| 139 | + cy.findByRole('dialog', { name: 'image.jpg' }) |
| 140 | + .should('not.exist') |
| 141 | + }) |
| 142 | + |
| 143 | + it('does not open folders but shows details', () => { |
| 144 | + cy.visit(`/apps/files/files/${folderId}?openfile`) |
| 145 | + |
| 146 | + // See the URL was replaced |
| 147 | + cy.url() |
| 148 | + .should('match', /[?&]opendetails(&|=|$)/) |
| 149 | + .and('not.match', /openfile/) |
| 150 | + |
| 151 | + // See the sidebar is correctly opened |
| 152 | + cy.get('[data-cy-sidebar]') |
| 153 | + .should('be.visible') |
| 154 | + .findByRole('heading', { name: 'folder' }) |
| 155 | + .should('be.visible') |
| 156 | + |
| 157 | + // see the folder was not changed |
| 158 | + getRowForFileId(imageId).should('exist') |
| 159 | + }) |
| 160 | + |
| 161 | + it('does not open unknown file types but shows details', () => { |
| 162 | + cy.visit(`/apps/files/files/${archiveId}?openfile`) |
| 163 | + |
| 164 | + // See the URL was replaced |
| 165 | + cy.url() |
| 166 | + .should('match', /[?&]opendetails(&|=|$)/) |
| 167 | + .and('not.match', /openfile/) |
| 168 | + |
| 169 | + // See the sidebar is correctly opened |
| 170 | + cy.get('[data-cy-sidebar]') |
| 171 | + .should('be.visible') |
| 172 | + .findByRole('heading', { name: 'archive.zst' }) |
| 173 | + .should('be.visible') |
| 174 | + |
| 175 | + // See no file was downloaded |
| 176 | + const downloadsFolder = Cypress.config('downloadsFolder') |
| 177 | + cy.readFile(join(downloadsFolder, 'archive.zst')).should('not.exist') |
| 178 | + }) |
| 179 | + }) |
| 180 | +}) |
0 commit comments