2019-11-12 13:32:30 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import mockFS from 'mock-fs';
|
2019-11-18 01:46:04 +00:00
|
|
|
import { LogLevel, NullLogger } from 'noicejs';
|
2019-11-17 00:10:23 +00:00
|
|
|
import { spy, stub } from 'sinon';
|
2019-11-12 13:32:30 +00:00
|
|
|
|
2019-11-18 01:46:04 +00:00
|
|
|
import { loadRuleFiles, loadRuleModules, loadRulePaths, loadRuleSource } from '../../src/rule';
|
|
|
|
import { SchemaRule } from '../../src/rule/SchemaRule';
|
2019-11-12 13:32:30 +00:00
|
|
|
import { VisitorContext } from '../../src/visitor/VisitorContext';
|
|
|
|
import { describeLeaks, itLeaks } from '../helpers/async';
|
|
|
|
|
|
|
|
const EXAMPLE_EMPTY = '{name: foo, definitions: {}, rules: []}';
|
|
|
|
const EXAMPLE_RULES = `{
|
|
|
|
name: foo,
|
|
|
|
definitions: {},
|
|
|
|
rules: [{
|
|
|
|
name: test,
|
2019-11-17 17:55:33 +00:00
|
|
|
desc: test-rule,
|
2019-11-12 13:32:30 +00:00
|
|
|
level: info,
|
2019-11-17 18:07:02 +00:00
|
|
|
tags: [],
|
|
|
|
check: {}
|
2019-11-12 13:32:30 +00:00
|
|
|
}]
|
|
|
|
}`;
|
|
|
|
|
2019-11-18 01:46:04 +00:00
|
|
|
function testContext() {
|
|
|
|
return new VisitorContext({
|
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-12 13:32:30 +00:00
|
|
|
describeLeaks('load rule file helper', async () => {
|
|
|
|
itLeaks('should add schema', async () => {
|
|
|
|
mockFS({
|
|
|
|
test: EXAMPLE_EMPTY,
|
|
|
|
});
|
|
|
|
|
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-11-12 13:32:30 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const schemaSpy = spy(ctx, 'addSchema');
|
|
|
|
|
|
|
|
const rules = await loadRuleFiles([
|
|
|
|
'test',
|
|
|
|
], ctx);
|
|
|
|
|
|
|
|
mockFS.restore();
|
|
|
|
|
|
|
|
expect(schemaSpy).to.have.been.calledWith('foo');
|
|
|
|
expect(rules.length).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
itLeaks('should load rules', async () => {
|
|
|
|
mockFS({
|
|
|
|
test: EXAMPLE_RULES,
|
|
|
|
});
|
|
|
|
|
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-11-12 13:32:30 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const rules = await loadRuleFiles([
|
|
|
|
'test',
|
|
|
|
], ctx);
|
|
|
|
|
|
|
|
mockFS.restore();
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
});
|
2019-11-17 22:56:59 +00:00
|
|
|
|
|
|
|
itLeaks('should validate rule files', async () => {
|
|
|
|
mockFS({
|
|
|
|
test: `{
|
|
|
|
name: foo,
|
|
|
|
definitions: [],
|
|
|
|
rules: {}
|
|
|
|
}`
|
|
|
|
});
|
|
|
|
|
|
|
|
const ctx = new VisitorContext({
|
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const rules = await loadRuleFiles([
|
|
|
|
'test',
|
|
|
|
], ctx);
|
|
|
|
|
|
|
|
mockFS.restore();
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(0);
|
|
|
|
});
|
2019-11-12 13:32:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describeLeaks('load rule path helper', async () => {
|
|
|
|
itLeaks('should only load matching rule files', async () => {
|
|
|
|
mockFS({
|
|
|
|
test: {
|
|
|
|
'bin.nope': '{}', // will parse but throw on lack of rules
|
|
|
|
'foo.yml': EXAMPLE_RULES,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-11-12 13:32:30 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const rules = await loadRulePaths([
|
|
|
|
'test',
|
|
|
|
], ctx);
|
|
|
|
|
|
|
|
mockFS.restore();
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
itLeaks('should recursively load rule files', async () => {
|
|
|
|
mockFS({
|
|
|
|
test: {
|
|
|
|
'bar-dir': {
|
|
|
|
'bar.yml': EXAMPLE_RULES.replace(/foo/g, 'bar'),
|
|
|
|
},
|
|
|
|
'bin.nope': '{}', // will parse but throw on lack of rules
|
|
|
|
'some-dir': {
|
|
|
|
'foo.yml': EXAMPLE_RULES,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-11-12 13:32:30 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const rules = await loadRulePaths([
|
|
|
|
'test',
|
|
|
|
], ctx);
|
|
|
|
|
|
|
|
mockFS.restore();
|
|
|
|
|
2019-11-19 12:03:48 +00:00
|
|
|
const EXPECTED_RULES = 2;
|
|
|
|
expect(rules.length).to.equal(EXPECTED_RULES);
|
2019-11-12 13:32:30 +00:00
|
|
|
});
|
|
|
|
});
|
2019-11-17 00:10:23 +00:00
|
|
|
|
|
|
|
describeLeaks('load rule module helper', async () => {
|
|
|
|
itLeaks('should load rule modules', async () => {
|
2019-11-18 01:46:04 +00:00
|
|
|
const ctx = testContext();
|
2019-11-17 00:10:23 +00:00
|
|
|
const requireStub = stub().withArgs('test').returns({
|
|
|
|
name: 'test',
|
|
|
|
rules: [{
|
|
|
|
check: {},
|
|
|
|
desc: 'testing rule',
|
|
|
|
level: 'info',
|
|
|
|
name: 'test-rule',
|
|
|
|
tags: [],
|
|
|
|
}],
|
2019-11-18 01:46:04 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
2019-11-17 00:10:23 +00:00
|
|
|
}) as any;
|
|
|
|
const rules = await loadRuleModules(['test'], ctx, requireStub);
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
});
|
|
|
|
|
2019-11-17 00:27:39 +00:00
|
|
|
itLeaks('should handle errors loading rule modules', async () => {
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
|
|
const requireStub = stub().throws(new Error('could not load this module')) as any;
|
2019-11-18 01:46:04 +00:00
|
|
|
const ctx = testContext();
|
2019-11-17 00:27:39 +00:00
|
|
|
|
|
|
|
return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]);
|
|
|
|
});
|
|
|
|
|
2019-11-17 22:56:59 +00:00
|
|
|
itLeaks('should validate rule module exports', async () => {
|
|
|
|
const requireStub = stub().returns({
|
|
|
|
name: 'test-rules',
|
|
|
|
rules: {},
|
2019-11-18 01:46:04 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
2019-11-17 22:56:59 +00:00
|
|
|
}) as any;
|
2019-11-18 01:46:04 +00:00
|
|
|
const ctx = testContext();
|
2019-11-17 22:56:59 +00:00
|
|
|
|
|
|
|
return expect(loadRuleModules(['test'], ctx, requireStub)).to.eventually.deep.equal([]);
|
|
|
|
});
|
2019-11-17 23:45:12 +00:00
|
|
|
|
|
|
|
itLeaks('should load module definitions', async () => {
|
|
|
|
const requireStub = stub().returns({
|
|
|
|
definitions: {
|
|
|
|
foo: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
name: 'test-rules',
|
|
|
|
rules: [],
|
2019-11-18 01:46:04 +00:00
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
2019-11-17 23:45:12 +00:00
|
|
|
}) as any;
|
2019-11-18 01:46:04 +00:00
|
|
|
const ctx = testContext();
|
2019-11-17 23:45:12 +00:00
|
|
|
|
|
|
|
await loadRuleModules(['test'], ctx, requireStub);
|
|
|
|
const schema = ctx.compile({
|
|
|
|
$ref: 'test-rules#/definitions/foo',
|
|
|
|
});
|
|
|
|
|
2019-11-19 12:03:48 +00:00
|
|
|
const NUMBER_VALUE = 2;
|
|
|
|
expect(schema(NUMBER_VALUE)).to.equal(false);
|
2019-11-17 23:45:12 +00:00
|
|
|
expect(schema('foo')).to.equal(true);
|
|
|
|
});
|
2019-11-18 01:46:04 +00:00
|
|
|
|
|
|
|
itLeaks('should not instantiate class instances', async () => {
|
|
|
|
class TestRule extends SchemaRule {}
|
|
|
|
const ctx = testContext();
|
|
|
|
|
|
|
|
const rules = await loadRuleSource({
|
|
|
|
name: 'test',
|
|
|
|
rules: [new TestRule({
|
|
|
|
check: {},
|
|
|
|
desc: 'test',
|
|
|
|
level: LogLevel.Info,
|
|
|
|
name: 'test',
|
|
|
|
select: '$',
|
|
|
|
tags: [],
|
|
|
|
})],
|
|
|
|
}, ctx);
|
|
|
|
expect(rules[0]).to.be.an.instanceOf(TestRule);
|
|
|
|
});
|
2019-11-17 00:10:23 +00:00
|
|
|
});
|