CommunitySolidServer/test/unit/storage/accessors/FileDataAccessor.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

492 lines
23 KiB
TypeScript

import 'jest-rdf';
import type { Readable } from 'stream';
import { DataFactory } from 'n3';
import type { Representation } from '../../../../src/http/representation/Representation';
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
import { FileDataAccessor } from '../../../../src/storage/accessors/FileDataAccessor';
import { ExtensionBasedMapper } from '../../../../src/storage/mapping/ExtensionBasedMapper';
import type { FileIdentifierMapper, ResourceLink } from '../../../../src/storage/mapping/FileIdentifierMapper';
import { APPLICATION_OCTET_STREAM } from '../../../../src/util/ContentTypes';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
import type { SystemError } from '../../../../src/util/errors/SystemError';
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
import type { Guarded } from '../../../../src/util/GuardedStream';
import { isContainerPath } from '../../../../src/util/PathUtil';
import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil';
import { toLiteral } from '../../../../src/util/TermUtil';
import { CONTENT_TYPE, DC, LDP, POSIX, RDF, SOLID_META, XSD } from '../../../../src/util/Vocabularies';
import { mockFileSystem } from '../../../util/Util';
const { namedNode, quad } = DataFactory;
jest.mock('fs');
jest.mock('fs-extra');
const rootFilePath = 'uploads';
const now = new Date();
// All relevant functions do not care about the milliseconds or remove them
now.setMilliseconds(0);
describe('A FileDataAccessor', (): void => {
const base = 'http://test.com/';
let mapper: FileIdentifierMapper;
let accessor: FileDataAccessor;
let cache: { data: any };
let metadata: RepresentationMetadata;
let data: Guarded<Readable>;
beforeEach(async(): Promise<void> => {
cache = mockFileSystem(rootFilePath, now);
mapper = new ExtensionBasedMapper(base, rootFilePath);
accessor = new FileDataAccessor(mapper);
metadata = new RepresentationMetadata(APPLICATION_OCTET_STREAM);
data = guardedStreamFrom([ 'data' ]);
});
it('can only handle binary data.', async(): Promise<void> => {
await expect(accessor.canHandle({ binary: true } as Representation)).resolves.toBeUndefined();
const result = accessor.canHandle({ binary: false } as Representation);
await expect(result).rejects.toThrow(UnsupportedMediaTypeHttpError);
await expect(result).rejects.toThrow('Only binary data is supported.');
});
describe('getting data', (): void => {
it('throws a 404 if the identifier does not start with the base.', async(): Promise<void> => {
await expect(accessor.getData({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if the identifier does not match an existing file.', async(): Promise<void> => {
await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if the identifier matches a directory.', async(): Promise<void> => {
cache.data = { resource: {}};
await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError);
});
it('returns the corresponding data.', async(): Promise<void> => {
cache.data = { resource: 'data' };
const stream = await accessor.getData({ path: `${base}resource` });
await expect(readableToString(stream)).resolves.toBe('data');
});
it('throws an error if something else went wrong.', async(): Promise<void> => {
jest.requireMock('fs-extra').stat = (): any => {
throw new Error('error');
};
await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow('error');
});
});
describe('getting metadata', (): void => {
it('throws a 404 if the identifier does not start with the base.', async(): Promise<void> => {
await expect(accessor.getMetadata({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if the identifier does not match an existing file.', async(): Promise<void> => {
await expect(accessor.getMetadata({ path: `${base}container/` })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if it matches something that is no file or directory.', async(): Promise<void> => {
cache.data = { resource: 5 };
await expect(accessor.getMetadata({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError);
});
it('throws an error if something else went wrong.', async(): Promise<void> => {
jest.requireMock('fs-extra').lstat = (): any => {
throw new Error('error');
};
await expect(accessor.getMetadata({ path: base })).rejects.toThrow('error');
});
it('throws a 404 if the trailing slash does not match its type.', async(): Promise<void> => {
cache.data = { resource: 'data' };
await expect(accessor.getMetadata({ path: `${base}resource/` })).rejects.toThrow(NotFoundHttpError);
cache.data = { container: {}};
await expect(accessor.getMetadata({ path: `${base}container` })).rejects.toThrow(NotFoundHttpError);
});
it('generates the metadata for a resource.', async(): Promise<void> => {
cache.data = { 'resource.ttl': 'data' };
metadata = await accessor.getMetadata({ path: `${base}resource.ttl` });
expect(metadata.identifier.value).toBe(`${base}resource.ttl`);
expect(metadata.contentType).toBe('text/turtle');
expect(metadata.get(RDF.terms.type)?.value).toBe(LDP.Resource);
expect(metadata.get(POSIX.terms.size)).toEqualRdfTerm(toLiteral('data'.length, XSD.terms.integer));
expect(metadata.get(DC.terms.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.terms.dateTime));
expect(metadata.get(POSIX.terms.mtime)).toEqualRdfTerm(toLiteral(Math.floor(now.getTime() / 1000),
XSD.terms.integer));
// `dc:modified` is in the default graph
expect(metadata.quads(null, null, null, SOLID_META.terms.ResponseMetadata)).toHaveLength(2);
});
it('does not generate size metadata for a container.', async(): Promise<void> => {
cache.data = { container: {}};
metadata = await accessor.getMetadata({ path: `${base}container/` });
expect(metadata.get(POSIX.terms.size)).toBeUndefined();
expect(metadata.get(DC.terms.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.terms.dateTime));
});
it('generates the metadata for a container.', async(): Promise<void> => {
cache.data = {
container: {
resource: 'data',
'resource.meta': 'metadata',
notAFile: 5,
container2: {},
},
};
metadata = await accessor.getMetadata({ path: `${base}container/` });
expect(metadata.identifier.value).toBe(`${base}container/`);
expect(metadata.getAll(RDF.terms.type)).toEqualRdfTermArray(
[ LDP.terms.Container, LDP.terms.BasicContainer, LDP.terms.Resource ],
);
expect(metadata.get(POSIX.terms.size)).toBeUndefined();
expect(metadata.get(DC.terms.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.terms.dateTime));
expect(metadata.get(POSIX.terms.mtime)).toEqualRdfTerm(toLiteral(Math.floor(now.getTime() / 1000),
XSD.terms.integer));
// `dc:modified` is in the default graph
expect(metadata.quads(null, null, null, SOLID_META.terms.ResponseMetadata)).toHaveLength(1);
});
it('generates metadata for container child resources.', async(): Promise<void> => {
cache.data = {
container: {
resource: 'data',
'resource.meta': 'metadata',
symlink: Symbol(`${rootFilePath}/container/resource`),
symlinkContainer: Symbol(`${rootFilePath}/container/container2`),
symlinkInvalid: Symbol(`${rootFilePath}/invalid`),
notAFile: 5,
container2: {},
},
};
const children = [];
for await (const child of accessor.getChildren({ path: `${base}container/` })) {
children.push(child);
}
// Identifiers
expect(children).toHaveLength(4);
expect(new Set(children.map((child): string => child.identifier.value))).toEqual(new Set([
`${base}container/container2/`,
`${base}container/resource`,
`${base}container/symlink`,
`${base}container/symlinkContainer/`,
]));
// Containers
for (const child of children.filter(({ identifier }): boolean => identifier.value.endsWith('/'))) {
const types = child.getAll(RDF.terms.type).map((term): string => term.value);
expect(types).toContain(LDP.Resource);
expect(types).toContain(LDP.Container);
expect(types).toContain(LDP.BasicContainer);
}
// Documents
for (const child of children.filter(({ identifier }): boolean => !identifier.value.endsWith('/'))) {
const types = child.getAll(RDF.terms.type).map((term): string => term.value);
expect(types).toContain(LDP.Resource);
expect(types).toContain('http://www.w3.org/ns/iana/media-types/application/octet-stream#Resource');
expect(types).not.toContain(LDP.Container);
expect(types).not.toContain(LDP.BasicContainer);
}
// All resources
for (const child of children) {
expect(child.get(DC.terms.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.terms.dateTime));
expect(child.get(POSIX.terms.mtime)).toEqualRdfTerm(toLiteral(Math.floor(now.getTime() / 1000),
XSD.terms.integer));
// `dc:modified` is in the default graph
expect(child.quads(null, null, null, SOLID_META.terms.ResponseMetadata))
.toHaveLength(isContainerPath(child.identifier.value) ? 1 : 2);
}
});
it('does not generate IANA URIs for children with invalid content-types.', async(): Promise<void> => {
cache.data = {
container: {
resource1: 'data',
resource2: 'badData',
},
};
const badMapper: jest.Mocked<FileIdentifierMapper> = {
mapFilePathToUrl: jest.fn(async(filePath: string, isContainer: boolean): Promise<ResourceLink> => {
const result = await mapper.mapFilePathToUrl(filePath, isContainer);
if (filePath.endsWith('resource2')) {
result.contentType = 'this is not a valid type';
}
return result;
}),
mapUrlToFilePath: jest.fn((...args): Promise<ResourceLink> => mapper.mapUrlToFilePath(...args)),
};
accessor = new FileDataAccessor(badMapper);
const children = [];
for await (const child of accessor.getChildren({ path: `${base}container/` })) {
children.push(child);
}
// Identifiers
expect(children).toHaveLength(2);
expect(new Set(children.map((child): string => child.identifier.value))).toEqual(new Set([
`${base}container/resource1`,
`${base}container/resource2`,
]));
const types1 = children[0].getAll(RDF.terms.type).map((term): string => term.value);
const types2 = children[1].getAll(RDF.terms.type).map((term): string => term.value);
expect(types1).toContain('http://www.w3.org/ns/iana/media-types/application/octet-stream#Resource');
for (const type of types2) {
expect(type).not.toMatch(/^http:\/\/www\.w3.org\/ns\/iana\/media-types\//u);
}
});
it('adds stored metadata when requesting metadata.', async(): Promise<void> => {
cache.data = { resource: 'data', 'resource.meta': '<http://this> <http://is> <http://metadata>.' };
metadata = await accessor.getMetadata({ path: `${base}resource` });
expect(metadata.quads().some((qd): boolean => qd.subject.value === 'http://this')).toBe(true);
cache.data = { container: { '.meta': '<http://this> <http://is> <http://metadata>.' }};
metadata = await accessor.getMetadata({ path: `${base}container/` });
expect(metadata.quads().some((qd): boolean => qd.subject.value === 'http://this')).toBe(true);
});
it('throws an error if there is a problem with the internal metadata.', async(): Promise<void> => {
cache.data = { resource: 'data', 'resource.meta': 'invalid metadata!.' };
await expect(accessor.getMetadata({ path: `${base}resource` })).rejects.toThrow();
});
});
describe('writing a document', (): void => {
it('throws a 404 if the identifier does not start with the base.', async(): Promise<void> => {
await expect(accessor.writeDocument({ path: 'badpath' }, data, metadata))
.rejects.toThrow(NotFoundHttpError);
});
it('writes the data to the corresponding file.', async(): Promise<void> => {
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
expect(cache.data.resource).toBe('data');
});
it('writes metadata to the corresponding metadata file.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: `${base}res.ttl` },
{ [CONTENT_TYPE]: 'text/turtle', likes: 'apples' });
await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).resolves.toBeUndefined();
expect(cache.data['res.ttl']).toBe('data');
expect(cache.data['res.ttl.meta']).toMatch(`<${base}res.ttl> <likes> "apples".`);
});
it('does not write metadata that is stored by the file system.', async(): Promise<void> => {
metadata.add(RDF.terms.type, LDP.terms.Resource);
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
expect(cache.data.resource).toBe('data');
expect(cache.data['resource.meta']).toBeUndefined();
});
it('deletes existing metadata if nothing new needs to be stored.', async(): Promise<void> => {
cache.data = { resource: 'data', 'resource.meta': 'metadata!' };
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
expect(cache.data.resource).toBe('data');
expect(cache.data['resource.meta']).toBeUndefined();
});
it('errors if there is a problem deleting the old metadata file.', async(): Promise<void> => {
cache.data = { resource: 'data', 'resource.meta': 'metadata!' };
jest.requireMock('fs-extra').remove = (): any => {
throw new Error('error');
};
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
.rejects.toThrow('error');
});
it('throws if something went wrong writing a file.', async(): Promise<void> => {
data.read = (): any => {
data.emit('error', new Error('error'));
return null;
};
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
.rejects.toThrow('error');
});
it('deletes the metadata file if something went wrong writing the file.', async(): Promise<void> => {
data.read = (): any => {
data.emit('error', new Error('error'));
return null;
};
metadata.add(namedNode('likes'), 'apples');
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
.rejects.toThrow('error');
expect(cache.data['resource.meta']).toBeUndefined();
});
it('updates the filename if the content-type gets updated.', async(): Promise<void> => {
cache.data = { 'resource$.ttl': '<this> <is> <data>.', 'resource.meta': '<this> <is> <metadata>.' };
metadata.identifier = DataFactory.namedNode(`${base}resource`);
metadata.contentType = 'text/plain';
metadata.add(namedNode('new'), 'metadata');
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
.resolves.toBeUndefined();
expect(cache.data).toEqual({
'resource$.txt': 'data',
'resource.meta': expect.stringMatching(`<${base}resource> <new> "metadata".`),
});
});
it('does not try to update the content-type if there is no original file.', async(): Promise<void> => {
metadata.identifier = DataFactory.namedNode(`${base}resource.txt`);
metadata.contentType = 'text/turtle';
metadata.add(namedNode('new'), 'metadata');
await expect(accessor.writeDocument({ path: `${base}resource.txt` }, data, metadata))
.resolves.toBeUndefined();
expect(cache.data).toEqual({
'resource.txt$.ttl': 'data',
'resource.txt.meta': expect.stringMatching(`<${base}resource.txt> <new> "metadata".`),
});
});
it('throws an error if there is an issue deleting the original file.', async(): Promise<void> => {
cache.data = { 'resource$.ttl': '<this> <is> <data>.' };
jest.requireMock('fs-extra').remove = (): any => {
const error = new Error('error') as SystemError;
error.code = 'EISDIR';
error.syscall = 'unlink';
throw error;
};
metadata.contentType = 'text/plain';
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
.rejects.toThrow('error');
});
});
describe('writing a container', (): void => {
it('throws a 404 if the identifier does not start with the base.', async(): Promise<void> => {
await expect(accessor.writeContainer({ path: 'badpath' }, metadata)).rejects.toThrow(NotFoundHttpError);
});
it('creates the corresponding directory.', async(): Promise<void> => {
await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined();
expect(cache.data.container).toEqual({});
});
it('can handle the directory already existing.', async(): Promise<void> => {
cache.data.container = {};
await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined();
expect(cache.data.container).toEqual({});
});
it('throws other errors when making a directory.', async(): Promise<void> => {
jest.requireMock('fs-extra').ensureDir = (): any => {
throw new Error('error');
};
await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow('error');
});
it('writes metadata to the corresponding metadata file.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: `${base}container/` }, { likes: 'apples' });
await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined();
expect(cache.data.container).toEqual({ '.meta': expect.stringMatching(`<${base}container/> <likes> "apples".`) });
});
it('overwrites existing metadata.', async(): Promise<void> => {
cache.data.container = { '.meta': `<${base}container/> <likes> "pears".` };
metadata = new RepresentationMetadata({ path: `${base}container/` }, { likes: 'apples' });
await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined();
expect(cache.data.container).toEqual({ '.meta': expect.stringMatching(`<${base}container/> <likes> "apples".`) });
});
it('does not write metadata that is stored by the file system.', async(): Promise<void> => {
metadata = new RepresentationMetadata(
{ path: `${base}container/` },
{ [RDF.type]: [ LDP.terms.BasicContainer, LDP.terms.Resource ]},
);
await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined();
expect(cache.data.container).toEqual({});
});
it('can write to the root container.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: `${base}` }, { likes: 'apples' });
await expect(accessor.writeContainer({ path: `${base}` }, metadata)).resolves.toBeUndefined();
expect(cache.data).toEqual({ '.meta': expect.stringMatching(`<${base}> <likes> "apples".`) });
});
});
describe('writing metadata', (): void => {
it('writes metadata to the metadata resource.', async(): Promise<void> => {
const resourceIdentifier = { path: `${base}resource` };
const inputMetadata = new RepresentationMetadata(resourceIdentifier, { [RDF.type]: LDP.terms.Resource });
await accessor.writeDocument(resourceIdentifier, data, inputMetadata);
const extraMetadata = new RepresentationMetadata(resourceIdentifier);
extraMetadata.addQuad(namedNode('a'), namedNode('b'), namedNode('c'));
await expect(accessor.writeMetadata(resourceIdentifier, extraMetadata)).resolves.toBeUndefined();
const outputMetadata = await accessor.getMetadata(resourceIdentifier);
expect(outputMetadata.quads(`${base}a`))
.toStrictEqual([ quad(namedNode(`${base}a`), namedNode(`${base}b`), namedNode(`${base}c`)) ]);
});
});
describe('deleting a resource', (): void => {
it('throws a 404 if the identifier does not start with the base.', async(): Promise<void> => {
await expect(accessor.deleteResource({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if the identifier does not match an existing entry.', async(): Promise<void> => {
await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if it matches something that is no file or directory.', async(): Promise<void> => {
cache.data = { resource: 5 };
await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError);
});
it('throws a 404 if the trailing slash does not match its type.', async(): Promise<void> => {
cache.data = { resource: 'apple', container: {}};
await expect(accessor.deleteResource({ path: `${base}resource/` })).rejects.toThrow(NotFoundHttpError);
await expect(accessor.deleteResource({ path: `${base}container` })).rejects.toThrow(NotFoundHttpError);
});
it('deletes the corresponding file for document.', async(): Promise<void> => {
cache.data = { resource: 'apple' };
await expect(accessor.deleteResource({ path: `${base}resource` })).resolves.toBeUndefined();
expect(cache.data.resource).toBeUndefined();
});
it('throws error if there is a problem with deleting existing metadata.', async(): Promise<void> => {
cache.data = { resource: 'apple', 'resource.meta': {}};
jest.requireMock('fs-extra').remove = (): any => {
throw new Error('error');
};
await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow();
});
it('removes the corresponding folder for containers.', async(): Promise<void> => {
cache.data = { container: {}};
await expect(accessor.deleteResource({ path: `${base}container/` })).resolves.toBeUndefined();
expect(cache.data.container).toBeUndefined();
});
it('removes the corresponding metadata.', async(): Promise<void> => {
cache.data = { container: { resource: 'apple', 'resource.meta': 'metaApple', '.meta': 'metadata' }};
await expect(accessor.deleteResource({ path: `${base}container/resource` })).resolves.toBeUndefined();
expect(cache.data.container.resource).toBeUndefined();
expect(cache.data.container['resource.meta']).toBeUndefined();
await expect(accessor.deleteResource({ path: `${base}container/` })).resolves.toBeUndefined();
expect(cache.data.container).toBeUndefined();
});
it('can delete the root container.', async(): Promise<void> => {
cache.data = { };
await expect(accessor.deleteResource({ path: `${base}` })).resolves.toBeUndefined();
expect(cache.data).toBeUndefined();
});
});
});