feat: Have ExtensionBasedMapper handle extensions correctly

This commit is contained in:
Joachim Van Herwegen
2020-10-02 17:15:52 +02:00
parent 0644f8d245
commit b47dc3f7f6
5 changed files with 387 additions and 119 deletions

View File

@@ -1,20 +1,157 @@
import fs from 'fs';
import { ExtensionBasedMapper } from '../../../src/storage/ExtensionBasedMapper';
import { ConflictHttpError } from '../../../src/util/errors/ConflictHttpError';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError';
import { trimTrailingSlashes } from '../../../src/util/Util';
jest.mock('fs');
describe('An ExtensionBasedMapper', (): void => {
const base = 'http://test.com/';
const rootFilepath = 'uploads/';
const resourceMapper = new ExtensionBasedMapper(base, rootFilepath);
const mapper = new ExtensionBasedMapper(base, rootFilepath);
let fsPromises: { [ id: string ]: jest.Mock };
it('returns the correct url of a file.', async(): Promise<void> => {
let result = resourceMapper.mapFilePathToUrl(`${rootFilepath}test.txt`);
expect(result).toEqual(`${base}test.txt`);
result = resourceMapper.mapFilePathToUrl(`${rootFilepath}image.jpg`);
expect(result).toEqual(`${base}image.jpg`);
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
fs.promises = {
readdir: jest.fn(),
} as any;
fsPromises = fs.promises as any;
});
it('errors when filepath does not contain rootFilepath.', async(): Promise<void> => {
expect((): string => resourceMapper.mapFilePathToUrl('random/test.txt')).toThrow(Error);
expect((): string => resourceMapper.mapFilePathToUrl('test.txt')).toThrow(Error);
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);
});
it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }))
.rejects.toThrow(new UnsupportedHttpError('URL needs a / after the base.'));
});
it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test/../test2` }))
.rejects.toThrow(new UnsupportedHttpError('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({
identifier: { path: `${base}container/` },
filePath: `${rootFilepath}container/`,
});
});
it('rejects URLs that end with "$.{extension}".', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test$.txt` }))
.rejects.toThrow(new UnsupportedHttpError('Identifiers cannot contain a dollar sign before their extension.'));
});
it('throws 404 when looking in a folder that does not exist.', async(): Promise<void> => {
fsPromises.readdir.mockImplementation((): void => {
throw new Error('does not exist');
});
await expect(mapper.mapUrlToFilePath({ path: `${base}no/test.txt` })).rejects.toThrow(NotFoundHttpError);
});
it('throws 404 when looking for a file that does not exist.', async(): Promise<void> => {
fsPromises.readdir.mockReturnValue([ 'test.ttl' ]);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).rejects.toThrow(NotFoundHttpError);
});
it('determines the content-type based on the extension.', async(): Promise<void> => {
fsPromises.readdir.mockReturnValue([ 'test.txt' ]);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'text/plain',
});
});
it('matches even if the content-type does not match the extension.', async(): Promise<void> => {
fsPromises.readdir.mockReturnValue([ 'test.txt$.ttl' ]);
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` })).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt$.ttl`,
contentType: 'text/turtle',
});
});
it('generates a file path if the content-type was provided.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'text/plain')).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'text/plain',
});
});
it('adds an extension if the given extension does not match the given content-type.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'text/turtle')).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt$.ttl`,
contentType: 'text/turtle',
});
});
it('throws 400 if the given content-type is not recognized.', async(): Promise<void> => {
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'fake/data'))
.rejects.toThrow(new UnsupportedHttpError(`Unsupported content-type fake/data.`));
});
});
describe('mapFilePathToUrl', (): void => {
it('throws an error if the input path does not contain the root file path.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl('invalid', true)).rejects.toThrow(Error);
});
it('returns a generated identifier for directories.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}container/`, true)).resolves.toEqual({
identifier: { path: `${base}container/` },
filePath: `${rootFilepath}container/`,
});
});
it('returns a generated identifier for files with corresponding content-type.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt`, false)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt`,
contentType: 'text/plain',
});
});
it('removes appended extensions.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test.txt$.ttl`, false)).resolves.toEqual({
identifier: { path: `${base}test.txt` },
filePath: `${rootFilepath}test.txt$.ttl`,
contentType: 'text/turtle',
});
});
it('sets the content-type to application/octet-stream if there is no extension.', async(): Promise<void> => {
await expect(mapper.mapFilePathToUrl(`${rootFilepath}test`, false)).resolves.toEqual({
identifier: { path: `${base}test` },
filePath: `${rootFilepath}test`,
contentType: 'application/octet-stream',
});
});
});
describe('extractDocumentName', (): void => {
it('throws an error if the input corresponds to root.', async(): Promise<void> => {
expect((): any => mapper.extractDocumentName({ path: base })).toThrow(ConflictHttpError);
expect((): any => mapper.extractDocumentName({ path: trimTrailingSlashes(base) }))
.toThrow(ConflictHttpError);
});
it('parses the identifier into container file path and document name.', async(): Promise<void> => {
expect(mapper.extractDocumentName({ path: `${base}test` })).toEqual({
containerPath: rootFilepath,
documentName: 'test',
});
expect(mapper.extractDocumentName({ path: `${base}test/` })).toEqual({
containerPath: `${rootFilepath}test/`,
});
});
});
});

View File

@@ -47,6 +47,14 @@ describe('A FileResourceStore', (): void => {
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
fs.promises = {
rmdir: jest.fn(),
lstat: jest.fn(),
readdir: jest.fn(),
mkdir: jest.fn(),
unlink: jest.fn(),
access: jest.fn(),
} as any;
store = new FileResourceStore(
new ExtensionBasedMapper(base, rootFilepath),
@@ -93,18 +101,18 @@ describe('A FileResourceStore', (): void => {
});
it('errors if a resource was not found.', async(): Promise<void> => {
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
(fsPromises.lstat as jest.Mock).mockImplementation((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.readdir as jest.Mock).mockImplementation((): any => []);
await expect(store.getRepresentation({ path: 'http://wrong.com/wrong' })).rejects.toThrow(NotFoundHttpError);
await expect(store.getRepresentation({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError);
await expect(store.getRepresentation({ path: `${base}wrong/` })).rejects.toThrow(NotFoundHttpError);
await expect(store.addResource({ path: 'http://wrong.com/wrong' }, representation))
.rejects.toThrow(NotFoundHttpError);
await expect(store.deleteResource({ path: 'wrong' })).rejects.toThrow(NotFoundHttpError);
await expect(store.deleteResource({ path: `${base}wrong` })).rejects.toThrow(NotFoundHttpError);
await expect(store.deleteResource({ path: `${base}wrong/` })).rejects.toThrow(NotFoundHttpError);
await expect(store.setRepresentation({ path: 'http://wrong.com/' }, representation))
.rejects.toThrow(NotFoundHttpError);
});
@@ -153,6 +161,7 @@ describe('A FileResourceStore', (): void => {
it('errors for container creation with path to non container.', async(): Promise<void> => {
// Mock the fs functions.
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([ 'foo' ]);
// Tests
representation.metadata.add(RDF.type, LDP.BasicContainer);
@@ -161,33 +170,39 @@ describe('A FileResourceStore', (): void => {
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
});
it('errors 405 for POST invalid path ending without slash.', async(): Promise<void> => {
it('errors 404 for POST invalid path ending without slash and 405 for valid.', async(): Promise<void> => {
// Mock the fs functions.
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([]);
// Tests
representation.metadata.add(RDF.type, LDP.BasicContainer);
representation.metadata.add(HTTP.slug, 'myContainer/');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
.rejects.toThrow(NotFoundHttpError);
expect(fsPromises.readdir as jest.Mock).toHaveBeenLastCalledWith(rootFilepath);
representation.metadata.set(RDF.type, LDP.Resource);
representation.metadata.set(HTTP.slug, 'file.txt');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
.rejects.toThrow(NotFoundHttpError);
expect(fsPromises.readdir as jest.Mock).toHaveBeenLastCalledWith(rootFilepath);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([ 'existingresource' ]);
representation.metadata.removeAll(RDF.type);
await expect(store.addResource({ path: `${base}existingresource` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource'));
expect(fsPromises.lstat as jest.Mock).toHaveBeenLastCalledWith(joinPath(rootFilepath, 'existingresource'));
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.mkdir as jest.Mock).mockImplementation((): void => {
throw new Error('not a directory');
});
representation.metadata.removeAll(RDF.type);
await expect(store.addResource({ path: `${base}existingresource/container/` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock)
.toHaveBeenLastCalledWith(joinPath(rootFilepath, 'existingresource'));
});
it('can set data.', async(): Promise<void> => {
@@ -203,6 +218,7 @@ describe('A FileResourceStore', (): void => {
// Mock: Get
stats = { ...stats };
stats.isFile = jest.fn((): any => true);
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'file.txt' ]);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([]));
@@ -228,6 +244,7 @@ describe('A FileResourceStore', (): void => {
it('can delete data.', async(): Promise<void> => {
// Mock the fs functions.
// Delete
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'file.txt' ]);
stats.isFile = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.unlink as jest.Mock).mockReturnValueOnce(true);
@@ -236,6 +253,7 @@ describe('A FileResourceStore', (): void => {
});
// Mock: Get
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ ]);
(fsPromises.lstat as jest.Mock).mockImplementationOnce((): any => {
throw new Error('Path does not exist.');
});
@@ -325,6 +343,7 @@ describe('A FileResourceStore', (): void => {
it('errors 404 when accessing non resource (file/directory), e.g. special files.', async(): Promise<void> => {
// Mock the fs functions.
(fsPromises.lstat as jest.Mock).mockReturnValue(stats);
(fsPromises.readdir as jest.Mock).mockReturnValue([ '14' ]);
// Tests
await expect(store.deleteResource({ path: `${base}dev/pts/14` })).rejects.toThrow(NotFoundHttpError);
@@ -476,6 +495,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fs.createReadStream as jest.Mock).mockReturnValueOnce(streamifyArray([ rawData ]));
(fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ '.htaccess' ]);
const result = await store.getRepresentation({ path: `${base}.htaccess` });
expect(result).toEqual({
@@ -516,6 +536,7 @@ describe('A FileResourceStore', (): void => {
stats.isDirectory = jest.fn((): any => true);
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
(fsPromises.mkdir as jest.Mock).mockReturnValue(true);
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ 'foo' ]);
// Tests
representation.metadata.add(RDF.type, LDP.BasicContainer);
@@ -545,6 +566,8 @@ describe('A FileResourceStore', (): void => {
const filePath: string = (fs.createWriteStream as jest.Mock).mock.calls[0][0];
expect(filePath.startsWith(rootFilepath)).toBeTruthy();
const name = filePath.slice(rootFilepath.length);
(fsPromises.readdir as jest.Mock).mockReturnValueOnce([ name ]);
const result = await store.getRepresentation({ path: `${base}${name}` });
expect(result).toEqual({
binary: true,