Skip to content

Commit

Permalink
test: add utils.ts coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Apr 15, 2024
1 parent 0fc5405 commit e9f7052
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
117 changes: 116 additions & 1 deletion packages/phishing-controller/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import * as sinon from 'sinon';

import { ListKeys, ListNames } from './PhishingController';
import { applyDiffs, fetchTimeNow } from './utils';
import {
applyDiffs,
domainToParts,
fetchTimeNow,
matchPartsAgainstList,
processConfigs,
processDomainList,
validateConfig,
} from './utils';

const exampleBlockedUrl = 'https://example-blocked-website.com';
const exampleBlockedUrlOne = 'https://another-example-blocked-website.com';
Expand Down Expand Up @@ -140,3 +148,110 @@ describe('applyDiffs', () => {
});
});
});

describe('validateConfig', () => {
it('throws an error if the config is not an object', () => {
expect(() => validateConfig(null)).toThrow('Invalid config');
});

it('throws an error if the config contains a tolerance without a fuzzylist', () => {
expect(() => validateConfig({ tolerance: 2 })).toThrow(
'Fuzzylist tolerance provided without fuzzylist',
);
});

it('throws an error if the config contains an invalid name', () => {
expect(() => validateConfig({ name: 123 })).toThrow(
"Invalid config parameter: 'name'",
);
});

it('throws an error if the config contains an invalid version', () => {
expect(() => validateConfig({ version: { foo: 'bar' } })).toThrow(
"Invalid config parameter: 'version'",
);
});
});

describe('domainToParts', () => {
it('correctly converts a domain string to an array of parts', () => {
const domain = 'example.com';
const result = domainToParts(domain);
expect(result).toStrictEqual(['com', 'example']);
});

it('correctly converts a domain string with subdomains to an array of parts', () => {
const domain = 'sub.example.com';
const result = domainToParts(domain);
expect(result).toStrictEqual(['com', 'example', 'sub']);
});

it('throws an error if the domain string is invalid', () => {
// @ts-expect-error testing invalid input
expect(() => domainToParts(123)).toThrow('123');
});
});

describe('processConfigs', () => {
it('correctly converts a list of configs to a list of processed configs', () => {
const configs = [
{
allowlist: ['example.com'],
blocklist: ['sub.example.com'],
fuzzylist: ['fuzzy.example.com'],
tolerance: 2,
},
];

const result = processConfigs(configs);

expect(result).toStrictEqual([
{
allowlist: [['com', 'example']],
blocklist: [['com', 'example', 'sub']],
fuzzylist: [['com', 'example', 'fuzzy']],
tolerance: 2,
},
]);
});

it('can be called with no arguments', () => {
expect(processConfigs()).toStrictEqual([]);
});
});

describe('processDomainList', () => {
it('correctly converts a list of domains to an array of parts', () => {
const domainList = ['example.com', 'sub.example.com'];

const result = processDomainList(domainList);

expect(result).toStrictEqual([
['com', 'example'],
['com', 'example', 'sub'],
]);
});
});

describe('matchPartsAgainstList', () => {
it('matches a domain against a list of parts', () => {
const domainParts = ['com', 'example'];
const list = [
['com', 'example', 'sub'],
['com', 'example'],
];

const result = matchPartsAgainstList(domainParts, list);

expect(result).toStrictEqual(['com', 'example']);
});

it('returns undefined if there is no match', () => {
const domainParts = ['com', 'examplea'];
const list = [['com', 'exampleb']];

const result = matchPartsAgainstList(domainParts, list);

expect(result).toBeUndefined();
});
});
22 changes: 22 additions & 0 deletions packages/phishing-controller/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ export const validateConfig = (
return true;
};

/**
* Converts a domain string to a list of domain parts.
*
* @param domain - the domain string to convert.
* @returns the list of domain parts.
*/
export const domainToParts = (domain: string) => {
try {
return domain.split('.').reverse();
Expand All @@ -133,10 +139,26 @@ export const domainToParts = (domain: string) => {
}
};

/**
* Converts a list of domain strings to a list of domain parts.
*
* @param list - the list of domain strings to convert.
* @returns the list of domain parts.
*/
export const processDomainList = (list: string[]) => {
return list.map(domainToParts);
};

/**
* Gets the default phishing detector configuration.
*
* @param override - the optional override for the configuration.
* @param override.allowlist - the optional allowlist to override.
* @param override.blocklist - the optional blocklist to override.
* @param override.fuzzylist - the optional fuzzylist to override.
* @param override.tolerance - the optional tolerance to override.
* @returns the default phishing detector configuration.
*/
export const getDefaultPhishingDetectorConfig = (override?: {
allowlist?: string[];
blocklist?: string[];
Expand Down

0 comments on commit e9f7052

Please sign in to comment.