mirror of
https://github.com/nextcloud/server.git
synced 2026-06-22 15:12:05 -04:00
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
import type { APIRequestContext } from '@playwright/test'
|
|
|
|
// we cannot import the enum directly from the files app.
|
|
// It references the window object and causes errors when imported in a node context,
|
|
// so we re-declare the relevant values here. The type assertion ensures we stay in sync.
|
|
export const SharePermission = {
|
|
READ: 1,
|
|
UPDATE: 2,
|
|
CREATE: 4,
|
|
DELETE: 8,
|
|
SHARE: 16,
|
|
} as const satisfies Partial<typeof import('@nextcloud/files').Permission>
|
|
|
|
/** All permissions a user share can grant. */
|
|
export const ALL_PERMISSIONS = SharePermission.READ
|
|
| SharePermission.UPDATE
|
|
| SharePermission.CREATE
|
|
| SharePermission.DELETE
|
|
| SharePermission.SHARE
|
|
|
|
/**
|
|
* Create a user-to-user share via the OCS Share API. Seeding shares through the
|
|
* API avoids driving the (flaky) share-editor sidebar.
|
|
*
|
|
* @param request - A request context authenticated as the share owner (e.g. the
|
|
* `ownerRequest` fixture)
|
|
* @param path - The path to share, relative to the owner's root
|
|
* @param shareWith - The user id of the share recipient
|
|
* @param permissions - The permission bitmask to grant (defaults to all)
|
|
*/
|
|
export async function createShare(
|
|
request: APIRequestContext,
|
|
path: string,
|
|
shareWith: string,
|
|
permissions: number = ALL_PERMISSIONS,
|
|
): Promise<void> {
|
|
const response = await request.post('/ocs/v2.php/apps/files_sharing/api/v1/shares?format=json', {
|
|
headers: { 'OCS-APIRequest': 'true' },
|
|
form: {
|
|
path,
|
|
shareType: 0, // user share
|
|
shareWith,
|
|
permissions,
|
|
},
|
|
})
|
|
// OCS returns HTTP 200 even on failure; the real status lives in ocs.meta
|
|
const { ocs } = await response.json()
|
|
if (ocs?.meta?.statuscode !== 200) {
|
|
throw new Error(`Creating share for ${path} failed: ${ocs?.meta?.statuscode} ${ocs?.meta?.message}`)
|
|
}
|
|
|
|
// A new share ignores the create-time permissions and always starts with the
|
|
// full set, so restricted permissions must be applied with a follow-up update.
|
|
if (permissions !== ALL_PERMISSIONS) {
|
|
const update = await request.put(`/ocs/v2.php/apps/files_sharing/api/v1/shares/${ocs.data.id}?format=json`, {
|
|
headers: { 'OCS-APIRequest': 'true' },
|
|
form: { permissions },
|
|
})
|
|
const updateMeta = (await update.json()).ocs?.meta
|
|
if (updateMeta?.statuscode !== 200) {
|
|
throw new Error(`Updating share ${ocs.data.id} failed: ${updateMeta?.statuscode} ${updateMeta?.message}`)
|
|
}
|
|
}
|
|
}
|