Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/e2e.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ including when starting the development runtime with `yarn stack up`.
`COZY_E2E_ONLYOFFICE_PORT`.
- A production build of Drive
- Playwright Chromium installed
- `unzip` available on the host to inspect downloaded ZIP archives

The default `cozy.localhost` names resolve to host loopback in the browser.
The stack, the Document Server, the OnlyOffice relay and a CoreDNS sidecar
Expand Down
9 changes: 8 additions & 1 deletion e2e/pages/PublicLinkPage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Page, Locator } from '@playwright/test'
import type { Download, Page, Locator } from '@playwright/test'

import { DrivePage } from './DrivePage'
import type { FileRow } from './FileRow'
Expand Down Expand Up @@ -46,6 +46,13 @@ export class PublicLinkPage {
await this.drive.openFolder(name)
}

async downloadFolder(): Promise<Download> {
const download = this.page.waitForEvent('download')
await this.page.getByTestId('more-menu').getByRole('button').click()
await this.page.getByRole('menuitem', { name: 'Download' }).click()
return await download
}

async expectFileContent(content: string): Promise<void> {
await expect(this.page.getByText(content, { exact: true })).toBeVisible()
}
Expand Down
81 changes: 79 additions & 2 deletions e2e/tests/z-link-sharing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { execFileSync } from 'child_process'
import { copyFile } from 'fs/promises'
import path from 'path'

import type { Browser, Page } from '@playwright/test'

import { USERS } from '../helpers/config'
import {
createFile,
createFolder,
findLinkPermission,
setLinkExpiry,
trashById
Expand Down Expand Up @@ -76,6 +79,75 @@ async function withAnonymousPage(
}

test.describe('Share by link', () => {
test('downloads the public folder as a ZIP with its nested files', async ({
alicePage,
aliceDrive,
publicPage
}) => {
await alicePage
.context()
.grantPermissions(['clipboard-read', 'clipboard-write'])

const folderName = `Public ZIP ${stamp()}`
const folderId = await createFolder({
instance: USERS.alice.instance,
name: folderName
})
try {
const nestedId = await createFolder({
instance: USERS.alice.instance,
name: 'nested',
parentId: folderId
})
await createFile({
instance: USERS.alice.instance,
name: 'root.txt',
content: 'root content',
parentId: folderId
})
await createFile({
instance: USERS.alice.instance,
name: 'nested.txt',
content: 'nested content',
parentId: nestedId
})

await alicePage.goto(`${ALICE_ROOT}/${folderId}`)
await aliceDrive.row('root.txt').waitVisible()
const modal = await aliceDrive.openShareModal()
const url = await new ShareByLinkPage(alicePage).createLink()
await modal.close()

await publicPage.goto(url)
const publicLink = new PublicLinkPage(publicPage)
await expect(publicLink.row('root.txt').cell).toBeVisible()
await expect(publicLink.row('nested').cell).toBeVisible()
const download = await publicLink.downloadFolder()
const archive = await download.path()
expect(archive).not.toBe(null)
if (!archive) throw new Error('The folder ZIP was not downloaded')

const rootPath = `files/${folderName}/root.txt`
const nestedPath = `files/${folderName}/nested/nested.txt`
const entries = execFileSync('unzip', ['-Z1', archive], {
encoding: 'utf8'
})
.trim()
.split('\n')
.filter(entry => !entry.endsWith('/'))
.sort()
expect(entries).toEqual([nestedPath, rootPath])
expect(
execFileSync('unzip', ['-p', archive, rootPath], { encoding: 'utf8' })
).toBe('root content')
expect(
execFileSync('unzip', ['-p', archive, nestedPath], { encoding: 'utf8' })
).toBe('nested content')
} finally {
await trashById(USERS.alice.instance, folderId)
}
})

test('moves a file into a folder through an editable public link', async ({
alicePage,
aliceDrive,
Expand Down Expand Up @@ -166,7 +238,10 @@ test.describe('Share by link', () => {
.context()
.grantPermissions(['clipboard-read', 'clipboard-write'])

const { folderId, link } = await openShareForNewFolder(alicePage, aliceDrive)
const { folderId, link } = await openShareForNewFolder(
alicePage,
aliceDrive
)
const url = await link.createLink()

await reopenShareModal(alicePage)
Expand All @@ -190,7 +265,9 @@ test.describe('Share by link', () => {
// Assert the rejection positively (the "link no longer available" page)
// rather than only the absence of content, which the SPA would satisfy
// momentarily before mounting regardless of expiry.
await expect(publicPage.unavailableMessage).toBeVisible({ timeout: 15_000 })
await expect(publicPage.unavailableMessage).toBeVisible({
timeout: 15_000
})
await expect(publicPage.fileList).toHaveCount(0)
})
})
Expand Down
4 changes: 3 additions & 1 deletion src/modules/public/PublicToolbarByLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ const PublicToolbarByLink = ({

const actions = makeActions(
[
download,
displayedFolder?.id &&
(options =>
download({ ...options, isSelectAll: true, displayedFolder })),
files.length > 1 && select,
addItems,
isMobile && (files.length > 1 || hasWriteAccess) && hr,
Expand Down
106 changes: 106 additions & 0 deletions src/modules/public/PublicToolbarByLink.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
fireEvent,
render,
screen,
waitFor,
within
} from '@testing-library/react'
import React from 'react'

import { createMockClient } from 'cozy-client'
import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'

import PublicToolbarByLink from './PublicToolbarByLink'
import AppLike from 'test/components/AppLike'

import { useDisplayedFolder } from '@/hooks'
import { download } from '@/modules/actions/download'

jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
useDisplayedFolder: jest.fn()
}))
jest.mock('cozy-ui/transpiled/react/providers/Breakpoints', () => ({
...jest.requireActual('cozy-ui/transpiled/react/providers/Breakpoints'),
__esModule: true,
default: jest.fn()
}))

const files = [
{ id: 'a', _id: 'a', type: 'file', name: 'a.txt' },
{ id: 'b', _id: 'b', type: 'directory', name: 'b' }
]

function setup(folder, isMobile = false) {
const client = createMockClient({})
client.stackClient.uri = 'http://cozy.tools'
client.stackClient.fetchJSON = jest.fn().mockResolvedValue({
links: { related: '/files/archive/secret/files.zip' }
})
const collection = client.collection('io.cozy.files')
collection.forceFileDownload = jest.fn()
client.collection = () => collection
useDisplayedFolder.mockReturnValue({ displayedFolder: folder })
useBreakpoints.mockReturnValue({ isMobile, isDesktop: !isMobile })
render(
<AppLike client={client}>
<PublicToolbarByLink files={files} hasWriteAccess={false} />
</AppLike>
)
fireEvent.click(within(screen.getByTestId('more-menu')).getByRole('button'))
return { client, collection }
}

describe('PublicToolbarByLink', () => {
it.each([
['root', false],
['subfolder', false],
['root', true]
])('downloads the displayed %s folder on mobile=%s', async (id, isMobile) => {
if (isMobile) jest.spyOn(console, 'warn').mockImplementation(() => {})
const { client, collection } = setup({ id, type: 'directory' }, isMobile)
fireEvent.click(screen.getByText('Download'))
await waitFor(() => {
expect(client.stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/files/archive',
{
data: {
type: 'io.cozy.archives',
attributes: { name: 'files', ids: [id] }
}
}
)
})
expect(collection.forceFileDownload).toHaveBeenCalledWith(
'http://cozy.tools/files/archive/secret/files.zip',
'files.zip'
)
})

it('keeps explicit selection downloads scoped to the selected documents', async () => {
const { client } = setup({ id: 'root', type: 'directory' })
const action = download({ client, t: key => key })
await action.action(files)
expect(client.stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/files/archive',
{
data: {
type: 'io.cozy.archives',
attributes: { name: 'files', ids: ['a', 'b'] }
}
}
)
})

it.each([null, { type: 'directory' }])(
'does not offer download before the displayed folder has an id',
folder => {
const { client } = setup(folder)
expect(screen.queryByText('Download')).toBe(null)
expect(screen.getByText('Select items')).toBeInTheDocument()
expect(client.stackClient.fetchJSON).not.toHaveBeenCalled()
}
)
})
Loading