From a9ba56908da4f9255d57c9d8a9f75d993346ba63 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 5 Oct 2020 14:33:37 +0530 Subject: [PATCH] Adds registerDomains function and all its test cases --- scripts/register-domains.js | 2 +- tests/register.test.js | 108 ++++++++++++++++++++++++++++++++++-- 2 files changed, 104 insertions(+), 6 deletions(-) diff --git a/scripts/register-domains.js b/scripts/register-domains.js index 784e36388..755a4e8be 100644 --- a/scripts/register-domains.js +++ b/scripts/register-domains.js @@ -21,7 +21,7 @@ const toHostList = R.chain(data => { const registerDomains = async ({ domainService, getDomains }) => { const domains = await getDomains().then(toHostList); - console.log(domains); + await domainService.updateHosts(domains); }; const main = () => { diff --git a/tests/register.test.js b/tests/register.test.js index 1b515a5cd..23d1ffd43 100644 --- a/tests/register.test.js +++ b/tests/register.test.js @@ -2,6 +2,13 @@ const { toHostList, registerDomains } = require('../scripts/register-domains'); const { TTL } = require('../utils/constants'); const { getDomainService } = require('../utils/domain-service'); +const getNc = ({ onSet, onGet } = {}) => ({ + dns: { + setHosts: (_, list) => onSet(list), + getHosts: (_) => onGet(), + }, +}); + describe('toHostList', () => { it('should flatten domain data to list of hosts (without https)', () => { const res = toHostList([ @@ -21,12 +28,103 @@ describe('toHostList', () => { }); describe('registerDomains', () => { - it('should register the new set of hosts generated from domains list', () => { - const localHosts = []; - const remoteHosts = []; + 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 = [ + { HostId: 1, Name: 'a', Type: 'CNAME', Address: 'boo' }, + { HostId: 2, Name: 'b', Type: 'CNAME', Address: 'goo' }, + { HostId: 2, Name: 'b', Type: 'CNAME', Address: 'xaa' }, + ]; - //const domainService = getDomainService({ Namecheap: }); - //registerDomains({ getDomains: async () => localHosts, domainService }); + const onSet = jest.fn(async () => ({})); + + const domainService = getDomainService({ nc: getNc({ onSet, onGet: async () => ({ hosts: remoteHosts }) }) }); + await registerDomains({ getDomains: async () => localHosts, domainService }); + + expect(onSet).toBeCalledTimes(1); + + const [hosts] = onSet.mock.calls[0]; + expect(hosts).toEqual([ + { HostId: 1, Address: 'hello', HostName: 'a', RecordType: 'CNAME', TTL }, + { HostId: 2, Address: 'xaa', HostName: 'b', RecordType: 'CNAME', TTL }, + ]); + }); + + it('should add the new set hosts', async () => { + const localHosts = [ + { name: 'a', record: { CNAME: 'boo' } }, + { name: 'b', record: { CNAME: 'xaa' } }, + { name: 'c', record: { CNAME: 'yello' } }, + ]; + const remoteHosts = [ + { HostId: 1, Name: 'a', Type: 'CNAME', Address: 'boo' }, + { HostId: 2, Name: 'b', Type: 'CNAME', Address: 'xaa' }, + ]; + + const onSet = jest.fn(async () => ({})); + + const domainService = getDomainService({ nc: getNc({ onSet, onGet: async () => ({ hosts: remoteHosts }) }) }); + await registerDomains({ getDomains: async () => localHosts, domainService }); + + expect(onSet).toBeCalledTimes(1); + + const [hosts] = onSet.mock.calls[0]; + expect(hosts).toEqual([ + { HostId: 1, Address: 'boo', HostName: 'a', RecordType: 'CNAME', TTL }, + { HostId: 2, Address: 'xaa', HostName: 'b', RecordType: 'CNAME', TTL }, + { Address: 'yello', HostName: 'c', RecordType: 'CNAME', TTL }, + ]); + }); + + it('should remove unlisted hosts', async () => { + const localHosts = [ + { name: 'a', record: { CNAME: 'boo' } }, + ]; + const remoteHosts = [ + { HostId: 1, Name: 'a', Type: 'CNAME', Address: 'boo' }, + { HostId: 2, Name: 'b', Type: 'CNAME', Address: 'xaa' }, + ]; + + const onSet = jest.fn(async () => ({})); + + const domainService = getDomainService({ nc: getNc({ onSet, onGet: async () => ({ hosts: remoteHosts }) }) }); + await registerDomains({ getDomains: async () => localHosts, domainService }); + + expect(onSet).toBeCalledTimes(1); + + const [hosts] = onSet.mock.calls[0]; + expect(hosts).toEqual([ + { HostId: 1, Address: 'boo', HostName: 'a', RecordType: 'CNAME', TTL }, + ]); + }); + + it('should change record type from cname to a', async () => { + const localHosts = [ + { name: 'a', record: { CNAME: 'boo' } }, + { name: 'b', record: { A: ['1', '2', '3'] } }, + ]; + const remoteHosts = [ + { HostId: 1, Name: 'a', Type: 'CNAME', Address: 'boo' }, + { HostId: 2, Name: 'b', Type: 'CNAME', Address: 'xaa' }, + ]; + + const onSet = jest.fn(async () => ({})); + + const domainService = getDomainService({ nc: getNc({ onSet, onGet: async () => ({ hosts: remoteHosts }) }) }); + await registerDomains({ getDomains: async () => localHosts, domainService }); + + expect(onSet).toBeCalledTimes(1); + + const [hosts] = onSet.mock.calls[0]; + expect(hosts).toEqual([ + { HostId: 1, Address: 'boo', HostName: 'a', RecordType: 'CNAME', TTL }, + { Address: '1', HostName: 'b', RecordType: 'A', TTL }, + { Address: '2', HostName: 'b', RecordType: 'A', TTL }, + { Address: '3', HostName: 'b', RecordType: 'A', TTL }, + ]); }); });