2019-10-29 00:31:47 +00:00
|
|
|
import { expect } from 'chai';
|
2019-11-12 14:25:09 +00:00
|
|
|
import { LogLevel, NullLogger } from 'noicejs';
|
2019-11-03 19:05:11 +00:00
|
|
|
import { mock, spy, stub } from 'sinon';
|
2019-10-29 00:31:47 +00:00
|
|
|
|
2019-11-14 13:56:49 +00:00
|
|
|
import { createRuleSelector, createRuleSources, resolveRules } from '../../src/rule';
|
|
|
|
import { RuleVisitor } from '../../src/rule/RuleVisitor';
|
2019-11-02 12:48:12 +00:00
|
|
|
import { SchemaRule } from '../../src/rule/SchemaRule';
|
2019-11-02 03:02:25 +00:00
|
|
|
import { VisitorContext } from '../../src/visitor/VisitorContext';
|
|
|
|
import { describeLeaks, itLeaks } from '../helpers/async';
|
2019-10-29 00:31:47 +00:00
|
|
|
|
2019-11-02 03:02:25 +00:00
|
|
|
const TEST_RULES = [new SchemaRule({
|
2019-10-29 00:31:47 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Info,
|
2019-10-29 00:31:47 +00:00
|
|
|
name: 'foo',
|
|
|
|
select: '$',
|
|
|
|
tags: ['all', 'foo'],
|
2019-11-02 03:02:25 +00:00
|
|
|
}), new SchemaRule({
|
2019-10-29 00:31:47 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Warn,
|
2019-10-29 00:31:47 +00:00
|
|
|
name: 'bar',
|
|
|
|
select: '$',
|
|
|
|
tags: ['all', 'test'],
|
2019-11-02 03:02:25 +00:00
|
|
|
}), new SchemaRule({
|
2019-10-29 00:31:47 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Warn,
|
2019-10-29 00:31:47 +00:00
|
|
|
name: 'bin',
|
|
|
|
select: '$',
|
|
|
|
tags: ['all', 'test'],
|
|
|
|
})];
|
|
|
|
|
|
|
|
describeLeaks('rule resolver', async () => {
|
|
|
|
describeLeaks('include by level', async () => {
|
|
|
|
itLeaks('should include info rules', async () => {
|
2019-11-02 12:48:12 +00:00
|
|
|
const info = await resolveRules(TEST_RULES, createRuleSelector({
|
2019-11-11 03:52:56 +00:00
|
|
|
includeLevel: [LogLevel.Info],
|
2019-10-29 00:31:47 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
expect(info.length).to.equal(1);
|
|
|
|
expect(info[0]).to.equal(TEST_RULES[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
itLeaks('should include warn rules', async () => {
|
2019-11-02 12:48:12 +00:00
|
|
|
const info = await resolveRules(TEST_RULES, createRuleSelector({
|
2019-11-11 03:52:56 +00:00
|
|
|
includeLevel: [LogLevel.Warn],
|
2019-10-29 00:31:47 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
expect(info.length).to.equal(2);
|
|
|
|
expect(info[0]).to.equal(TEST_RULES[1]);
|
|
|
|
expect(info[1]).to.equal(TEST_RULES[2]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describeLeaks('include by name', async () => {
|
|
|
|
itLeaks('should include foo rules', async () => {
|
2019-11-02 12:48:12 +00:00
|
|
|
const rules = await resolveRules(TEST_RULES, createRuleSelector({
|
2019-10-29 00:31:47 +00:00
|
|
|
includeName: ['foo'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
expect(rules[0].name).to.equal('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describeLeaks('include by tag', async () => {
|
|
|
|
itLeaks('should include test rules', async () => {
|
2019-11-02 12:48:12 +00:00
|
|
|
const rules = await resolveRules(TEST_RULES, createRuleSelector({
|
2019-10-29 00:31:47 +00:00
|
|
|
includeTag: ['test'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(2);
|
|
|
|
expect(rules[0]).to.equal(TEST_RULES[1]);
|
|
|
|
expect(rules[1]).to.equal(TEST_RULES[2]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describeLeaks('exclude by name', async () => {
|
|
|
|
itLeaks('should exclude foo rules', async () => {
|
2019-11-02 12:48:12 +00:00
|
|
|
const rules = await resolveRules(TEST_RULES, createRuleSelector({
|
2019-10-29 00:31:47 +00:00
|
|
|
excludeName: ['foo'],
|
|
|
|
includeTag: ['all'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(2);
|
|
|
|
expect(rules[0]).to.equal(TEST_RULES[1]);
|
|
|
|
expect(rules[1]).to.equal(TEST_RULES[2]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describeLeaks('exclude by tag', async () => {
|
|
|
|
itLeaks('should exclude test rules', async () => {
|
2019-11-02 12:48:12 +00:00
|
|
|
const rules = await resolveRules(TEST_RULES, createRuleSelector({
|
2019-10-29 00:31:47 +00:00
|
|
|
excludeTag: ['test'],
|
|
|
|
includeTag: ['all'],
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(rules.length).to.equal(1);
|
|
|
|
expect(rules[0]).to.equal(TEST_RULES[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describeLeaks('rule visitor', async () => {
|
|
|
|
itLeaks('should only call visit for selected items', async () => {
|
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-10-29 00:31:47 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const data = {};
|
2019-11-02 03:02:25 +00:00
|
|
|
const rule = new SchemaRule({
|
2019-10-29 00:31:47 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Info,
|
2019-10-29 00:31:47 +00:00
|
|
|
name: 'foo',
|
|
|
|
select: '$',
|
|
|
|
tags: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockRule = mock(rule);
|
|
|
|
mockRule.expects('visit').never();
|
|
|
|
|
|
|
|
const pickStub = mockRule.expects('pick').once().withArgs(ctx, data);
|
|
|
|
pickStub.onFirstCall().returns(Promise.resolve([]));
|
|
|
|
pickStub.throws();
|
|
|
|
|
2019-11-14 13:56:49 +00:00
|
|
|
const visitor = new RuleVisitor({
|
|
|
|
rules: [rule],
|
|
|
|
});
|
|
|
|
await visitor.visit(ctx, {});
|
2019-10-29 00:31:47 +00:00
|
|
|
|
|
|
|
mockRule.verify();
|
|
|
|
expect(ctx.errors.length).to.equal(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
itLeaks('should call visit for each selected item', async () => {
|
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-10-29 00:31:47 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const data = {};
|
2019-11-02 03:02:25 +00:00
|
|
|
const rule = new SchemaRule({
|
2019-10-29 00:31:47 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Info,
|
2019-10-29 00:31:47 +00:00
|
|
|
name: 'foo',
|
|
|
|
select: '$',
|
|
|
|
tags: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockRule = mock(rule);
|
|
|
|
|
|
|
|
const pickStub = mockRule.expects('pick').once().withArgs(ctx, data);
|
|
|
|
pickStub.onFirstCall().returns(Promise.resolve([data]));
|
|
|
|
pickStub.throws();
|
|
|
|
|
|
|
|
const visitStub = mockRule.expects('visit').once().withArgs(ctx, data);
|
|
|
|
visitStub.onFirstCall().returns(Promise.resolve(ctx));
|
|
|
|
visitStub.throws();
|
|
|
|
|
2019-11-14 13:56:49 +00:00
|
|
|
const visitor = new RuleVisitor({
|
|
|
|
rules: [rule],
|
|
|
|
});
|
|
|
|
await visitor.visit(ctx, {});
|
2019-10-29 00:31:47 +00:00
|
|
|
|
|
|
|
mockRule.verify();
|
|
|
|
expect(ctx.errors.length).to.equal(0);
|
|
|
|
});
|
2019-10-29 03:23:10 +00:00
|
|
|
|
2019-11-03 19:05:11 +00:00
|
|
|
itLeaks('should visit individual items', async () => {
|
2019-10-29 03:23:10 +00:00
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-10-29 03:23:10 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const data = {
|
2019-11-03 19:05:11 +00:00
|
|
|
foo: [1, 2, 3],
|
2019-10-29 03:23:10 +00:00
|
|
|
};
|
2019-11-02 03:02:25 +00:00
|
|
|
const rule = new SchemaRule({
|
2019-10-29 03:23:10 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Info,
|
2019-10-29 03:23:10 +00:00
|
|
|
name: 'foo',
|
2019-11-03 19:05:11 +00:00
|
|
|
select: '$.foo.*',
|
2019-10-29 03:23:10 +00:00
|
|
|
tags: [],
|
|
|
|
});
|
|
|
|
|
2019-11-03 19:05:11 +00:00
|
|
|
const pickSpy = spy(rule, 'pick');
|
|
|
|
const visitStub = stub(rule, 'visit').returns(Promise.resolve({
|
|
|
|
changes: [],
|
|
|
|
errors: [],
|
|
|
|
}));
|
|
|
|
|
2019-11-14 13:56:49 +00:00
|
|
|
const visitor = new RuleVisitor({
|
|
|
|
rules: [rule],
|
|
|
|
});
|
|
|
|
await visitor.visit(ctx, data);
|
2019-11-03 19:05:11 +00:00
|
|
|
|
|
|
|
expect(pickSpy).to.have.callCount(1).and.to.have.been.calledWithExactly(ctx, data);
|
|
|
|
expect(visitStub).to.have.callCount(3);
|
2019-10-29 03:23:10 +00:00
|
|
|
});
|
|
|
|
|
2019-11-03 19:05:11 +00:00
|
|
|
itLeaks('should visit individual items', async () => {
|
2019-10-29 03:23:10 +00:00
|
|
|
const ctx = new VisitorContext({
|
2019-11-12 14:25:09 +00:00
|
|
|
logger: NullLogger.global,
|
|
|
|
schemaOptions: {
|
2019-10-29 03:23:10 +00:00
|
|
|
coerce: false,
|
|
|
|
defaults: false,
|
|
|
|
mutate: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const data = {
|
2019-11-03 19:05:11 +00:00
|
|
|
foo: [1, 2, 3],
|
2019-10-29 03:23:10 +00:00
|
|
|
};
|
2019-11-02 03:02:25 +00:00
|
|
|
const rule = new SchemaRule({
|
2019-10-29 03:23:10 +00:00
|
|
|
check: {},
|
|
|
|
desc: '',
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Info,
|
2019-10-29 03:23:10 +00:00
|
|
|
name: 'foo',
|
2019-11-03 19:05:11 +00:00
|
|
|
select: '$.foo.*',
|
2019-10-29 03:23:10 +00:00
|
|
|
tags: [],
|
|
|
|
});
|
|
|
|
|
2019-11-03 19:05:11 +00:00
|
|
|
const visitStub = stub(rule, 'visit').returns(Promise.resolve({
|
|
|
|
changes: [],
|
|
|
|
errors: [{
|
|
|
|
data: {},
|
2019-11-11 03:52:56 +00:00
|
|
|
level: LogLevel.Error,
|
2019-11-03 19:05:11 +00:00
|
|
|
msg: 'kaboom!',
|
|
|
|
}],
|
|
|
|
}));
|
|
|
|
|
2019-11-14 13:56:49 +00:00
|
|
|
const visitor = new RuleVisitor({
|
|
|
|
rules: [rule],
|
|
|
|
});
|
|
|
|
await visitor.visit(ctx, data);
|
2019-11-03 19:05:11 +00:00
|
|
|
|
|
|
|
expect(visitStub).to.have.callCount(3);
|
|
|
|
expect(ctx.errors.length).to.equal(3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create rule sources helper', () => {
|
|
|
|
it('should ensure every field is an array', () => {
|
|
|
|
const sources = createRuleSources({});
|
|
|
|
|
|
|
|
expect(sources).to.have.deep.property('ruleFile', []);
|
|
|
|
expect(sources).to.have.deep.property('ruleModule', []);
|
|
|
|
expect(sources).to.have.deep.property('rulePath', []);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create rule selector helper', () => {
|
|
|
|
it('should ensure every field is an array', () => {
|
|
|
|
const sources = createRuleSelector({});
|
|
|
|
|
|
|
|
expect(sources).to.have.deep.property('excludeLevel', []);
|
|
|
|
expect(sources).to.have.deep.property('excludeName', []);
|
|
|
|
expect(sources).to.have.deep.property('excludeTag', []);
|
|
|
|
expect(sources).to.have.deep.property('includeLevel', []);
|
|
|
|
expect(sources).to.have.deep.property('includeName', []);
|
|
|
|
expect(sources).to.have.deep.property('includeTag', []);
|
2019-10-29 03:23:10 +00:00
|
|
|
});
|
2019-10-29 00:31:47 +00:00
|
|
|
});
|