import { DataFactory } from 'n3'; import streamifyArray from 'streamify-array'; import type { Representation } from '../../../../src/ldp/representation/Representation'; import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata'; import { FileDataAccessor } from '../../../../src/storage/accessors/FileDataAccessor'; import { ExtensionBasedMapper } from '../../../../src/storage/ExtensionBasedMapper'; import { APPLICATION_OCTET_STREAM } from '../../../../src/util/ContentTypes'; import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError'; import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError'; import type { SystemError } from '../../../../src/util/errors/SystemError'; import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; import { MetadataController } from '../../../../src/util/MetadataController'; import { CONTENT_TYPE, DCTERMS, LDP, POSIX, RDF, XSD } from '../../../../src/util/UriConstants'; import { toNamedNode, toTypedLiteral } from '../../../../src/util/UriUtil'; import { readableToString } from '../../../../src/util/Util'; import { mockFs } from '../../../util/Util'; jest.mock('fs'); const rootFilePath = 'uploads'; const now = new Date(); describe('A FileDataAccessor', (): void => { const base = 'http://test.com/'; let accessor: FileDataAccessor; let cache: { data: any }; let metadata: RepresentationMetadata; beforeEach(async(): Promise => { cache = mockFs(rootFilePath, now); accessor = new FileDataAccessor( new ExtensionBasedMapper(base, rootFilePath), new MetadataController(), ); metadata = new RepresentationMetadata({ [CONTENT_TYPE]: APPLICATION_OCTET_STREAM }); }); it('can only handle binary data.', async(): Promise => { await expect(accessor.canHandle({ binary: true } as Representation)).resolves.toBeUndefined(); await expect(accessor.canHandle({ binary: false } as Representation)).rejects .toThrow(new UnsupportedMediaTypeHttpError('Only binary data is supported.')); }); describe('getting data', (): void => { it('throws a 404 if the identifier does not start with the base.', async(): Promise => { await expect(accessor.getData({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError); }); it('throws a 404 if the identifier does not match an existing file.', async(): Promise => { await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); }); it('throws a 404 if the identifier matches a directory.', async(): Promise => { cache.data = { resource: {}}; await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); }); it('returns the corresponding data.', async(): Promise => { cache.data = { resource: 'data' }; const stream = await accessor.getData({ path: `${base}resource` }); await expect(readableToString(stream)).resolves.toBe('data'); }); }); describe('getting metadata', (): void => { it('throws a 404 if the identifier does not start with the base.', async(): Promise => { await expect(accessor.getMetadata({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError); }); it('throws a 404 if the identifier does not match an existing file.', async(): Promise => { 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 => { 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 => { jest.requireMock('fs').promises.lstat = (): any => { throw new Error('error'); }; await expect(accessor.getMetadata({ path: base })).rejects.toThrow(new Error('error')); }); it('throws a 404 if the trailing slash does not match its type.', async(): Promise => { 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 => { 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.type)?.value).toBe(LDP.Resource); expect(metadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral('data'.length, XSD.integer)); expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime)); expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer)); }); it('generates the metadata for a container and its non-meta children.', async(): Promise => { 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.type)).toEqualRdfTermArray( [ toNamedNode(LDP.Container), toNamedNode(LDP.BasicContainer), toNamedNode(LDP.Resource) ], ); expect(metadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral(0, XSD.integer)); expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime)); expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer)); expect(metadata.getAll(LDP.contains)).toEqualRdfTermArray( [ toNamedNode(`${base}container/resource`), toNamedNode(`${base}container/container2/`) ], ); const childQuads = metadata.quads().filter((quad): boolean => quad.subject.value === `${base}container/resource`); const childMetadata = new RepresentationMetadata(`${base}container/resource`).addQuads(childQuads); expect(childMetadata.get(RDF.type)?.value).toBe(LDP.Resource); expect(childMetadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral('data'.length, XSD.integer)); expect(childMetadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime)); expect(childMetadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer)); }); it('adds stored metadata when requesting metadata.', async(): Promise => { cache.data = { resource: 'data', 'resource.meta': ' .' }; metadata = await accessor.getMetadata({ path: `${base}resource` }); expect(metadata.quads().some((quad): boolean => quad.subject.value === 'this')).toBe(true); cache.data = { container: { '.meta': ' .' }}; metadata = await accessor.getMetadata({ path: `${base}container/` }); expect(metadata.quads().some((quad): boolean => quad.subject.value === 'this')).toBe(true); }); it('throws an error if there is a problem with the internal metadata.', async(): Promise => { 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 => { await expect(accessor.writeDocument({ path: 'badpath' }, streamifyArray([]), metadata)) .rejects.toThrow(NotFoundHttpError); }); it('throws an error when writing to a metadata path.', async(): Promise => { await expect(accessor.writeDocument({ path: `${base}resource.meta` }, streamifyArray([]), metadata)) .rejects.toThrow(new ConflictHttpError('Not allowed to create files with the metadata extension.')); }); it('writes the data to the corresponding file.', async(): Promise => { const data = streamifyArray([ 'data' ]); 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 => { const data = streamifyArray([ 'data' ]); metadata = new RepresentationMetadata(`${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> "apples".`); }); it('does not write metadata that is stored by the file system.', async(): Promise => { const data = streamifyArray([ 'data' ]); metadata.add(RDF.type, toNamedNode(LDP.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 => { cache.data = { resource: 'data', 'resource.meta': 'metadata!' }; const data = streamifyArray([ 'data' ]); 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 => { cache.data = { resource: 'data', 'resource.meta': 'metadata!' }; jest.requireMock('fs').promises.unlink = (): any => { throw new Error('error'); }; const data = streamifyArray([ 'data' ]); await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)) .rejects.toThrow(new Error('error')); }); it('throws if something went wrong writing a file.', async(): Promise => { const data = streamifyArray([ 'data' ]); data.read = (): any => { data.emit('error', new Error('error')); return null; }; await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)) .rejects.toThrow(new Error('error')); }); it('deletes the metadata file if something went wrong writing the file.', async(): Promise => { const data = streamifyArray([ 'data' ]); data.read = (): any => { data.emit('error', new Error('error')); return null; }; metadata.add('likes', 'apples'); await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)) .rejects.toThrow(new Error('error')); expect(cache.data['resource.meta']).toBeUndefined(); }); it('updates the filename if the content-type gets updated.', async(): Promise => { cache.data = { 'resource$.ttl': ' .', 'resource.meta': ' .' }; metadata.identifier = DataFactory.namedNode(`${base}resource`); metadata.contentType = 'text/plain'; metadata.add('new', 'metadata'); await expect(accessor.writeDocument({ path: `${base}resource` }, streamifyArray([ 'text' ]), metadata)) .resolves.toBeUndefined(); expect(cache.data).toEqual({ 'resource$.txt': 'text', 'resource.meta': expect.stringMatching(`<${base}resource> "metadata".`), }); }); it('throws an error if there is an issue deleting the original file.', async(): Promise => { cache.data = { 'resource$.ttl': ' .' }; jest.requireMock('fs').promises.unlink = (): any => { const error = new Error('error') as SystemError; error.code = 'ENOENT'; error.syscall = 'unlink'; throw error; }; // `unlink` throwing ENOENT should not be an issue if the content-type does not change metadata.contentType = 'text/turtle'; await expect(accessor.writeDocument({ path: `${base}resource` }, streamifyArray([ 'text' ]), metadata)) .resolves.toBeUndefined(); metadata.contentType = 'text/plain'; await expect(accessor.writeDocument({ path: `${base}resource` }, streamifyArray([ 'text' ]), metadata)) .rejects.toThrow(new Error('error')); }); }); describe('writing a container', (): void => { it('throws a 404 if the identifier does not start with the base.', async(): Promise => { await expect(accessor.writeContainer({ path: 'badpath' }, metadata)).rejects.toThrow(NotFoundHttpError); }); it('creates the corresponding directory.', async(): Promise => { await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); expect(cache.data.container).toEqual({}); }); it('can handle the directory already existing.', async(): Promise => { 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 => { jest.requireMock('fs').promises.mkdir = (): any => { throw new Error('error'); }; await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow(new Error('error')); }); it('writes metadata to the corresponding metadata file.', async(): Promise => { metadata = new RepresentationMetadata(`${base}container/`, { likes: 'apples' }); await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); expect(cache.data.container).toEqual({ '.meta': expect.stringMatching(`<${base}container/> "apples".`) }); }); it('overwrites existing metadata.', async(): Promise => { cache.data.container = { '.meta': `<${base}container/> "pears".` }; metadata = new RepresentationMetadata(`${base}container/`, { likes: 'apples' }); await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); expect(cache.data.container).toEqual({ '.meta': expect.stringMatching(`<${base}container/> "apples".`) }); }); it('does not write metadata that is stored by the file system.', async(): Promise => { metadata = new RepresentationMetadata( `${base}container/`, { [RDF.type]: [ toNamedNode(LDP.BasicContainer), toNamedNode(LDP.Resource) ]}, ); await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); expect(cache.data.container).toEqual({}); }); }); describe('deleting a resource', (): void => { it('throws a 404 if the identifier does not start with the base.', async(): Promise => { await expect(accessor.deleteResource({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError); }); it('throws a 404 if the identifier does not match an existing entry.', async(): Promise => { 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 => { 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 => { 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 => { 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 => { cache.data = { resource: 'apple', 'resource.meta': {}}; await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(); }); it('removes the corresponding folder for containers.', async(): Promise => { cache.data = { container: {}}; await expect(accessor.deleteResource({ path: `${base}container/` })).resolves.toBeUndefined(); expect(cache.data.container).toBeUndefined(); }); it('removes the corresponding metadata.', async(): Promise => { 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(); }); }); });