From c6814e703eb5dba28d525dbe243eca94a8c8e187 Mon Sep 17 00:00:00 2001 From: Hans Ott Date: Fri, 7 Feb 2025 11:17:24 +0100 Subject: [PATCH] Update mock server --- end2end/server/src/handlers/lists.js | 4 +++- end2end/server/src/handlers/updateLists.js | 6 +++--- library/agent/Agent.test.ts | 3 ++- library/agent/ServiceConfig.ts | 2 +- library/agent/api/fetchBlockedLists.ts | 14 ++++++++------ library/sources/HTTPServer.test.ts | 7 ++----- library/sources/Hono.test.ts | 7 ++----- 7 files changed, 21 insertions(+), 22 deletions(-) diff --git a/end2end/server/src/handlers/lists.js b/end2end/server/src/handlers/lists.js index 9334a1f2..23c2d232 100644 --- a/end2end/server/src/handlers/lists.js +++ b/end2end/server/src/handlers/lists.js @@ -24,6 +24,8 @@ module.exports = function lists(req, res) { }, ] : [], - blockedUserAgents: blockedUserAgents, + blockedUserAgentsV2: Array.isArray(blockedUserAgents) + ? blockedUserAgents + : [], }); }; diff --git a/end2end/server/src/handlers/updateLists.js b/end2end/server/src/handlers/updateLists.js index e0c7d908..290f9880 100644 --- a/end2end/server/src/handlers/updateLists.js +++ b/end2end/server/src/handlers/updateLists.js @@ -32,10 +32,10 @@ module.exports = function updateIPLists(req, res) { updateBlockedIPAddresses(req.app, req.body.blockedIPAddresses); if ( - req.body.blockedUserAgents && - typeof req.body.blockedUserAgents === "string" + req.body.blockedUserAgentsV2 && + typeof req.body.blockedUserAgentsV2 === "string" ) { - updateBlockedUserAgents(req.app, req.body.blockedUserAgents); + updateBlockedUserAgents(req.app, req.body.blockedUserAgentsV2); } res.json({ success: true }); diff --git a/library/agent/Agent.test.ts b/library/agent/Agent.test.ts index 496ee433..f4c3ab81 100644 --- a/library/agent/Agent.test.ts +++ b/library/agent/Agent.test.ts @@ -18,6 +18,7 @@ import { Wrapper } from "./Wrapper"; import { Context } from "./Context"; import { createTestAgent } from "../helpers/createTestAgent"; import { setTimeout } from "node:timers/promises"; +import type { Response } from "./api/fetchBlockedLists"; wrap(fetch, "fetch", function mock() { return async function mock() { @@ -42,7 +43,7 @@ wrap(fetch, "fetch", function mock() { pattern: "Bytespider", }, ], - }), + } satisfies Response), }; }; }); diff --git a/library/agent/ServiceConfig.ts b/library/agent/ServiceConfig.ts index 61c978db..5a992105 100644 --- a/library/agent/ServiceConfig.ts +++ b/library/agent/ServiceConfig.ts @@ -2,7 +2,7 @@ import { IPMatcher } from "../helpers/ip-matcher/IPMatcher"; import { LimitedContext, matchEndpoints } from "../helpers/matchEndpoints"; import { Endpoint } from "./Config"; import { - Blocklist as BlocklistType, + IPBlocklist as BlocklistType, AgentBlockList, } from "./api/fetchBlockedLists"; diff --git a/library/agent/api/fetchBlockedLists.ts b/library/agent/api/fetchBlockedLists.ts index e34b120c..d725b6b7 100644 --- a/library/agent/api/fetchBlockedLists.ts +++ b/library/agent/api/fetchBlockedLists.ts @@ -2,7 +2,7 @@ import { fetch } from "../../helpers/fetch"; import { getAPIURL } from "../getAPIURL"; import { Token } from "./Token"; -export type Blocklist = { +export type IPBlocklist = { key: string; source: string; description: string; @@ -14,8 +14,13 @@ export type AgentBlockList = { pattern: string; // e.g. "Googlebot|Bingbot" }; +export type Response = { + blockedIPAddresses: IPBlocklist[]; + blockedUserAgentsV2: AgentBlockList[]; +}; + export async function fetchBlockedLists(token: Token): Promise<{ - blockedIPAddresses: Blocklist[]; + blockedIPAddresses: IPBlocklist[]; blockedUserAgents: AgentBlockList[]; }> { const baseUrl = getAPIURL(); @@ -34,10 +39,7 @@ export async function fetchBlockedLists(token: Token): Promise<{ throw new Error(`Failed to fetch blocked lists: ${statusCode}`); } - const result: { - blockedIPAddresses: Blocklist[]; - blockedUserAgentsV2: string; - } = JSON.parse(body); + const result: Response = JSON.parse(body); return { blockedIPAddresses: diff --git a/library/sources/HTTPServer.test.ts b/library/sources/HTTPServer.test.ts index 6c3dda8e..a376e9a7 100644 --- a/library/sources/HTTPServer.test.ts +++ b/library/sources/HTTPServer.test.ts @@ -8,7 +8,7 @@ import { wrap } from "../helpers/wrap"; import { HTTPServer } from "./HTTPServer"; import { join } from "path"; import { createTestAgent } from "../helpers/createTestAgent"; -import type { Blocklist, AgentBlockList } from "../agent/api/fetchBlockedLists"; +import type { Response } from "../agent/api/fetchBlockedLists"; import * as fetchBlockedLists from "../agent/api/fetchBlockedLists"; import { mkdtemp, writeFile, unlink } from "fs/promises"; import { exec } from "child_process"; @@ -51,10 +51,7 @@ const agent = createTestAgent({ agent.start([new HTTPServer()]); wrap(fetchBlockedLists, "fetchBlockedLists", function fetchBlockedLists() { - return async function fetchBlockedLists(): Promise<{ - blockedIPAddresses: Blocklist[]; - blockedUserAgentsV2: AgentBlockList[]; - }> { + return async function fetchBlockedLists(): Promise { return { blockedIPAddresses: [ { diff --git a/library/sources/Hono.test.ts b/library/sources/Hono.test.ts index a4bede43..26c50091 100644 --- a/library/sources/Hono.test.ts +++ b/library/sources/Hono.test.ts @@ -1,6 +1,6 @@ /* eslint-disable prefer-rest-params */ import * as t from "tap"; -import type { Blocklist, AgentBlockList } from "../agent/api/fetchBlockedLists"; +import type { Response } from "../agent/api/fetchBlockedLists"; import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting"; import { Token } from "../agent/api/Token"; import { setUser } from "../agent/context/user"; @@ -38,10 +38,7 @@ wrap(fetch, "fetch", function mock(original) { pattern: "hacker|attacker", }, ], - } satisfies { - blockedIPAddresses: Blocklist[]; - blockedUserAgentsV2: AgentBlockList[]; - }), + } satisfies Response), }; }