Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piyalbasu committed Oct 11, 2024
1 parent 2d87f59 commit 0bfc0d5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 13 deletions.
14 changes: 8 additions & 6 deletions src/helper/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,13 @@ jest
};
},
);
async function getDevServer() {
async function getDevServer(
blockaidConfig = {
useBlockaidAssetScanning: true,
useBlockaidDappScanning: true,
useBlockaidTxScanning: true,
},
) {
const server = await initApiServer(
mockMercuryClient,
blockAidService,
Expand All @@ -624,11 +630,7 @@ async function getDevServer() {
true,
register,
"development",
{
useBlockaidAssetScanning: true,
useBlockaidDappScanning: true,
useBlockaidTxScanning: true,
},
blockaidConfig,
);

await server.listen();
Expand Down
110 changes: 110 additions & 0 deletions src/route/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
});
},
};
};
});

Expand Down Expand Up @@ -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();
});
});
});
15 changes: 8 additions & 7 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
},
});

Expand Down Expand Up @@ -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,
});
},
});

Expand Down

0 comments on commit 0bfc0d5

Please sign in to comment.