mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00

* feat: implemented SizeReporter and FileSizeReporter * test: FileSizeReporter tests * feat: added QuotedDataAccessor * test: added extra test to check recursiveness of filesizereporter * feat: added QuotaStrategy interface * feat: further progress in different files * feat: wrote doc, tests and improved code * feat: fixed bugs and code is now runnable and buildable * feat: finished implementation * fix: revert accidental chanegs * fix: fileSizeReported did not count container size * fix: bug calculating container sizes fixed * test: FileSizeReporter tests * test: QuotaDataValidator tests * test: QuotaError tests * fix: removed console.log * doc: added doc to several files * doc: changed doc for QuotaStrategy to new implementation * fix: improved content length regex * feat: improved GlobalQuotaStrategy code * fix: made FileSizeReported readonly * feat: added comments to quota-file.json * fix: changed default tempFilePath variable * test: included new tempFilePath variable in testing * chore: created seperate command for start:file:quota to pass tests * feat: removed all sync fs calls from FileSizeReporter * feat: minor changes in multple files * fix: changed function signatures to be in line with others * feat: optimized quota data validation * feat: improved FileSizeReporter code * fix: corrected calculation of containersizes and fixed erroring edgecase * feat: save content-length as number in metadata * feat: added comments and changed GlobalQuotaStrategy constructor * feat: changed file names and added small comment * test: AtomicFileDataAccessor tests * test: completed FileSizeReporter tests * fix: content-length is now saved correctly in RepresentationMetadata * feat: adapted content length metadata + tests * fix: removed tempFilePath variable * fix: reverted .gitignore * fix: forgot to remove tempFilePath variable from componentsjs config * test: GlobalQuotaStrategy tests * feat: replaced DataValidator with Validator * feat: reworked DataValidator * feat: added calcultateChunkSize() to SizeReporter * test: updated FileSizeReporter tests * fix: tempFile location now relative to rootFilePath * test: QuotaDataValidator tests * fix: corrected FileSizeReporter tests * fix: adapted FileSizeReporter tests * fix: FileSizeReporter bug on Windows * fix: regex linting error * feat: changed Validator class * feat: added PodQuotaStrategy to enable suota on a per pod basis * chore: bump context versions * fix: Capitalized comments in json file * chore: renamed ValidatorArgs to ValidatorInput * chore: order all exports * fix: made TODO comment clearer * chore: added seperated config files for global and pod based quota + fixed comments * chore: made minor changes to comments * feat: added PassthroughDataAccessor * feat: added PasstroughtDataAccessor + tests * fix: added invalid header check to ContentLengthParser * chore: improved mocks * chore: move quota limit higher up in config * fix: atomicity issue in AtomicFileDataAccessor * chore: moved .internal folder to config from FileSizeReporter * fix: improved algorithm to ignore folders while calculating file size in FileSizeReporter * fix: changes to support containers in the future * fix: added error handling to prevent reading of unexistent files * feat: added generic type to SizeReporter to calculate chunk sizes * test: use mocked DataAccessor * chore: added some comments to test and made minor improvement * fix: fs mock rename * chore: QuotaStrategy.estimateSize refactor * chore: move trackAvailableSpace to abstract class QuotaStrategy * fix: improved test case * test: quota integration tests * chore: edited some comments * chore: change lstat to stat * feat: moved estimateSize to SizeReporter to be consistent with calcultateChunkSize * test: finish up tests to reach coverage * fix: basic config * fix: minor changes to test CI run * fix: small fix for windows * fix: improved writing to file * chore: linting errors * chore: rename trackAvailableSpace * test: improved integration tests * test: logging info for test debugging * test: extra logging for debugging * test: logging for debugging * test: logging for debugging * test: logging for debugging * test: improved Quota integration test setup * test: improve quota tests for CI run * test: debugging Quota test * test: uncommented global quota test * test: changed global quota parameters * test: logging for debugging * test: logging cleanup * chore: minor changes, mostly typo fixes * chore: remove console.log * fix: getting inconsistent results * chore: try fix index.ts CI error * chore: try fix CI error * chore: try fix CI error * chore: revert last commits * chore: fix inconsistent files with origin * test: minor test improvements * chore: minor refactors and improvements * fix: added extra try catch for breaking bug * chore: improve config * chore: minor code improvements * test: use mockFs * feat: add extra check in podQuotaStrategy * chore: replace handle by handleSafe in ValidatingDataAccessor * chore: typo * test: improved Quota integration tests * test: made comment in test more correct * fix: rm -> rmdir for backwards compatibility * fix: fsPromises issue * chore: leave out irrelevant config * chore: removed start script from package.json * fix: Small fixes Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com>
300 lines
14 KiB
TypeScript
300 lines
14 KiB
TypeScript
import 'jest-rdf';
|
|
import { defaultGraph, literal, namedNode, quad } from '@rdfjs/data-model';
|
|
import type { NamedNode, Quad } from 'rdf-js';
|
|
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
|
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
|
|
|
|
// Helper functions to filter quads
|
|
function getQuads(quads: Quad[], subject?: string, predicate?: string, object?: string, graph?: string): Quad[] {
|
|
return quads.filter((qq): boolean =>
|
|
(!subject || qq.subject.value === subject) &&
|
|
(!predicate || qq.predicate.value === predicate) &&
|
|
(!object || qq.object.value === object) &&
|
|
(!graph || qq.graph.value === graph));
|
|
}
|
|
|
|
function removeQuads(quads: Quad[], subject?: string, predicate?: string, object?: string, graph?: string): Quad[] {
|
|
const filtered = getQuads(quads, subject, predicate, object, graph);
|
|
return quads.filter((qq): boolean => !filtered.includes(qq));
|
|
}
|
|
|
|
describe('A RepresentationMetadata', (): void => {
|
|
let metadata: RepresentationMetadata;
|
|
const identifier = namedNode('http://example.com/id');
|
|
const graphNode = namedNode('http://graph');
|
|
const inputQuads = [
|
|
quad(identifier, namedNode('has'), literal('data')),
|
|
quad(identifier, namedNode('has'), literal('moreData')),
|
|
quad(identifier, namedNode('hasOne'), literal('otherData')),
|
|
quad(identifier, namedNode('has'), literal('data'), graphNode),
|
|
quad(namedNode('otherNode'), namedNode('linksTo'), identifier),
|
|
quad(namedNode('otherNode'), namedNode('has'), literal('otherData')),
|
|
quad(namedNode('otherNode'), namedNode('graphData'), literal('otherData'), graphNode),
|
|
];
|
|
|
|
describe('constructor', (): void => {
|
|
it('creates a blank node if no identifier was given.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata();
|
|
expect(metadata.identifier.termType).toEqual('BlankNode');
|
|
expect(metadata.quads()).toHaveLength(0);
|
|
});
|
|
|
|
it('stores the given identifier if given.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata(namedNode('identifier'));
|
|
expect(metadata.identifier).toEqualRdfTerm(namedNode('identifier'));
|
|
});
|
|
|
|
it('converts identifiers to named nodes.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata({ path: 'identifier' });
|
|
expect(metadata.identifier).toEqualRdfTerm(namedNode('identifier'));
|
|
});
|
|
|
|
it('converts string to content type.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata('text/turtle');
|
|
expect(metadata.contentType).toEqual('text/turtle');
|
|
|
|
metadata = new RepresentationMetadata({ path: 'identifier' }, 'text/turtle');
|
|
expect(metadata.contentType).toEqual('text/turtle');
|
|
|
|
metadata = new RepresentationMetadata(new RepresentationMetadata(), 'text/turtle');
|
|
expect(metadata.contentType).toEqual('text/turtle');
|
|
});
|
|
|
|
it('stores the content-length correctly.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata();
|
|
metadata.contentLength = 50;
|
|
expect(metadata.contentLength).toEqual(50);
|
|
|
|
metadata = new RepresentationMetadata();
|
|
metadata.contentLength = undefined;
|
|
expect(metadata.contentLength).toBeUndefined();
|
|
});
|
|
|
|
it('copies an other metadata object.', async(): Promise<void> => {
|
|
const other = new RepresentationMetadata({ path: 'otherId' }, { 'test:pred': 'objVal' });
|
|
metadata = new RepresentationMetadata(other);
|
|
expect(metadata.identifier).toEqualRdfTerm(namedNode('otherId'));
|
|
expect(metadata.quads()).toBeRdfIsomorphic([
|
|
quad(namedNode('otherId'), namedNode('test:pred'), literal('objVal')) ]);
|
|
});
|
|
|
|
it('takes overrides for specific predicates.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata({ predVal: 'objVal' });
|
|
expect(metadata.get('predVal')).toEqualRdfTerm(literal('objVal'));
|
|
|
|
metadata = new RepresentationMetadata({ predVal: literal('objVal') });
|
|
expect(metadata.get('predVal')).toEqualRdfTerm(literal('objVal'));
|
|
|
|
metadata = new RepresentationMetadata({ predVal: [ 'objVal1', literal('objVal2') ], predVal2: 'objVal3' });
|
|
expect(metadata.getAll('predVal')).toEqualRdfTermArray([ literal('objVal1'), literal('objVal2') ]);
|
|
expect(metadata.get('predVal2')).toEqualRdfTerm(literal('objVal3'));
|
|
});
|
|
|
|
it('can combine overrides with an identifier.', async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata(identifier, { predVal: 'objVal' });
|
|
expect(metadata.quads()).toBeRdfIsomorphic([
|
|
quad(identifier, namedNode('predVal'), literal('objVal')) ]);
|
|
});
|
|
|
|
it('can combine overrides with other metadata.', async(): Promise<void> => {
|
|
const other = new RepresentationMetadata({ path: 'otherId' }, { 'test:pred': 'objVal' });
|
|
metadata = new RepresentationMetadata(other, { 'test:pred': 'objVal2' });
|
|
expect(metadata.quads()).toBeRdfIsomorphic([
|
|
quad(namedNode('otherId'), namedNode('test:pred'), literal('objVal2')) ]);
|
|
});
|
|
});
|
|
|
|
describe('instantiated', (): void => {
|
|
beforeEach(async(): Promise<void> => {
|
|
metadata = new RepresentationMetadata(identifier).addQuads(inputQuads);
|
|
});
|
|
|
|
it('can get all quads.', async(): Promise<void> => {
|
|
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads);
|
|
});
|
|
|
|
it('can query quads.', async(): Promise<void> => {
|
|
expect(metadata.quads(null, namedNode('has'))).toHaveLength(getQuads(inputQuads, undefined, 'has').length);
|
|
expect(metadata.quads(null, null, literal('otherData')))
|
|
.toHaveLength(getQuads(inputQuads, undefined, undefined, 'otherData').length);
|
|
});
|
|
|
|
it('can change the stored identifier.', async(): Promise<void> => {
|
|
const newIdentifier = namedNode('newNode');
|
|
metadata.identifier = newIdentifier;
|
|
const newQuads = inputQuads.map((triple): Quad => {
|
|
if (triple.subject.equals(identifier)) {
|
|
return quad(newIdentifier, triple.predicate, triple.object, triple.graph);
|
|
}
|
|
if (triple.object.equals(identifier)) {
|
|
return quad(triple.subject, triple.predicate, newIdentifier, triple.graph);
|
|
}
|
|
return triple;
|
|
});
|
|
expect(metadata.identifier).toEqualRdfTerm(newIdentifier);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(newQuads);
|
|
});
|
|
|
|
it('can copy metadata.', async(): Promise<void> => {
|
|
const other = new RepresentationMetadata(identifier, { 'test:pred': 'objVal' });
|
|
metadata.setMetadata(other);
|
|
|
|
expect(metadata.identifier).toEqual(other.identifier);
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ ...inputQuads,
|
|
quad(identifier, namedNode('test:pred'), literal('objVal')) ]);
|
|
});
|
|
|
|
it('updates its identifier when copying metadata.', async(): Promise<void> => {
|
|
const other = new RepresentationMetadata({ path: 'otherId' }, { 'test:pred': 'objVal' });
|
|
metadata.setMetadata(other);
|
|
|
|
// `setMetadata` should have the same result as the following
|
|
const expectedMetadata = new RepresentationMetadata(identifier).addQuads(inputQuads);
|
|
expectedMetadata.identifier = namedNode('otherId');
|
|
expectedMetadata.add('test:pred', 'objVal');
|
|
|
|
expect(metadata.identifier).toEqual(other.identifier);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(expectedMetadata.quads());
|
|
});
|
|
|
|
it('can add a quad.', async(): Promise<void> => {
|
|
const newQuad = quad(namedNode('random'), namedNode('new'), literal('triple'));
|
|
metadata.addQuad('random', 'new', 'triple');
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ ...inputQuads, newQuad ]);
|
|
});
|
|
|
|
it('can add a quad with a graph.', async(): Promise<void> => {
|
|
const newQuad = quad(namedNode('random'), namedNode('new'), literal('triple'), namedNode('graph'));
|
|
metadata.addQuad('random', 'new', 'triple', 'graph');
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ ...inputQuads, newQuad ]);
|
|
});
|
|
|
|
it('can add quads.', async(): Promise<void> => {
|
|
const newQuads: Quad[] = [
|
|
quad(namedNode('random'), namedNode('new'), namedNode('triple')),
|
|
];
|
|
metadata.addQuads(newQuads);
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ ...newQuads, ...inputQuads ]);
|
|
});
|
|
|
|
it('can remove a quad.', async(): Promise<void> => {
|
|
const old = inputQuads[0];
|
|
metadata.removeQuad(old.subject as any, old.predicate as any, old.object as any, old.graph as any);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads.slice(1));
|
|
});
|
|
|
|
it('removes all matching triples if graph is undefined.', async(): Promise<void> => {
|
|
metadata.removeQuad(identifier, 'has', 'data');
|
|
expect(metadata.quads()).toHaveLength(inputQuads.length - 2);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(removeQuads(inputQuads, identifier.value, 'has', 'data'));
|
|
});
|
|
|
|
it('can remove quads.', async(): Promise<void> => {
|
|
metadata.removeQuads([ inputQuads[0] ]);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(inputQuads.slice(1));
|
|
});
|
|
|
|
it('can add a single value for a predicate.', async(): Promise<void> => {
|
|
const newQuad = quad(identifier, namedNode('new'), namedNode('triple'));
|
|
metadata.add(newQuad.predicate as NamedNode, newQuad.object as NamedNode);
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ newQuad, ...inputQuads ]);
|
|
});
|
|
|
|
it('can add single values as string.', async(): Promise<void> => {
|
|
const newQuad = quad(identifier, namedNode('new'), literal('triple'));
|
|
metadata.add(newQuad.predicate as NamedNode, newQuad.object.value);
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ newQuad, ...inputQuads ]);
|
|
});
|
|
|
|
it('can add multiple values for a predicate.', async(): Promise<void> => {
|
|
const newQuads = [
|
|
quad(identifier, namedNode('new'), namedNode('triple')),
|
|
quad(identifier, namedNode('new'), namedNode('triple2')),
|
|
];
|
|
metadata.add(namedNode('new'), [ namedNode('triple'), namedNode('triple2') ]);
|
|
expect(metadata.quads()).toBeRdfIsomorphic([ ...newQuads, ...inputQuads ]);
|
|
});
|
|
|
|
it('can remove a single value for a predicate.', async(): Promise<void> => {
|
|
metadata.remove(namedNode('has'), literal('data'));
|
|
expect(metadata.quads()).toBeRdfIsomorphic(removeQuads(inputQuads, identifier.value, 'has', 'data'));
|
|
});
|
|
|
|
it('can remove single values as string.', async(): Promise<void> => {
|
|
metadata.remove(namedNode('has'), 'data');
|
|
expect(metadata.quads()).toBeRdfIsomorphic(removeQuads(inputQuads, identifier.value, 'has', 'data'));
|
|
});
|
|
|
|
it('can remove multiple values for a predicate.', async(): Promise<void> => {
|
|
metadata.remove(namedNode('has'), [ literal('data'), 'moreData' ]);
|
|
let expected = removeQuads(inputQuads, identifier.value, 'has', 'data');
|
|
expected = removeQuads(expected, identifier.value, 'has', 'moreData');
|
|
expect(metadata.quads()).toBeRdfIsomorphic(expected);
|
|
});
|
|
|
|
it('can remove all values for a predicate.', async(): Promise<void> => {
|
|
const pred = namedNode('has');
|
|
metadata.removeAll(pred);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(removeQuads(inputQuads, identifier.value, 'has'));
|
|
});
|
|
|
|
it('can remove all values for a predicate in a specific graph.', async(): Promise<void> => {
|
|
const pred = namedNode('has');
|
|
metadata.removeAll(pred, graphNode);
|
|
expect(metadata.quads()).toBeRdfIsomorphic(
|
|
removeQuads(inputQuads, identifier.value, 'has', undefined, graphNode.value),
|
|
);
|
|
});
|
|
|
|
it('can get all values for a predicate.', async(): Promise<void> => {
|
|
expect(metadata.getAll(namedNode('has'))).toEqualRdfTermArray(
|
|
[ literal('data'), literal('moreData'), literal('data') ],
|
|
);
|
|
});
|
|
|
|
it('can get all values for a predicate in a graph.', async(): Promise<void> => {
|
|
expect(metadata.getAll(namedNode('has'), defaultGraph())).toEqualRdfTermArray(
|
|
[ literal('data'), literal('moreData') ],
|
|
);
|
|
});
|
|
|
|
it('can get the single value for a predicate.', async(): Promise<void> => {
|
|
expect(metadata.get(namedNode('hasOne'))).toEqualRdfTerm(literal('otherData'));
|
|
});
|
|
|
|
it('returns undefined if getting an undefined predicate.', async(): Promise<void> => {
|
|
expect(metadata.get(namedNode('doesntExist'))).toBeUndefined();
|
|
});
|
|
|
|
it('errors if there are multiple values when getting a value.', async(): Promise<void> => {
|
|
expect((): any => metadata.get(namedNode('has'))).toThrow(Error);
|
|
expect((): any => metadata.get('has')).toThrow(Error);
|
|
});
|
|
|
|
it('can set the value of a predicate.', async(): Promise<void> => {
|
|
metadata.set(namedNode('has'), literal('singleValue'));
|
|
expect(metadata.get(namedNode('has'))).toEqualRdfTerm(literal('singleValue'));
|
|
});
|
|
|
|
it('can set multiple values of a predicate.', async(): Promise<void> => {
|
|
metadata.set(namedNode('has'), [ literal('value1'), literal('value2') ]);
|
|
expect(metadata.getAll(namedNode('has'))).toEqualRdfTermArray([ literal('value1'), literal('value2') ]);
|
|
});
|
|
|
|
it('has a shorthand for content-type.', async(): Promise<void> => {
|
|
expect(metadata.contentType).toBeUndefined();
|
|
metadata.contentType = 'a/b';
|
|
expect(metadata.get(CONTENT_TYPE)).toEqualRdfTerm(literal('a/b'));
|
|
expect(metadata.contentType).toEqual('a/b');
|
|
metadata.contentType = undefined;
|
|
expect(metadata.contentType).toBeUndefined();
|
|
});
|
|
|
|
it('errors if a shorthand has multiple values.', async(): Promise<void> => {
|
|
metadata.add(CONTENT_TYPE, 'a/b');
|
|
metadata.add(CONTENT_TYPE, 'c/d');
|
|
expect((): any => metadata.contentType).toThrow();
|
|
});
|
|
});
|
|
});
|