Registerdomain tests

This commit is contained in:
Akshay Nair
2020-10-11 23:04:24 +05:30
parent 85e5181327
commit 44287b850e
2 changed files with 89 additions and 4 deletions
+3 -3
View File
@@ -18,19 +18,19 @@ const toHostList = R.chain(data => {
, rs);
});
const registerDomains = async ({ domainService, getDomains }) => {
const registerDomains = async ({ domainService, getDomains, log = () => {} }) => {
const domains = await getDomains().then(toHostList);
if (domains.length === 0)
return Promise.reject(new Error('Nothing to register'));
console.log(`Publishing ${domains.length} records...`);
log(`Publishing ${domains.length} records...`);
return domainService.updateHosts(domains);
};
const main = async () => {
console.log(`Running in ${ENV} mode`);
const result = await registerDomains({ domainService: dc, getDomains: gd });
const result = await registerDomains({ domainService: dc, getDomains: gd, log: console.log });
console.log(result);
};
+86 -1
View File
@@ -1,5 +1,20 @@
const R = require('ramda');
const { toHostList, registerDomains } = require('../scripts/register-domains');
const { TTL } = require('../utils/constants');
const { TTL, DOMAIN_DOMAIN } = require('../utils/constants');
const { getDomainService } = require('../utils/domain-service');
const getCpanel = ({ zone, addZone, editZone, redir, addRedir, editRedir } = {}) => ({
zone: {
fetch: (_) => zone(),
add: (rec) => addZone(rec),
edit: (rec) => editZone(rec),
},
redirection: {
fetch: (_) => redir(),
add: (rec) => addRedir(rec),
edit: (rec) => editRedir(rec),
},
});
describe('toHostList', () => {
it('should flatten domain data to list of hosts (without https)', () => {
@@ -19,3 +34,73 @@ describe('toHostList', () => {
});
});
describe('registerDomains', () => {
const addZone = jest.fn(async () => ({}));
const editZone = jest.fn(async () => ({}));
const addRedir = jest.fn(async () => ({}));
const editRedir = jest.fn(async () => ({}));
const mockDS = ({ zones, redirections }) => getDomainService({ cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addRedir,
editZone,
editRedir,
}) });
const getRecordCalls = recfn => recfn.mock.calls.map(R.head).map(R.pick(['name', 'type', 'address', 'redirect', 'domain']));
beforeEach(() => {
addZone.mockClear();
editZone.mockClear();
addRedir.mockClear();
editRedir.mockClear();
});
it('should register the new set of hosts generated from domains list', async () => {
const localHosts = [
{ name: 'a', record: { CNAME: 'hello' } },
{ name: 'b', record: { CNAME: 'xaa' } },
];
const remoteHosts = [
{ someididk: 1, name: 'a', type: 'CNAME', address: 'hello' },
{ someididk: 2, name: 'b', type: 'CNAME', address: 'goo' },
{ someididk: 2, name: 'b', type: 'CNAME', address: 'xaa' },
];
const remoteRedirections = [];
const domainService = mockDS({ zones: remoteHosts, redirections: remoteRedirections });
await registerDomains({ getDomains: async () => localHosts, domainService });
expect(addZone).toBeCalledTimes(0);
expect(editZone).toBeCalledTimes(0);
expect(addRedir).toBeCalledTimes(0);
expect(editRedir).toBeCalledTimes(0);
});
it('should add the new set hosts', async () => {
const localHosts = [
{ name: 'a', record: { CNAME: 'boo', URL: 'z' } },
{ name: 'b', record: { CNAME: 'xaa', URL: 'x' } },
{ name: 'c', record: { CNAME: 'yello', URL: 'https://google.com' } },
];
const remoteHosts = [
{ someididk: 1, name: 'a', type: 'CNAME', address: 'boo' },
{ someididk: 2, name: 'b', type: 'CNAME', address: 'xaa' },
];
const remoteRedirections = [
{ domain: `b.${DOMAIN_DOMAIN}`, destination: 'x' },
{ domain: `a.${DOMAIN_DOMAIN}`, destination: 'y' },
];
const domainService = mockDS({ zones: remoteHosts, redirections: remoteRedirections });
await registerDomains({ getDomains: async () => localHosts, domainService });
expect(addZone).toBeCalledTimes(1);
expect(editZone).toBeCalledTimes(0);
expect(addRedir).toBeCalledTimes(1);
expect(editRedir).toBeCalledTimes(1);
});
});