2021-06-14 19:53:24 +03:00
|
|
|
const should = require('should');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const ObjectId = require('bson-objectid');
|
2021-10-06 13:12:21 +03:00
|
|
|
const testUtils = require('../../../../utils');
|
|
|
|
const models = require('../../../../../core/server/models');
|
2021-06-15 13:45:37 +03:00
|
|
|
|
2021-10-06 13:12:21 +03:00
|
|
|
const validateSchema = require('../../../../../core/server/data/schema/validator');
|
2021-06-14 19:53:24 +03:00
|
|
|
|
|
|
|
describe('Validate Schema', function () {
|
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('models.add', function () {
|
|
|
|
it('blank model', function () {
|
|
|
|
// NOTE: Fields with `defaultTo` are getting ignored. This is handled on the DB level.
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('posts', models.Post.forge(), {method: 'insert'})
|
2021-06-14 19:53:24 +03:00
|
|
|
.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'
|
|
|
|
}));
|
|
|
|
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('posts', postModel, {method: 'insert'})
|
2021-06-14 19:53:24 +03:00
|
|
|
.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 () {
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema(
|
2021-06-14 19:53:24 +03:00
|
|
|
'posts',
|
|
|
|
models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'title'})),
|
|
|
|
{method: 'insert'}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('transforms 0 and 1', function () {
|
|
|
|
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'test', featured: 0}));
|
|
|
|
post.get('featured').should.eql(0);
|
|
|
|
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('posts', post, {method: 'insert'})
|
2021-06-14 19:53:24 +03:00
|
|
|
.then(function () {
|
|
|
|
post.get('featured').should.eql(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps true or false', function () {
|
|
|
|
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({slug: 'test', featured: true}));
|
|
|
|
post.get('featured').should.eql(true);
|
|
|
|
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('posts', post, {method: 'insert'})
|
2021-06-14 19:53:24 +03:00
|
|
|
.then(function () {
|
|
|
|
post.get('featured').should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('webhooks.add', function () {
|
|
|
|
it('event name is not lowercase', function () {
|
|
|
|
const webhook = models.Webhook.forge(testUtils.DataGenerator.forKnex.createWebhook({
|
|
|
|
event: 'Test',
|
|
|
|
integration_id: testUtils.DataGenerator.Content.integrations[0].id
|
|
|
|
}));
|
|
|
|
|
|
|
|
// NOTE: Fields with `defaultTo` are getting ignored. This is handled on the DB level.
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('webhooks', webhook, {method: 'insert'})
|
2021-06-14 19:53:24 +03:00
|
|
|
.then(function () {
|
|
|
|
throw new Error('Expected ValidationError.');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
if (!_.isArray(err)) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err.length.should.eql(1);
|
|
|
|
err[0].errorType.should.eql('ValidationError');
|
|
|
|
err[0].message.should.match(/isLowercase/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('models.edit', function () {
|
|
|
|
it('uuid is invalid', function () {
|
|
|
|
const postModel = models.Post.forge({id: ObjectId().toHexString(), uuid: '1234'});
|
|
|
|
|
|
|
|
postModel.changed = {uuid: postModel.get('uuid')};
|
|
|
|
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('posts', postModel)
|
2021-06-14 19:53:24 +03:00
|
|
|
.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().toHexString(), created_at: null});
|
|
|
|
|
|
|
|
postModel.changed = {created_at: postModel.get('updated_at')};
|
|
|
|
|
2021-06-15 13:45:37 +03:00
|
|
|
return validateSchema('posts', postModel)
|
2021-06-14 19:53:24 +03:00
|
|
|
.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/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|