feat: Allow custom root ACL.

This commit is contained in:
Ruben Verborgh
2021-01-15 12:38:22 +01:00
committed by Joachim Van Herwegen
parent 87f1450d0d
commit e544e6dc11
6 changed files with 61 additions and 30 deletions

View File

@@ -1,9 +1,15 @@
import type { AclManager } from '../../../src/authorization/AclManager';
import { AclInitializer } from '../../../src/init/AclInitializer';
import { BasicRepresentation } from '../../../src/ldp/representation/BasicRepresentation';
import type { ResourceIdentifier } from '../../../src/ldp/representation/ResourceIdentifier';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
jest.mock('../../../src/ldp/representation/BasicRepresentation');
// eslint-disable-next-line @typescript-eslint/naming-convention
const RepresentationMock: jest.Mock<BasicRepresentation> = BasicRepresentation as any;
describe('AclInitializer', (): void => {
const store: jest.Mocked<ResourceStore> = {
getRepresentation: jest.fn().mockRejectedValue(new NotFoundHttpError()),
@@ -14,22 +20,40 @@ describe('AclInitializer', (): void => {
} as any;
const baseUrl = 'http://localhost:3000/';
let initializer: AclInitializer;
beforeEach(async(): Promise<void> => {
initializer = new AclInitializer(baseUrl, store, aclManager);
});
afterEach((): void => {
jest.clearAllMocks();
});
it('invokes ACL initialization.', async(): Promise<void> => {
it('sets the default ACL when none exists already.', async(): Promise<void> => {
const initializer = new AclInitializer({ baseUrl, store, aclManager });
await initializer.handle();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/.acl' }, {});
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
expect(store.setRepresentation).toHaveBeenCalledWith(
{ path: 'http://test.com/.acl' }, RepresentationMock.mock.instances[0],
);
expect(RepresentationMock).toHaveBeenCalledWith(
expect.stringMatching('<#authorization>'), { path: 'http://test.com/.acl' }, 'text/turtle',
);
});
it('sets the specific ACL when one was specified.', async(): Promise<void> => {
const initializer = new AclInitializer({ baseUrl, store, aclManager, aclPath: __filename });
await initializer.handle();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.getRepresentation).toHaveBeenCalledTimes(1);
expect(store.getRepresentation).toHaveBeenCalledWith({ path: 'http://test.com/.acl' }, {});
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
expect(store.setRepresentation).toHaveBeenCalledWith(
{ path: 'http://test.com/.acl' }, RepresentationMock.mock.instances[0],
);
expect(RepresentationMock).toHaveBeenCalledWith(
expect.stringMatching('Joachim'), { path: 'http://test.com/.acl' }, 'text/turtle',
);
});
it('does not invoke ACL initialization when a root ACL already exists.', async(): Promise<void> => {
@@ -37,6 +61,7 @@ describe('AclInitializer', (): void => {
data: { destroy: jest.fn() },
} as any));
const initializer = new AclInitializer({ baseUrl, store, aclManager });
await initializer.handle();
expect(aclManager.getAclDocument).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
@@ -47,6 +72,8 @@ describe('AclInitializer', (): void => {
it('errors when the root ACL check errors.', async(): Promise<void> => {
store.getRepresentation.mockRejectedValueOnce(new Error('Fatal'));
const initializer = new AclInitializer({ baseUrl, store, aclManager });
await expect(initializer.handle()).rejects.toThrow('Fatal');
});
});

View File

@@ -8,7 +8,7 @@ describe('A RootContainerInitializer', (): void => {
getRepresentation: jest.fn().mockRejectedValue(new NotFoundHttpError()),
setRepresentation: jest.fn(),
} as any;
const initializer = new RootContainerInitializer(baseUrl, store);
const initializer = new RootContainerInitializer({ store, baseUrl });
afterEach((): void => {
jest.clearAllMocks();