mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-15 22:15:09 +00:00
Adds simple diffing for records + updateRecords
This commit is contained in:
1 parent
fea85040ce
commit
281f88ea5f
3 files changed
+124
-110
No files matched your search
@@ -1,14 +1,22 @@
|
||||
const R = require('ramda');
|
||||
const { getDomainService, diffRecords } = require('../utils/domain-service');
|
||||
const {DOMAIN_DOMAIN} = require('../utils/constants');
|
||||
|
||||
const getCpanel = ({ zone, redir, setZone, setRedir } = {}) => ({
|
||||
addZoneRecord: (rec) => setZone(rec),
|
||||
addRedirection: (rec) => setRedir(rec),
|
||||
fetchZoneRecords: (_) => zone(),
|
||||
fetchRedirections: (_) => redir(),
|
||||
const getCpanel = ({ zone, addZone, editZone, redir, addRedir, editRedir } = {}) => ({
|
||||
zone: {
|
||||
fetch: (_) => zone(),
|
||||
add: (rec) => addZone(rec),
|
||||
edit: (rec) => editZone(rec),
|
||||
},
|
||||
redirection: {
|
||||
fetch: (_) => redir(),
|
||||
add: (rec) => addRedir(rec),
|
||||
edit: (rec) => editZone(rec),
|
||||
},
|
||||
});
|
||||
|
||||
describe('diffRecords', () => {
|
||||
return;
|
||||
it('should show added record', () => {
|
||||
const oldRecords = [
|
||||
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
|
||||
@@ -70,21 +78,28 @@ describe('diffRecords', () => {
|
||||
});
|
||||
|
||||
describe('Domain service', () => {
|
||||
const setZone = jest.fn(async () => ({}));
|
||||
const setRedir = jest.fn(async () => ({}));
|
||||
const addZone = jest.fn(async () => ({}));
|
||||
const editZone = jest.fn(async () => ({}));
|
||||
const addRedir = jest.fn(async () => ({}));
|
||||
const editRedir = jest.fn(async () => ({}));
|
||||
|
||||
const mockDS = ({ zones, redirections }) => getDomainService({ cpanel: getCpanel({
|
||||
zone: async () => ({ hosts: zones }),
|
||||
redir: async () => ({ hosts: redirections }),
|
||||
setZone,
|
||||
setRedir,
|
||||
zone: async () => zones,
|
||||
redir: async () => redirections,
|
||||
addZone,
|
||||
addRedir,
|
||||
editZone,
|
||||
editRedir,
|
||||
}) });
|
||||
|
||||
const getZoneCalls = () => setZone.mock.calls.map(R.head).map(R.pick(['name', 'type', 'address']));
|
||||
const getRecordCalls = recfn => recfn.mock.calls.map(R.head).map(R.pick(['name', 'type', 'address']));
|
||||
const getZoneCalls = () => getRecordCalls(addZone);
|
||||
|
||||
beforeEach(() => {
|
||||
setZone.mockClear();
|
||||
setRedir.mockClear();
|
||||
addZone.mockClear();
|
||||
editZone.mockClear();
|
||||
addRedir.mockClear();
|
||||
editRedir.mockClear();
|
||||
});
|
||||
|
||||
describe('getHosts', () => {
|
||||
@@ -137,19 +152,19 @@ describe('Domain service', () => {
|
||||
{ name: 'foo1', type: 'URL', address: 'https://duck.com' },
|
||||
];
|
||||
|
||||
const setZone = jest.fn(async () => {});
|
||||
const setRedir = jest.fn(async () => {});
|
||||
const addZone = jest.fn(async () => {});
|
||||
const addRedir = jest.fn(async () => {});
|
||||
|
||||
const mockDomainService = getDomainService({ cpanel: getCpanel({ setZone, setRedir }) });
|
||||
const mockDomainService = getDomainService({ cpanel: getCpanel({ addZone, addRedir }) });
|
||||
await mockDomainService.setHosts(records);
|
||||
|
||||
expect(setZone).toBeCalledTimes(2);
|
||||
expect(setRedir).toBeCalledTimes(2);
|
||||
expect(setZone.mock.calls.map(R.head)).toEqual([
|
||||
expect(addZone).toBeCalledTimes(2);
|
||||
expect(addRedir).toBeCalledTimes(2);
|
||||
expect(addZone.mock.calls.map(R.head)).toEqual([
|
||||
{ name: 'xx', type: 'CNAME', address: 'fck.com' },
|
||||
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
|
||||
]);
|
||||
expect(setRedir.mock.calls.map(R.head)).toEqual([
|
||||
expect(addRedir.mock.calls.map(R.head)).toEqual([
|
||||
{
|
||||
domain: 'foo.booboo.xyz',
|
||||
redirect: 'https://google.com',
|
||||
@@ -169,70 +184,65 @@ describe('Domain service', () => {
|
||||
});
|
||||
|
||||
describe('updateHosts', () => {
|
||||
return;
|
||||
|
||||
it('should append new hosts with existing ones and set it', async () => {
|
||||
const zones = [
|
||||
{ HostId: 1, name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ HostId: 2, name: 'b', type: 'CNAME', address: 'goo' },
|
||||
{ someid: 1, name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ someid: 2, name: 'b', type: 'CNAME', address: 'goo' },
|
||||
];
|
||||
const redirections = [];
|
||||
|
||||
const mockDomainService = mockDS({ zones, redirections });;
|
||||
const mockDomainService = mockDS({ zones, redirections });
|
||||
await mockDomainService.updateHosts([
|
||||
{ name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ name: 'b', type: 'CNAME', address: 'goo' },
|
||||
{ name: 'c', type: 'A', address: '12.131321.213' },
|
||||
]);
|
||||
|
||||
expect(setZone).toBeCalledTimes(1);
|
||||
expect(getZoneCalls()).toEqual([
|
||||
expect(addZone).toBeCalledTimes(1);
|
||||
expect(getRecordCalls(addZone)).toEqual([
|
||||
{ name: 'c', type: 'A', address: '12.131321.213' },
|
||||
]);
|
||||
expect(editZone).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
it('should update matching host and set it', async () => {
|
||||
const zones = [
|
||||
{ HostId: 1, Name: 'a', Type: 'CNAME', address: 'boo' },
|
||||
{ HostId: 2, Name: 'b', Type: 'CNAME', address: 'goo' },
|
||||
{ someid: 1, name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ someid: 2, name: 'b', type: 'CNAME', address: 'goo' },
|
||||
];
|
||||
const redirections = [];
|
||||
|
||||
const mockDomainService = mockDS({ zones, redirections });;
|
||||
const mockDomainService = mockDS({ zones, redirections });
|
||||
await mockDomainService.updateHosts([
|
||||
{ name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
|
||||
]);
|
||||
|
||||
expect(setZone).toBeCalledTimes(2);
|
||||
expect(getZoneCalls()).toEqual([
|
||||
{ name: 'a', type: 'CNAME', address: 'boo' },
|
||||
expect(addZone).toBeCalledTimes(0);
|
||||
expect(editZone).toBeCalledTimes(1);
|
||||
expect(getRecordCalls(editZone)).toEqual([
|
||||
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
|
||||
]);
|
||||
});
|
||||
return;
|
||||
|
||||
it('should update matching host and set it', async () => {
|
||||
const records = [
|
||||
{ HostId: 1, Name: 'a', Type: 'CNAME', address: 'boo' },
|
||||
{ HostId: 2, Name: 'b', Type: 'CNAME', address: 'goo' },
|
||||
{ HostId: 2, Name: 'b', Type: 'CNAME', address: 'xaa' },
|
||||
const zones = [
|
||||
{ someid: 1, name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ someid: 2, name: 'b', type: 'CNAME', address: 'goo' },
|
||||
{ someid: 2, name: 'b', type: 'CNAME', address: 'xaa' },
|
||||
];
|
||||
const redirections = [];
|
||||
|
||||
const onGet = () => Promise.resolve({ hosts: records });
|
||||
const onSet = jest.fn(async () => ({}));
|
||||
|
||||
const mockDomainService = getDomainService({ cpanel: getCpanel({ onSet, onGet }) });
|
||||
const mockDomainService = mockDS({ zones, redirections });
|
||||
await mockDomainService.updateHosts([
|
||||
{ name: 'a', type: 'CNAME', address: 'boo' },
|
||||
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
|
||||
{ name: 'b', type: 'CNAME', address: 'farboo' },
|
||||
]);
|
||||
|
||||
const [hosts] = onSet.mock.calls[0];
|
||||
|
||||
expect(hosts.map(R.pick(['name', 'type', 'address']))).toEqual([
|
||||
{ name: 'a', type: 'CNAME', address: 'boo' },
|
||||
expect(addZone).toBeCalledTimes(0);
|
||||
expect(editZone).toBeCalledTimes(2);
|
||||
expect(getRecordCalls(editZone)).toEqual([
|
||||
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
|
||||
{ name: 'b', type: 'CNAME', address: 'farboo' },
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user