Adds updateHosts function cases

This commit is contained in:
Akshay Nair
2020-10-04 20:27:36 +05:30
parent d01aba314b
commit 9aeaeab812
2 changed files with 70 additions and 12 deletions
+59
View File
@@ -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);
});
});
});
+11 -12
View File
@@ -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 = {