CommunitySolidServer/test/unit/storage/accessors/SparqlDataAccessor.test.ts
Wout Slabbinck ca62511d12
feat: Edit metadata resources (#1188)
* feat: only allow metadata to be edited with PATCH request & only allow metadata files to be edited when a resource is available

* fix: remove unnecesary log at POST

* feat: PUT resets metadata contents + not possible to add metadata with PUT to container

* feat: add metadataStrategy (auxiliaryStrategy) + use that strategy in operationhandlers

* feat: PUT request on existing LDPC is not allowed as it would be possible to edit (read reset) metadata

* test: add unit tests to operationhandlers to handle metadata editing

* test: add unit tests to representationPatchHandler to handle metadata editing

* fix: update dependency of meta.json to version 3.0.0

* fix: lint and dependency still v2

* fix: replaced file references to resource references + moved Patch check to new patchhandler which is more generic

* fix: moved checking metadata resources checking from DELETE and POST handler down to DABS

* fix: remove PATCH message about metadata extension

* fix: move PATCH message about metadata extension

* WIP: adding writeMetadata + getMetadata in DABS and add writeMetadata to DataAccessors (part 1)

* WIP: implement writeMetadata in memorybackend + change resourceExists

* WIP: implement writeMetadata in SparqlDataAccessor.ts

* test: fix test interfaces

* test: InMemoryDataAccessor.ts resulted into changing identifier for writeMetadata in DataAccessor.ts (now taking subject identifier instead of metadata resource identifier)

* test: accessor tests implemented for metadata

* test: add RdfImmutableCheckPatcher.test.ts

* test: add tests in DataAccessorBasedStore.test.ts

* test: fix template config for DynamicPods test

* test: add integration tests for metadata

* fix: change metaStrategy to metadataStrategy

* refactor: comments updated to new location CSS on github + some alphabetical edits

* refactor: remove getMetadata function in DABS as it is only used once

* refactor: add DataAccessorBasedStoreArgs to DataAccessorBasedStore.ts

* docs: modify documentation for writeMetadata function in DataAccessor.ts

* feat: ldp:contains is also part of the metadata resource of a container

* refactor: change function name and move check to DataAccessorBasedStore

* fix: fix tests for DABS and PutOperationHandler

* feat: avoid cloneRepresentation by introducing RdfPatcher, RdfStorePatcher and modifying ImmutableMetadataPatcher, N3Patcher, patching.json and SparqlUpdatePatcher

* test: fix patcher tests

* feat: create sparqlInsertMetadata in SparqlDataAccessor.ts

* fix: move check during put on container if it exists already back to PutOperationHandler.ts after discussion in PR

* test: update tests PutOperationHandler.ts and DataAccessorBasedStore.ts regarding previous commit

* test: add converter to DABS and replace rejection on data during container creation to warning

* test: implemented RdfPatcher test

* feat: remove ContainerPatcher

* fix: fix lint

* fix: fix integration tests

* refactor: fix minor issues mentioned in the PR

* WIP: problem with removeResponseMetadata

* refactor: remove responseMetadata in QuadToRdfConverter.ts

* feat: handle ResponeMetadata when writing to the store via a patch

* refactor: refactor based on comments in PR

* feat: make ImmutableMetadataPatcher.ts instantiation more clear

* test: achieve 100% coverage again

* fix: fix lint

* refactor: return to explicit arguments for the DABS

* fix: return to explicit arguments for the DABS (missed one)

* feat: optimise immutable checker

* fix: fix, enhance docs + optimise config files

* fix: DABS + QuadToRdfConverter feedback implemented

* fix: patching feedback implemented

* test: update operationhandler tests

* test: update integration tests after feedback

* test: update DABS tests after feedback

* test: update ImmutableMetadataPatcher.test.ts after feedback

* test: update patch tests after feedback

* docs: add documentation about editing metadata

* fix: config: intendation + name change + extra filters | filter pattern

* docs: tsdoc added to RdfStorePatcher.ts

* fix: DABS split implemented for getRepresentation + comment refactoring

* docs: further documentation on removing response data on serialization

* fix: DABS getRepresentation method

* docs: apply feedback from Joachim on the documentation of metadata-editing.md

* fix: indentation fix + fix metadata-editing.md documentation after feedback from Joachim

* docs: small fix in metadata-editing.md documentation after feedback from Joachim

* fix: fix metadata-editing.md documentation after feedback from Joachim

* fix: fix tests meta-editing after feedback Joachim

* feat: first attempt at RELEASE_NOTES.md

* docs: update release notes based on feedback

* docs: fix newline

* fix: patching config changes after feedback

* docs: metadata editing documentation changes after feedback

* docs: metadata editing documentation changes after feedback

* docs: metadata editing documentation changes after feedback

* feat: optimisation on ImmutableMetadataPatcher.ts algorithm

* feat: remove converter from DABS and add conversion for metadata resources in the RCS

* fix: Fix documentation RepresentationPatchHandler.ts + fix response graph not being stored due to convertingstore

* feat: make RepresentationPatcher generic

* test: generic RepresentationPatcher tests

* test: 100% coverage for patchers again

* feat: containers can be created with POST with no content-type

* feat: Immutable checks always with subject identifier

* feat: create AuxiliaryLinkMetadataWriter for adding description resources Link Header

* test: add tests for AuxiliaryLinkMetadataWriter and update them for ImmutableMetadataPatcher

* feat: remove metadataGenerator from acl.json and fix tests accordingly

* WIP: preserve metadata on PUT

* feat: preserve metadata on PUT

* fix: keep metadata on PATCHes

* test: add unit tests for preserving metadata on PUT

* fix: remove inConverter from sparql endpoint as that is already the default in the (converting.json)

* fix: add metadatastrategy to RepresentationConvertingStore in regex.json

* test: add integration tests for preserving metadata on PUT

* docs: update release notes and adding documentation about preserving metadata on PUT

* WIP: Template create setRepresentation

* fix: Move container exists and not allowed check to setRepresentation

* test: fix lint

* fix: update configs and documentation

* refactor: update and add documentation + small refactoring

* refactor: update and add documentation + small refactoring + fix tests

* fix: Dynamic pod config + tests

* fix: TemplatedResourcesGenerator does not create containers when they already exist

* fix: metadata preservation now deals with complex content types

* docs: explain the case when there is no content-type

* fix: minor comments
2022-08-04 15:24:36 +02:00

307 lines
15 KiB
TypeScript

import 'jest-rdf';
import { Readable } from 'stream';
import arrayifyStream from 'arrayify-stream';
import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint';
import { DataFactory } from 'n3';
import type { Quad } from 'rdf-js';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { SparqlDataAccessor } from '../../../../src/storage/accessors/SparqlDataAccessor';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
import type { Guarded } from '../../../../src/util/GuardedStream';
import { SingleRootIdentifierStrategy } from '../../../../src/util/identifiers/SingleRootIdentifierStrategy';
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
import { CONTENT_TYPE_TERM, LDP, RDF } from '../../../../src/util/Vocabularies';
const { literal, namedNode, quad } = DataFactory;
jest.mock('fetch-sparql-endpoint');
function simplifyQuery(query: string | string[]): string {
if (Array.isArray(query)) {
query = query.join(' ');
}
return query.replace(/\n/gu, ' ').trim();
}
describe('A SparqlDataAccessor', (): void => {
const endpoint = 'http://test.com/sparql';
const base = 'http://test.com/';
const identifierStrategy = new SingleRootIdentifierStrategy(base);
let accessor: SparqlDataAccessor;
let metadata: RepresentationMetadata;
let data: Guarded<Readable>;
let fetchTriples: jest.Mock<Promise<Readable>>;
let fetchUpdate: jest.Mock<Promise<void>>;
let triples: Quad[];
let fetchError: any;
let updateError: any;
beforeEach(async(): Promise<void> => {
metadata = new RepresentationMetadata();
data = guardedStreamFrom(
[ quad(namedNode('http://name'), namedNode('http://pred'), literal('value')) ],
);
triples = [ quad(namedNode('this'), namedNode('a'), namedNode('triple')) ];
// Makes it so the `SparqlEndpointFetcher` will always return the contents of the `triples` array
fetchTriples = jest.fn(async(): Promise<Readable> => {
if (fetchError) {
throw fetchError;
}
return Readable.from(triples);
});
fetchUpdate = jest.fn(async(): Promise<void> => {
if (updateError) {
throw updateError;
}
});
(SparqlEndpointFetcher as any).mockImplementation((): any => ({
fetchTriples,
fetchUpdate,
}));
// This needs to be last so the fetcher can be mocked first
accessor = new SparqlDataAccessor(endpoint, identifierStrategy);
});
it('can only handle quad data.', async(): Promise<void> => {
let representation = new BasicRepresentation(data, metadata, true);
await expect(accessor.canHandle(representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
representation = new BasicRepresentation(data, 'internal/newInternalType', false);
await expect(accessor.canHandle(representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
representation = new BasicRepresentation(data, INTERNAL_QUADS, false);
metadata.contentType = INTERNAL_QUADS;
await expect(accessor.canHandle(representation)).resolves.toBeUndefined();
});
it('returns the corresponding quads when data is requested.', async(): Promise<void> => {
const result = await accessor.getData({ path: 'http://identifier' });
await expect(arrayifyStream(result)).resolves.toBeRdfIsomorphic([
quad(namedNode('this'), namedNode('a'), namedNode('triple')),
]);
expect(fetchTriples).toHaveBeenCalledTimes(1);
expect(fetchTriples.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery(
'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH <http://identifier> { ?s ?p ?o. } }',
));
});
it('returns the corresponding metadata when requested.', async(): Promise<void> => {
metadata = await accessor.getMetadata({ path: 'http://identifier' });
expect(metadata.quads()).toBeRdfIsomorphic([
quad(namedNode('this'), namedNode('a'), namedNode('triple')),
quad(namedNode('http://identifier'), CONTENT_TYPE_TERM, literal(INTERNAL_QUADS)),
]);
expect(fetchTriples).toHaveBeenCalledTimes(1);
expect(fetchTriples.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery(
'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH <meta:http://identifier> { ?s ?p ?o. } }',
));
});
it('does not set the content-type for container metadata.', async(): Promise<void> => {
metadata = await accessor.getMetadata({ path: 'http://container/' });
expect(metadata.quads()).toBeRdfIsomorphic([
quad(namedNode('this'), namedNode('a'), namedNode('triple')),
]);
expect(fetchTriples).toHaveBeenCalledTimes(1);
expect(fetchTriples.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery(
'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH <meta:http://container/> { ?s ?p ?o. } }',
));
});
it('requests the container data to find its children.', async(): Promise<void> => {
triples = [ quad(namedNode('http://container/'), LDP.terms.contains, namedNode('http://container/child')) ];
const children = [];
for await (const child of accessor.getChildren({ path: 'http://container/' })) {
children.push(child);
}
expect(children).toHaveLength(1);
expect(children[0].identifier.value).toBe('http://container/child');
expect(fetchTriples).toHaveBeenCalledTimes(1);
expect(fetchTriples.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery(
'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH <http://container/> { ?s ?p ?o. } }',
));
});
it('throws 404 if no metadata was found.', async(): Promise<void> => {
// Clear triples array
triples = [];
await expect(accessor.getMetadata({ path: 'http://identifier' })).rejects.toThrow(NotFoundHttpError);
expect(fetchTriples).toHaveBeenCalledTimes(1);
expect(fetchTriples.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery(
'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH <meta:http://identifier> { ?s ?p ?o. } }',
));
});
it('overwrites the metadata when writing a container and updates parent.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/' },
{ [RDF.type]: [ LDP.terms.Resource, LDP.terms.Container ]});
await expect(accessor.writeContainer({ path: 'http://test.com/container/' }, metadata)).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <meta:http://test.com/container/> { ?s ?p ?o. } };',
'INSERT DATA {',
' GRAPH <meta:http://test.com/container/> {',
' <http://test.com/container/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Resource>.',
' <http://test.com/container/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Container>.',
' }',
' GRAPH <http://test.com/> { <http://test.com/> <http://www.w3.org/ns/ldp#contains> <http://test.com/container/>. }',
'}',
]));
});
it('does not write containment triples when writing to a root container.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/' },
{ [RDF.type]: [ LDP.terms.Resource, LDP.terms.Container ]});
await expect(accessor.writeContainer({ path: 'http://test.com/' }, metadata)).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <meta:http://test.com/> { ?s ?p ?o. } };',
'INSERT DATA {',
' GRAPH <meta:http://test.com/> {',
' <http://test.com/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Resource>.',
' <http://test.com/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Container>.',
' }',
'}',
]));
});
it('overwrites the data and metadata when writing a resource and updates parent.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/resource' },
{ [RDF.type]: [ LDP.terms.Resource ]});
await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata))
.resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <http://test.com/container/resource> { ?s ?p ?o. } };',
'DELETE WHERE { GRAPH <meta:http://test.com/container/resource> { ?s ?p ?o. } };',
'INSERT DATA {',
' GRAPH <meta:http://test.com/container/resource> { <http://test.com/container/resource> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Resource>. }',
' GRAPH <http://test.com/container/> { <http://test.com/container/> <http://www.w3.org/ns/ldp#contains> <http://test.com/container/resource>. }',
' GRAPH <http://test.com/container/resource> { <http://name> <http://pred> "value". }',
'}',
]));
});
it('overwrites the data and metadata when writing an empty resource.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/resource' },
{ [RDF.type]: [ LDP.terms.Resource ]});
const empty = guardedStreamFrom([]);
await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, empty, metadata))
.resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <http://test.com/container/resource> { ?s ?p ?o. } };',
'DELETE WHERE { GRAPH <meta:http://test.com/container/resource> { ?s ?p ?o. } };',
'INSERT DATA {',
' GRAPH <meta:http://test.com/container/resource> { <http://test.com/container/resource> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Resource>. }',
' GRAPH <http://test.com/container/> { <http://test.com/container/> <http://www.w3.org/ns/ldp#contains> <http://test.com/container/resource>. }',
'}',
]));
});
it('removes all references when deleting a resource.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/' },
{ [RDF.type]: [ LDP.terms.Resource, LDP.terms.Container ]});
await expect(accessor.deleteResource({ path: 'http://test.com/container/' })).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <http://test.com/container/> { ?s ?p ?o. } };',
'DELETE WHERE { GRAPH <meta:http://test.com/container/> { ?s ?p ?o. } };',
'DELETE DATA { GRAPH <http://test.com/> { <http://test.com/> <http://www.w3.org/ns/ldp#contains> <http://test.com/container/>. } }',
]));
});
it('does not try to remove containment triples when deleting a root container.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/' },
{ [RDF.type]: [ LDP.terms.Resource, LDP.terms.Container ]});
await expect(accessor.deleteResource({ path: 'http://test.com/' })).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <http://test.com/> { ?s ?p ?o. } };',
'DELETE WHERE { GRAPH <meta:http://test.com/> { ?s ?p ?o. } }',
]));
});
it('errors when trying to write to a metadata document.', async(): Promise<void> => {
const result = accessor.writeDocument({ path: 'meta:http://test.com/container/resource' }, data, metadata);
await expect(result).rejects.toThrow(ConflictHttpError);
await expect(result).rejects.toThrow('Not allowed to create NamedNodes with the metadata extension.');
});
it('errors when writing triples in a non-default graph.', async(): Promise<void> => {
data = guardedStreamFrom(
[ quad(namedNode('http://name'), namedNode('http://pred'), literal('value'), namedNode('badGraph!')) ],
);
const result = accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata);
await expect(result).rejects.toThrow(NotImplementedHttpError);
await expect(result).rejects.toThrow('Only triples in the default graph are supported.');
});
it('errors when the SPARQL endpoint fails during reading.', async(): Promise<void> => {
fetchError = 'error';
await expect(accessor.getMetadata({ path: 'http://identifier' })).rejects.toBe(fetchError);
fetchError = new Error('read error');
await expect(accessor.getMetadata({ path: 'http://identifier' })).rejects.toThrow(fetchError);
fetchError = undefined;
});
it('errors when the SPARQL endpoint fails during writing.', async(): Promise<void> => {
const identifier = { path: 'http://test.com/container/' };
metadata = new RepresentationMetadata(identifier);
updateError = 'error';
await expect(accessor.writeContainer(identifier, metadata)).rejects.toBe(updateError);
updateError = new Error('write error');
await expect(accessor.writeContainer(identifier, metadata)).rejects.toThrow(updateError);
updateError = undefined;
});
it('overwrites the metadata when writing metadata.', async(): Promise<void> => {
const resourceIdentifier = { path: `${base}resource` };
const newMetadata = new RepresentationMetadata(resourceIdentifier);
newMetadata.addQuad(namedNode(`${base}a`), namedNode(`${base}b`), namedNode(`${base}c`));
await expect(accessor.writeMetadata(resourceIdentifier, newMetadata)).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <meta:http://test.com/resource> { ?s ?p ?o. } };',
'INSERT DATA {',
'GRAPH <meta:http://test.com/resource> { <http://test.com/a> <http://test.com/b> <http://test.com/c>. }',
'}',
]));
});
});