From 9aeaeab812265faf8f74f0c718b50fa4b54d5035 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 4 Oct 2020 20:26:47 +0530 Subject: [PATCH] Adds updateHosts function cases --- tests/domain-service.test.js | 59 ++++++++++++++++++++++++++++++++++++ utils/domain-service.js | 23 +++++++------- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/tests/domain-service.test.js b/tests/domain-service.test.js index 982616f80..90a73bba4 100644 --- a/tests/domain-service.test.js +++ b/tests/domain-service.test.js @@ -40,5 +40,64 @@ describe('Domain service', () => { expect(onSet).toBeCalledTimes(1); }); }); + + describe('updateHosts', () => { + it('should append new hosts with existing ones and set it', async () => { + const records = [ + { HostId: 1, HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, + { HostId: 2, HostName: 'b', RecordType: 'CNAME', Address: 'goo' }, + ]; + + const onGet = () => Promise.resolve({ hosts: records }); + const onSet = jest.fn(async () => ({})); + + const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) }); + await mockDomainService.updateHosts([ + { HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, + { HostName: 'b', RecordType: 'CNAME', Address: 'goo' }, + { HostName: 'c', RecordType: 'A', Address: '12.131321.213' }, + ]); + + console.log(onSet.mock.calls); + //expect(onSet).toBeCalledTimes(1); + }); + it('should update matching host and set it', async () => { + const records = [ + { HostId: 1, HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, + { HostId: 2, HostName: 'b', RecordType: 'CNAME', Address: 'goo' }, + ]; + + const onGet = () => Promise.resolve({ hosts: records }); + const onSet = jest.fn(async () => ({})); + + const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) }); + await mockDomainService.updateHosts([ + { HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, + { HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' }, + ]); + + console.log(onSet.mock.calls); + //expect(onSet).toBeCalledTimes(1); + }); + it('should maintain existing entries on the server', async () => { + const records = [ + { HostId: 1, HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, + { HostId: 2, HostName: 'b', RecordType: 'CNAME', Address: 'goo' }, + { HostId: 3, HostName: 'c', RecordType: 'A', Address: '12.131321.213' }, + ]; + + const onGet = () => Promise.resolve({ hosts: records }); + const onSet = jest.fn(async () => ({})); + + const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) }); + await mockDomainService.updateHosts([ + { HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, + { HostName: 'b', RecordType: 'CNAME', Address: 'goo' }, + ]); + + console.log(onSet.mock.calls); + // expect(onSet).toBeCalledTimes(1); + }); + }); }); diff --git a/utils/domain-service.js b/utils/domain-service.js index 980b77047..031e12b47 100644 --- a/utils/domain-service.js +++ b/utils/domain-service.js @@ -18,8 +18,6 @@ const getDomainService = ({ Namecheap }) => { let hostList = []; - console.log(NC_USER, NC_DOMAIN, NC_API_KEY); - const getHosts = async () => { if (hostList.length) return hostList; @@ -39,30 +37,31 @@ const getDomainService = ({ Namecheap }) => { return list; }; - const setHosts = hosts => nc.dns.setHosts(NC_DOMAIN, hosts); + const setHosts = hosts => { + return nc.dns.setHosts(NC_DOMAIN, hosts); + }; - const findHost = async host => { + const findIndexHost = async host => { const list = await getHosts(); - const matchIndex = list.findIndex(R.whereEq({ + return list.findIndex(R.whereEq({ RecordType: host.RecordType, HostName: host.HostName, Address: host.Address, // MXPref: host.MXPref, TTL: host.TTL || TTL, })); - - return matchIndex; }; - const mergeHosts = async hosts => { - return hosts; - //const hostList = await getHosts(); - //hosts.map() + const updateHosts = async hosts => { + const hostList = await getHosts(); + hosts.map(host => { + // + }); // If source is bigger, merge all matching items and add new ones // If dest is bigger, merge all matching items and add missing ones }; - return { getHosts, setHosts, findHost }; + return { getHosts, setHosts, updateHosts }; } module.exports = {