mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-15 06:16:09 +00:00
Aggregates fetching of records from zone and redirections
This commit is contained in:
1 parent
fa9ad8baef
commit
49e8dd7fd0
3 files changed
+62
-23
No files matched your search
@@ -1,20 +1,24 @@
|
|||||||
const R = require('ramda');
|
const R = require('ramda');
|
||||||
const { getDomainService } = require('../utils/domain-service');
|
const { getDomainService } = require('../utils/domain-service');
|
||||||
|
|
||||||
const getCpanel = ({ onSet, onGet } = {}) => ({
|
const getCpanel = ({ zone, redir, setZone, setRedir } = {}) => ({
|
||||||
addZoneRecord: (host) => onSet(host),
|
addZoneRecord: (host) => setZone(host),
|
||||||
fetchZoneRecords: (_) => onGet(),
|
addRedirection: (_) => setRedir(),
|
||||||
|
fetchZoneRecords: (_) => zone(),
|
||||||
|
fetchRedirections: (_) => redir(),
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Domain service', () => {
|
describe('Domain service', () => {
|
||||||
describe('getHosts', () => {
|
describe('getHosts', () => {
|
||||||
it('should resolve with a list of hosts', async () => {
|
it('should resolve with a list of hosts', async () => {
|
||||||
const hosts = [
|
const zones = [
|
||||||
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
|
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
|
||||||
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
||||||
];
|
];
|
||||||
const onGet = async () => hosts;
|
const redirections = [];
|
||||||
const mockDomainService = getDomainService({ cpanel: getCpanel({ onGet }) });
|
const zone = async () => zones;
|
||||||
|
const redir = async () => redirections;
|
||||||
|
const mockDomainService = getDomainService({ cpanel: getCpanel({ zone, redir }) });
|
||||||
const list = await mockDomainService.getHosts();
|
const list = await mockDomainService.getHosts();
|
||||||
|
|
||||||
expect(list).toEqual([
|
expect(list).toEqual([
|
||||||
@@ -22,23 +26,45 @@ describe('Domain service', () => {
|
|||||||
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('setHosts', () => {
|
it('should resolve with a redirections', async () => {
|
||||||
it('should resolve with a list of hosts', async () => {
|
const zones = [
|
||||||
const records = [ { x: 'y' }, { z: 'a' } ];
|
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
|
||||||
|
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
||||||
|
];
|
||||||
|
const redirections = [
|
||||||
|
{ domain: 'foo.booboo.xyz', destination: 'https://google.com' },
|
||||||
|
{ domain: 'foo1.booboo.xyz', destination: 'https://duck.com' },
|
||||||
|
];
|
||||||
|
const zone = async () => zones;
|
||||||
|
const redir = async () => redirections;
|
||||||
|
const mockDomainService = getDomainService({ cpanel: getCpanel({ zone, redir }) });
|
||||||
|
const list = await mockDomainService.getHosts();
|
||||||
|
|
||||||
const onSet = jest.fn(async () => {});
|
expect(list).toEqual([
|
||||||
|
{ name: 'xx', type: 'CNAME', address: 'fck.com' },
|
||||||
const mockDomainService = getDomainService({ cpanel: getCpanel({ onSet }) });
|
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
||||||
await mockDomainService.setHosts(records);
|
{ name: 'foo', type: 'URL', address: 'https://google.com' },
|
||||||
|
{ name: 'foo1', type: 'URL', address: 'https://duck.com' },
|
||||||
expect(onSet).toBeCalledTimes(2);
|
]);
|
||||||
expect(onSet.mock.calls.map(R.head)).toEqual([ { x: 'y' }, { z: 'a' } ]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
describe('setHosts', () => {
|
||||||
|
it('should resolve with a list of hosts', async () => {
|
||||||
|
const records = [ { x: 'y' }, { z: 'a' } ];
|
||||||
|
|
||||||
|
const setZone = jest.fn(async () => {});
|
||||||
|
|
||||||
|
const mockDomainService = getDomainService({ cpanel: getCpanel({ setZone }) });
|
||||||
|
await mockDomainService.setHosts(records);
|
||||||
|
|
||||||
|
expect(setZone).toBeCalledTimes(2);
|
||||||
|
expect(setZone.mock.calls.map(R.head)).toEqual([ { x: 'y' }, { z: 'a' } ]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('updateHosts', () => {
|
describe('updateHosts', () => {
|
||||||
it('should append new hosts with existing ones and set it', async () => {
|
it('should append new hosts with existing ones and set it', async () => {
|
||||||
|
|||||||
+15
-5
@@ -1,19 +1,29 @@
|
|||||||
const R = require('ramda');
|
const R = require('ramda');
|
||||||
const { cpanel } = require('./lib/cpanel');
|
const { cpanel } = require('./lib/cpanel');
|
||||||
|
const {DOMAIN_DOMAIN} = require('./constants');
|
||||||
|
|
||||||
const flattenPromise = xs => Promise.all(xs);
|
const flattenPromise = xs => Promise.all(xs);
|
||||||
|
|
||||||
const getDomainService = ({ cpanel }) => {
|
const getDomainService = ({ cpanel }) => {
|
||||||
let hostList = [];
|
let hostList = [];
|
||||||
|
|
||||||
|
const fetchZoneRecords = () => cpanel.fetchZoneRecords().then(R.map(host => ({
|
||||||
|
...host,
|
||||||
|
name: `${host.name}`,
|
||||||
|
type: `${host.type}`,
|
||||||
|
address: `${host.cname || host.address}`.replace(/\.$/g, ''),
|
||||||
|
})));
|
||||||
|
|
||||||
|
const fetchRedirections = () => cpanel.fetchRedirections().then(R.map(host => ({
|
||||||
|
name: `${host.domain}`.replace('.' + DOMAIN_DOMAIN, ''),
|
||||||
|
type: 'URL',
|
||||||
|
address: `${host.destination}`,
|
||||||
|
})));
|
||||||
|
|
||||||
const getHosts = async () => {
|
const getHosts = async () => {
|
||||||
if (hostList.length) return hostList;
|
if (hostList.length) return hostList;
|
||||||
|
|
||||||
const list = await cpanel.fetchZoneRecords()
|
const list = await Promise.all([fetchZoneRecords(), fetchRedirections()]).then(R.flatten);
|
||||||
.then(R.map(host => R.omit(['Name', 'Type'], {
|
|
||||||
...host,
|
|
||||||
address: `${host.cname || host.address}`.replace(/\.$/g, ''),
|
|
||||||
})));
|
|
||||||
|
|
||||||
hostList = list;
|
hostList = list;
|
||||||
return list;
|
return list;
|
||||||
|
|||||||
+4
-1
@@ -51,7 +51,10 @@ const CpanelClient = (options) => {
|
|||||||
|
|
||||||
// {}
|
// {}
|
||||||
// -> { }
|
// -> { }
|
||||||
fetchRedirections: uapi('Mime', 'list_redirects'),
|
fetchRedirections: R.compose(
|
||||||
|
p => p.then(R.pathOr([], ['data'])),
|
||||||
|
uapi('Mime', 'list_redirects'),
|
||||||
|
),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user