From 380ba04ec181ae1744c5ef5bf2217ce509e496b8 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sun, 17 Sep 2023 21:48:56 +0530 Subject: [PATCH] chore: fixes test cases --- tests/register.test.js | 207 ++++++++++++++++++++++++++--------------- 1 file changed, 132 insertions(+), 75 deletions(-) diff --git a/tests/register.test.js b/tests/register.test.js index 9917bf1f0..bd967a298 100644 --- a/tests/register.test.js +++ b/tests/register.test.js @@ -1,7 +1,7 @@ -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, @@ -11,7 +11,7 @@ const getCpanel = ({ addRedir, removeRedir, addEmail, - removeEmail + removeEmail, } = {}) => ({ zone: { fetch: (_) => zone(), @@ -27,7 +27,7 @@ const getCpanel = ({ add: (rec) => addEmail(rec), remove: (rec) => removeEmail(rec), }, -}); +}) describe('toHostList', () => { it('should flatten domain data to list of hosts (without https)', () => { @@ -36,7 +36,7 @@ describe('toHostList', () => { { 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 }, @@ -47,106 +47,163 @@ describe('toHostList', () => { { 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 () => ({})); + 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(); - }); + 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 = []; + ] + 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: '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'] } } - ]; + { 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).toHaveBeenCalledWith({ name: 'b', type: 'A', address: '1.1.1.2', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ name: 'd', type: 'CNAME', cname: 'foobar', address: 'foobar', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ name: 'b', type: 'A', address: '1.1.1.2', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ name: 'b', type: 'TXT', address: 'some txt', txtdata: 'some txt', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ name: 'e', type: 'A', address: '2.2.2.2', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ name: 'e', type: 'TXT', address: 'some', txtdata: 'some', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ name: 'e', type: 'TXT', address: 'extra', txtdata: 'extra', line: undefined }); - expect(addZone).toHaveBeenCalledWith({ 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).toHaveBeenCalledWith({ line: 3 }); + expect(removeZone).toHaveBeenCalledTimes(1) + expect(removeZone.mock.calls).toEqual([[{ line: 3 }]]) - expect(addRedir).toHaveBeenCalledTimes(1); - expect(addRedir).toHaveBeenCalledWith({ - domain: 'c.booboo.xyz', - redirect: 'https://google.com', - redirect_wildcard: 1, - redirect_www: 1, - type: 'permanent', - }); - - expect(addEmail).toHaveBeenCalledTimes(1); - expect(addEmail).toHaveBeenCalledWith({ domain: 'b.is-a.dev', exchanger: 'somemx', priority: 20 }); - }); -}); + 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 }], + ]) + }) +})