mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Generalize RootInitializer to ContainerInitializer
The initializer can now be used for any container. The initializer also does not handle the repeat check anymore, this is now configured with a ConditionalHandler.
This commit is contained in:
73
test/unit/init/ContainerInitializer.test.ts
Normal file
73
test/unit/init/ContainerInitializer.test.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { ContainerInitializer } from '../../../src/init/ContainerInitializer';
|
||||
import type { Logger } from '../../../src/logging/Logger';
|
||||
import { getLoggerFor } from '../../../src/logging/LogUtil';
|
||||
import type { Resource, ResourcesGenerator } from '../../../src/pods/generate/ResourcesGenerator';
|
||||
import type { KeyValueStorage } from '../../../src/storage/keyvalue/KeyValueStorage';
|
||||
import type { ResourceStore } from '../../../src/storage/ResourceStore';
|
||||
|
||||
jest.mock('../../../src/logging/LogUtil', (): any => {
|
||||
const logger: Logger = { warn: jest.fn(), debug: jest.fn(), info: jest.fn() } as any;
|
||||
return { getLoggerFor: (): Logger => logger };
|
||||
});
|
||||
|
||||
describe('A ContainerInitializer', (): void => {
|
||||
const baseUrl = 'http://test.com/';
|
||||
const path = 'foo/';
|
||||
let store: jest.Mocked<ResourceStore>;
|
||||
let generatorData: Resource[];
|
||||
let generator: jest.Mocked<ResourcesGenerator>;
|
||||
const storageKey = 'done';
|
||||
let storage: jest.Mocked<KeyValueStorage<string, boolean>>;
|
||||
let initializer: ContainerInitializer;
|
||||
let logger: jest.Mocked<Logger>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
store = {
|
||||
setRepresentation: jest.fn(),
|
||||
} as any;
|
||||
|
||||
generatorData = [
|
||||
{ identifier: { path: '/.acl' }, representation: '/.acl' as any },
|
||||
{ identifier: { path: '/container/' }, representation: '/container/' as any },
|
||||
];
|
||||
generator = {
|
||||
generate: jest.fn(async function* (): any {
|
||||
yield* generatorData;
|
||||
}),
|
||||
} as any;
|
||||
|
||||
const map = new Map();
|
||||
storage = {
|
||||
get: jest.fn((id: string): any => map.get(id)),
|
||||
set: jest.fn((id: string, value: any): any => map.set(id, value)),
|
||||
} as any;
|
||||
|
||||
initializer = new ContainerInitializer({
|
||||
baseUrl,
|
||||
path,
|
||||
store,
|
||||
generator,
|
||||
storageKey,
|
||||
storage,
|
||||
});
|
||||
logger = getLoggerFor(initializer) as any;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('writes resources and sets the storage value to true.', async(): Promise<void> => {
|
||||
await expect(initializer.handle()).resolves.toBeUndefined();
|
||||
expect(generator.generate).toHaveBeenCalledTimes(1);
|
||||
expect(store.setRepresentation).toHaveBeenCalledTimes(2);
|
||||
expect(storage.get(storageKey)).toBe(true);
|
||||
});
|
||||
|
||||
it('logs warnings if there was a problem creating a resource.', async(): Promise<void> => {
|
||||
store.setRepresentation.mockRejectedValueOnce(new Error('bad input'));
|
||||
|
||||
await expect(initializer.handle()).resolves.toBeUndefined();
|
||||
expect(generator.generate).toHaveBeenCalledTimes(1);
|
||||
expect(store.setRepresentation).toHaveBeenCalledTimes(2);
|
||||
expect(logger.warn).toHaveBeenCalledTimes(1);
|
||||
expect(logger.warn).toHaveBeenLastCalledWith('Failed to create resource /.acl: bad input');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user