From 0bfc0d5b5fb58dc80285cdc38232559178194d5c Mon Sep 17 00:00:00 2001 From: Piyal Basu Date: Fri, 11 Oct 2024 12:55:25 -0400 Subject: [PATCH] add tests --- src/helper/test-helper.ts | 14 ++--- src/route/index.test.ts | 110 ++++++++++++++++++++++++++++++++++++++ src/route/index.ts | 15 +++--- 3 files changed, 126 insertions(+), 13 deletions(-) diff --git a/src/helper/test-helper.ts b/src/helper/test-helper.ts index 03b8da9..ad7c51a 100644 --- a/src/helper/test-helper.ts +++ b/src/helper/test-helper.ts @@ -615,7 +615,13 @@ jest }; }, ); -async function getDevServer() { +async function getDevServer( + blockaidConfig = { + useBlockaidAssetScanning: true, + useBlockaidDappScanning: true, + useBlockaidTxScanning: true, + }, +) { const server = await initApiServer( mockMercuryClient, blockAidService, @@ -624,11 +630,7 @@ async function getDevServer() { true, register, "development", - { - useBlockaidAssetScanning: true, - useBlockaidDappScanning: true, - useBlockaidTxScanning: true, - }, + blockaidConfig, ); await server.listen(); diff --git a/src/route/index.test.ts b/src/route/index.test.ts index 7db2520..6c9a19b 100644 --- a/src/route/index.test.ts +++ b/src/route/index.test.ts @@ -7,6 +7,7 @@ import { } from "../helper/test-helper"; import { transformAccountHistory } from "../service/mercury/helpers/transformers"; import { query } from "../service/mercury/queries"; +import { defaultBenignResponse } from "../service/blockaid/helpers/addScanResults"; jest.mock("@blockaid/client", () => { return class Blockaid { @@ -41,6 +42,24 @@ jest.mock("@blockaid/client", () => { return Promise.resolve({ results: res }); }, }; + token = { + scan: ({ address }: { address: string }) => { + if ( + address === + "BLND-GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56" + ) { + return Promise.resolve({ + result_type: "Malicious", + malicious_score: 1, + }); + } + + return Promise.resolve({ + result_type: "Benign", + malicious_score: 0, + }); + }, + }; }; }); @@ -288,5 +307,96 @@ describe("API routes", () => { register.clear(); await server.close(); }); + it("does not scan assets when config is disabled", async () => { + const asset_ids = [ + "BLND-GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56", + "FOO-CDP3XWJ4ZN222LKYBMWIY3GYXZYX3KA6WVNDS6V7WKXSYWLAEMYW7DTZ", + ]; + const server = await getDevServer({ + useBlockaidAssetScanning: false, + useBlockaidDappScanning: false, + useBlockaidTxScanning: false, + }); + const url = new URL( + `http://localhost:${ + (server?.server?.address() as any).port + }/api/v1/scan-asset-bulk`, + ); + url.searchParams.append("network", "PUBLIC"); + for (const id of asset_ids) { + url.searchParams.append("asset_ids", id); + } + const response = await fetch(url.href); + const data = await response.json(); + + expect(response.status).toEqual(200); + expect( + data.data.results[ + "BLND-GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56" + ], + ).toEqual({ + ...defaultBenignResponse, + }); + expect( + data.data.results[ + "FOO-CDP3XWJ4ZN222LKYBMWIY3GYXZYX3KA6WVNDS6V7WKXSYWLAEMYW7DTZ" + ], + ).toEqual({ + ...defaultBenignResponse, + }); + register.clear(); + await server.close(); + }); + }); + describe("/scan-asset", () => { + it("can scan an asset", async () => { + const server = await getDevServer(); + const url = new URL( + `http://localhost:${ + (server?.server?.address() as any).port + }/api/v1/scan-asset`, + ); + url.searchParams.append( + "address", + "BLND-GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56", + ); + const response = await fetch(url.href); + const data = await response.json(); + + expect(response.status).toEqual(200); + expect(data.data).toEqual({ + result_type: "Malicious", + malicious_score: 1, + }); + register.clear(); + await server.close(); + }); + it("does not scan an asset when config is disabled", async () => { + const server = await getDevServer({ + useBlockaidAssetScanning: false, + useBlockaidDappScanning: false, + useBlockaidTxScanning: false, + }); + const url = new URL( + `http://localhost:${ + (server?.server?.address() as any).port + }/api/v1/scan-asset`, + ); + url.searchParams.append( + "address", + "BLND-GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56", + ); + const response = await fetch(url.href); + const data = await response.json(); + + expect(response.status).toEqual(200); + expect(data.data).toEqual({ + ...defaultBenignResponse, + address: + "BLND-GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56", + }); + register.clear(); + await server.close(); + }); }); }); diff --git a/src/route/index.ts b/src/route/index.ts index 5bbe230..7416807 100644 --- a/src/route/index.ts +++ b/src/route/index.ts @@ -687,7 +687,10 @@ export async function initApiServer( } return reply .code(200) - .send({ data: null, error: ERROR.SCAN_ASSET_DISABLED }); + .send({ + data: { ...defaultBenignResponse, address }, + error: ERROR.SCAN_ASSET_DISABLED, + }); }, }); @@ -735,12 +738,10 @@ export async function initApiServer( ...defaultBenignResponse, }; }); - return reply - .code(200) - .send({ - data: { results: defaultResponse }, - error: ERROR.SCAN_ASSET_DISABLED, - }); + return reply.code(200).send({ + data: { results: defaultResponse }, + error: ERROR.SCAN_ASSET_DISABLED, + }); }, });