Move validateHostname to domains code

This commit is contained in:
Girish Ramakrishnan
2018-08-30 20:05:08 -07:00
parent 068113bd5d
commit 619d1e44e5
4 changed files with 130 additions and 88 deletions

View File

@@ -176,44 +176,6 @@ describe('Apps', function () {
], done);
});
describe('validateHostname', function () {
it('does not allow admin subdomain', function () {
expect(apps._validateHostname('my', DOMAIN_0.domain, 'my.' + DOMAIN_0.domain)).to.be.an(Error);
});
it('cannot have >63 length subdomains', function () {
var s = Array(64).fill('s').join('');
expect(apps._validateHostname(s, 'example.com', s + '.example.com')).to.be.an(Error);
expect(apps._validateHostname(`dev.${s}`, 'example.com', `dev.${s}.example.com`)).to.be.an(Error);
});
it('allows only alphanumerics and hypen', function () {
expect(apps._validateHostname('#2r', 'example.com', '#2r.example.com')).to.be.an(Error);
expect(apps._validateHostname('a%b', 'example.com', 'a%b.example.com')).to.be.an(Error);
expect(apps._validateHostname('ab_', 'example.com', 'ab_.example.com')).to.be.an(Error);
expect(apps._validateHostname('ab.', 'example.com', 'ab.example.com')).to.be.an(Error);
expect(apps._validateHostname('ab..c', 'example.com', 'ab..c.example.com')).to.be.an(Error);
expect(apps._validateHostname('.ab', 'example.com', '.ab.example.com')).to.be.an(Error);
expect(apps._validateHostname('-ab', 'example.com', '-ab.example.com')).to.be.an(Error);
expect(apps._validateHostname('ab-', 'example.com', 'ab-.example.com')).to.be.an(Error);
});
it('total length cannot exceed 255', function () {
var s = '';
for (var i = 0; i < (255 - 'example.com'.length); i++) s += 's';
expect(apps._validateHostname(s, 'example.com', s + '.example.com')).to.be.an(Error);
});
it('allow valid domains', function () {
expect(apps._validateHostname('a', 'example.com', 'a.example.com')).to.be(null);
expect(apps._validateHostname('a0-x', 'example.com', 'a0-x.example.com')).to.be(null);
expect(apps._validateHostname('a0.x', 'example.com', 'a0-x.example.com')).to.be(null);
expect(apps._validateHostname('a0.x.y', 'example.com', 'a0.x.y.example.com')).to.be(null);
expect(apps._validateHostname('01', 'example.com', '01.example.com')).to.be(null);
});
});
describe('validatePortBindings', function () {
it('does not allow invalid host port', function () {
expect(apps._validatePortBindings({ port: -1 }, { tcpPorts: { port: 5000 } })).to.be.an(Error);

78
src/test/domains-test.js Normal file
View File

@@ -0,0 +1,78 @@
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
var async = require('async'),
config = require('../config.js'),
database = require('../database.js'),
domains = require('../domains.js'),
expect = require('expect.js');
describe('Domains', function () {
before(function (done) {
config._reset();
async.series([
database.initialize,
database._clear
], done);
});
after(function (done) {
async.series([
database._clear,
database.uninitialize
], done);
});
let domain = {
domain: 'example.com',
zoneName: 'example.com',
config: {}
};
describe('validateHostname', function () {
it('does not allow admin subdomain', function () {
config.setFqdn('example.com');
config.setAdminFqdn('my.example.com');
expect(domains.validateHostname('my', domain)).to.be.an(Error);
});
it('cannot have >63 length subdomains', function () {
var s = Array(64).fill('s').join('');
expect(domains.validateHostname(s, domain)).to.be.an(Error);
domain.zoneName = `dev.${s}.example.com`;
expect(domains.validateHostname(`dev.${s}`, domain)).to.be.an(Error);
});
it('allows only alphanumerics and hypen', function () {
expect(domains.validateHostname('#2r', domain)).to.be.an(Error);
expect(domains.validateHostname('a%b', domain)).to.be.an(Error);
expect(domains.validateHostname('ab_', domain)).to.be.an(Error);
expect(domains.validateHostname('ab.', domain)).to.be.an(Error);
expect(domains.validateHostname('ab..c', domain)).to.be.an(Error);
expect(domains.validateHostname('.ab', domain)).to.be.an(Error);
expect(domains.validateHostname('-ab', domain)).to.be.an(Error);
expect(domains.validateHostname('ab-', domain)).to.be.an(Error);
});
it('total length cannot exceed 255', function () {
var s = '';
for (var i = 0; i < (255 - 'example.com'.length); i++) s += 's';
expect(domains.validateHostname(s, domain)).to.be.an(Error);
});
it('allow valid domains', function () {
expect(domains.validateHostname('a', domain)).to.be(null);
expect(domains.validateHostname('a0-x', domain)).to.be(null);
expect(domains.validateHostname('a0.x', domain)).to.be(null);
expect(domains.validateHostname('a0.x.y', domain)).to.be(null);
expect(domains.validateHostname('01', domain)).to.be(null);
});
});
});