From 8328f1a5bfbbbb2fa185a4c7894bd50f985d9984 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Thu, 18 Aug 2022 14:38:36 +1200 Subject: [PATCH] Add tests for the config loader. --- .../src/util/__test__/config.loader.test.ts | 116 ++++++++++++++++++ .../lambda-tiler/src/util/config.loader.ts | 2 +- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 packages/lambda-tiler/src/util/__test__/config.loader.test.ts diff --git a/packages/lambda-tiler/src/util/__test__/config.loader.test.ts b/packages/lambda-tiler/src/util/__test__/config.loader.test.ts new file mode 100644 index 000000000..0b2ec3df1 --- /dev/null +++ b/packages/lambda-tiler/src/util/__test__/config.loader.test.ts @@ -0,0 +1,116 @@ +import { base58, ConfigProviderMemory } from '@basemaps/config'; +import { fsa } from '@basemaps/shared'; +import { LambdaHttpResponse } from '@linzjs/lambda'; +import o from 'ospec'; +import { createSandbox } from 'sinon'; +import { FsMemory } from '../../routes/__tests__/memory.fs.js'; +import { FakeData } from '../../__tests__/config.data.js'; +import { Api, mockRequest, mockUrlRequest } from '../../__tests__/xyz.util.js'; +import { CachedConfig } from '../config.cache.js'; +import { ConfigLoader } from '../config.loader.js'; + +o.spec('ConfigLoader', () => { + const memory = new FsMemory(); + const config = new ConfigProviderMemory(); + const sandbox = createSandbox(); + + o.before(() => { + fsa.register('memory://', memory); + }); + + o.beforeEach(() => { + sandbox.stub(ConfigLoader, 'getDefaultConfig').resolves(config); + }); + + o.afterEach(() => { + sandbox.restore(); + config.objects.clear(); + memory.files.clear(); + CachedConfig.cache.clear(); + }); + + o('should return default config', async () => { + const provider = await ConfigLoader.load(mockRequest('/v1/fonts.json')); + o(provider).deepEquals(config); + }); + + o('should Not working with wrong config url', async () => { + const error = await ConfigLoader.load( + mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=notapath`, Api.header), + ) + .then(() => null) + .catch((e) => e); + + o(error instanceof LambdaHttpResponse).equals(true); + o((error as LambdaHttpResponse).status).equals(400); + o((error as LambdaHttpResponse).statusDescription).equals('Invalid config location'); + }); + + o('should Not working with wrong protocol', async () => { + const error = await ConfigLoader.load( + mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=memory1://linz-basemaps/config`, Api.header), + ) + .then(() => null) + .catch((e) => e); + + o(error instanceof LambdaHttpResponse).equals(true); + o((error as LambdaHttpResponse).status).equals(400); + o((error as LambdaHttpResponse).statusDescription).equals('Invalid configuration location protocol:memory1'); + }); + + o('should Not working with wrong s3 bucket', async () => { + const error = await ConfigLoader.load( + mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=s3://wrong-bucket/config`, Api.header), + ) + .then(() => null) + .catch((e) => e); + + o(error instanceof LambdaHttpResponse).equals(true); + o((error as LambdaHttpResponse).status).equals(400); + o((error as LambdaHttpResponse).statusDescription).equals( + 'Bucket: "wrong-bucket" is not a allowed bucket location', + ); + }); + + const location = 'memory://linz-basemaps/config.json'; + + o('should Not working with no file in the path', async () => { + const error = await ConfigLoader.load( + mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=${location}`, Api.header), + ) + .then(() => null) + .catch((e) => e); + + o(error instanceof LambdaHttpResponse).equals(true); + o((error as LambdaHttpResponse).status).equals(404); + o((error as LambdaHttpResponse).statusDescription).equals('Config Not Fond from the location'); + }); + + o('should get expected config file', async () => { + const expectedConfig = new ConfigProviderMemory(); + expectedConfig.put(FakeData.tileSetRaster('aerial')); + await fsa.write(location, Buffer.from(JSON.stringify(expectedConfig.toJson()))); + + const provider = await ConfigLoader.load( + mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=${location}`, Api.header), + ); + + o(await provider.Imagery.get('aerial')).deepEquals(await expectedConfig.Imagery.get('aerial')); + }); + + o('should get expected config file with base58 location', async () => { + const expectedConfig = new ConfigProviderMemory(); + expectedConfig.put(FakeData.tileSetVector('topographic')); + await fsa.write(location, Buffer.from(JSON.stringify(expectedConfig.toJson()))); + + const provider = await ConfigLoader.load( + mockUrlRequest( + '/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', + `?config=${base58.encode(Buffer.from(location))}`, + Api.header, + ), + ); + + o(await provider.Imagery.get('topographic')).deepEquals(await expectedConfig.Imagery.get('topographic')); + }); +}); diff --git a/packages/lambda-tiler/src/util/config.loader.ts b/packages/lambda-tiler/src/util/config.loader.ts index 480bdad06..758df7c6c 100644 --- a/packages/lambda-tiler/src/util/config.loader.ts +++ b/packages/lambda-tiler/src/util/config.loader.ts @@ -36,7 +36,7 @@ export class ConfigLoader { req.timer.start('config:load'); return CachedConfig.get(configLocation).then((f) => { req.timer.end('config:load'); - if (f == null) throw new LambdaHttpResponse(400, `Invalid config location`); + if (f == null) throw new LambdaHttpResponse(404, `Config Not Fond from the location`); return f; }); }