-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Make davRootPath
and davRemoteURL
support public shares
#996
Open
susnux
wants to merge
1
commit into
main
Choose a base branch
from
feat/public-share-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−119
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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
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 |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import type { ArgumentsType } from 'vitest' | ||
import type { FileStat } from 'webdav' | ||
import type { davResultToNode } from '../../lib/dav/dav' | ||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
||
const initialState = vi.hoisted(() => ({ loadState: vi.fn() })) | ||
const router = vi.hoisted(() => ({ generateRemoteUrl: vi.fn() })) | ||
const auth = vi.hoisted(() => ({ getCurrentUser: vi.fn() })) | ||
|
||
vi.mock('@nextcloud/auth', () => auth) | ||
vi.mock('@nextcloud/initial-state', () => initialState) | ||
vi.mock('@nextcloud/router', () => router) | ||
|
||
const restoreMocks = () => { | ||
vi.resetAllMocks() | ||
router.generateRemoteUrl.mockImplementation((service) => `https://example.com/remote.php/${service}`) | ||
} | ||
|
||
const mockPublicShare = () => { | ||
auth.getCurrentUser.mockImplementationOnce(() => null) | ||
initialState.loadState.mockImplementation((app, key) => { | ||
if (key === 'isPublic') { | ||
return true | ||
} else if (key === 'sharingToken') { | ||
return 'token-1234' | ||
} | ||
throw new Error('Unexpected loadState') | ||
}) | ||
} | ||
|
||
const mockLegacyPublicShare = () => { | ||
initialState.loadState.mockImplementationOnce(() => null) | ||
auth.getCurrentUser.mockImplementationOnce(() => null) | ||
|
||
const input = document.createElement('input') | ||
input.id = 'isPublic' | ||
input.name = 'isPublic' | ||
input.type = 'hidden' | ||
input.value = '1' | ||
document.body.appendChild(input) | ||
|
||
const token = document.createElement('input') | ||
token.id = 'sharingToken' | ||
token.type = 'hidden' | ||
token.value = 'legacy-token' | ||
document.body.appendChild(token) | ||
} | ||
|
||
describe('DAV path functions', () => { | ||
|
||
beforeEach(() => { | ||
vi.resetModules() | ||
restoreMocks() | ||
}) | ||
|
||
test('root path is correct on public shares', async () => { | ||
mockPublicShare() | ||
|
||
const { davGetRootPath } = await import('../../lib/dav/dav') | ||
expect(davGetRootPath()).toBe('/files/token-1234') | ||
}) | ||
|
||
test('root path is correct on legacy public shares', async () => { | ||
mockLegacyPublicShare() | ||
|
||
const { davGetRootPath } = await import('../../lib/dav/dav') | ||
expect(davGetRootPath()).toBe('/files/legacy-token') | ||
}) | ||
|
||
test('remote URL is correct on public shares', async () => { | ||
mockPublicShare() | ||
|
||
const { davGetRemoteURL } = await import('../../lib/dav/dav') | ||
expect(davGetRemoteURL()).toBe('https://example.com/public.php/dav') | ||
}) | ||
|
||
test('remote URL is correct on public shares', async () => { | ||
mockLegacyPublicShare() | ||
|
||
const { davGetRemoteURL } = await import('../../lib/dav/dav') | ||
expect(davGetRemoteURL()).toBe('https://example.com/public.php/dav') | ||
}) | ||
}) | ||
|
||
describe('on public shares', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
vi.resetModules() | ||
}) | ||
|
||
// Wrapper function as we can not static import the function to allow mocking the modules | ||
const resultToNode = async (...rest: ArgumentsType<typeof davResultToNode>) => { | ||
const { davResultToNode } = await import('../../lib/dav/dav') | ||
return davResultToNode(...rest) | ||
} | ||
|
||
const isPublicShare = async () => { | ||
const { isPublicShare: publicShare } = await import('../../lib') | ||
return publicShare() | ||
} | ||
|
||
/* | ||
* Result of: | ||
* davGetClient().getDirectoryContents(`${davRootPath}${path}`, { details: true }) | ||
*/ | ||
const result: FileStat = { | ||
filename: '/files/test/New folder/Neue Textdatei.md', | ||
basename: 'Neue Textdatei.md', | ||
lastmod: 'Tue, 25 Jul 2023 12:29:34 GMT', | ||
size: 123, | ||
type: 'file', | ||
etag: '7a27142de0a62ed27a7293dbc16e93bc', | ||
mime: 'text/markdown', | ||
props: { | ||
resourcetype: { collection: false }, | ||
displayname: 'New File', | ||
getcontentlength: '123', | ||
getcontenttype: 'text/markdown', | ||
getetag: '"7a27142de0a62ed27a7293dbc16e93bc"', | ||
getlastmodified: 'Tue, 25 Jul 2023 12:29:34 GMT', | ||
}, | ||
} | ||
|
||
describe('isPublicShare', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
restoreMocks() | ||
// reset JSDom | ||
document.body.innerHTML = '' | ||
}) | ||
|
||
test('no public share', async () => { | ||
initialState.loadState.mockImplementation(() => null) | ||
|
||
expect(await isPublicShare()).toBe(false) | ||
expect(initialState.loadState).toBeCalledWith('files_sharing', 'isPublic', null) | ||
}) | ||
|
||
test('public share', async () => { | ||
mockPublicShare() | ||
|
||
expect(await isPublicShare()).toBe(true) | ||
expect(initialState.loadState).toBeCalledWith('files_sharing', 'isPublic', null) | ||
}) | ||
|
||
test('legacy public share', async () => { | ||
mockLegacyPublicShare() | ||
|
||
expect(await isPublicShare()).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('davResultToNode', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
restoreMocks() | ||
}) | ||
|
||
test('has correct owner set on public shares', async () => { | ||
mockPublicShare() | ||
|
||
const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' } | ||
const node = await resultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav') | ||
|
||
expect(node.isDavRessource).toBe(true) | ||
expect(node.owner).toBe('anonymous') | ||
expect(initialState.loadState).toBeCalledWith('files_sharing', 'isPublic', null) | ||
}) | ||
|
||
test('has correct owner set on legacy public shares', async () => { | ||
mockLegacyPublicShare() | ||
|
||
const remoteResult = { ...result, filename: '/root/New folder/Neue Textdatei.md' } | ||
const node = await resultToNode(remoteResult, '/root', 'http://example.com/remote.php/dav') | ||
|
||
expect(node.isDavRessource).toBe(true) | ||
expect(node.owner).toBe('anonymous') | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, seems a bit more fitting 👍