|
| 1 | +import { createReadStream } from 'fs'; |
| 2 | +import fetch from 'cross-fetch'; |
| 3 | +import type { Quad } from 'n3'; |
| 4 | +import { DataFactory, Parser, Store } from 'n3'; |
| 5 | +import { joinFilePath, PIM, RDF } from '@solid/community-server'; |
| 6 | +import type { App } from '@solid/community-server'; |
| 7 | +import { |
| 8 | + deleteResource, |
| 9 | + expectQuads, |
| 10 | + getResource, |
| 11 | + patchResource, |
| 12 | + postResource, |
| 13 | + putResource, |
| 14 | +} from '../util/FetchUtil'; |
| 15 | +import { getPort } from '../util/Util'; |
| 16 | +import { |
| 17 | + getDefaultVariables, |
| 18 | + getPresetConfigPath, |
| 19 | + getTestConfigPath, |
| 20 | + getTestFolder, |
| 21 | + instantiateFromConfig, |
| 22 | + removeFolder, |
| 23 | +} from './Config'; |
| 24 | +const { literal, namedNode, quad } = DataFactory; |
| 25 | + |
| 26 | +const port = getPort('LpdHandlerWithoutAuth'); |
| 27 | +const baseUrl = `http://localhost:${port}/`; |
| 28 | +const metaSuffix = '.meta'; |
| 29 | +const rootFilePath = getTestFolder('full-config-no-auth'); |
| 30 | +const stores: [string, any][] = [ |
| 31 | + [ 'in-memory storage', { |
| 32 | + storeConfig: 'storage/backend/memory.json', |
| 33 | + teardown: jest.fn(), |
| 34 | + }], |
| 35 | + [ 'on-disk storage', { |
| 36 | + storeConfig: 'storage/backend/file.json', |
| 37 | + teardown: async(): Promise<void> => removeFolder(rootFilePath), |
| 38 | + }], |
| 39 | +]; |
| 40 | + |
| 41 | +describe.each(stores)('An LDP handler allowing all requests %s', (name, { storeConfig, teardown }): void => { |
| 42 | + let app: App; |
| 43 | + |
| 44 | + beforeAll(async(): Promise<void> => { |
| 45 | + const variables = { |
| 46 | + ...getDefaultVariables(port, baseUrl), |
| 47 | + 'urn:solid-server:default:variable:rootFilePath': rootFilePath, |
| 48 | + }; |
| 49 | + |
| 50 | + // Create and start the server |
| 51 | + const instances = await instantiateFromConfig( |
| 52 | + 'urn:solid-server:test:Instances', |
| 53 | + [ |
| 54 | + getPresetConfigPath(storeConfig), |
| 55 | + getTestConfigPath('ldp-with-auth.json'), |
| 56 | + ], |
| 57 | + variables, |
| 58 | + ) as Record<string, any>; |
| 59 | + ({ app } = instances); |
| 60 | + |
| 61 | + await app.start(); |
| 62 | + }); |
| 63 | + |
| 64 | + afterAll(async(): Promise<void> => { |
| 65 | + await teardown(); |
| 66 | + await app.stop(); |
| 67 | + }); |
| 68 | + |
| 69 | + const shape = `<http://example.org/exampleshape> a <http://www.w3.org/ns/shacl#NodeShape>. |
| 70 | + <http://example.org/exampleshape> <http://www.w3.org/ns/shacl#targetClass> <http://example.org/c>. |
| 71 | + <http://example.org/exampleshape> <http://www.w3.org/ns/shacl#property> <http://example.org/property>. |
| 72 | + <http://example.org/property> <http://www.w3.org/ns/shacl#path> <http://xmlns.com/foaf/0.1/name>. |
| 73 | + <http://example.org/property> <http://www.w3.org/ns/shacl#minCount> 1. |
| 74 | + <http://example.org/property> <http://www.w3.org/ns/shacl#maxCount> 1. |
| 75 | + <http://example.org/property> <http://www.w3.org/ns/shacl#datatype> <http://www.w3.org/2001/XMLSchema#string>.`; |
| 76 | + const conformingResource = `<a> a <http://example.org/c>. |
| 77 | + <a> <http://xmlns.com/foaf/0.1/name> "test".`; |
| 78 | + |
| 79 | + it('can validate RDF resources using SHACL shapes.', async(): Promise<void> => { |
| 80 | + const shapeURL = `${baseUrl}shape`; |
| 81 | + const constrainedContainerURL = `${baseUrl}constrained/`; |
| 82 | + |
| 83 | + // PUT shape |
| 84 | + await putResource(shapeURL, { contentType: 'text/turtle', body: shape }); |
| 85 | + |
| 86 | + // PUT container for constrained resources |
| 87 | + await putResource(constrainedContainerURL, { contentType: 'text/turtle' }); |
| 88 | + |
| 89 | + // PATCH: Add shape constraint to container |
| 90 | + await patchResource( |
| 91 | + constrainedContainerURL + metaSuffix, |
| 92 | + `INSERT DATA { <${constrainedContainerURL}> <http://www.w3.org/ns/ldp#constrainedBy> <${shapeURL}>}`, |
| 93 | + 'sparql', |
| 94 | + true, |
| 95 | + ); |
| 96 | + |
| 97 | + const constrainedContainerResponse = await getResource(constrainedContainerURL); |
| 98 | + expect(constrainedContainerResponse.headers.get('link')) |
| 99 | + .toContain(`<${shapeURL}>; rel="http://www.w3.org/ns/ldp#constrainedBy"`); |
| 100 | + |
| 101 | + // POST: Add resource to constrained container which is valid |
| 102 | + const postResponse = await postResource( |
| 103 | + constrainedContainerURL, { contentType: 'text/turtle', body: conformingResource }, |
| 104 | + ); |
| 105 | + |
| 106 | + // POST: Add resource to constrained container that is not valid |
| 107 | + const response1 = await fetch(constrainedContainerURL, { |
| 108 | + method: 'POST', |
| 109 | + headers: { 'content-type': 'text/turtle' }, |
| 110 | + body: '<a> <b> <c>.', |
| 111 | + }); |
| 112 | + expect(response1.status).toBe(400); |
| 113 | + |
| 114 | + // PUT: Add container resource (invalid) |
| 115 | + const response2 = await fetch(`${constrainedContainerURL}container/`, { |
| 116 | + method: 'PUT', |
| 117 | + headers: { 'content-type': 'text/turtle' }, |
| 118 | + }); |
| 119 | + expect(response2.status).toBe(400); |
| 120 | + |
| 121 | + // DELETE |
| 122 | + expect(await deleteResource(postResponse.headers.get('location')!)).toBeUndefined(); |
| 123 | + expect(await deleteResource(constrainedContainerURL)).toBeUndefined(); |
| 124 | + expect(await deleteResource(shapeURL)).toBeUndefined(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('can not validate non-RDF resources using SHACL shapes.', async(): Promise<void> => { |
| 128 | + const shapeURL = `${baseUrl}shape`; |
| 129 | + const constrainedContainerURL = `${baseUrl}constrained/`; |
| 130 | + |
| 131 | + // PUT shape |
| 132 | + await putResource(shapeURL, { contentType: 'text/turtle', body: shape }); |
| 133 | + |
| 134 | + // PUT container for constrained resources |
| 135 | + await putResource(constrainedContainerURL, { contentType: 'text/turtle' }); |
| 136 | + |
| 137 | + // PATCH: Add shape constraint to container |
| 138 | + await patchResource( |
| 139 | + constrainedContainerURL + metaSuffix, |
| 140 | + `INSERT DATA { <${constrainedContainerURL}> <http://www.w3.org/ns/ldp#constrainedBy> <${shapeURL}>}`, |
| 141 | + 'sparql', |
| 142 | + true, |
| 143 | + ); |
| 144 | + |
| 145 | + const constrainedContainerResponse = await getResource(constrainedContainerURL); |
| 146 | + expect(constrainedContainerResponse.headers.get('link')) |
| 147 | + .toContain(`<${shapeURL}>; rel="http://www.w3.org/ns/ldp#constrainedBy"`); |
| 148 | + |
| 149 | + // POST non-RDF resource |
| 150 | + const response1 = await fetch(constrainedContainerURL, { |
| 151 | + method: 'POST', |
| 152 | + headers: { 'content-type': 'text/plain' }, |
| 153 | + body: 'Hello world!', |
| 154 | + }); |
| 155 | + expect(response1.status).toBe(400); |
| 156 | + |
| 157 | + // DELETE |
| 158 | + expect(await deleteResource(constrainedContainerURL)).toBeUndefined(); |
| 159 | + expect(await deleteResource(shapeURL)).toBeUndefined(); |
| 160 | + }); |
| 161 | +}); |
0 commit comments