1 letter subdomains + prettier

This commit is contained in:
William Harrison committed 2024-07-29 08:34:45 +08:00
1 parent a7c625d278
commit 5163935340
16 files changed
+1552 -1281

No files matched your search

+190 -175
View File
@@ -1,195 +1,210 @@
const R = require('ramda');
const { CpanelClient } = require('../utils/lib/cpanel');
const R = require("ramda");
const { CpanelClient } = require("../utils/lib/cpanel");
const mockFetch = (expectRequest, decorate = R.identity) => (reqUrl, request) => {
expectRequest(reqUrl, request);
return Promise.resolve({
json: async () => decorate(request),
});
};
describe('Cpanel client', () => {
describe('fetchzonerecords', () => {
it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/json-api/cpanel?customonly=0&domain=a.b&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
const mockFetch =
(expectRequest, decorate = R.identity) =>
(reqUrl, request) => {
expectRequest(reqUrl, request);
return Promise.resolve({
json: async () => decorate(request),
});
});
};
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
describe("Cpanel client", () => {
describe("fetchzonerecords", () => {
it("should make the correct request", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/json-api/cpanel?customonly=0&domain=a.b&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
await cpanel.zone.fetch();
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.zone.fetch();
});
it("should make the correct request with query", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/json-api/cpanel?customonly=0&domain=foobar.boeey&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.zone.fetch({ domain: "foobar.boeey" });
});
});
it('should make the correct request with query', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/json-api/cpanel?customonly=0&domain=foobar.boeey&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=fetchzone_records&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
describe("addzonerecord", () => {
it("should make the correct request", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/json-api/cpanel?domain=a.b&name=googo&type=CNAME&cname=beey&ttl=2020&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=add_zone_record&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.zone.add({
name: "googo",
type: "boyee",
cname: "beey",
type: "CNAME",
ttl: 2020,
});
});
});
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
await cpanel.zone.fetch({ domain: 'foobar.boeey' });
});
});
describe('addzonerecord', () => {
it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/json-api/cpanel?domain=a.b&name=googo&type=CNAME&cname=beey&ttl=2020&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=add_zone_record&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
describe("addzonerecord", () => {
it("should make the correct request", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/json-api/cpanel?domain=a.b&line=500&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=remove_zone_record&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.zone.remove({
line: 500,
});
});
});
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
await cpanel.zone.add({
name: 'googo',
type: 'boyee',
cname: 'beey',
type: 'CNAME',
ttl: 2020,
});
});
});
describe('addzonerecord', () => {
it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/json-api/cpanel?domain=a.b&line=500&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=ZoneEdit&cpanel_jsonapi_func=remove_zone_record&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
describe("fetchredirections", () => {
it("should make the correct request", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/execute/Mime/list_redirects?cpanel_jsonapi_user=boy&cpanel_jsonapi_module=Mime&cpanel_jsonapi_func=list_redirects&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.redirection.fetch();
});
});
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
await cpanel.zone.remove({
line: 500,
});
});
});
describe("addredirection", () => {
it("should make the correct request", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/execute/Mime/add_redirect?domain=googo&destination=https%3A%2F%2Foodf.com&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=Mime&cpanel_jsonapi_func=add_redirect&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
describe('fetchredirections', () => {
it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/execute/Mime/list_redirects?cpanel_jsonapi_user=boy&cpanel_jsonapi_module=Mime&cpanel_jsonapi_func=list_redirects&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.redirection.add({
domain: "googo",
destination: "https://oodf.com",
});
});
});
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
await cpanel.redirection.fetch();
});
});
describe('addredirection', () => {
it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/execute/Mime/add_redirect?domain=googo&destination=https%3A%2F%2Foodf.com&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=Mime&cpanel_jsonapi_func=add_redirect&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
describe("deleteredirection", () => {
it("should make the correct request", async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe(
"https://example.com:2000/execute/Mime/delete_redirect?domain=googo&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=Mime&cpanel_jsonapi_func=delete_redirect&cpanel_jsonapi_apiversion=2",
);
expect(request).toEqual({
headers: {
Authorization: "cpanel boy:boybyebye",
},
rejectUnauthorized: false,
});
});
const cpanel = CpanelClient({
host: "example.com",
port: 2000,
username: "boy",
apiKey: "boybyebye",
domain: "a.b",
dependencies: { fetch },
});
await cpanel.redirection.remove({ domain: "googo" });
});
});
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
await cpanel.redirection.add({
domain: 'googo',
destination: 'https://oodf.com'
});
});
});
describe('deleteredirection', () => {
it('should make the correct request', async () => {
const fetch = mockFetch((url, request) => {
expect(url).toBe('https://example.com:2000/execute/Mime/delete_redirect?domain=googo&cpanel_jsonapi_user=boy&cpanel_jsonapi_module=Mime&cpanel_jsonapi_func=delete_redirect&cpanel_jsonapi_apiversion=2');
expect(request).toEqual({
headers: {
Authorization: 'cpanel boy:boybyebye',
},
rejectUnauthorized: false,
});
});
const cpanel = CpanelClient({
host: 'example.com',
port: 2000,
username: 'boy',
apiKey: 'boybyebye',
domain: 'a.b',
dependencies: { fetch },
});
await cpanel.redirection.remove({ domain: 'googo' });
});
});
});
+391 -314
View File
@@ -1,344 +1,421 @@
const R = require('ramda');
const { getDomainService, diffRecords } = require('../utils/domain-service');
const { DOMAIN_DOMAIN } = require('../utils/constants');
const R = require("ramda");
const { getDomainService, diffRecords } = require("../utils/domain-service");
const { DOMAIN_DOMAIN } = require("../utils/constants");
const getCpanel = ({ zone, addZone, removeZone, redir, addRedir, removeRedir, addEmail, removeEmail } = {}) => ({
zone: {
fetch: (_) => zone(),
add: (rec) => addZone(rec),
remove: (rec) => removeZone(rec),
},
redirection: {
fetch: (_) => redir(),
add: (rec) => addRedir(rec),
remove: (rec) => removeRedir(rec),
},
email: {
add: (rec) => addEmail(rec),
remove: (rec) => removeEmail(rec),
},
const getCpanel = ({
zone,
addZone,
removeZone,
redir,
addRedir,
removeRedir,
addEmail,
removeEmail,
} = {}) => ({
zone: {
fetch: (_) => zone(),
add: (rec) => addZone(rec),
remove: (rec) => removeZone(rec),
},
redirection: {
fetch: (_) => redir(),
add: (rec) => addRedir(rec),
remove: (rec) => removeRedir(rec),
},
email: {
add: (rec) => addEmail(rec),
remove: (rec) => removeEmail(rec),
},
});
describe('diffRecords', () => {
it('should show added record', () => {
const oldRecords = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xa', type: 'A', address: '111.1.1212.1' },
];
const newRecords = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xa', type: 'A', address: '111.1.1212.1' },
{ name: 'boo', type: 'CNAME', address: 'x.com' },
];
describe("diffRecords", () => {
it("should show added record", () => {
const oldRecords = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xa", type: "A", address: "111.1.1212.1" },
];
const newRecords = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xa", type: "A", address: "111.1.1212.1" },
{ name: "boo", type: "CNAME", address: "x.com" },
];
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [],
add: [
{ name: 'boo', type: 'CNAME', address: 'x.com' },
],
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [],
add: [{ name: "boo", type: "CNAME", address: "x.com" }],
});
});
});
it('should show edited records', () => {
const oldRecords = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xa', type: 'A', address: '111.1.1212.1' },
];
const newRecords = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xa', type: 'A', address: '69.69.69.69' },
];
it("should show edited records", () => {
const oldRecords = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xa", type: "A", address: "111.1.1212.1" },
];
const newRecords = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xa", type: "A", address: "69.69.69.69" },
];
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [
{ name: 'xa', type: 'A', address: '111.1.1212.1' },
],
add: [
{ name: 'xa', type: 'A', address: '69.69.69.69' },
],
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [{ name: "xa", type: "A", address: "111.1.1212.1" }],
add: [{ name: "xa", type: "A", address: "69.69.69.69" }],
});
});
});
it('should show added records with the same name and record type', () => {
const oldRecords = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xa', type: 'A', address: '69.69.69.69' },
];
const newRecords = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xa', type: 'A', address: '69.69.69.69' },
{ name: 'xa', type: 'A', address: '69.69.4.20' },
];
it("should show added records with the same name and record type", () => {
const oldRecords = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xa", type: "A", address: "69.69.69.69" },
];
const newRecords = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xa", type: "A", address: "69.69.69.69" },
{ name: "xa", type: "A", address: "69.69.4.20" },
];
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [],
add: [
{ name: 'xa', type: 'A', address: '69.69.4.20' },
],
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [],
add: [{ name: "xa", type: "A", address: "69.69.4.20" }],
});
});
});
it('should diff complex changes', () => {
const oldRecords = [
{ name: 'a', type: 'CNAME', address: 'fck.com.' },
{ name: 'b', type: 'A', address: '69.69.69.69' },
{ name: '111', type: 'CNAME', address: 'x' },
{ name: 'd', type: 'A', address: '69.69.4.20' },
];
const newRecords = [
{ name: '111', type: 'CNAME', address: 'x' },
{ name: 'd', type: 'CNAME', address: 'duck.com' },
{ name: 'a', type: 'CNAME', address: 'og.com' },
{ name: 'b', type: 'A', address: '69.69.69.69' },
{ name: 'b', type: 'A', address: '69.69.4.20' },
{ name: 'c', type: 'CNAME', address: 'ccc.cc' },
];
it("should diff complex changes", () => {
const oldRecords = [
{ name: "a", type: "CNAME", address: "fck.com." },
{ name: "b", type: "A", address: "69.69.69.69" },
{ name: "111", type: "CNAME", address: "x" },
{ name: "d", type: "A", address: "69.69.4.20" },
];
const newRecords = [
{ name: "111", type: "CNAME", address: "x" },
{ name: "d", type: "CNAME", address: "duck.com" },
{ name: "a", type: "CNAME", address: "og.com" },
{ name: "b", type: "A", address: "69.69.69.69" },
{ name: "b", type: "A", address: "69.69.4.20" },
{ name: "c", type: "CNAME", address: "ccc.cc" },
];
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [
{ name: 'a', type: 'CNAME', address: 'fck.com.' },
{ name: 'd', type: 'A', address: '69.69.4.20' },
],
add: [
{ name: 'd', type: 'CNAME', address: 'duck.com' },
{ name: 'a', type: 'CNAME', address: 'og.com' },
{ name: 'b', type: 'A', address: '69.69.4.20' },
{ name: 'c', type: 'CNAME', address: 'ccc.cc' },
],
const result = diffRecords(oldRecords, newRecords);
expect(result).toEqual({
remove: [
{ name: "a", type: "CNAME", address: "fck.com." },
{ name: "d", type: "A", address: "69.69.4.20" },
],
add: [
{ name: "d", type: "CNAME", address: "duck.com" },
{ name: "a", type: "CNAME", address: "og.com" },
{ name: "b", type: "A", address: "69.69.4.20" },
{ name: "c", type: "CNAME", address: "ccc.cc" },
],
});
});
});
});
describe('Domain service', () => {
const addZone = jest.fn(async () => ({}));
const removeZone = jest.fn(async () => ({}));
const addRedir = jest.fn(async () => ({}));
const removeRedir = jest.fn(async () => ({}));
const addEmail = jest.fn(async () => ({}));
const removeEmail = jest.fn(async () => ({}));
describe("Domain service", () => {
const addZone = jest.fn(async () => ({}));
const removeZone = jest.fn(async () => ({}));
const addRedir = jest.fn(async () => ({}));
const removeRedir = jest.fn(async () => ({}));
const addEmail = jest.fn(async () => ({}));
const removeEmail = jest.fn(async () => ({}));
const mockDS = ({ zones, redirections }) => getDomainService({
cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addEmail,
addRedir,
removeZone,
removeRedir,
removeEmail,
})
});
const mockDS = ({ zones, redirections }) =>
getDomainService({
cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addEmail,
addRedir,
removeZone,
removeRedir,
removeEmail,
}),
});
const getRecordCalls = recfn => recfn.mock.calls
.map(R.head)
.map(R.pick(['name', 'type', 'address', 'redirect', 'domain', 'line', 'priority', 'exchanger']));
const getRecordCalls = (recfn) =>
recfn.mock.calls
.map(R.head)
.map(
R.pick([
"name",
"type",
"address",
"redirect",
"domain",
"line",
"priority",
"exchanger",
]),
);
beforeEach(() => {
addZone.mockClear();
removeZone.mockClear();
addRedir.mockClear();
removeRedir.mockClear();
addEmail.mockClear();
removeEmail.mockClear();
});
describe('getHosts', () => {
it('should resolve with a list of hosts', async () => {
const zones = [
{ name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
];
const redirections = [];
const zone = async () => zones;
const redir = async () => redirections;
const mockDomainService = getDomainService({ cpanel: getCpanel({ zone, redir }) });
const list = await mockDomainService.getHosts();
expect(list).toEqual([
{ name: 'xx', type: 'CNAME', address: 'fck.com' },
{ name: 'xx', type: 'A', address: '111.1.1212.1' },
]);
beforeEach(() => {
addZone.mockClear();
removeZone.mockClear();
addRedir.mockClear();
removeRedir.mockClear();
addEmail.mockClear();
removeEmail.mockClear();
});
it('should resolve with a redirections', async () => {
const zones = [
{ line: '111', name: 'xx', type: 'CNAME', address: 'fck.com.' },
{ line: '112', 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();
describe("getHosts", () => {
it("should resolve with a list of hosts", async () => {
const zones = [
{ name: "xx", type: "CNAME", address: "fck.com." },
{ name: "xx", type: "A", address: "111.1.1212.1" },
];
const redirections = [];
const zone = async () => zones;
const redir = async () => redirections;
const mockDomainService = getDomainService({
cpanel: getCpanel({ zone, redir }),
});
const list = await mockDomainService.getHosts();
expect(list).toEqual([
{ id: '111', name: 'xx', type: 'CNAME', address: 'fck.com' },
{ id: '112', name: 'xx', type: 'A', address: '111.1.1212.1' },
{ id: `foo.${DOMAIN_DOMAIN}`, name: 'foo', type: 'URL', address: 'https://google.com' },
{ id: `foo1.${DOMAIN_DOMAIN}`, name: 'foo1', type: 'URL', address: 'https://duck.com' },
]);
});
});
expect(list).toEqual([
{ name: "xx", type: "CNAME", address: "fck.com" },
{ name: "xx", type: "A", address: "111.1.1212.1" },
]);
});
describe('updateHosts', () => {
it('should append new hosts with existing ones and set it', async () => {
const zones = [
{ line: 1, name: 'a', type: 'CNAME', address: 'boo' },
{ line: 2, name: 'b', type: 'CNAME', address: 'goo' },
];
const redirections = [];
it("should resolve with a redirections", async () => {
const zones = [
{ line: "111", name: "xx", type: "CNAME", address: "fck.com." },
{ line: "112", 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 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' },
{ name: 'c', type: 'MX', address: 'foobar.com', priority: 2 },
]);
expect(addZone).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addZone)).toEqual([
{ name: 'c', type: 'A', address: '12.131321.213' },
]);
expect(addEmail).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addEmail)).toEqual([
{ domain: 'c.is-a.dev', exchanger: 'foobar.com', priority: 2 },
]);
expect(removeZone).toHaveBeenCalledTimes(0);
expect(removeEmail).toHaveBeenCalledTimes(0);
expect(list).toEqual([
{ id: "111", name: "xx", type: "CNAME", address: "fck.com" },
{ id: "112", name: "xx", type: "A", address: "111.1.1212.1" },
{
id: `foo.${DOMAIN_DOMAIN}`,
name: "foo",
type: "URL",
address: "https://google.com",
},
{
id: `foo1.${DOMAIN_DOMAIN}`,
name: "foo1",
type: "URL",
address: "https://duck.com",
},
]);
});
});
it('should update matching host and set it', async () => {
const zones = [
{ line: 1, name: 'a', type: 'CNAME', address: 'boo' },
{ line: 2, name: 'b', type: 'CNAME', address: 'goo' },
];
const redirections = [];
describe("updateHosts", () => {
it("should append new hosts with existing ones and set it", async () => {
const zones = [
{ line: 1, name: "a", type: "CNAME", address: "boo" },
{ line: 2, name: "b", type: "CNAME", address: "goo" },
];
const redirections = [];
const mockDomainService = mockDS({ zones, redirections });
await mockDomainService.updateHosts([
{ name: 'a', type: 'CNAME', address: 'boo' },
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
]);
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" },
{ name: "c", type: "MX", address: "foobar.com", priority: 2 },
]);
expect(addZone).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addZone)).toEqual([
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
]);
expect(removeZone).toHaveBeenCalledTimes(1);
expect(getRecordCalls(removeZone)).toEqual([
{ line: 2 },
]);
expect(addZone).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addZone)).toEqual([
{ name: "c", type: "A", address: "12.131321.213" },
]);
expect(addEmail).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addEmail)).toEqual([
{ domain: "c.is-a.dev", exchanger: "foobar.com", priority: 2 },
]);
expect(removeZone).toHaveBeenCalledTimes(0);
expect(removeEmail).toHaveBeenCalledTimes(0);
});
it("should update matching host and set it", async () => {
const zones = [
{ line: 1, name: "a", type: "CNAME", address: "boo" },
{ line: 2, name: "b", type: "CNAME", address: "goo" },
];
const redirections = [];
const mockDomainService = mockDS({ zones, redirections });
await mockDomainService.updateHosts([
{ name: "a", type: "CNAME", address: "boo" },
{ name: "b", type: "CNAME", address: "googoogaga" },
]);
expect(addZone).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addZone)).toEqual([
{ name: "b", type: "CNAME", address: "googoogaga" },
]);
expect(removeZone).toHaveBeenCalledTimes(1);
expect(getRecordCalls(removeZone)).toEqual([{ line: 2 }]);
});
it("should update matching host and set it", async () => {
const zones = [
{ line: 1, name: "a", type: "CNAME", address: "boo" },
{ line: 2, name: "b", type: "CNAME", address: "goo" },
{ line: 3, name: "b", type: "CNAME", address: "xaa" },
];
const redirections = [];
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" },
]);
expect(addZone).toHaveBeenCalledTimes(2);
expect(getRecordCalls(addZone)).toEqual([
{ name: "b", type: "CNAME", address: "googoogaga" },
{ name: "b", type: "CNAME", address: "farboo" },
]);
expect(removeZone).toHaveBeenCalledTimes(2);
expect(getRecordCalls(removeZone)).toEqual([
{ line: 3 },
{ line: 2 },
]);
});
it("should workout this complex example", async () => {
const zones = [
{ line: 1, name: "a", type: "CNAME", address: "world" },
{ line: 2, name: "b", type: "A", address: "1" },
{ line: 3, name: "b", type: "A", address: "2" },
{ line: 4, name: "c", type: "CNAME", address: "hello.com" },
{
line: 5,
name: "c",
type: "MX",
address: "mx1.hello.com",
priority: 20,
},
{
line: 6,
name: "c",
type: "MX",
address: "mx2.hello.com",
priority: 21,
},
{
line: 7,
name: "b",
type: "MX",
address: "foo.bar",
priority: 20,
},
{ line: 101, name: "x", type: "A", address: "1" },
{ line: 99, name: "y", type: "A", address: "2" },
{ line: 100, name: "z", type: "A", address: "3" },
];
const redirections = [
{
domain: `b.${DOMAIN_DOMAIN}`,
destination: "https://foobar.com",
},
{
domain: `c.${DOMAIN_DOMAIN}`,
destination: "https://goobar.com",
},
{
domain: `x.${DOMAIN_DOMAIN}`,
destination: "https://example.com",
},
];
const mockDomainService = mockDS({ zones, redirections });
await mockDomainService.updateHosts([
{ name: "a", type: "CNAME", address: "boo" },
{ name: "b", type: "A", address: "1" },
{ name: "b", type: "A", address: "2" },
{ name: "b", type: "A", address: "3" },
{ name: "b", type: "URL", address: "https://wowow.com" },
{ name: "c", type: "CNAME", address: "hello.com" },
{ name: "c", type: "URL", address: "https://goobar.com" },
{ name: "d", type: "CNAME", address: "helo.com" },
{ name: "d", type: "URL", address: "https://hhh.com" },
{ name: "x", type: "URL", address: "https://example69.com" },
{
name: "c",
type: "MX",
address: "mx2.hello.com",
priority: 21,
},
{ name: "a", type: "MX", address: "example.com", priority: 20 },
]);
expect(addZone).toHaveBeenCalledTimes(3);
expect(getRecordCalls(addZone)).toEqual([
{ name: "a", type: "CNAME", address: "boo" },
{ name: "b", type: "A", address: "3" },
{ name: "d", type: "CNAME", address: "helo.com" },
]);
expect(removeZone).toHaveBeenCalledTimes(4);
expect(getRecordCalls(removeZone)).toEqual([
{ line: 101 },
{ line: 100 },
{ line: 99 },
{ line: 1 },
]);
expect(addEmail).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addEmail)).toEqual([
{
domain: "a.is-a.dev",
exchanger: "example.com",
priority: 20,
},
]);
expect(removeEmail).toHaveBeenCalledTimes(2);
expect(getRecordCalls(removeEmail)).toEqual([
{ domain: "b.is-a.dev", exchanger: "foo.bar", priority: 20 },
{
domain: "c.is-a.dev",
exchanger: "mx1.hello.com",
priority: 20,
},
]);
expect(addRedir).toHaveBeenCalledTimes(3);
expect(getRecordCalls(addRedir)).toEqual([
{
domain: `b.${DOMAIN_DOMAIN}`,
type: "permanent",
redirect: "https://wowow.com",
},
{
domain: `d.${DOMAIN_DOMAIN}`,
type: "permanent",
redirect: "https://hhh.com",
},
{
domain: `x.${DOMAIN_DOMAIN}`,
type: "permanent",
redirect: "https://example69.com",
},
]);
expect(removeRedir).toHaveBeenCalledTimes(2);
expect(getRecordCalls(removeRedir)).toEqual([
{ domain: `b.${DOMAIN_DOMAIN}` },
{ domain: `x.${DOMAIN_DOMAIN}` },
]);
});
});
it('should update matching host and set it', async () => {
const zones = [
{ line: 1, name: 'a', type: 'CNAME', address: 'boo' },
{ line: 2, name: 'b', type: 'CNAME', address: 'goo' },
{ line: 3, name: 'b', type: 'CNAME', address: 'xaa' },
];
const redirections = [];
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' },
]);
expect(addZone).toHaveBeenCalledTimes(2);
expect(getRecordCalls(addZone)).toEqual([
{ name: 'b', type: 'CNAME', address: 'googoogaga' },
{ name: 'b', type: 'CNAME', address: 'farboo' },
]);
expect(removeZone).toHaveBeenCalledTimes(2);
expect(getRecordCalls(removeZone)).toEqual([
{ line: 3 },
{ line: 2 },
]);
});
it('should workout this complex example', async () => {
const zones = [
{ line: 1, name: 'a', type: 'CNAME', address: 'world' },
{ line: 2, name: 'b', type: 'A', address: '1' },
{ line: 3, name: 'b', type: 'A', address: '2' },
{ line: 4, name: 'c', type: 'CNAME', address: 'hello.com' },
{ line: 5, name: 'c', type: 'MX', address: 'mx1.hello.com', priority: 20 },
{ line: 6, name: 'c', type: 'MX', address: 'mx2.hello.com', priority: 21 },
{ line: 7, name: 'b', type: 'MX', address: 'foo.bar', priority: 20 },
{ line: 101, name: 'x', type: 'A', address: '1' },
{ line: 99, name: 'y', type: 'A', address: '2' },
{ line: 100, name: 'z', type: 'A', address: '3' },
];
const redirections = [
{ domain: `b.${DOMAIN_DOMAIN}`, destination: 'https://foobar.com' },
{ domain: `c.${DOMAIN_DOMAIN}`, destination: 'https://goobar.com' },
{ domain: `x.${DOMAIN_DOMAIN}`, destination: 'https://example.com' },
];
const mockDomainService = mockDS({ zones, redirections });
await mockDomainService.updateHosts([
{ name: 'a', type: 'CNAME', address: 'boo' },
{ name: 'b', type: 'A', address: '1' },
{ name: 'b', type: 'A', address: '2' },
{ name: 'b', type: 'A', address: '3' },
{ name: 'b', type: 'URL', address: 'https://wowow.com' },
{ name: 'c', type: 'CNAME', address: 'hello.com' },
{ name: 'c', type: 'URL', address: 'https://goobar.com' },
{ name: 'd', type: 'CNAME', address: 'helo.com' },
{ name: 'd', type: 'URL', address: 'https://hhh.com' },
{ name: 'x', type: 'URL', address: 'https://example69.com' },
{ name: 'c', type: 'MX', address: 'mx2.hello.com', priority: 21 },
{ name: 'a', type: 'MX', address: 'example.com', priority: 20 },
]);
expect(addZone).toHaveBeenCalledTimes(3);
expect(getRecordCalls(addZone)).toEqual([
{ name: 'a', type: 'CNAME', address: 'boo' },
{ name: 'b', type: 'A', address: '3' },
{ name: 'd', type: 'CNAME', address: 'helo.com' },
]);
expect(removeZone).toHaveBeenCalledTimes(4);
expect(getRecordCalls(removeZone)).toEqual([
{ line: 101 },
{ line: 100 },
{ line: 99 },
{ line: 1 },
]);
expect(addEmail).toHaveBeenCalledTimes(1);
expect(getRecordCalls(addEmail)).toEqual([
{ domain: 'a.is-a.dev', exchanger: 'example.com', priority: 20 },
]);
expect(removeEmail).toHaveBeenCalledTimes(2);
expect(getRecordCalls(removeEmail)).toEqual([
{ domain: 'b.is-a.dev', exchanger: 'foo.bar', priority: 20 },
{ domain: 'c.is-a.dev', exchanger: 'mx1.hello.com', priority: 20 },
]);
expect(addRedir).toHaveBeenCalledTimes(3);
expect(getRecordCalls(addRedir)).toEqual([
{ domain: `b.${DOMAIN_DOMAIN}`, type: 'permanent', redirect: 'https://wowow.com' },
{ domain: `d.${DOMAIN_DOMAIN}`, type: 'permanent', redirect: 'https://hhh.com' },
{ domain: `x.${DOMAIN_DOMAIN}`, type: 'permanent', redirect: 'https://example69.com' },
]);
expect(removeRedir).toHaveBeenCalledTimes(2);
expect(getRecordCalls(removeRedir)).toEqual([
{ domain: `b.${DOMAIN_DOMAIN}` },
{ domain: `x.${DOMAIN_DOMAIN}` },
]);
});
});
});
+6 -7
View File
@@ -1,9 +1,8 @@
const { getDomains } = require('../utils/get-domain');
const { getDomains } = require("../utils/get-domain");
describe('getDomains', () => {
it('should resolve with the list of domains', async () => {
const list = await getDomains();
expect(Array.isArray(list)).toBe(true);
});
describe("getDomains", () => {
it("should resolve with the list of domains", async () => {
const list = await getDomains();
expect(Array.isArray(list)).toBe(true);
});
});
+34 -28
View File
@@ -1,31 +1,37 @@
const R = require('ramda');
const fs = require('fs');
const { getDomains } = require('../utils/get-domain');
const { validateDomainData } = require('../utils/validations');
const { DOMAINS_PATH } = require('../utils/constants');
const R = require("ramda");
const fs = require("fs");
const { getDomains } = require("../utils/get-domain");
const { validateDomainData } = require("../utils/validations");
const { DOMAINS_PATH } = require("../utils/constants");
describe('Domains', () => {
it('should all be json', async () => {
const files = await fs.promises.readdir(DOMAINS_PATH, {});
expect(files.filter(f => !/\.json$/g.test(f)).length).toBe(0);
});
describe("Domains", () => {
it("should all be json", async () => {
const files = await fs.promises.readdir(DOMAINS_PATH, {});
expect(files.filter((f) => !/\.json$/g.test(f)).length).toBe(0);
});
it('should be valid', (done) => {
getDomains()
.then(R.reject(R.propEq('name', '_psl')))
.then(R.map(data => {
const { errors } = validateDomainData(data);
if (errors.length) {
const message = errors
.map(([key, { reason }]) => `[${data.name}.${key}]: ${reason}`)
.join('\n');
return `\nValidation errors in ${data.name}.json: \n${message}`;
}
return '';
}))
.then(R.filter(R.complement(R.isEmpty)))
.then(messages => messages.length ? done(messages.join('\n')) : done())
.catch(done);
});
it("should be valid", (done) => {
getDomains()
.then(R.reject(R.propEq("name", "_psl")))
.then(
R.map((data) => {
const { errors } = validateDomainData(data);
if (errors.length) {
const message = errors
.map(
([key, { reason }]) =>
`[${data.name}.${key}]: ${reason}`,
)
.join("\n");
return `\nValidation errors in ${data.name}.json: \n${message}`;
}
return "";
}),
)
.then(R.filter(R.complement(R.isEmpty)))
.then((messages) =>
messages.length ? done(messages.join("\n")) : done(),
)
.catch(done);
});
});
+225 -192
View File
@@ -1,209 +1,242 @@
const R = require('ramda')
const { toHostList, registerDomains } = require('../scripts/register-domains')
const { TTL, DOMAIN_DOMAIN } = require('../utils/constants')
const { getDomainService } = require('../utils/domain-service')
const R = require("ramda");
const { toHostList, registerDomains } = require("../scripts/register-domains");
const { TTL, DOMAIN_DOMAIN } = require("../utils/constants");
const { getDomainService } = require("../utils/domain-service");
const getCpanel = ({
zone,
addZone,
removeZone,
redir,
addRedir,
removeRedir,
addEmail,
removeEmail,
zone,
addZone,
removeZone,
redir,
addRedir,
removeRedir,
addEmail,
removeEmail,
} = {}) => ({
zone: {
fetch: (_) => zone(),
add: (rec) => addZone(rec),
remove: (rec) => removeZone(rec),
},
redirection: {
fetch: (_) => redir(),
add: (rec) => addRedir(rec),
remove: (rec) => removeRedir(rec),
},
email: {
add: (rec) => addEmail(rec),
remove: (rec) => removeEmail(rec),
},
})
zone: {
fetch: (_) => zone(),
add: (rec) => addZone(rec),
remove: (rec) => removeZone(rec),
},
redirection: {
fetch: (_) => redir(),
add: (rec) => addRedir(rec),
remove: (rec) => removeRedir(rec),
},
email: {
add: (rec) => addEmail(rec),
remove: (rec) => removeEmail(rec),
},
});
describe('toHostList', () => {
it('should flatten domain data to list of hosts (without https)', () => {
const res = toHostList([
{ name: 'akshay', record: { CNAME: 'phenax.github.io' } },
{ name: 'foobar', record: { CNAME: 'v.io' } },
{ name: 'xx', record: { A: ['1.2.3.4', '5.6.3.2', '1.2.31.1'] } },
{ name: 'xx', record: { CNAME: 'foobar.com', MX: ['as.com', 'f.com'] } },
])
describe("toHostList", () => {
it("should flatten domain data to list of hosts (without https)", () => {
const res = toHostList([
{ name: "akshay", record: { CNAME: "phenax.github.io" } },
{ name: "foobar", record: { CNAME: "v.io" } },
{ name: "xx", record: { A: ["1.2.3.4", "5.6.3.2", "1.2.31.1"] } },
{
name: "xx",
record: { CNAME: "foobar.com", MX: ["as.com", "f.com"] },
},
]);
expect(res).toEqual([
{ name: 'akshay', type: 'CNAME', address: 'phenax.github.io', ttl: TTL },
{ name: 'foobar', type: 'CNAME', address: 'v.io', ttl: TTL },
{ name: 'xx', type: 'A', address: '1.2.3.4', ttl: TTL },
{ name: 'xx', type: 'A', address: '5.6.3.2', ttl: TTL },
{ name: 'xx', type: 'A', address: '1.2.31.1', ttl: TTL },
{ name: 'xx', type: 'CNAME', address: 'foobar.com', ttl: TTL },
{ name: 'xx', type: 'MX', address: 'as.com', priority: 20, ttl: TTL },
{ name: 'xx', type: 'MX', address: 'f.com', priority: 21, ttl: TTL },
])
})
})
expect(res).toEqual([
{
name: "akshay",
type: "CNAME",
address: "phenax.github.io",
ttl: TTL,
},
{ name: "foobar", type: "CNAME", address: "v.io", ttl: TTL },
{ name: "xx", type: "A", address: "1.2.3.4", ttl: TTL },
{ name: "xx", type: "A", address: "5.6.3.2", ttl: TTL },
{ name: "xx", type: "A", address: "1.2.31.1", ttl: TTL },
{ name: "xx", type: "CNAME", address: "foobar.com", ttl: TTL },
{
name: "xx",
type: "MX",
address: "as.com",
priority: 20,
ttl: TTL,
},
{
name: "xx",
type: "MX",
address: "f.com",
priority: 21,
ttl: TTL,
},
]);
});
});
describe('registerDomains', () => {
const addZone = jest.fn(async () => ({}))
const removeZone = jest.fn(async () => ({}))
const addRedir = jest.fn(async () => ({}))
const removeRedir = jest.fn(async () => ({}))
const addEmail = jest.fn(async () => ({}))
const removeEmail = jest.fn(async () => ({}))
describe("registerDomains", () => {
const addZone = jest.fn(async () => ({}));
const removeZone = jest.fn(async () => ({}));
const addRedir = jest.fn(async () => ({}));
const removeRedir = jest.fn(async () => ({}));
const addEmail = jest.fn(async () => ({}));
const removeEmail = jest.fn(async () => ({}));
const mockDS = ({ zones, redirections }) =>
getDomainService({
cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addEmail,
addRedir,
removeZone,
removeRedir,
removeEmail,
}),
})
const mockDS = ({ zones, redirections }) =>
getDomainService({
cpanel: getCpanel({
zone: async () => zones,
redir: async () => redirections,
addZone,
addEmail,
addRedir,
removeZone,
removeRedir,
removeEmail,
}),
});
beforeEach(() => {
addZone.mockClear()
removeZone.mockClear()
addRedir.mockClear()
removeRedir.mockClear()
addEmail.mockClear()
removeEmail.mockClear()
})
beforeEach(() => {
addZone.mockClear();
removeZone.mockClear();
addRedir.mockClear();
removeRedir.mockClear();
addEmail.mockClear();
removeEmail.mockClear();
});
it('should register the new set of hosts generated from domains list', async () => {
const localHosts = [
{ name: 'a', record: { CNAME: 'hello' } },
{ name: 'b', record: { CNAME: 'xaa' } },
]
const remoteHosts = [
{ line: 1, name: 'a', type: 'CNAME', address: 'hello' },
{ line: 2, name: 'b', type: 'CNAME', address: 'goo' },
{ line: 3, name: 'b', type: 'CNAME', address: 'xaa' },
]
const remoteRedirections = []
it("should register the new set of hosts generated from domains list", async () => {
const localHosts = [
{ name: "a", record: { CNAME: "hello" } },
{ name: "b", record: { CNAME: "xaa" } },
];
const remoteHosts = [
{ line: 1, name: "a", type: "CNAME", address: "hello" },
{ line: 2, name: "b", type: "CNAME", address: "goo" },
{ line: 3, name: "b", type: "CNAME", address: "xaa" },
];
const remoteRedirections = [];
const domainService = mockDS({
zones: remoteHosts,
redirections: remoteRedirections,
})
await registerDomains({ getDomains: async () => localHosts, domainService })
const domainService = mockDS({
zones: remoteHosts,
redirections: remoteRedirections,
});
await registerDomains({
getDomains: async () => localHosts,
domainService,
});
expect(addZone).toHaveBeenCalledTimes(0)
expect(removeZone).toHaveBeenCalledTimes(1)
expect(addRedir).toHaveBeenCalledTimes(0)
expect(removeRedir).toHaveBeenCalledTimes(0)
})
expect(addZone).toHaveBeenCalledTimes(0);
expect(removeZone).toHaveBeenCalledTimes(1);
expect(addRedir).toHaveBeenCalledTimes(0);
expect(removeRedir).toHaveBeenCalledTimes(0);
});
it('should add the new set hosts', async () => {
const localHosts = [
{ name: 'a', record: { CNAME: 'boo' } },
{
name: 'b',
record: { A: ['1.1.1.1', '1.1.1.2'], MX: 'somemx', TXT: 'some txt' },
},
{ name: 'c', record: { URL: 'https://google.com' } },
{ name: 'd', record: { CNAME: 'foobar' } },
{ name: 'e', record: { A: ['2.2.2.2'], TXT: ['some', 'extra', 'txt'] } },
]
const remoteHosts = [
{ line: 1, name: 'a', type: 'CNAME', address: 'boo' },
{ line: 2, name: 'b', type: 'MX', address: 'othermx' },
{ line: 3, name: 'd', type: 'CNAME', address: 'foobaz' },
]
const remoteRedirections = [
{ domain: `b.${DOMAIN_DOMAIN}`, destination: 'x' },
{ domain: `a.${DOMAIN_DOMAIN}`, destination: 'y' },
]
it("should add the new set hosts", async () => {
const localHosts = [
{ name: "a", record: { CNAME: "boo" } },
{
name: "b",
record: {
A: ["1.1.1.1", "1.1.1.2"],
MX: "somemx",
TXT: "some txt",
},
},
{ name: "c", record: { URL: "https://google.com" } },
{ name: "d", record: { CNAME: "foobar" } },
{
name: "e",
record: { A: ["2.2.2.2"], TXT: ["some", "extra", "txt"] },
},
];
const remoteHosts = [
{ line: 1, name: "a", type: "CNAME", address: "boo" },
{ line: 2, name: "b", type: "MX", address: "othermx" },
{ line: 3, name: "d", type: "CNAME", address: "foobaz" },
];
const remoteRedirections = [
{ domain: `b.${DOMAIN_DOMAIN}`, destination: "x" },
{ domain: `a.${DOMAIN_DOMAIN}`, destination: "y" },
];
const domainService = mockDS({
zones: remoteHosts,
redirections: remoteRedirections,
})
await registerDomains({ getDomains: async () => localHosts, domainService })
const domainService = mockDS({
zones: remoteHosts,
redirections: remoteRedirections,
});
await registerDomains({
getDomains: async () => localHosts,
domainService,
});
expect(addZone).toHaveBeenCalledTimes(8)
expect(addZone.mock.calls).toEqual([
[{ name: 'b', type: 'A', address: '1.1.1.1', line: undefined }],
[{ name: 'b', type: 'A', address: '1.1.1.2', line: undefined }],
[
{
name: 'b',
type: 'TXT',
address: 'some txt',
txtdata: 'some txt',
line: undefined,
},
],
[
{
name: 'd',
type: 'CNAME',
cname: 'foobar',
address: 'foobar',
line: undefined,
},
],
[{ name: 'e', type: 'A', address: '2.2.2.2', line: undefined }],
[
{
name: 'e',
type: 'TXT',
address: 'some',
txtdata: 'some',
line: undefined,
},
],
[
{
name: 'e',
type: 'TXT',
address: 'extra',
txtdata: 'extra',
line: undefined,
},
],
[
{
name: 'e',
type: 'TXT',
address: 'txt',
txtdata: 'txt',
line: undefined,
},
],
])
expect(addZone).toHaveBeenCalledTimes(8);
expect(addZone.mock.calls).toEqual([
[{ name: "b", type: "A", address: "1.1.1.1", line: undefined }],
[{ name: "b", type: "A", address: "1.1.1.2", line: undefined }],
[
{
name: "b",
type: "TXT",
address: "some txt",
txtdata: "some txt",
line: undefined,
},
],
[
{
name: "d",
type: "CNAME",
cname: "foobar",
address: "foobar",
line: undefined,
},
],
[{ name: "e", type: "A", address: "2.2.2.2", line: undefined }],
[
{
name: "e",
type: "TXT",
address: "some",
txtdata: "some",
line: undefined,
},
],
[
{
name: "e",
type: "TXT",
address: "extra",
txtdata: "extra",
line: undefined,
},
],
[
{
name: "e",
type: "TXT",
address: "txt",
txtdata: "txt",
line: undefined,
},
],
]);
expect(removeZone).toHaveBeenCalledTimes(1)
expect(removeZone.mock.calls).toEqual([[{ line: 3 }]])
expect(removeZone).toHaveBeenCalledTimes(1);
expect(removeZone.mock.calls).toEqual([[{ line: 3 }]]);
expect(addRedir).toHaveBeenCalledTimes(1)
expect(addRedir.mock.calls).toEqual([
[
{
domain: 'c.booboo.xyz',
redirect: 'https://google.com',
redirect_wildcard: 1,
redirect_www: 1,
type: 'permanent',
},
],
])
expect(addRedir).toHaveBeenCalledTimes(1);
expect(addRedir.mock.calls).toEqual([
[
{
domain: "c.booboo.xyz",
redirect: "https://google.com",
redirect_wildcard: 1,
redirect_www: 1,
type: "permanent",
},
],
]);
expect(addEmail).toHaveBeenCalledTimes(1)
expect(addEmail.mock.calls).toEqual([
[{ domain: 'b.is-a.dev', exchanger: 'somemx', priority: 20 }],
])
})
})
expect(addEmail).toHaveBeenCalledTimes(1);
expect(addEmail.mock.calls).toEqual([
[{ domain: "b.is-a.dev", exchanger: "somemx", priority: 20 }],
]);
});
});
+154 -120
View File
@@ -1,129 +1,163 @@
const { validateDomainData, isValidDomain } = require('../utils/validations');
const INVALID_NAMES = require('../utils/invalid-domains.json');
const { validateDomainData, isValidDomain } = require("../utils/validations");
const INVALID_NAMES = require("../utils/invalid-domains.json");
const defaultDomain = {
name: 'aaa',
record: {
A: ['121.121.121.121']
},
owner: {
username: 'betsy',
email: 'betsyfuckyoassup@foobar.com',
},
name: "aaa",
record: {
A: ["121.121.121.121"],
},
owner: {
username: "betsy",
email: "betsyfuckyoassup@foobar.com",
},
};
const getstroflen = len => Array(len).fill('a').join('');
const getstroflen = (len) => Array(len).fill("a").join("");
describe('isValidMX', () => {
it('should be valid mx record', () => {
const cases = [
{ mx: 'foobar.com', result: true },
{ mx: 'as.as', result: true },
{ mx: 'ASPMX.L.GOOGLE.COM', result: true },
{ mx: 'ALT4.ASPMX.L.GOOGLE.COM', result: true },
{ mx: 'hello', result: false },
{ mx: 'helalsds-asd5sjdsd.com', result: true },
{ mx: 'helalsds?asd5sjdsd.com', result: false },
{ mx: 'helalsds_asd5sjdsd.com', result: false },
describe("isValidMX", () => {
it("should be valid mx record", () => {
const cases = [
{ mx: "foobar.com", result: true },
{ mx: "as.as", result: true },
{ mx: "ASPMX.L.GOOGLE.COM", result: true },
{ mx: "ALT4.ASPMX.L.GOOGLE.COM", result: true },
{ mx: "hello", result: false },
{ mx: "helalsds-asd5sjdsd.com", result: true },
{ mx: "helalsds?asd5sjdsd.com", result: false },
{ mx: "helalsds_asd5sjdsd.com", result: false },
];
cases.forEach(({ mx, result }) => {
expect(isValidDomain(mx)).toBe(result);
});
});
});
describe("validateDomainData", () => {
const invalidCases = [
{},
{ name: "helo" },
{ name: "wwow", record: { A: ["12312"] } },
...[
"",
" ",
undefined,
"hlo wld",
"g32++13",
"ajsdD_123yq",
"khsda%",
"122*dsd",
getstroflen(101),
].map((name) => ({
...defaultDomain,
name,
})),
{ ...defaultDomain, record: { CNAME: "sd", A: ["121,3213"] } },
{ ...defaultDomain, record: { A: ["121", "12"], FOOBAR: ["sd"] } },
{ ...defaultDomain, record: { A: [] } },
{ ...defaultDomain, owner: {} },
{ ...defaultDomain, owner: { username: "hwelo" } },
{ ...defaultDomain, owner: { email: "hwelo" } },
{ ...defaultDomain, record: { CNAME: "http://foobar.com" } },
{ ...defaultDomain, record: { CNAME: "https://foobar.com" } },
{ ...defaultDomain, record: { URL: "foobar.com" } },
{
...defaultDomain,
record: { CNAME: "foobar.com", A: ["11.22.22.33"] },
},
{
...defaultDomain,
record: { CNAME: "foobar.com", MX: ["ALT4.ASPMX.L.GOOGLE.COM"] },
},
...INVALID_NAMES.map((name) => ({ ...defaultDomain, name })).slice(
0,
1,
),
{ ...defaultDomain, name: "a.b" },
{ ...defaultDomain, name: "ww2.baa" },
{ ...defaultDomain, name: "help.baa" },
{ ...defaultDomain, name: "_github-pages-challenge-is-a-dev" },
{ ...defaultDomain, name: "_github-challenge-is-a-dev" },
{ ...defaultDomain, record: { AAAA: [] } },
{ ...defaultDomain, record: { AAAA: ["182.22.222.22", "::1"] } },
{ ...defaultDomain, record: { AAAA: "182.22.222.22" } },
{ ...defaultDomain, record: { A: "::1" } },
{ ...defaultDomain, name: "_discord" },
{ ...defaultDomain, name: "_gitlab-pages-verification-code" },
{ ...defaultDomain, name: "_acme-challenge" },
{ ...defaultDomain, name: "_dmarc" },
{ ...defaultDomain, name: "_gh-is-a-dev" },
{ ...defaultDomain, name: "_domainkey" },
];
cases.forEach(({ mx, result }) => {
expect(isValidDomain(mx)).toBe(result);
const validCases = [
defaultDomain,
...[
"hello",
"hello-world",
"11111111111",
"--wow--",
"wow--",
"--wow",
].map((name) => ({
...defaultDomain,
name,
})),
{
...defaultDomain,
description: getstroflen(99),
},
{ ...defaultDomain, record: { CNAME: "aa.sd" } },
{ ...defaultDomain, record: { URL: "https://foobar.com" } },
{ ...defaultDomain, record: { URL: "http://foobar.com/foobar/" } },
{ ...defaultDomain, record: { MX: ["ALT4.ASPMX.L.GOOGLE.COM"] } },
{ ...defaultDomain, record: { TXT: "foobar wow nice!!!" } },
{
...defaultDomain,
record: { A: ["1.1.1.1"], MX: ["mx1.example.com"] },
},
{ ...defaultDomain, name: "gogo.foo.bar" },
{ ...defaultDomain, name: "ww9.baa" },
{ ...defaultDomain, name: "_github-pages-challenge-phenax.akshay" },
{ ...defaultDomain, name: "_github-pages-challenge-hello01-ga" },
{ ...defaultDomain, name: "_github-pages-challenge-hello01_ga" },
{ ...defaultDomain, name: "_github-challenge-phenax.akshay" },
{ ...defaultDomain, name: "_github-challenge-hello01-ga" },
{ ...defaultDomain, name: "_github-challenge-hello01_ga" },
{
...defaultDomain,
record: { TXT: ["foobar wow nice!!!", "more text"] },
},
{
...defaultDomain,
record: { AAAA: ["::1", "2001:db8:3333:4444:5555:6666:7777:8888"] },
},
{ ...defaultDomain, record: { A: ["122.222.222.222"] } },
{ ...defaultDomain, name: "_discord.subdomain" },
{ ...defaultDomain, name: "_gitlab-pages-verification-code.subdomain" },
{ ...defaultDomain, name: "_acme-challenge.subdomain" },
{ ...defaultDomain, name: "_dmarc.subdomain" },
{ ...defaultDomain, name: "_gh-phenax.akshay" },
{ ...defaultDomain, name: "_gh-hello01-ga" },
{ ...defaultDomain, name: "_gh-hello01_ga" },
{ ...defaultDomain, name: "_domainkey.subdomain" },
{ ...defaultDomain, name: "mx._domainkey.subdomain" },
];
it("should return false for invalid data", () => {
invalidCases.forEach((data) => {
const { valid, errors } = validateDomainData(data);
expect(valid).toBe(false);
expect(errors.length).toBeGreaterThan(0);
});
});
it("should return true if the name is valid", () => {
validCases.forEach((data) => {
const { valid, errors } = validateDomainData(data);
if (!valid) console.log(JSON.stringify(errors, null, 2));
expect(valid).toBe(true);
expect(errors).toEqual([]);
});
});
});
});
describe('validateDomainData', () => {
const invalidCases = [
{},
{ name: 'helo' },
{ name: 'wwow', record: { A: ['12312'] } },
...['', ' ', undefined, 'hlo wld', 'g32++13', 'ajsdD_123yq', 'khsda%', '122*dsd', getstroflen(101)].map(name => ({
...defaultDomain,
name,
})),
{ ...defaultDomain, record: { CNAME: 'sd', A: ['121,3213'] } },
{ ...defaultDomain, record: { A: ['121', '12'], FOOBAR: ['sd'] } },
{ ...defaultDomain, record: { A: [] } },
{ ...defaultDomain, owner: {}, },
{ ...defaultDomain, owner: { username: 'hwelo', }, },
{ ...defaultDomain, owner: { email: 'hwelo' }, },
{ ...defaultDomain, record: { CNAME: 'http://foobar.com' } },
{ ...defaultDomain, record: { CNAME: 'https://foobar.com' } },
{ ...defaultDomain, record: { URL: 'foobar.com' } },
{ ...defaultDomain, record: { CNAME: 'foobar.com', A: ['11.22.22.33'] } },
{ ...defaultDomain, record: { CNAME: 'foobar.com', MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
...INVALID_NAMES.map(name => ({ ...defaultDomain, name })).slice(0, 1),
{ ...defaultDomain, name: 'a.b' },
{ ...defaultDomain, name: 'ww2.baa' },
{ ...defaultDomain, name: 'help.baa' },
{ ...defaultDomain, name: '_github-pages-challenge-is-a-dev' },
{ ...defaultDomain, name: '_github-challenge-is-a-dev' },
{ ...defaultDomain, record: { AAAA: [] } },
{ ...defaultDomain, record: { AAAA: ['182.22.222.22', '::1'] } },
{ ...defaultDomain, record: { AAAA: '182.22.222.22' } },
{ ...defaultDomain, record: { A: '::1' } },
{ ...defaultDomain, name: '_discord' },
{ ...defaultDomain, name: '_gitlab-pages-verification-code' },
{ ...defaultDomain, name: '_acme-challenge' },
{ ...defaultDomain, name: '_dmarc' },
{ ...defaultDomain, name: '_gh-is-a-dev' },
{ ...defaultDomain, name: '_domainkey' },
];
const validCases = [
defaultDomain,
...['hello', 'hello-world', '11111111111', '--wow--', 'wow--', '--wow'].map(name => ({
...defaultDomain,
name,
})),
{
...defaultDomain,
description: getstroflen(99),
},
{ ...defaultDomain, record: { CNAME: 'aa.sd' } },
{ ...defaultDomain, record: { URL: 'https://foobar.com' } },
{ ...defaultDomain, record: { URL: 'http://foobar.com/foobar/' } },
{ ...defaultDomain, record: { MX: ['ALT4.ASPMX.L.GOOGLE.COM'] } },
{ ...defaultDomain, record: { TXT: 'foobar wow nice!!!' } },
{ ...defaultDomain, record: { A: ['1.1.1.1'], MX: ['mx1.example.com'] } },
{ ...defaultDomain, name: 'gogo.foo.bar' },
{ ...defaultDomain, name: 'ww9.baa' },
{ ...defaultDomain, name: '_github-pages-challenge-phenax.akshay' },
{ ...defaultDomain, name: '_github-pages-challenge-hello01-ga' },
{ ...defaultDomain, name: '_github-pages-challenge-hello01_ga' },
{ ...defaultDomain, name: '_github-challenge-phenax.akshay' },
{ ...defaultDomain, name: '_github-challenge-hello01-ga' },
{ ...defaultDomain, name: '_github-challenge-hello01_ga' },
{ ...defaultDomain, record: { TXT: ['foobar wow nice!!!', 'more text'] } },
{ ...defaultDomain, record: { AAAA: ['::1', '2001:db8:3333:4444:5555:6666:7777:8888'] } },
{ ...defaultDomain, record: { A: ['122.222.222.222'] } },
{ ...defaultDomain, name: '_discord.subdomain' },
{ ...defaultDomain, name: '_gitlab-pages-verification-code.subdomain' },
{ ...defaultDomain, name: '_acme-challenge.subdomain' },
{ ...defaultDomain, name: '_dmarc.subdomain' },
{ ...defaultDomain, name: '_gh-phenax.akshay' },
{ ...defaultDomain, name: '_gh-hello01-ga' },
{ ...defaultDomain, name: '_gh-hello01_ga' },
{ ...defaultDomain, name: '_domainkey.subdomain' },
{ ...defaultDomain, name: 'mx._domainkey.subdomain' },
];
it('should return false for invalid data', () => {
invalidCases.forEach(data => {
const { valid, errors } = validateDomainData(data);
expect(valid).toBe(false);
expect(errors.length).toBeGreaterThan(0);
});
});
it('should return true if the name is valid', () => {
validCases.forEach(data => {
const { valid, errors } = validateDomainData(data);
if (!valid) console.log(JSON.stringify(errors, null, 2));
expect(valid).toBe(true);
expect(errors).toEqual([]);
});
});
});