Refactors domain service

This commit is contained in:
Akshay Nair committed 2020-10-05 14:15:28 +05:30
1 parent 18430f296e
commit b170074efd
4 files changed
+36 -21

No files matched your search

+2
View File
@@ -20,6 +20,8 @@ const toHostList = R.chain(data => {
const registerDomains = async ({ domainService, getDomains }) => { const registerDomains = async ({ domainService, getDomains }) => {
const domains = await getDomains().then(toHostList); const domains = await getDomains().then(toHostList);
console.log(domains);
}; };
const main = () => { const main = () => {
+9 -9
View File
@@ -1,12 +1,12 @@
const R = require('ramda'); const R = require('ramda');
const { getDomainService } = require('../utils/domain-service'); const { getDomainService } = require('../utils/domain-service');
const getNcClass = ({ onSet, onGet } = {}) => class Namecheap { const getNc = ({ onSet, onGet } = {}) => ({
dns = { dns: {
setHosts: (_, list) => onSet(list), setHosts: (_, list) => onSet(list),
getHosts: (_) => onGet(), getHosts: (_) => onGet(),
}; },
}; });
describe('Domain service', () => { describe('Domain service', () => {
describe('getHosts', () => { describe('getHosts', () => {
@@ -16,7 +16,7 @@ describe('Domain service', () => {
{ Name: 'xx', Type: 'A', Address: '111.1.1212.1' }, { Name: 'xx', Type: 'A', Address: '111.1.1212.1' },
]; ];
const onGet = async () => ({ hosts }) const onGet = async () => ({ hosts })
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onGet }) }); const mockDomainService = getDomainService({ nc: getNc({ onGet }) });
const list = await mockDomainService.getHosts(); const list = await mockDomainService.getHosts();
expect(list).toEqual([ expect(list).toEqual([
@@ -35,7 +35,7 @@ describe('Domain service', () => {
return Promise.resolve(null); return Promise.resolve(null);
}); });
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet }) }); const mockDomainService = getDomainService({ nc: getNc({ onSet }) });
await mockDomainService.setHosts(records); await mockDomainService.setHosts(records);
expect(onSet).toBeCalledTimes(1); expect(onSet).toBeCalledTimes(1);
}); });
@@ -51,7 +51,7 @@ describe('Domain service', () => {
const onGet = () => Promise.resolve({ hosts: records }); const onGet = () => Promise.resolve({ hosts: records });
const onSet = jest.fn(async () => ({})); const onSet = jest.fn(async () => ({}));
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) }); const mockDomainService = getDomainService({ nc: getNc({ onSet, onGet }) });
await mockDomainService.updateHosts([ await mockDomainService.updateHosts([
{ HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, { HostName: 'a', RecordType: 'CNAME', Address: 'boo' },
{ HostName: 'b', RecordType: 'CNAME', Address: 'goo' }, { HostName: 'b', RecordType: 'CNAME', Address: 'goo' },
@@ -76,7 +76,7 @@ describe('Domain service', () => {
const onGet = () => Promise.resolve({ hosts: records }); const onGet = () => Promise.resolve({ hosts: records });
const onSet = jest.fn(async () => ({})); const onSet = jest.fn(async () => ({}));
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) }); const mockDomainService = getDomainService({ nc: getNc({ onSet, onGet }) });
await mockDomainService.updateHosts([ await mockDomainService.updateHosts([
{ HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, { HostName: 'a', RecordType: 'CNAME', Address: 'boo' },
{ HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' }, { HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' },
@@ -100,7 +100,7 @@ describe('Domain service', () => {
const onGet = () => Promise.resolve({ hosts: records }); const onGet = () => Promise.resolve({ hosts: records });
const onSet = jest.fn(async () => ({})); const onSet = jest.fn(async () => ({}));
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) }); const mockDomainService = getDomainService({ nc: getNc({ onSet, onGet }) });
await mockDomainService.updateHosts([ await mockDomainService.updateHosts([
{ HostName: 'a', RecordType: 'CNAME', Address: 'boo' }, { HostName: 'a', RecordType: 'CNAME', Address: 'boo' },
{ HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' }, { HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' },
+13 -1
View File
@@ -1,5 +1,6 @@
const { toHostList } = require('../scripts/register-domains'); const { toHostList, registerDomains } = require('../scripts/register-domains');
const { TTL } = require('../utils/constants'); const { TTL } = require('../utils/constants');
const { getDomainService } = require('../utils/domain-service');
describe('toHostList', () => { describe('toHostList', () => {
it('should flatten domain data to list of hosts (without https)', () => { it('should flatten domain data to list of hosts (without https)', () => {
@@ -18,3 +19,14 @@ describe('toHostList', () => {
]); ]);
}); });
}); });
describe('registerDomains', () => {
it('should register the new set of hosts generated from domains list', () => {
const localHosts = [];
const remoteHosts = [];
//const domainService = getDomainService({ Namecheap: });
//registerDomains({ getDomains: async () => localHosts, domainService });
});
});
+12 -11
View File
@@ -4,14 +4,7 @@ const { NC_DOMAIN, NC_USER, NC_API_KEY, ENV, IP_ADDRESS } = require('../utils/co
const IS_SANDBOX = ENV === 'sandbox'; const IS_SANDBOX = ENV === 'sandbox';
const getDomainService = ({ Namecheap }) => { const getDomainService = ({ nc }) => {
const nc = new Namecheap({
user: NC_USER,
key: NC_API_KEY,
ip: IP_ADDRESS,
sandbox: IS_SANDBOX,
});
let hostList = []; let hostList = [];
const getHosts = async () => { const getHosts = async () => {
@@ -57,10 +50,18 @@ const getDomainService = ({ Namecheap }) => {
}; };
return { getHosts, setHosts, updateHosts }; return { getHosts, setHosts, updateHosts };
} };
const nc = new Namecheap({
user: NC_USER,
key: NC_API_KEY,
ip: IP_ADDRESS,
sandbox: IS_SANDBOX,
});
const domainService = getDomainService({ nc });
module.exports = { module.exports = {
getDomainService, getDomainService,
domainService: getDomainService({ Namecheap }), domainService,
}; };