From f99a102ad99bad51ecabcc521eeabe29fc75407c Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 25 Jul 2023 18:36:54 +0200 Subject: [PATCH] feat: Add function to query current path of the files list Signed-off-by: Ferdinand Thiessen --- __mocks__/@nextcloud/auth.js | 4 + __mocks__/@nextcloud/router.js | 1 + __tests__/dav/dav.spec.ts | 77 +++++++++++++++++++ .../fixtures/favorites-inner-response.json | 1 + .../fixtures/favorites-inner-response.xml | 2 + __tests__/fixtures/favorites-response.json | 1 + __tests__/fixtures/favorites-response.xml | 2 + lib/dav/dav.ts | 2 +- lib/files/path.ts | 35 +++++++++ package.json | 4 +- 10 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 __mocks__/@nextcloud/router.js create mode 100644 __tests__/dav/dav.spec.ts create mode 100644 __tests__/fixtures/favorites-inner-response.json create mode 100644 __tests__/fixtures/favorites-inner-response.xml create mode 100644 __tests__/fixtures/favorites-response.json create mode 100644 __tests__/fixtures/favorites-response.xml create mode 100644 lib/files/path.ts diff --git a/__mocks__/@nextcloud/auth.js b/__mocks__/@nextcloud/auth.js index 821eaf3e..b6bfcda5 100644 --- a/__mocks__/@nextcloud/auth.js +++ b/__mocks__/@nextcloud/auth.js @@ -5,3 +5,7 @@ export const getCurrentUser = function() { isAdmin: false, } } + +export const getRequestToken = function() { + return 'some-token-string' +} diff --git a/__mocks__/@nextcloud/router.js b/__mocks__/@nextcloud/router.js new file mode 100644 index 00000000..b05f9694 --- /dev/null +++ b/__mocks__/@nextcloud/router.js @@ -0,0 +1 @@ +export const generateRemoteUrl = (path) => `https://localhost/${path}` diff --git a/__tests__/dav/dav.spec.ts b/__tests__/dav/dav.spec.ts new file mode 100644 index 00000000..12738dee --- /dev/null +++ b/__tests__/dav/dav.spec.ts @@ -0,0 +1,77 @@ +import { afterAll, describe, expect, test, vi } from 'vitest' +import { readFile } from 'fs/promises' + +import { File, Folder, davDefaultRootUrl, davGetDefaultPropfind, davGetFavoritesReport, davRootPath, getFavoriteNodes } from '../../lib' + +vi.mock('@nextcloud/auth') +vi.mock('@nextcloud/router') + +afterAll(() => { + vi.resetAllMocks() +}) + +describe('DAV functions', () => { + test('root path is correct', () => { + expect(davRootPath).toBe('/files/test') + }) + + test('root url is correct', () => { + expect(davDefaultRootUrl).toBe('https://localhost/dav/files/test') + }) +}) + +describe('DAV requests', () => { + test('request all favorite files', async () => { + const favoritesResponseJSON = JSON.parse((await readFile(new URL('../fixtures/favorites-response.json', import.meta.url))).toString()) + + // Mock the WebDAV client + const client = { + getDirectoryContents: vi.fn((path: string, options: any) => { + if (options?.details) { + return { + data: favoritesResponseJSON, + } + } + return favoritesResponseJSON + }), + } + + const nodes = await getFavoriteNodes(client as never) + // Check client was called correctly + expect(client.getDirectoryContents).toBeCalled() + expect(client.getDirectoryContents.mock.lastCall?.at(0)).toBe('/') + expect(client.getDirectoryContents.mock.lastCall?.at(1)?.data).toBe(davGetFavoritesReport()) + expect(client.getDirectoryContents.mock.lastCall?.at(1)?.headers?.method).toBe('REPORT') + // Check for correct output + expect(nodes.length).toBe(2) + expect(nodes[0] instanceof Folder).toBe(true) + expect(nodes[0].basename).toBe('Neuer Ordner') + expect(nodes[0].mtime?.getTime()).toBe(Date.parse('Mon, 24 Jul 2023 16:30:44 GMT')) + expect(nodes[1] instanceof File).toBe(true) + }) + + test('request inner favorites', async () => { + const favoritesResponseJSON = JSON.parse((await readFile(new URL('../fixtures/favorites-inner-response.json', import.meta.url))).toString()) + + // Mock the WebDAV client + const client = { + getDirectoryContents: vi.fn((path: string, options: any) => { + if (options?.details) { + return { + data: favoritesResponseJSON, + } + } + return favoritesResponseJSON + }), + } + + const nodes = await getFavoriteNodes(client as never, '/Neuer Ordner') + // Check client was called correctly + expect(client.getDirectoryContents).toBeCalled() + expect(client.getDirectoryContents.mock.lastCall?.at(0)).toBe('/Neuer Ordner') + expect(client.getDirectoryContents.mock.lastCall?.at(1)?.data).toBe(davGetDefaultPropfind()) + expect(client.getDirectoryContents.mock.lastCall?.at(1)?.headers?.method).toBe('PROPFIND') + // There are no inner nodes + expect(nodes.length).toBe(0) + }) +}) diff --git a/__tests__/fixtures/favorites-inner-response.json b/__tests__/fixtures/favorites-inner-response.json new file mode 100644 index 00000000..bb8eb553 --- /dev/null +++ b/__tests__/fixtures/favorites-inner-response.json @@ -0,0 +1 @@ +[{"filename":"/Neuer Ordner","basename":"Neuer Ordner","lastmod":"Mon, 24 Jul 2023 16:30:44 GMT","size":0,"type":"directory","etag":"64bea734d3987","props":{"getetag":"\"64bea734d3987\"","getlastmodified":"Mon, 24 Jul 2023 16:30:44 GMT","quota-available-bytes":-3,"resourcetype":{"collection":""},"has-preview":false,"is-encrypted":0,"mount-type":"","share-attributes":"[]","comments-unread":0,"favorite":1,"fileid":74,"owner-display-name":"user1","owner-id":"user1","permissions":"RGDNVCK","share-types":{"share-type":3},"size":0,"share-permissions":31}}] \ No newline at end of file diff --git a/__tests__/fixtures/favorites-inner-response.xml b/__tests__/fixtures/favorites-inner-response.xml new file mode 100644 index 00000000..9d7b9bf3 --- /dev/null +++ b/__tests__/fixtures/favorites-inner-response.xml @@ -0,0 +1,2 @@ + +/remote.php/dav/files/user1/Neuer%20Ordner/"64bea734d3987"Mon, 24 Jul 2023 16:30:44 GMT-3false0[]0174user1user1RGDNVCK3031HTTP/1.1 200 OKHTTP/1.1 404 Not Found diff --git a/__tests__/fixtures/favorites-response.json b/__tests__/fixtures/favorites-response.json new file mode 100644 index 00000000..a88ddacb --- /dev/null +++ b/__tests__/fixtures/favorites-response.json @@ -0,0 +1 @@ +[{"filename":"/Neuer Ordner","basename":"Neuer Ordner","lastmod":"Mon, 24 Jul 2023 16:30:44 GMT","size":0,"type":"directory","etag":"64bea734d3987","props":{"getetag":"\"64bea734d3987\"","getlastmodified":"Mon, 24 Jul 2023 16:30:44 GMT","quota-available-bytes":-3,"resourcetype":{"collection":""},"has-preview":false,"is-encrypted":0,"mount-type":"","share-attributes":"[]","comments-unread":0,"favorite":1,"fileid":74,"owner-display-name":"user1","owner-id":"user1","permissions":"RGDNVCK","share-types":{"share-type":3},"size":0,"share-permissions":31}},{"filename":"/New folder/Neue Textdatei.md","basename":"Neue Textdatei.md","lastmod":"Tue, 25 Jul 2023 12:29:34 GMT","size":0,"type":"file","etag":"7a27142de0a62ed27a7293dbc16e93bc","mime":"text/markdown","props":{"getcontentlength":0,"getcontenttype":"text/markdown","getetag":"\"7a27142de0a62ed27a7293dbc16e93bc\"","getlastmodified":"Tue, 25 Jul 2023 12:29:34 GMT","resourcetype":"","has-preview":false,"mount-type":"shared","share-attributes":"[{\"scope\":\"permissions\",\"key\":\"download\",\"enabled\":false}]","comments-unread":0,"favorite":1,"fileid":80,"owner-display-name":"admin","owner-id":"admin","permissions":"SRGDNVW","share-types":"","size":0,"share-permissions":19}}] \ No newline at end of file diff --git a/__tests__/fixtures/favorites-response.xml b/__tests__/fixtures/favorites-response.xml new file mode 100644 index 00000000..615194be --- /dev/null +++ b/__tests__/fixtures/favorites-response.xml @@ -0,0 +1,2 @@ + +/remote.php/dav/files/user1/"632a3876842ffbf86f9e02df59829a56"Tue, 25 Jul 2023 12:29:34 GMT-3false0[]0057user1user1RGDNVCK17131HTTP/1.1 200 OKHTTP/1.1 404 Not Found diff --git a/lib/dav/dav.ts b/lib/dav/dav.ts index a3a308be..1a8b0799 100644 --- a/lib/dav/dav.ts +++ b/lib/dav/dav.ts @@ -84,7 +84,7 @@ export const davGetClient = function(davURL = davDefaultRootUrl) { * @param path Base path for the favorites, if unset all favorites are queried */ export const getFavoriteNodes = async (davClient: WebDAVClient, path = '/') => { - const contentsResponse = await davClient.getDirectoryContents('/', { + const contentsResponse = await davClient.getDirectoryContents(path, { details: true, // Only filter favorites if we're at the root data: path === '/' ? davGetFavoritesReport() : davGetDefaultPropfind(), diff --git a/lib/files/path.ts b/lib/files/path.ts new file mode 100644 index 00000000..7290c49d --- /dev/null +++ b/lib/files/path.ts @@ -0,0 +1,35 @@ +/** + * @copyright Copyright (c) 2023 John Molakvoæ + * + * @author John Molakvoæ + * @author Ferdinand Thiessen + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +/** + * Return the current directory of the files list, fallback to root + * + * @return {string} The current directory + */ +export const getCurrentDirectory = () => { + const currentDirInfo = window.OCA?.Files?.App?.currentFileList?.dirInfo + || { path: '/', name: '' } + + // Make sure we don't have double slashes + return `${currentDirInfo.path}/${currentDirInfo.name}`.replace(/\/\//gi, '/') +} diff --git a/package.json b/package.json index e6b9e0a2..f284897d 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "description": "Nextcloud files utils", "type": "module", "main": "dist/index.cjs", - "module": "dist/index.js", + "module": "dist/index.mjs", "types": "dist/index.d.ts", "exports": { ".": { "types": "./dist/index.d.ts", - "import": "./dist/index.js", + "import": "./dist/index.mjs", "require": "./dist/index.cjs" } },