|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { Folder } from '../../node/folder.ts' |
| 8 | +import { Permission } from '../../permissions.ts' |
| 9 | +import { UploadCancelledError } from '../errors/UploadCancelledError.ts' |
| 10 | +import { UploadStatus } from './Upload.ts' |
| 11 | +import { Uploader, UploaderStatus } from './Uploader.ts' |
| 12 | + |
| 13 | +const nextcloudAuth = vi.hoisted(() => ({ |
| 14 | + getCurrentUser: vi.fn(() => ({ uid: 'test', displayName: 'Test', isAdmin: false })), |
| 15 | +})) |
| 16 | +vi.mock('@nextcloud/auth', () => nextcloudAuth) |
| 17 | + |
| 18 | +const nextcloudCapabilities = vi.hoisted(() => ({ |
| 19 | + getCapabilities: vi.fn(() => ({ |
| 20 | + files: { chunked_upload: { max_parallel_count: 2 } }, |
| 21 | + dav: { public_shares_chunking: true }, |
| 22 | + })), |
| 23 | +})) |
| 24 | +vi.mock('@nextcloud/capabilities', () => nextcloudCapabilities) |
| 25 | + |
| 26 | +const nextcloudAxios = vi.hoisted(() => ({ |
| 27 | + default: { |
| 28 | + request: vi.fn(), |
| 29 | + }, |
| 30 | + isCancel: vi.fn(() => false), |
| 31 | +})) |
| 32 | +vi.mock('@nextcloud/axios', () => nextcloudAxios) |
| 33 | + |
| 34 | +const dav = vi.hoisted(() => ({ |
| 35 | + getClient: vi.fn(() => ({ |
| 36 | + createDirectory: vi.fn(), |
| 37 | + })), |
| 38 | + defaultRemoteURL: 'https://localhost/remote.php/dav', |
| 39 | + defaultRootPath: '/files/test', |
| 40 | +})) |
| 41 | +vi.mock('../../dav/dav.ts', () => dav) |
| 42 | + |
| 43 | +const uploadUtils = vi.hoisted(() => ({ |
| 44 | + getChunk: vi.fn(async (file: File) => new Blob([file], { type: file.type || 'application/octet-stream' })), |
| 45 | + initChunkWorkspace: vi.fn(), |
| 46 | + uploadData: vi.fn(async (_url: string, _blob: Blob, options: any) => { |
| 47 | + options?.onUploadProgress?.({ bytes: 50 }) |
| 48 | + return { |
| 49 | + status: 201, |
| 50 | + statusText: 'Created', |
| 51 | + headers: {}, |
| 52 | + config: { headers: {} }, |
| 53 | + data: { ok: true }, |
| 54 | + } |
| 55 | + }), |
| 56 | +})) |
| 57 | +vi.mock('../utils/upload.ts', () => uploadUtils) |
| 58 | + |
| 59 | +vi.mock('../utils/filesystem.ts', () => ({ |
| 60 | + isFileSystemDirectoryEntry: vi.fn(() => false), |
| 61 | + isFileSystemFileEntry: vi.fn(() => false), |
| 62 | +})) |
| 63 | + |
| 64 | +vi.mock('../../utils/logger.ts', () => ({ |
| 65 | + default: { |
| 66 | + debug: vi.fn(), |
| 67 | + info: vi.fn(), |
| 68 | + warn: vi.fn(), |
| 69 | + error: vi.fn(), |
| 70 | + }, |
| 71 | +})) |
| 72 | + |
| 73 | +describe('Uploader', () => { |
| 74 | + beforeEach(() => { |
| 75 | + (window as any).OC = { |
| 76 | + appConfig: { |
| 77 | + files: { |
| 78 | + max_chunk_size: 0, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + nextcloudAuth.getCurrentUser.mockReturnValue({ uid: 'test', displayName: 'Test', isAdmin: false }) |
| 84 | + nextcloudCapabilities.getCapabilities.mockReturnValue({ |
| 85 | + files: { chunked_upload: { max_parallel_count: 2 } }, |
| 86 | + dav: { public_shares_chunking: true }, |
| 87 | + }) |
| 88 | + nextcloudAxios.isCancel.mockReturnValue(false) |
| 89 | + uploadUtils.getChunk.mockClear() |
| 90 | + uploadUtils.uploadData.mockClear() |
| 91 | + dav.getClient.mockClear() |
| 92 | + }) |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + delete (window as any).OC |
| 96 | + vi.restoreAllMocks() |
| 97 | + }) |
| 98 | + |
| 99 | + it('initializes with the default destination for logged in users', () => { |
| 100 | + const uploader = new Uploader() |
| 101 | + |
| 102 | + expect(uploader.destination).toBeInstanceOf(Folder) |
| 103 | + expect(uploader.destination.owner).toBe('test') |
| 104 | + expect(uploader.root).toBe('https://localhost/remote.php/dav/files/test') |
| 105 | + expect(uploader.info.status).toBe(UploaderStatus.IDLE) |
| 106 | + }) |
| 107 | + |
| 108 | + it('throws when no user is logged in and uploader is not public', () => { |
| 109 | + nextcloudAuth.getCurrentUser.mockReturnValue(null as any) |
| 110 | + |
| 111 | + expect(() => new Uploader(false)).toThrowError('User is not logged in') |
| 112 | + }) |
| 113 | + |
| 114 | + it('uses anonymous owner in public mode', () => { |
| 115 | + nextcloudAuth.getCurrentUser.mockReturnValue(null as any) |
| 116 | + const uploader = new Uploader(true) |
| 117 | + |
| 118 | + expect(uploader.destination.owner).toBe('anonymous') |
| 119 | + }) |
| 120 | + |
| 121 | + it('manages custom headers through a cloned public getter', () => { |
| 122 | + const uploader = new Uploader() |
| 123 | + uploader.setCustomHeader('X-NC-Test', '1') |
| 124 | + |
| 125 | + const headers = uploader.customHeaders |
| 126 | + headers['X-NC-Test'] = '2' |
| 127 | + |
| 128 | + expect(uploader.customHeaders['X-NC-Test']).toBe('1') |
| 129 | + |
| 130 | + uploader.deleteCustomerHeader('X-NC-Test') |
| 131 | + expect(uploader.customHeaders['X-NC-Test']).toBeUndefined() |
| 132 | + }) |
| 133 | + |
| 134 | + it('rejects invalid destination values', () => { |
| 135 | + const uploader = new Uploader() |
| 136 | + |
| 137 | + expect(() => { |
| 138 | + uploader.destination = { type: null, source: '' } as any |
| 139 | + }).toThrowError('Invalid destination folder') |
| 140 | + }) |
| 141 | + |
| 142 | + it('uploads files in regular mode and notifies listeners', async () => { |
| 143 | + const uploader = new Uploader() |
| 144 | + uploader.setCustomHeader('X-NC-Test', 'value') |
| 145 | + const notifier = vi.fn() |
| 146 | + uploader.addNotifier(notifier) |
| 147 | + |
| 148 | + const file = new File(['x'.repeat(100)], 'report.txt', { type: 'text/plain', lastModified: 1000 }) |
| 149 | + const upload = await uploader.upload('/docs/report.txt', file) |
| 150 | + |
| 151 | + await vi.waitFor(() => { |
| 152 | + expect(upload.status).toBe(UploadStatus.FINISHED) |
| 153 | + }) |
| 154 | + |
| 155 | + expect(upload.uploaded).toBe(upload.size) |
| 156 | + expect(upload.response?.status).toBe(201) |
| 157 | + expect(uploadUtils.getChunk).toHaveBeenCalledTimes(1) |
| 158 | + expect(uploadUtils.uploadData).toHaveBeenCalledTimes(1) |
| 159 | + expect(notifier).toHaveBeenCalledTimes(1) |
| 160 | + expect(notifier).toHaveBeenCalledWith(upload) |
| 161 | + |
| 162 | + await vi.waitFor(() => { |
| 163 | + expect(uploader.info.status).toBe(UploaderStatus.IDLE) |
| 164 | + }) |
| 165 | + }) |
| 166 | + |
| 167 | + it('converts callback cancellation in batch upload to UploadCancelledError', async () => { |
| 168 | + const uploader = new Uploader(false, new Folder({ |
| 169 | + id: 1, |
| 170 | + owner: 'test', |
| 171 | + permissions: Permission.ALL, |
| 172 | + root: '/files/test', |
| 173 | + source: 'https://localhost/dav/files/test', |
| 174 | + })) |
| 175 | + |
| 176 | + const notifier = vi.fn() |
| 177 | + uploader.addNotifier(notifier) |
| 178 | + const file = new File(['data'], 'a.txt', { type: 'text/plain' }) |
| 179 | + |
| 180 | + await expect(uploader.batchUpload('/uploads', [file], { |
| 181 | + callback: async () => false, |
| 182 | + })).rejects.toBeInstanceOf(UploadCancelledError) |
| 183 | + |
| 184 | + expect(dav.getClient).toHaveBeenCalledTimes(1) |
| 185 | + expect(notifier).toHaveBeenCalledTimes(1) |
| 186 | + expect(notifier.mock.calls[0][0].status).toBe(UploadStatus.CANCELLED) |
| 187 | + }) |
| 188 | +}) |
0 commit comments