improvement: validate timezones (#7143)

no issue
- add isTimezone to schema validation
This commit is contained in:
Katharina Irrgang 2016-07-26 11:23:20 +02:00 committed by Kevin Ansfield
parent 417b9b6b7c
commit c9dc367bf0
3 changed files with 37 additions and 0 deletions

View File

@ -49,6 +49,7 @@
"activeTimezone": {
"defaultValue": "Etc/UTC",
"validations": {
"isTimezone": true,
"isNull": false
}
},

View File

@ -1,6 +1,7 @@
var schema = require('../schema').tables,
_ = require('lodash'),
validator = require('validator'),
moment = require('moment'),
assert = require('assert'),
Promise = require('bluebird'),
errors = require('../../errors'),
@ -37,6 +38,10 @@ validator.extend('notContains', function notContains(str, badString) {
return !_.includes(str, badString);
});
validator.extend('isTimezone', function isTimezone(str) {
return moment.tz.zone(str) ? true : false;
});
validator.extend('isEmptyOrURL', function isEmptyOrURL(str) {
return (_.isEmpty(str) || validator.isURL(str, {require_protocol: false}));
});

View File

@ -194,4 +194,35 @@ describe('Settings API', function () {
done();
}).catch(done);
});
it('set activeTimezone: unknown timezone', function (done) {
return callApiWithContext(defaultContext, 'edit', {settings: [{key: 'activeTimezone', value: 'MFG'}]}, {})
.then(function () {
done(new Error('We expect that the activeTimezone cannot be stored'));
}).catch(function (errors) {
should.exist(errors);
errors.length.should.eql(1);
errors[0].errorType.should.eql('ValidationError');
done();
}).catch(done);
});
it('set activeTimezone: unknown timezone', function (done) {
return callApiWithContext(defaultContext, 'edit', {settings: [{key: 'activeTimezone', value: 'MFG'}]}, {})
.then(function () {
done(new Error('We expect that the activeTimezone cannot be stored'));
}).catch(function (errors) {
should.exist(errors);
errors.length.should.eql(1);
errors[0].errorType.should.eql('ValidationError');
done();
}).catch(done);
});
it('set activeTimezone: known timezone', function (done) {
return callApiWithContext(defaultContext, 'edit', {settings: [{key: 'activeTimezone', value: 'Etc/UTC'}]}, {})
.then(function () {
done();
}).catch(done);
});
});