|
| 1 | +import { DataFactory } from 'n3'; |
| 2 | +import type { AuxiliaryStrategy } from '@solid/community-server' |
| 3 | +import { BasicRepresentation } from '@solid/community-server' |
| 4 | +import type { Representation } from '@solid/community-server' |
| 5 | +import type { RepresentationConverter } from '@solid/community-server' |
| 6 | +import type { ResourceStore } from '@solid/community-server' |
| 7 | +import { ShapeValidationStore } from '../../../src/storage/ShapeValidationStore'; |
| 8 | +import type { ShapeValidator } from '../../../src/storage/validators/ShapeValidator'; |
| 9 | +import { INTERNAL_QUADS } from '@solid/community-server' |
| 10 | +import { BadRequestHttpError } from '@solid/community-server' |
| 11 | +import { SingleRootIdentifierStrategy } from '@solid/community-server' |
| 12 | +import { guardedStreamFrom } from '@solid/community-server' |
| 13 | +import { LDP } from '../../../src/util/Vocabularies'; |
| 14 | +import { SimpleSuffixStrategy } from '../../util/SimpleSuffixStrategy'; |
| 15 | +import namedNode = DataFactory.namedNode; |
| 16 | +import quad = DataFactory.quad; |
| 17 | + |
| 18 | +describe('ShapeValidationStore', (): void => { |
| 19 | + let validator: ShapeValidator; |
| 20 | + let metadataStrategy: AuxiliaryStrategy; |
| 21 | + let source: ResourceStore; |
| 22 | + let converter: RepresentationConverter; |
| 23 | + const root = 'http://test.com/'; |
| 24 | + const identifierStrategy = new SingleRootIdentifierStrategy(root); |
| 25 | + const metaSuffix = '.meta'; |
| 26 | + let store: ShapeValidationStore; |
| 27 | + |
| 28 | + beforeEach((): void => { |
| 29 | + source = { |
| 30 | + getRepresentation: jest.fn(async(): Promise<any> => new BasicRepresentation()), |
| 31 | + addResource: jest.fn(async(): Promise<any> => 'add'), |
| 32 | + setRepresentation: jest.fn(async(): Promise<any> => 'set'), |
| 33 | + } as unknown as ResourceStore; |
| 34 | + |
| 35 | + metadataStrategy = new SimpleSuffixStrategy(metaSuffix); |
| 36 | + converter = { |
| 37 | + handleSafe: jest.fn(), |
| 38 | + canHandle: jest.fn(), |
| 39 | + handle: jest.fn(), |
| 40 | + }; |
| 41 | + validator = { |
| 42 | + handleSafe: jest.fn(), |
| 43 | + canHandle: jest.fn(), |
| 44 | + handle: jest.fn(), |
| 45 | + }; |
| 46 | + store = new ShapeValidationStore(source, identifierStrategy, metadataStrategy, converter, validator); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('adding a Resource', (): void => { |
| 50 | + it('calls the validator with the correct arguments.', async(): Promise<void> => { |
| 51 | + const resourceID = { path: root }; |
| 52 | + const representation = new BasicRepresentation(); |
| 53 | + const parentRepresentation = new BasicRepresentation(); |
| 54 | + source.getRepresentation = jest.fn().mockReturnValue(parentRepresentation); |
| 55 | + |
| 56 | + await expect(store.addResource(resourceID, representation)).resolves.toBe('add'); |
| 57 | + expect(validator.handleSafe).toHaveBeenCalledTimes(1); |
| 58 | + expect(validator.handleSafe).toHaveBeenCalledWith({ parentRepresentation, representation }); |
| 59 | + expect(source.getRepresentation).toHaveBeenCalledTimes(1); |
| 60 | + expect(source.getRepresentation).toHaveBeenLastCalledWith(resourceID, {}); |
| 61 | + expect(source.addResource).toHaveBeenCalledTimes(1); |
| 62 | + expect(source.addResource).toHaveBeenLastCalledWith(resourceID, representation, undefined); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('setting a Representation', (): void => { |
| 67 | + let parentRepresentation: Representation; |
| 68 | + let representation: Representation; |
| 69 | + const shapeURL = `${root}shape`; |
| 70 | + const containerURL = `${root}container/`; |
| 71 | + const metadataURL = containerURL + metaSuffix; |
| 72 | + const shapeConstraintQuad = quad(namedNode(containerURL), LDP.terms.constrainedBy, namedNode(shapeURL)); |
| 73 | + const shapeConstraintQuad2 = quad(namedNode(containerURL), LDP.terms.constrainedBy, namedNode(`${shapeURL}1`)); |
| 74 | + let metadataRepresentation: Representation = |
| 75 | + |
| 76 | + beforeEach((): void => { |
| 77 | + representation = new BasicRepresentation(); |
| 78 | + |
| 79 | + parentRepresentation = new BasicRepresentation(); |
| 80 | + source.getRepresentation = jest.fn().mockReturnValue(parentRepresentation); |
| 81 | + |
| 82 | + metadataRepresentation = new BasicRepresentation( |
| 83 | + guardedStreamFrom([ shapeConstraintQuad ]), |
| 84 | + { path: metadataURL }, |
| 85 | + INTERNAL_QUADS, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('calls the source setRepresentation when the resource ID is the root.', async(): Promise<void> => { |
| 90 | + const resourceID = { path: root }; |
| 91 | + |
| 92 | + await expect(store.setRepresentation(resourceID, representation)).resolves.toBe('set'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('calls the validator with the correct arguments.', async(): Promise<void> => { |
| 96 | + const resourceID = { path: `${root}resource.ttl` }; |
| 97 | + |
| 98 | + await expect(store.setRepresentation(resourceID, representation)).resolves.toBe('set'); |
| 99 | + expect(validator.handleSafe).toHaveBeenCalledTimes(1); |
| 100 | + expect(validator.handleSafe).toHaveBeenCalledWith({ parentRepresentation, representation }); |
| 101 | + expect(source.getRepresentation).toHaveBeenCalledTimes(1); |
| 102 | + expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: root }, {}); |
| 103 | + expect(source.setRepresentation).toHaveBeenCalledTimes(1); |
| 104 | + expect(source.setRepresentation).toHaveBeenLastCalledWith(resourceID, representation, undefined); |
| 105 | + }); |
| 106 | + |
| 107 | + it('errors when the validation fails.', async(): Promise<void> => { |
| 108 | + const resourceID = { path: `${root}resource.ttl` }; |
| 109 | + validator.handleSafe = jest.fn().mockRejectedValue(new BadRequestHttpError()); |
| 110 | + |
| 111 | + await expect(store.setRepresentation(resourceID, representation)).rejects.toThrow(BadRequestHttpError); |
| 112 | + expect(validator.handleSafe).toHaveBeenCalledTimes(1); |
| 113 | + expect(validator.handleSafe).toHaveBeenCalledWith({ parentRepresentation, representation }); |
| 114 | + expect(source.getRepresentation).toHaveBeenCalledTimes(1); |
| 115 | + expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: root }, {}); |
| 116 | + }); |
| 117 | + |
| 118 | + it('allows adding a shape constraint when none is currently present.', async(): Promise<void> => { |
| 119 | + const resourceID = { path: containerURL + metaSuffix }; |
| 120 | + |
| 121 | + converter.handleSafe = jest.fn().mockReturnValueOnce(metadataRepresentation).mockReturnValueOnce( |
| 122 | + new BasicRepresentation(guardedStreamFrom([ ]), resourceID, INTERNAL_QUADS), |
| 123 | + ); |
| 124 | + |
| 125 | + await expect(store.setRepresentation(resourceID, representation)).resolves.toBe('set'); |
| 126 | + expect(validator.handleSafe).toHaveBeenCalledTimes(1); |
| 127 | + expect(validator.handleSafe).toHaveBeenCalledWith({ parentRepresentation, representation }); |
| 128 | + expect(source.getRepresentation).toHaveBeenCalledTimes(2); |
| 129 | + expect(source.getRepresentation).toHaveBeenNthCalledWith(1, resourceID, {}); |
| 130 | + expect(source.setRepresentation).toHaveBeenCalledTimes(1); |
| 131 | + expect(source.setRepresentation).toHaveBeenLastCalledWith(resourceID, representation, undefined); |
| 132 | + }); |
| 133 | + |
| 134 | + it('errors when adding multiple shape constraints.', async(): Promise<void> => { |
| 135 | + const resourceID = { path: containerURL + metaSuffix }; |
| 136 | + |
| 137 | + converter.handleSafe = jest.fn().mockReturnValueOnce( |
| 138 | + new BasicRepresentation(guardedStreamFrom([ |
| 139 | + shapeConstraintQuad, shapeConstraintQuad2, |
| 140 | + ]), resourceID, INTERNAL_QUADS), |
| 141 | + ).mockReturnValueOnce( |
| 142 | + new BasicRepresentation(guardedStreamFrom([ |
| 143 | + ]), resourceID, INTERNAL_QUADS), |
| 144 | + ); |
| 145 | + |
| 146 | + await expect(store.setRepresentation(resourceID, representation)).rejects.toThrow( |
| 147 | + new BadRequestHttpError('A container can only be constrained by at most one shape resource.'), |
| 148 | + ); |
| 149 | + expect(source.getRepresentation).toHaveBeenCalledTimes(1); |
| 150 | + expect(source.getRepresentation).toHaveBeenCalledWith(resourceID, {}); |
| 151 | + }); |
| 152 | + |
| 153 | + it('errors when adding a shape constraint when resources are already present in the container.', |
| 154 | + async(): Promise<void> => { |
| 155 | + const resourceID = { path: containerURL + metaSuffix }; |
| 156 | + converter.handleSafe = jest.fn().mockReturnValueOnce( |
| 157 | + new BasicRepresentation(guardedStreamFrom([ |
| 158 | + shapeConstraintQuad, |
| 159 | + quad(namedNode(containerURL), LDP.terms.contains, namedNode(`${containerURL}resource`)), |
| 160 | + ]), resourceID, INTERNAL_QUADS), |
| 161 | + ).mockReturnValueOnce( |
| 162 | + new BasicRepresentation(guardedStreamFrom([ |
| 163 | + ]), resourceID, INTERNAL_QUADS), |
| 164 | + ); |
| 165 | + await expect(store.setRepresentation(resourceID, representation)).rejects.toThrow(new BadRequestHttpError( |
| 166 | + 'A container can only be constrained when there are no resources present in that container.', |
| 167 | + )); |
| 168 | + }); |
| 169 | + |
| 170 | + it('allows adding the same shape constraint that was already present and some resources are present.', |
| 171 | + async(): Promise<void> => { |
| 172 | + const resourceID = { path: containerURL + metaSuffix }; |
| 173 | + |
| 174 | + converter.handleSafe = jest.fn().mockReturnValueOnce( |
| 175 | + new BasicRepresentation(guardedStreamFrom([ |
| 176 | + shapeConstraintQuad, |
| 177 | + quad(namedNode(containerURL), LDP.terms.contains, namedNode(`${containerURL}resource`)), |
| 178 | + ]), resourceID, INTERNAL_QUADS), |
| 179 | + ).mockReturnValueOnce(metadataRepresentation); |
| 180 | + |
| 181 | + await expect(store.setRepresentation(resourceID, representation)).resolves.toBe('set'); |
| 182 | + expect(validator.handleSafe).toHaveBeenCalledTimes(1); |
| 183 | + expect(validator.handleSafe).toHaveBeenCalledWith({ parentRepresentation, representation }); |
| 184 | + expect(source.getRepresentation).toHaveBeenCalledTimes(2); |
| 185 | + expect(source.getRepresentation).toHaveBeenNthCalledWith(1, resourceID, {}); |
| 186 | + expect(source.setRepresentation).toHaveBeenCalledTimes(1); |
| 187 | + expect(source.setRepresentation).toHaveBeenLastCalledWith(resourceID, representation, undefined); |
| 188 | + }); |
| 189 | + |
| 190 | + it('errors when changing the shape constraint when some resources are present.', async(): Promise<void> => { |
| 191 | + const resourceID = { path: containerURL + metaSuffix }; |
| 192 | + |
| 193 | + converter.handleSafe = jest.fn().mockReturnValueOnce( |
| 194 | + new BasicRepresentation(guardedStreamFrom([ |
| 195 | + shapeConstraintQuad2, |
| 196 | + quad(namedNode(containerURL), LDP.terms.contains, namedNode(`${containerURL}resource`)), |
| 197 | + ]), resourceID, INTERNAL_QUADS), |
| 198 | + ).mockReturnValueOnce(metadataRepresentation); |
| 199 | + await expect(store.setRepresentation(resourceID, representation)).rejects.toThrow(new BadRequestHttpError( |
| 200 | + 'A container can only be constrained when there are no resources present in that container.', |
| 201 | + )); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments