mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-08-11 02:22:54 +00:00
Adds updateHosts function cases
This commit is contained in:
@@ -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
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user