2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const ObjectId = require('bson-objectid');
|
|
|
|
const testUtils = require('../../../utils');
|
|
|
|
const models = require('../../../../core/server/models');
|
|
|
|
const validation = require('../../../../core/server/data/validation');
|
2016-10-14 17:31:20 +03:00
|
|
|
|
|
|
|
// Validate our customisations
|
|
|
|
describe('Validation', function () {
|
2018-02-16 02:49:15 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2016-10-14 17:31:20 +03:00
|
|
|
it('should export our required functions', function () {
|
|
|
|
should.exist(validation);
|
|
|
|
|
|
|
|
validation.should.have.properties(
|
2020-07-15 18:11:27 +03:00
|
|
|
['validate', 'validator', 'validateSchema']
|
2016-10-14 17:31:20 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
validation.validate.should.be.a.Function();
|
2017-10-26 13:01:24 +03:00
|
|
|
validation.validatePassword.should.be.a.Function();
|
2016-10-14 17:31:20 +03:00
|
|
|
validation.validateSchema.should.be.a.Function();
|
|
|
|
|
|
|
|
validation.validator.should.have.properties(['empty', 'notContains', 'isTimezone', 'isEmptyOrURL', 'isSlug']);
|
|
|
|
});
|
|
|
|
|
2018-02-16 02:49:15 +03:00
|
|
|
describe('Validate Schema', function () {
|
|
|
|
describe('models.add', function () {
|
|
|
|
it('blank model', function () {
|
|
|
|
// NOTE: Fields with `defaultTo` are getting ignored. This is handled on the DB level.
|
|
|
|
return validation.validateSchema('posts', models.Post.forge(), {method: 'insert'})
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('Expected ValidationError.');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (!_.isArray(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err.length.should.eql(7);
|
|
|
|
|
|
|
|
const errorMessages = _.map(err, function (object) {
|
|
|
|
return object.message;
|
|
|
|
}).join(',');
|
|
|
|
|
|
|
|
// NOTE: Some of these fields are auto-filled in the model layer (e.g. author_id, created_at etc.)
|
|
|
|
['id', 'uuid', 'slug', 'title', 'author_id', 'created_at', 'created_by'].forEach(function (attr) {
|
|
|
|
errorMessages.should.match(new RegExp('posts.' + attr));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('blank id', function () {
|
|
|
|
const postModel = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({
|
|
|
|
id: null,
|
|
|
|
slug: 'test'
|
|
|
|
}));
|
|
|
|
|
|
|
|
return validation.validateSchema('posts', postModel, {method: 'insert'})
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('Expected ValidationError.');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (!_.isArray(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err.length.should.eql(1);
|
|
|
|
err[0].message.should.match(/posts\.id/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should pass', function () {
|
|
|
|
return validation.validateSchema(
|
|
|
|
'posts',
|
|
|
|
models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'title'})),
|
|
|
|
{method: 'insert'}
|
|
|
|
);
|
|
|
|
});
|
2018-06-26 17:00:54 +03:00
|
|
|
|
|
|
|
it('transforms 0 and 1', function () {
|
2019-09-16 13:51:54 +03:00
|
|
|
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'test', featured: 0}));
|
2018-06-26 17:00:54 +03:00
|
|
|
post.get('featured').should.eql(0);
|
|
|
|
|
|
|
|
return validation.validateSchema('posts', post, {method: 'insert'})
|
|
|
|
.then(function () {
|
|
|
|
post.get('featured').should.eql(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps true or false', function () {
|
2019-09-16 13:51:54 +03:00
|
|
|
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'test', featured: true}));
|
2018-06-26 17:00:54 +03:00
|
|
|
post.get('featured').should.eql(true);
|
|
|
|
|
|
|
|
return validation.validateSchema('posts', post, {method: 'insert'})
|
|
|
|
.then(function () {
|
|
|
|
post.get('featured').should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
2018-02-16 02:49:15 +03:00
|
|
|
});
|
|
|
|
|
2018-10-06 23:13:52 +03:00
|
|
|
describe('webhooks.add', function () {
|
|
|
|
it('event name is not lowercase', function () {
|
|
|
|
const webhook = models.Webhook.forge(testUtils.DataGenerator.forKnex.createWebhook({event: 'Test'}));
|
|
|
|
|
|
|
|
// NOTE: Fields with `defaultTo` are getting ignored. This is handled on the DB level.
|
|
|
|
return validation.validateSchema('webhooks', webhook, {method: 'insert'})
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('Expected ValidationError.');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (!_.isArray(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2021-03-02 17:01:52 +03:00
|
|
|
err.length.should.eql(2);
|
2018-10-06 23:13:52 +03:00
|
|
|
err[0].errorType.should.eql('ValidationError');
|
2021-03-02 17:01:52 +03:00
|
|
|
err[1].errorType.should.eql('ValidationError');
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-16 02:49:15 +03:00
|
|
|
describe('models.edit', function () {
|
|
|
|
it('uuid is invalid', function () {
|
|
|
|
const postModel = models.Post.forge({id: ObjectId.generate(), uuid: '1234'});
|
|
|
|
|
|
|
|
postModel.changed = {uuid: postModel.get('uuid')};
|
|
|
|
|
|
|
|
return validation.validateSchema('posts', postModel)
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('Expected ValidationError.');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (!_.isArray(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err.length.should.eql(1);
|
|
|
|
err[0].message.should.match(/isUUID/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('date is null', function () {
|
|
|
|
const postModel = models.Post.forge({id: ObjectId.generate(), created_at: null});
|
|
|
|
|
|
|
|
postModel.changed = {created_at: postModel.get('updated_at')};
|
|
|
|
|
|
|
|
return validation.validateSchema('posts', postModel)
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('Expected ValidationError.');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (!_.isArray(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err.length.should.eql(1);
|
|
|
|
err[0].message.should.match(/posts\.created_at/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Assert the Validator dependency', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const validator = validation.validator;
|
2016-10-14 17:31:20 +03:00
|
|
|
|
|
|
|
it('isEmptyOrUrl filters javascript urls', function () {
|
|
|
|
validator.isEmptyOrURL('javascript:alert(0)').should.be.false();
|
2017-01-10 15:05:25 +03:00
|
|
|
validator.isEmptyOrURL('http://example.com/lol/<script>lalala</script>/').should.be.false();
|
|
|
|
validator.isEmptyOrURL('http://example.com/lol?somequery=<script>lalala</script>').should.be.false();
|
2016-10-14 17:31:20 +03:00
|
|
|
validator.isEmptyOrURL('').should.be.true();
|
|
|
|
validator.isEmptyOrURL('http://localhost:2368').should.be.true();
|
2017-01-10 15:05:25 +03:00
|
|
|
validator.isEmptyOrURL('http://example.com/test/').should.be.true();
|
|
|
|
validator.isEmptyOrURL('http://www.example.com/test/').should.be.true();
|
|
|
|
validator.isEmptyOrURL('http://example.com/foo?somequery=bar').should.be.true();
|
|
|
|
validator.isEmptyOrURL('example.com/test/').should.be.true();
|
2016-10-14 17:31:20 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|