Switch to ava as a test runner (#1778)

* 0.17.1

* ava conversion

* ava for all

* Remove unneeded

* Remove Jest config

* Pacify package.json checker
This commit is contained in:
Jaco
2022-07-08 12:42:46 +02:00
committed by GitHub
parent 4f05efc9e5
commit 5aa2b3c096
10 changed files with 1070 additions and 259 deletions
-12
View File
@@ -1,12 +0,0 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0
const config = require('@polkadot/dev/config/jest.cjs');
module.exports = {
...config,
moduleNameMapper: {},
setupFilesAfterEnv: ['<rootDir>/jest/setupEnv.cjs'],
testEnvironment: 'jsdom',
testTimeout: 2 * 60 * 1000
};
+15 -4
View File
@@ -23,10 +23,11 @@
"clean": "polkadot-dev-clean-build", "clean": "polkadot-dev-clean-build",
"deno": "yarn polkadot-dev-deno-map && yarn build && deno check --import-map=import_map.json mod.ts", "deno": "yarn polkadot-dev-deno-map && yarn build && deno check --import-map=import_map.json mod.ts",
"lint": "polkadot-dev-run-lint", "lint": "polkadot-dev-run-lint",
"phishing:addrcheck": "polkadot-dev-run-test --runInBand --detectOpenHandles packages/phishing/src/addrcheck", "phishing:addrcheck": "ava packages/phishing/src/addrcheck.spec.ts",
"phishing:crosscheck": "polkadot-dev-run-test --runInBand --detectOpenHandles packages/phishing/src/crosscheck", "phishing:crosscheck": "ava packages/phishing/src/crosscheck.spec.ts",
"postinstall": "polkadot-dev-yarn-only", "postinstall": "polkadot-dev-yarn-only",
"test": "polkadot-dev-run-test --coverage --runInBand --detectOpenHandles --testPathIgnorePatterns addrcheck --testPathIgnorePatterns crosscheck" "test": "ava --match '!CI*'",
"test:one": "yarn test"
}, },
"dependencies": { "dependencies": {
"@pinata/sdk": "^1.1.26", "@pinata/sdk": "^1.1.26",
@@ -35,9 +36,19 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.18.6", "@babel/core": "^7.18.6",
"@polkadot/dev": "^0.67.63", "@polkadot/dev": "^0.67.63",
"@types/jest": "^28.1.4" "ava": "^4.3.0",
"ts-node": "^10.8.2"
}, },
"resolutions": { "resolutions": {
"typescript": "^4.7.4" "typescript": "^4.7.4"
},
"ava": {
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=ts-node/esm",
"--experimental-specifier-resolution=node"
]
} }
} }
+1
View File
@@ -27,6 +27,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/js-yaml": "^4.0.5", "@types/js-yaml": "^4.0.5",
"ava": "^4.3.0",
"js-yaml": "^4.1.0" "js-yaml": "^4.1.0"
} }
} }
+15 -23
View File
@@ -1,6 +1,7 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors // Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import test from 'ava';
import fs from 'fs'; import fs from 'fs';
import { decodeAddress } from '@polkadot/util-crypto'; import { decodeAddress } from '@polkadot/util-crypto';
@@ -33,8 +34,7 @@ const TOP_LEVEL = [
'zapto.org' 'zapto.org'
]; ];
describe('added addresses', (): void => { test('has no malformed addresses', (t): void => {
it('has no malformed addresses', (): void => {
const invalids = Object const invalids = Object
.entries(addresses) .entries(addresses)
.map(([url, addrs]): [string, string[]] => { .map(([url, addrs]): [string, string[]] => {
@@ -48,14 +48,13 @@ describe('added addresses', (): void => {
} }
})]; })];
}) })
.filter(([, addrs]) => addrs.length); .filter(([, addrs]) => addrs.length)
.map(([url, addrs]) => `${url}: ${addrs.join(', ')}`);
if (invalids.length) { t.deepEqual(invalids, []);
throw new Error(`Invalid ss58 checksum addresses found: ${invalids.map(([url, addrs]) => `\n\t${url}: ${addrs.join(', ')}`).join('')}`);
}
}); });
it('has no entries on the known addresses list', (): void => { test('has no entries on the known addresses list', (t): void => {
const added = Object const added = Object
.values(addresses) .values(addresses)
.reduce<string[]>((all, addrs) => all.concat(addrs), []); .reduce<string[]>((all, addrs) => all.concat(addrs), []);
@@ -64,12 +63,10 @@ describe('added addresses', (): void => {
.reduce<[string, string][]>((all, [site, addrs]) => all.concat(addrs.map((a) => [site, a])), []) .reduce<[string, string][]>((all, [site, addrs]) => all.concat(addrs.map((a) => [site, a])), [])
.filter(([, a]) => added.includes(a)); .filter(([, a]) => added.includes(a));
expect(dupes).toEqual([]); t.deepEqual(dupes, []);
});
}); });
describe('added urls', (): void => { test('has no entries for allowed top-level domains', (t): void => {
it('has no entries for allowed top-level domains', (): void => {
const invalids = all.deny.filter((u) => const invalids = all.deny.filter((u) =>
TOP_LEVEL.some((t) => TOP_LEVEL.some((t) =>
t.startsWith('*.') t.startsWith('*.')
@@ -78,10 +75,10 @@ describe('added urls', (): void => {
) )
); );
expect(invalids).toEqual([]); t.deepEqual(invalids, []);
}); });
it('has no malformed domain-only entries', (): void => { test('has no malformed domain-only entries', (t): void => {
const invalids = all.deny.filter((u) => const invalids = all.deny.filter((u) =>
u.includes('/') || // don't allow paths u.includes('/') || // don't allow paths
u.includes('?') || // don't allow query params u.includes('?') || // don't allow query params
@@ -89,18 +86,18 @@ describe('added urls', (): void => {
!u.includes('.') // need at least a domain !u.includes('.') // need at least a domain
); );
expect(invalids).toEqual([]); t.deepEqual(invalids, []);
}); });
it('has no urls starting with www. (domain-only inclusions)', (): void => { test('has no urls starting with www. (domain-only inclusions)', (t): void => {
const invalids = all.deny.filter((u) => const invalids = all.deny.filter((u) =>
u.startsWith('www.') u.startsWith('www.')
); );
expect(invalids).toEqual([]); t.deepEqual(invalids, []);
}); });
it('has no duplicate entries', (): void => { test('has no duplicate entries', (t): void => {
const checks: string[] = []; const checks: string[] = [];
const dupes = all.deny.reduce<string[]>((dupes, url) => { const dupes = all.deny.reduce<string[]>((dupes, url) => {
@@ -113,10 +110,5 @@ describe('added urls', (): void => {
return dupes; return dupes;
}, []); }, []);
expect( t.deepEqual(process.env.CI_LOG ? dupes : [], []);
process.env.CI_LOG
? []
: dupes
).toEqual([]);
});
}); });
+4 -7
View File
@@ -1,6 +1,7 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors // Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import test from 'ava';
import fs from 'fs'; import fs from 'fs';
import { decodeAddress } from '@polkadot/util-crypto'; import { decodeAddress } from '@polkadot/util-crypto';
@@ -157,12 +158,9 @@ function checkAll (): Promise<[string, string[]][]> {
]); ]);
} }
describe('addrcheck', (): void => { test('CI: has all known addresses', async (t): Promise<void> => {
beforeAll((): void => { t.timeout(2 * 60 * 1000);
jest.setTimeout(2 * 60 * 1000);
});
it('has all known addresses', async (): Promise<void> => {
const _results = await checkAll(); const _results = await checkAll();
const results = _results.map(([url, addrs]): [string, string[]] => { const results = _results.map(([url, addrs]): [string, string[]] => {
return [url, addrs.filter((a) => { return [url, addrs.filter((a) => {
@@ -197,6 +195,5 @@ describe('addrcheck', (): void => {
sites.length && process.env.CI_LOG && fs.appendFileSync('./.github/addrcheck.md', `\n\n${sites.length} urls with missing entries found at ${new Date().toUTCString()}:\n\n${TICKS}\n${JSON.stringify(mapMiss, null, 2)}\n${TICKS}\n`); sites.length && process.env.CI_LOG && fs.appendFileSync('./.github/addrcheck.md', `\n\n${sites.length} urls with missing entries found at ${new Date().toUTCString()}:\n\n${TICKS}\n${JSON.stringify(mapMiss, null, 2)}\n${TICKS}\n`);
expect(sites).toEqual([]); t.deepEqual(sites, []);
});
}); });
@@ -0,0 +1,24 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0
import test from 'ava';
import { checkAddress } from '.';
test('returns null if the address is not found', async (t): Promise<void> => {
t.true(
await checkAddress('5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY') === null
);
});
test('returns the site when the address is found', async (t): Promise<void> => {
t.true(
await checkAddress('14Vxs7UB9FqfQ53wwTJUBAJThs5N7b3bg89HscRU6eBqrFhQ') === 'polkadot.center'
);
});
test('returns the site even if the ss58 is different', async (t): Promise<void> => {
t.true(
await checkAddress('5FkmzcdNekhdSA7j4teSSyHGUnKT8bzNBFvVVeZSGmbSpYHH') === 'polkadots.network'
);
});
@@ -0,0 +1,48 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0
import test from 'ava';
import { checkIfDenied } from '.';
test('returns false when host is not listed', async (t): Promise<void> => {
t.false(
await checkIfDenied('polkadot.network')
);
});
test('returns false when host is not listed (with protocol)', async (t): Promise<void> => {
t.false(
await checkIfDenied('https://polkadot.network')
);
});
test('returns true when host in list', async (t): Promise<void> => {
t.true(
await checkIfDenied('polkawallets.site')
);
});
test('returns true when host in list (www-prefix)', async (t): Promise<void> => {
t.true(
await checkIfDenied('www.polkadotfund.com')
);
});
test('returns true when host in list (protocol)', async (t): Promise<void> => {
t.true(
await checkIfDenied('https://polkawallets.site')
);
});
test('returns true when host in list (protocol + path)', async (t): Promise<void> => {
t.true(
await checkIfDenied('https://polkawallets.site/something/index.html')
);
});
test('returns true in list (protocol + path + #)', async (t): Promise<void> => {
t.true(
await checkIfDenied('https://robonomics-network-xrt.cyberfi-tech-rewards-programs-claims-erc20-token.com/myetherwallet/access-my-wallet/#/input-privatekey-mnemonic-phrase-claim-bonus')
);
});
+18 -19
View File
@@ -1,6 +1,7 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors // Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import test from 'ava';
import fs from 'fs'; import fs from 'fs';
import { load as yamlParse } from 'js-yaml'; import { load as yamlParse } from 'js-yaml';
@@ -24,9 +25,8 @@ const TICKS = '```';
const ourSiteList = JSON.parse(fs.readFileSync('all.json', 'utf-8')) as { allow: string[]; deny: string[] }; const ourSiteList = JSON.parse(fs.readFileSync('all.json', 'utf-8')) as { allow: string[]; deny: string[] };
function assertAndLog (check: boolean, site: string, missing: unknown): void { function log (check: boolean, site: string, missing: unknown): void {
if (!check) { !check && process.env.CI_LOG && fs.appendFileSync('./.github/crosscheck.md', `
process.env.CI_LOG && fs.appendFileSync('./.github/crosscheck.md', `
Missing entries found from ${site}: Missing entries found from ${site}:
@@ -34,9 +34,6 @@ ${TICKS}
${JSON.stringify(missing, null, 2)} ${JSON.stringify(missing, null, 2)}
${TICKS} ${TICKS}
`); `);
throw new Error(site);
}
} }
function matchName (_url: string): boolean { function matchName (_url: string): boolean {
@@ -48,14 +45,11 @@ function matchName (_url: string): boolean {
const CRYPTODB = 'https://raw.githubusercontent.com/CryptoScamDB/blacklist/master/data/urls.yaml'; const CRYPTODB = 'https://raw.githubusercontent.com/CryptoScamDB/blacklist/master/data/urls.yaml';
const ETHPHISH = 'https://raw.githubusercontent.com/MetaMask/eth-phishing-detect/master/src/config.json'; const ETHPHISH = 'https://raw.githubusercontent.com/MetaMask/eth-phishing-detect/master/src/config.json';
describe('crosscheck', (): void => {
const ours: string[] = ourSiteList.deny; const ours: string[] = ourSiteList.deny;
beforeAll((): void => { test('CI: has all the relevant entries from CryptoScamDb', async (t): Promise<void> => {
jest.setTimeout(120000); t.timeout(120000);
});
it('has all the relevant entries from CryptoScamDb', async (): Promise<void> => {
const raw = await fetch(CRYPTODB).then((r) => r.text()); const raw = await fetch(CRYPTODB).then((r) => r.text());
// this is a hack, the text slipped in upstream // this is a hack, the text slipped in upstream
@@ -65,20 +59,25 @@ describe('crosscheck', (): void => {
!ours.includes(url.replace(/https:\/\/|http:\/\//, '').split('/')[0]) !ours.includes(url.replace(/https:\/\/|http:\/\//, '').split('/')[0])
); );
console.log('CryptoScamDb found\n', JSON.stringify(filtered, null, 2)); // console.log('CryptoScamDb found\n', JSON.stringify(filtered, null, 2));
console.log('CryptoScamDb missing\n', JSON.stringify(missing, null, 2)); // console.log('CryptoScamDb missing\n', JSON.stringify(missing, null, 2));
assertAndLog(missing.length === 0, 'CryptoScamDB', missing); log(missing.length === 0, 'CryptoScamDB', missing);
t.true(missing.length === 0);
}); });
it('has polkadot/kusama entries from eth-phishing-detect', async (): Promise<void> => { test('CI: has polkadot/kusama entries from eth-phishing-detect', async (t): Promise<void> => {
t.timeout(120000);
const ethDb = await fetch(ETHPHISH).then<EthPhishing>((r) => r.json()); const ethDb = await fetch(ETHPHISH).then<EthPhishing>((r) => r.json());
const filtered = ethDb.blacklist.filter((url) => matchName(url)); const filtered = ethDb.blacklist.filter((url) => matchName(url));
const missing = filtered.filter((url) => !ours.includes(url)); const missing = filtered.filter((url) => !ours.includes(url));
console.log('eth-phishing-detect found\n', JSON.stringify(filtered, null, 2)); // console.log('eth-phishing-detect found\n', JSON.stringify(filtered, null, 2));
console.log('eth-phishing-detect missing\n', JSON.stringify(missing, null, 2)); // console.log('eth-phishing-detect missing\n', JSON.stringify(missing, null, 2));
assertAndLog(missing.length === 0, 'eth-phishing-detect', missing); log(missing.length === 0, 'eth-phishing-detect', missing);
});
t.true(missing.length === 0);
}); });
-70
View File
@@ -1,70 +0,0 @@
// Copyright 2020-2022 @polkadot/phishing authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { checkAddress, checkIfDenied } from '.';
// *sigh* Jest breaks yet again...
describe.skip('checkIfDenied', (): void => {
it('returns false when host is not listed', async (): Promise<void> => {
expect(
await checkIfDenied('polkadot.network')
).toEqual(false);
});
it('returns false when host is not listed (with protocol)', async (): Promise<void> => {
expect(
await checkIfDenied('https://polkadot.network')
).toEqual(false);
});
it('returns true when host in list', async (): Promise<void> => {
expect(
await checkIfDenied('polkawallets.site')
).toEqual(true);
});
it('returns true when host in list (www-prefix)', async (): Promise<void> => {
expect(
await checkIfDenied('www.polkadotfund.com')
).toEqual(true);
});
it('returns true when host in list (protocol)', async (): Promise<void> => {
expect(
await checkIfDenied('https://polkawallets.site')
).toEqual(true);
});
it('returns true when host in list (protocol + path)', async (): Promise<void> => {
expect(
await checkIfDenied('https://polkawallets.site/something/index.html')
).toEqual(true);
});
it('returns true in list (protocol + path + #)', async (): Promise<void> => {
expect(
await checkIfDenied('https://robonomics-network-xrt.cyberfi-tech-rewards-programs-claims-erc20-token.com/myetherwallet/access-my-wallet/#/input-privatekey-mnemonic-phrase-claim-bonus')
).toEqual(true);
});
});
describe.skip('checkAddress', (): void => {
it('returns null if the address is not found', async (): Promise<void> => {
expect(
await checkAddress('5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY')
).toEqual(null);
});
it('returns the site when the address is found', async (): Promise<void> => {
expect(
await checkAddress('14Vxs7UB9FqfQ53wwTJUBAJThs5N7b3bg89HscRU6eBqrFhQ')
).toEqual('polkadot.center');
});
it('returns the site even if the ss58 is different', async (): Promise<void> => {
expect(
await checkAddress('5FkmzcdNekhdSA7j4teSSyHGUnKT8bzNBFvVVeZSGmbSpYHH')
).toEqual('polkadots.network');
});
});
+843 -22
View File
File diff suppressed because it is too large Load Diff