From dfb8ac938d96291c7bddfd79cc4dad39af213a35 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Mon, 5 Jul 2021 15:41:35 +0200 Subject: [PATCH] test: Add acl test for file paths with spaces --- test/integration/LdpHandlerWithAuth.test.ts | 42 ++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/test/integration/LdpHandlerWithAuth.test.ts b/test/integration/LdpHandlerWithAuth.test.ts index 6f9bdfa8f..db7cd4ab4 100644 --- a/test/integration/LdpHandlerWithAuth.test.ts +++ b/test/integration/LdpHandlerWithAuth.test.ts @@ -1,6 +1,7 @@ +import { promises as fsPromises } from 'fs'; import fetch from 'cross-fetch'; import type { ResourceStore, App } from '../../src/'; -import { BasicRepresentation } from '../../src/'; +import { BasicRepresentation, isSystemError, joinFilePath } from '../../src/'; import { AclHelper } from '../util/AclHelper'; import { deleteResource, getResource, postResource, putResource } from '../util/FetchUtil'; import { getPort } from '../util/Util'; @@ -193,4 +194,43 @@ describe.each(stores)('An LDP handler with auth using %s', (name, { storeConfig, expect(response.status).toBe(401); expect(response.headers.get('www-authenticate')).toBe('Bearer scope="openid webid"'); }); + + it('supports file paths with spaces.', async(): Promise => { + // Specific problem for file store, so only do this if the rootFilePath folder exists + try { + await fsPromises.lstat(rootFilePath); + } catch (error: unknown) { + if (isSystemError(error) && error.code === 'ENOENT') { + return; + } + throw error; + } + + // Correct identifier when there are spaces + const identifier = { path: `${baseUrl}with%20spaces.txt` }; + + // Set acl specifically for this identifier + const acl = `@prefix acl: . +@prefix foaf: . +<#authorization> + a acl:Authorization; + acl:agentClass foaf:Agent; + acl:mode acl:Read; + acl:accessTo <${identifier.path}>.`; + + // Prevent other access to make sure there is no hierarchy bug + await aclHelper.setSimpleAcl(baseUrl, { + permissions: {}, + agentClass: 'agent', + default: true, + }); + + // Write files manually to make sure there are spaces + await fsPromises.writeFile(joinFilePath(rootFilePath, 'with spaces.txt.acl'), acl); + await fsPromises.writeFile(joinFilePath(rootFilePath, 'with spaces.txt'), 'valid data'); + + // GET file + const response = await getResource(identifier.path); + expect(await response.text()).toContain('valid data'); + }); });