merge domaindb.js into domains.js
This commit is contained in:
+94
-121
@@ -1,3 +1,4 @@
|
||||
/* jslint node:true */
|
||||
/* global it:false */
|
||||
/* global describe:false */
|
||||
/* global before:false */
|
||||
@@ -5,146 +6,118 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const common = require('./common.js'),
|
||||
const appdb = require('../appdb.js'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
common = require('./common.js'),
|
||||
domains = require('../domains.js'),
|
||||
expect = require('expect.js'),
|
||||
js2xml = require('js2xmlparser').parse,
|
||||
nock = require('nock');
|
||||
safe = require('safetydance'),
|
||||
util = require('util');
|
||||
|
||||
describe('Domains', function () {
|
||||
const { setup, cleanup, app, domain } = common;
|
||||
const { setup, cleanup, domain, app, auditSource } = common;
|
||||
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
describe('validateHostname', function () {
|
||||
it('does not allow admin subdomain', function () {
|
||||
expect(domains.validateHostname('my', domain)).to.be.an(Error);
|
||||
});
|
||||
const DOMAIN_0 = {
|
||||
domain: 'z0.com',
|
||||
zoneName: 'z0.com',
|
||||
provider: 'noop',
|
||||
config: { },
|
||||
fallbackCertificate: null,
|
||||
tlsConfig: {
|
||||
provider: 'fallback'
|
||||
},
|
||||
wellKnown: null
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
it('can add domain', async function () {
|
||||
await domains.add(DOMAIN_0.domain, DOMAIN_0, auditSource);
|
||||
});
|
||||
|
||||
describe('getName', function () {
|
||||
it('works with zoneName==domain', function () {
|
||||
const d = {
|
||||
domain: 'example.com',
|
||||
zoneName: 'example.com',
|
||||
config: {}
|
||||
};
|
||||
|
||||
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');
|
||||
|
||||
expect(domains.getName(d, '', 'MX')).to.be('');
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
it('works when zoneName!=domain', function () {
|
||||
const d = {
|
||||
domain: 'dev.example.com',
|
||||
zoneName: 'example.com',
|
||||
config: {}
|
||||
};
|
||||
|
||||
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');
|
||||
|
||||
expect(domains.getName(d, '', 'MX')).to.be('dev');
|
||||
|
||||
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');
|
||||
});
|
||||
it('cannot add same domain twice', async function () {
|
||||
const [error] = await safe(domains.add(DOMAIN_0.domain, DOMAIN_0, auditSource));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
|
||||
describe('register', function () {
|
||||
let awsHostedZones;
|
||||
it('can get domain', async function () {
|
||||
const result = await domains.get(DOMAIN_0.domain);
|
||||
expect(result.domain).to.equal(DOMAIN_0.domain);
|
||||
expect(result.zoneName).to.equal(DOMAIN_0.zoneName);
|
||||
expect(result.config).to.eql(DOMAIN_0.config);
|
||||
});
|
||||
|
||||
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'
|
||||
};
|
||||
it('cannot get non-existent domain', async function () {
|
||||
const result = await domains.get('random');
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
|
||||
nock.cleanAll();
|
||||
it('can update domain', async function () {
|
||||
const newConfig = {};
|
||||
const newTlsConfig = { provider: 'letsencrypt-staging' };
|
||||
const newDomain = Object.assign({}, DOMAIN_0, { config: newConfig, tlsConfig: newTlsConfig });
|
||||
|
||||
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' } }));
|
||||
await domains.update(DOMAIN_0.domain, newDomain, auditSource);
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
const result = await domains.get(DOMAIN_0.domain);
|
||||
expect(result.domain).to.equal(DOMAIN_0.domain);
|
||||
expect(result.zoneName).to.equal(DOMAIN_0.zoneName);
|
||||
expect(result.provider).to.equal(DOMAIN_0.provider);
|
||||
expect(result.config).to.eql(newConfig);
|
||||
expect(result.tlsConfig).to.eql(newTlsConfig);
|
||||
|
||||
it('unregisters subdomain', function (done) {
|
||||
nock.cleanAll();
|
||||
DOMAIN_0.config = newConfig;
|
||||
DOMAIN_0.tlsConfig = newTlsConfig;
|
||||
});
|
||||
|
||||
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' } }));
|
||||
it('can get all domains', async function () {
|
||||
const result = await domains.list();
|
||||
expect(result.length).to.equal(2);
|
||||
|
||||
domains.unregisterLocations([ { subdomain: app.location, domain: app.domain } ], (/*progress*/) => {}, function (error) {
|
||||
expect(error).to.be(null);
|
||||
expect(awsScope.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
// sorted by domain
|
||||
expect(result[0].domain).to.equal(domain.domain);
|
||||
expect(result[0].zoneName).to.equal(domain.zoneName);
|
||||
expect(result[0].provider).to.equal(domain.provider);
|
||||
expect(result[0].config).to.eql(domain.config);
|
||||
expect(result[0].tlsConfig).to.eql(domain.tlsConfig);
|
||||
|
||||
expect(result[1].domain).to.equal(DOMAIN_0.domain);
|
||||
expect(result[1].zoneName).to.equal(DOMAIN_0.zoneName);
|
||||
expect(result[1].provider).to.equal(DOMAIN_0.provider);
|
||||
expect(result[1].config).to.eql(DOMAIN_0.config);
|
||||
expect(result[1].tlsConfig).to.eql(DOMAIN_0.tlsConfig);
|
||||
});
|
||||
|
||||
it('cannot delete non-existing domain', async function () {
|
||||
const [error] = await safe(domains.del('not.exists', auditSource));
|
||||
expect(error).to.be.a(BoxError);
|
||||
expect(error.reason).to.equal(BoxError.NOT_FOUND);
|
||||
});
|
||||
|
||||
it('cannot delete dashboard domain', async function () {
|
||||
const [error] = await safe(domains.del(domain.domain, auditSource));
|
||||
expect(error).to.be.a(BoxError);
|
||||
expect(error.reason).to.equal(BoxError.CONFLICT);
|
||||
expect(error.message).to.equal('Cannot remove admin domain');
|
||||
});
|
||||
|
||||
it('cannot delete referenced domain', async function () {
|
||||
const appCopy = Object.assign({}, app, { id: 'into', location: 'xx', domain: DOMAIN_0.domain, portBindings: {} });
|
||||
|
||||
await util.promisify(appdb.add)(appCopy.id, appCopy.appStoreId, appCopy.manifest, appCopy.location, appCopy.domain, appCopy.portBindings, appCopy);
|
||||
|
||||
const [error] = await safe(domains.del(DOMAIN_0.domain, auditSource));
|
||||
expect(error.reason).to.equal(BoxError.CONFLICT);
|
||||
expect(error.message).to.contain('Domain is in use by one or more app');
|
||||
|
||||
await util.promisify(appdb.del)(appCopy.id);
|
||||
});
|
||||
|
||||
it('can delete existing domain', async function () {
|
||||
await domains.del(DOMAIN_0.domain, auditSource);
|
||||
|
||||
const result = await domains.get(DOMAIN_0.domain);
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user