Petr Reshetin 268f714357
feat: Schema based validation of service config (#7335)
Co-authored-by: Mariusz Nowak <medyk@medikoo.com>
2020-08-03 15:41:05 +02:00

175 lines
5.3 KiB
JavaScript

'use strict';
const expect = require('chai').expect;
const fixtures = require('../../../tests/fixtures');
const runServerless = require('../../../tests/utils/run-serverless');
describe('ConfigSchemaHandler', () => {
describe('#constructor', () => {
it('should freeze parts of schema for service', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
expect(() => {
serverless.configSchemaHandler.schema.properties.service.name = 'changed';
}).to.throw(Error);
});
});
it('should freeze parts of schema for plugins', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
expect(() => {
serverless.configSchemaHandler.schema.properties.plugins.properties = 'changed';
}).to.throw(Error);
});
});
it('should freeze parts of schema for resources', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
expect(() => {
serverless.configSchemaHandler.schema.properties.resources.something = 'changed';
}).to.throw(Error);
});
});
it('should freeze parts of schema for package', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
expect(() => {
serverless.configSchemaHandler.schema.properties.package.properties.oneMore = {
type: 'string',
};
}).to.throw(Error);
});
});
it('should freeze parts of schema for layers', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
expect(() => {
serverless.configSchemaHandler.schema.properties.layers.properties = 'changed';
}).to.throw(Error);
});
});
});
describe('#validateConfig', () => {
it('should run without errors for valid config', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
});
});
});
describe('#defineFunctionEvent', () => {
it('should extend schema with defineFunctionEvent method', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
const expectedPieceOfSchema = {
type: 'object',
properties: {
someEvent: {
type: 'object',
properties: {
someRequiredStringProp: { type: 'string' },
someNumberProp: { type: 'number' },
},
required: ['someRequiredStringProp'],
additionalProperties: false,
},
},
required: ['someEvent'],
additionalProperties: false,
};
expect(
serverless.serverless.configSchemaHandler.schema.properties.functions.patternProperties[
'^[a-zA-Z0-9-_]+$'
].properties.events.items.anyOf[1]
).to.deep.equal(expectedPieceOfSchema);
return;
});
});
});
describe('#defineCustomProperty', () => {
it('should extend schema with defineCustomProperty method', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
const someCustomStringProp = {
type: 'string',
};
expect(
serverless.serverless.configSchemaHandler.schema.properties.custom.properties
.someCustomStringProp
).to.deep.equal(someCustomStringProp);
return;
});
});
});
describe('#defineTopLevelProperty', () => {
it('should extend schema with defineTopLevelProperty method', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
const expectedAppPropSchema = {
type: 'string',
};
expect(serverless.serverless.configSchemaHandler.schema.properties.top).to.deep.equal(
expectedAppPropSchema
);
return;
});
});
});
describe('#defineProvider', () => {
it('should extend schema with defineProvider method', () => {
return runServerless({
cwd: fixtures.map.configSchemaExtensions,
cliArgs: ['info'],
}).then(serverless => {
const providerPieceOfSchema = {
type: 'object',
properties: {
name: { const: 'someProvider' },
stage: { type: 'string' },
variableSyntax: { type: 'string' },
},
required: ['name'],
additionalProperties: false,
};
expect(serverless.serverless.configSchemaHandler.schema.properties.provider).to.deep.equal(
providerPieceOfSchema
);
const expectedHandlerPieceOfSchema = { type: 'string' };
expect(
serverless.serverless.configSchemaHandler.schema.properties.functions.patternProperties[
'^[a-zA-Z0-9-_]+$'
].properties.handler
).to.deep.equal(expectedHandlerPieceOfSchema);
return;
});
});
});
});