feat: Move all metadata file knowledge to file mapper

This commit is contained in:
Joachim Van Herwegen
2021-07-20 15:58:45 +02:00
parent 6495d650c2
commit 57da67f9ee
11 changed files with 215 additions and 114 deletions

View File

@@ -1,5 +1,6 @@
import { BaseFileIdentifierMapper } from '../../../../src/storage/mapping/BaseFileIdentifierMapper';
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
import { trimTrailingSlashes } from '../../../../src/util/PathUtil';
@@ -12,51 +13,71 @@ describe('An BaseFileIdentifierMapper', (): void => {
describe('mapUrlToFilePath', (): void => {
it('throws 404 if the input path does not contain the base.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: 'invalid' })).rejects.toThrow(NotFoundHttpError);
await expect(mapper.mapUrlToFilePath({ path: 'invalid' }, false)).rejects.toThrow(NotFoundHttpError);
});
it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
const result = mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` });
const result = mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }, false);
await expect(result).rejects.toThrow(BadRequestHttpError);
await expect(result).rejects.toThrow('URL needs a / after the base');
});
it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
const result = mapper.mapUrlToFilePath({ path: `${base}test/../test2` });
const result = mapper.mapUrlToFilePath({ path: `${base}test/../test2` }, false);
await expect(result).rejects.toThrow(BadRequestHttpError);
await expect(result).rejects.toThrow('Disallowed /.. segment in URL');
});
it('returns the corresponding file path for container identifiers.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}container/` })).resolves.toEqual({
await expect(mapper.mapUrlToFilePath({ path: `${base}container/` }, false)).resolves.toEqual({
identifier: { path: `${base}container/` },
filePath: `${rootFilepath}container/`,
isMetadata: false,
});
});
it('returns the default content-type.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test` })).resolves.toEqual({
await expect(mapper.mapUrlToFilePath({ path: `${base}test` }, false)).resolves.toEqual({
identifier: { path: `${base}test` },
filePath: `${rootFilepath}test`,
contentType: 'application/octet-stream',
isMetadata: false,
});
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` })).resolves.toEqual({
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` }, false)).resolves.toEqual({
identifier: { path: `${base}test.ttl` },
filePath: `${rootFilepath}test.ttl`,
contentType: 'application/octet-stream',
isMetadata: false,
});
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, false)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'application/octet-stream',
isMetadata: false,
});
});
it('generates a file path if supported content-type was provided.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` }, 'text/turtle')).resolves.toEqual({
await expect(mapper.mapUrlToFilePath({ path: `${base}test.ttl` }, false, 'text/turtle')).resolves.toEqual({
identifier: { path: `${base}test.ttl` },
filePath: `${rootFilepath}test.ttl`,
contentType: 'text/turtle',
isMetadata: false,
});
});
it('errors on metadata identifiers.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.meta` }, true)).rejects.toThrow(ConflictHttpError);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.meta` }, true))
.rejects.toThrow('Not allowed to create files with the metadata extension.');
});
it('generates correct metadata file paths.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, true)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt.meta`,
contentType: 'application/octet-stream',
isMetadata: true,
});
});
});
@@ -70,6 +91,7 @@ describe('An BaseFileIdentifierMapper', (): void => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}container/`, true)).resolves.toEqual({
identifier: { path: `${base}container/` },
filePath: `${rootFilepath}container/`,
isMetadata: false,
});
});
@@ -78,16 +100,28 @@ describe('An BaseFileIdentifierMapper', (): void => {
identifier: { path: `${base}test` },
filePath: `${rootFilepath}test`,
contentType: 'application/octet-stream',
isMetadata: false,
});
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.ttl`, false)).resolves.toEqual({
identifier: { path: `${base}test.ttl` },
filePath: `${rootFilepath}test.ttl`,
contentType: 'application/octet-stream',
isMetadata: false,
});
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'application/octet-stream',
isMetadata: false,
});
});
it('identifies metadata files.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.meta`, false)).resolves.toEqual({
identifier: { path: `${base}test` },
filePath: `${rootFilepath}test.meta`,
contentType: 'application/octet-stream',
isMetadata: true,
});
});
});