-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use public-share aware functions from
@nextcloud/files
in…
…stead of custom Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
5 changed files
with
54 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,65 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
import { Uploader } from '../../lib/uploader.js' | ||
|
||
import type { Uploader } from '../../lib/uploader' | ||
import type { NextcloudUser } from '@nextcloud/auth' | ||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
||
const initialState = vi.hoisted(() => ({ loadState: vi.fn() })) | ||
const auth = vi.hoisted(() => ({ getCurrentUser: vi.fn<never, NextcloudUser | null>(() => null) })) | ||
vi.mock('@nextcloud/initial-state', () => initialState) | ||
vi.mock('@nextcloud/auth', () => auth) | ||
|
||
describe('uploader', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
vi.resetAllMocks() | ||
const node = document.getElementById('sharingToken') | ||
if (node) { | ||
document.body.removeChild(node) | ||
} | ||
// Reset mocks of DOM | ||
document.body.innerHTML = '' | ||
}) | ||
|
||
// wrapper to enforce reimport for mocking different dependency state | ||
const newUploader = async (...args: ConstructorParameters<typeof Uploader>) => { | ||
const { Uploader } = await import('../../lib/uploader.js') | ||
return new Uploader(...args) | ||
} | ||
|
||
test('constructor sets default target folder for user', async () => { | ||
auth.getCurrentUser.mockImplementationOnce(() => ({ uid: 'my-user', displayName: 'User', isAdmin: false })) | ||
const uploader = new Uploader() | ||
auth.getCurrentUser.mockImplementation(() => ({ uid: 'my-user', displayName: 'User', isAdmin: false })) | ||
const uploader = await newUploader() | ||
expect(uploader.destination.source).match(/\/remote\.php\/dav\/files\/my-user\/?$/) | ||
}) | ||
|
||
test('constructor sets default target folder for public share', async () => { | ||
initialState.loadState.mockImplementationOnce((app, key) => app === 'files_sharing' && key === 'sharingToken' ? 'token-123' : null) | ||
const uploader = new Uploader(true) | ||
expect(uploader.destination.source).match(/\/public\.php\/dav\/files\/token-123\/?$/) | ||
const isPublicInput = document.createElement('input') | ||
isPublicInput.id = 'initial-state-files_sharing-isPublic' | ||
isPublicInput.value = btoa(JSON.stringify(true)) | ||
document.body.appendChild(isPublicInput) | ||
|
||
const input = document.createElement('input') | ||
input.id = 'initial-state-files_sharing-sharingToken' | ||
input.value = btoa(JSON.stringify('modern-token')) | ||
document.body.appendChild(input) | ||
|
||
const uploader = await newUploader(true) | ||
expect(uploader.destination.source).match(/\/public\.php\/dav\/files\/modern-token\/?$/) | ||
}) | ||
|
||
test('constructor sets default target folder for legacy public share', async () => { | ||
const isPublicInput = document.createElement('input') | ||
isPublicInput.id = 'isPublic' | ||
isPublicInput.name = 'isPublic' | ||
isPublicInput.type = 'hidden' | ||
isPublicInput.value = '1' | ||
document.body.appendChild(isPublicInput) | ||
|
||
const input = document.createElement('input') | ||
input.id = 'sharingToken' | ||
input.name = 'sharingToken' | ||
input.type = 'hidden' | ||
input.value = 'legacy-token' | ||
document.body.appendChild(input) | ||
const uploader = new Uploader(true) | ||
expect(uploader.destination.source).match(/\/public\.php\/dav\/files\/legacy-token\/?$/) | ||
}) | ||
|
||
test('fails if no sharingToken on public share', async () => { | ||
expect(() => new Uploader(true)).toThrow(/No sharing token found/) | ||
const uploader = await newUploader(true) | ||
expect(uploader.destination.source).match(/\/public\.php\/dav\/files\/legacy-token\/?$/) | ||
}) | ||
|
||
test('fails if not logged in and not on public share', async () => { | ||
expect(() => new Uploader()).toThrow(/User is not logged in/) | ||
expect(initialState.loadState).not.toBeCalled() | ||
expect(async () => await newUploader()).rejects.toThrow(/User is not logged in/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters