Files
cloudron-box/src/test/domains-test.js
T

151 lines
6.2 KiB
JavaScript
Raw Normal View History

2018-08-30 20:05:08 -07:00
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
2021-08-13 15:49:59 -07:00
const common = require('./common.js'),
2018-08-30 20:05:08 -07:00
domains = require('../domains.js'),
expect = require('expect.js'),
2021-03-15 12:47:57 -07:00
js2xml = require('js2xmlparser').parse,
2021-08-13 15:49:59 -07:00
nock = require('nock');
2021-03-15 12:47:57 -07:00
2018-08-30 20:05:08 -07:00
describe('Domains', function () {
2021-08-13 15:49:59 -07:00
const { setup, cleanup, app, domain } = common;
2018-08-30 20:05:08 -07:00
2021-08-13 15:49:59 -07:00
before(setup);
after(cleanup);
2018-08-30 20:05:08 -07:00
describe('validateHostname', function () {
it('does not allow admin subdomain', function () {
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);
});
});
2018-10-31 15:41:02 -07:00
describe('getName', function () {
2020-08-15 18:40:59 -07:00
it('works with zoneName==domain', function () {
2021-08-13 15:49:59 -07:00
const d = {
2018-10-31 15:41:02 -07:00
domain: 'example.com',
zoneName: 'example.com',
config: {}
};
2021-08-13 15:49:59 -07:00
expect(domains.getName(d, '', 'A')).to.be('');
expect(domains.getName(d, 'www', 'A')).to.be('www');
expect(domains.getName(d, 'www.dev', 'A')).to.be('www.dev');
2018-10-31 15:41:02 -07:00
2021-08-13 15:49:59 -07:00
expect(domains.getName(d, '', 'MX')).to.be('');
2018-10-31 15:41:02 -07:00
2021-08-13 15:49:59 -07:00
expect(domains.getName(d, '', 'TXT')).to.be('');
expect(domains.getName(d, 'www', 'TXT')).to.be('www');
expect(domains.getName(d, 'www.dev', 'TXT')).to.be('www.dev');
2018-10-31 15:41:02 -07:00
});
2020-08-15 18:40:59 -07:00
it('works when zoneName!=domain', function () {
2021-08-13 15:49:59 -07:00
const d = {
2018-10-31 15:41:02 -07:00
domain: 'dev.example.com',
zoneName: 'example.com',
config: {}
};
2021-08-13 15:49:59 -07:00
expect(domains.getName(d, '', 'A')).to.be('dev');
expect(domains.getName(d, 'www', 'A')).to.be('www.dev');
expect(domains.getName(d, 'www.dev', 'A')).to.be('www.dev.dev');
2018-10-31 15:41:02 -07:00
2021-08-13 15:49:59 -07:00
expect(domains.getName(d, '', 'MX')).to.be('dev');
2018-10-31 15:41:02 -07:00
2021-08-13 15:49:59 -07:00
expect(domains.getName(d, '', 'TXT')).to.be('dev');
expect(domains.getName(d, 'www', 'TXT')).to.be('www.dev');
expect(domains.getName(d, 'www.dev', 'TXT')).to.be('www.dev.dev');
2018-10-31 15:41:02 -07:00
});
});
2021-03-15 12:47:57 -07:00
2021-08-13 15:49:59 -07:00
describe('register', function () {
let awsHostedZones;
it('registers subdomain', function (done) {
awsHostedZones = {
HostedZones: [{
Id: '/hostedzone/ZONEID',
Name: `${domain.domain}.`,
CallerReference: '305AFD59-9D73-4502-B020-F4E6F889CB30',
ResourceRecordSetCount: 2,
ChangeInfo: {
Id: '/change/CKRTFJA0ANHXB',
Status: 'INSYNC'
}
}],
IsTruncated: false,
MaxItems: '100'
};
nock.cleanAll();
let awsScope = nock('http://localhost:5353')
.get('/2013-04-01/hostedzonesbyname?dnsname=example.com.&maxitems=1')
.times(2)
.reply(200, js2xml('ListHostedZonesResponse', awsHostedZones, { wrapHandlers: { HostedZones: () => 'HostedZone'} }))
.get('/2013-04-01/hostedzone/ZONEID/rrset?maxitems=1&name=applocation.' + domain.domain + '.&type=A')
.reply(200, js2xml('ListResourceRecordSetsResponse', { ResourceRecordSets: [ ] }, { 'Content-Type': 'application/xml' }))
.post('/2013-04-01/hostedzone/ZONEID/rrset/')
.reply(200, js2xml('ChangeResourceRecordSetsResponse', { ChangeInfo: { Id: 'RRID', Status: 'INSYNC' } }));
domains.registerLocations([ { subdomain: app.location, domain: app.domain } ], { overwriteDns: true }, (/*progress*/) => {}, function (error) {
expect(error).to.be(null);
expect(awsScope.isDone()).to.be.ok();
done();
});
2021-03-15 12:47:57 -07:00
});
2021-08-13 15:49:59 -07:00
it('unregisters subdomain', function (done) {
nock.cleanAll();
2021-03-15 12:47:57 -07:00
2021-08-13 15:49:59 -07:00
let awsScope = nock('http://localhost:5353')
.get('/2013-04-01/hostedzonesbyname?dnsname=example.com.&maxitems=1')
.reply(200, js2xml('ListHostedZonesResponse', awsHostedZones, { wrapHandlers: { HostedZones: () => 'HostedZone'} }))
.post('/2013-04-01/hostedzone/ZONEID/rrset/')
.reply(200, js2xml('ChangeResourceRecordSetsResponse', { ChangeInfo: { Id: 'RRID', Status: 'INSYNC' } }));
2021-03-15 12:47:57 -07:00
2021-08-13 15:49:59 -07:00
domains.unregisterLocations([ { subdomain: app.location, domain: app.domain } ], (/*progress*/) => {}, function (error) {
expect(error).to.be(null);
expect(awsScope.isDone()).to.be.ok();
done();
});
2021-03-15 12:47:57 -07:00
});
});
2018-08-30 20:05:08 -07:00
});