feat: Added resourceExists method to ResourceStore

* feat: added resourceExists method to ResourceStore

* Merge remote-tracking branch 'origin/main' into feat/add-resourceExists-method-to-ResourceStore

* fix: adapted to review

* fix: adapted to review
This commit is contained in:
Arne Vandoorslaer
2021-02-25 13:43:58 +01:00
committed by GitHub
parent e5b7d99da4
commit b3f292d718
24 changed files with 136 additions and 67 deletions

View File

@@ -3,7 +3,6 @@ import { AclInitializer } from '../../../src/init/AclInitializer';
import type { AuxiliaryIdentifierStrategy } from '../../../src/ldp/auxiliary/AuxiliaryIdentifierStrategy';
import { BasicRepresentation } from '../../../src/ldp/representation/BasicRepresentation';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
import { joinFilePath } from '../../../src/util/PathUtil';
const createReadStream = jest.spyOn(fs, 'createReadStream').mockReturnValue('file contents' as any);
@@ -15,8 +14,8 @@ const RepresentationMock: jest.Mock<BasicRepresentation> = BasicRepresentation a
describe('AclInitializer', (): void => {
const store: jest.Mocked<ResourceStore> = {
getRepresentation: jest.fn().mockRejectedValue(new NotFoundHttpError()),
setRepresentation: jest.fn(),
resourceExists: jest.fn().mockImplementation((): any => false),
} as any;
const aclIdentifier = { path: 'http://test.com/.acl' };
const aclStrategy: jest.Mocked<AuxiliaryIdentifierStrategy> = {
@@ -33,8 +32,8 @@ describe('AclInitializer', (): void => {
await initializer.handle();
expect(aclStrategy.getAuxiliaryIdentifier).toHaveBeenCalledWith({ path: baseUrl });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith(aclIdentifier, {});
expect(store.resourceExists).toHaveBeenCalledTimes(1);
expect(store.resourceExists).toHaveBeenCalledWith(aclIdentifier);
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
expect(store.setRepresentation).toHaveBeenCalledWith(
{ path: 'http://test.com/.acl' }, RepresentationMock.mock.instances[0],
@@ -49,8 +48,8 @@ describe('AclInitializer', (): void => {
await initializer.handle();
expect(aclStrategy.getAuxiliaryIdentifier).toHaveBeenCalledWith({ path: baseUrl });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith(aclIdentifier, {});
expect(store.resourceExists).toHaveBeenCalledTimes(1);
expect(store.resourceExists).toHaveBeenCalledWith(aclIdentifier);
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
expect(store.setRepresentation).toHaveBeenCalledWith(
{ path: 'http://test.com/.acl' }, RepresentationMock.mock.instances[0],
@@ -61,21 +60,19 @@ describe('AclInitializer', (): void => {
});
it('does not invoke ACL initialization when a root ACL already exists.', async(): Promise<void> => {
store.getRepresentation.mockReturnValueOnce(Promise.resolve({
data: { destroy: jest.fn() },
} as any));
store.resourceExists.mockResolvedValueOnce(true);
const initializer = new AclInitializer({ baseUrl, store, aclStrategy });
await initializer.handle();
expect(aclStrategy.getAuxiliaryIdentifier).toHaveBeenCalledWith({ path: baseUrl });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith(aclIdentifier, {});
expect(store.resourceExists).toHaveBeenCalledTimes(1);
expect(store.resourceExists).toHaveBeenCalledWith(aclIdentifier);
expect(store.setRepresentation).toHaveBeenCalledTimes(0);
});
it('errors when the root ACL check errors.', async(): Promise<void> => {
store.getRepresentation.mockRejectedValueOnce(new Error('Fatal'));
store.resourceExists.mockRejectedValueOnce(new Error('Fatal'));
const initializer = new AclInitializer({ baseUrl, store, aclStrategy });
await expect(initializer.handle()).rejects.toThrow('Fatal');

View File

@@ -7,6 +7,7 @@ describe('A RootContainerInitializer', (): void => {
const store: jest.Mocked<ResourceStore> = {
getRepresentation: jest.fn().mockRejectedValue(new NotFoundHttpError()),
setRepresentation: jest.fn(),
resourceExists: jest.fn(),
} as any;
const initializer = new RootContainerInitializer({ store, baseUrl });
@@ -15,27 +16,29 @@ describe('A RootContainerInitializer', (): void => {
});
it('invokes ResourceStore initialization.', async(): Promise<void> => {
store.resourceExists.mockResolvedValueOnce(false);
await initializer.handle();
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: baseUrl }, {});
expect(store.resourceExists).toHaveBeenCalledTimes(1);
expect(store.resourceExists).toHaveBeenCalledWith({ path: baseUrl });
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
});
it('does not invoke ResourceStore initialization when a root container already exists.', async(): Promise<void> => {
store.resourceExists.mockResolvedValueOnce(true);
store.getRepresentation.mockReturnValueOnce(Promise.resolve({
data: { destroy: jest.fn() },
} as any));
await initializer.handle();
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/' }, {});
expect(store.resourceExists).toHaveBeenCalledTimes(1);
expect(store.resourceExists).toHaveBeenCalledWith({ path: 'http://test.com/' });
expect(store.setRepresentation).toHaveBeenCalledTimes(0);
});
it('errors when the store errors writing the root container.', async(): Promise<void> => {
store.getRepresentation.mockRejectedValueOnce(new Error('Fatal'));
store.resourceExists.mockRejectedValueOnce(new Error('Fatal'));
await expect(initializer.handle()).rejects.toThrow('Fatal');
});
});