Skip to content

Commit

Permalink
Merge pull request #415 from AikidoSec/check-test-types
Browse files Browse the repository at this point in the history
Check types in test files
  • Loading branch information
hansott authored Oct 18, 2024
2 parents f09bfdc + 233bdcf commit d9890c3
Show file tree
Hide file tree
Showing 38 changed files with 383 additions and 280 deletions.
14 changes: 7 additions & 7 deletions library/agent/Context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,26 @@ t.test("it clears cache when context is mutated", async (t) => {
const context = { ...sampleContext };

runWithContext(context, () => {
t.same(extractStringsFromUserInputCached(getContext(), "body"), undefined);
t.same(extractStringsFromUserInputCached(getContext()!, "body"), undefined);
t.same(
extractStringsFromUserInputCached(getContext(), "query"),
extractStringsFromUserInputCached(getContext()!, "query"),
new Map(Object.entries({ abc: ".", def: ".abc" }))
);

updateContext(getContext(), "query", {});
t.same(extractStringsFromUserInputCached(getContext(), "body"), undefined);
updateContext(getContext()!, "query", {});
t.same(extractStringsFromUserInputCached(getContext()!, "body"), undefined);
t.same(
extractStringsFromUserInputCached(getContext(), "query"),
extractStringsFromUserInputCached(getContext()!, "query"),
new Map(Object.entries({}))
);

runWithContext({ ...context, body: { a: "z" }, query: { b: "y" } }, () => {
t.same(
extractStringsFromUserInputCached(getContext(), "body"),
extractStringsFromUserInputCached(getContext()!, "body"),
new Map(Object.entries({ a: ".", z: ".a" }))
);
t.same(
extractStringsFromUserInputCached(getContext(), "query"),
extractStringsFromUserInputCached(getContext()!, "query"),
new Map(Object.entries({ b: ".", y: ".b" }))
);
});
Expand Down
7 changes: 4 additions & 3 deletions library/agent/ServiceConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as t from "tap";
import { ServiceConfig } from "./ServiceConfig";

t.test("it returns false if empty rules", async () => {
const config = new ServiceConfig([], 0, [], []);
const config = new ServiceConfig([], 0, [], [], false);
t.same(config.getLastUpdatedAt(), 0);
t.same(config.isUserBlocked("id"), false);
t.same(config.isAllowedIP("1.2.3.4"), false);
Expand Down Expand Up @@ -52,7 +52,8 @@ t.test("it works", async () => {
],
0,
["123"],
[]
[],
false
);

t.same(config.isUserBlocked("123"), true);
Expand All @@ -79,7 +80,7 @@ t.test("it works", async () => {
});

t.test("it checks if IP is allowed", async () => {
const config = new ServiceConfig([], 0, [], ["1.2.3.4"]);
const config = new ServiceConfig([], 0, [], ["1.2.3.4"], false);
t.same(config.isAllowedIP("1.2.3.4"), true);
t.same(config.isAllowedIP("1.2.3.5"), false);
});
1 change: 1 addition & 0 deletions library/agent/api-discovery/getApiAuthType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ t.test("no auth", async (t) => {
t.same(get(getContext()), undefined);
t.same(get(getContext({})), undefined);
t.same(get(getContext({ authorization: "" })), undefined);
// @ts-expect-error Testing edge case
t.same(get({}), undefined);
});
2 changes: 1 addition & 1 deletion library/agent/api-discovery/getDataSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ t.test("it works", async (t) => {
});

t.test("test max depth", async (t) => {
const generateTestObjectWithDepth = (depth: number) => {
const generateTestObjectWithDepth = (depth: number): object | string => {
if (depth === 0) {
return "testValue";
}
Expand Down
4 changes: 4 additions & 0 deletions library/agent/api/ReportingAPINodeHTTP.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function generateStartedEvent(): Event {
},
stack: [],
serverless: false,
platform: {
version: "version",
arch: "arch",
},
},
};
}
Expand Down
16 changes: 16 additions & 0 deletions library/agent/api/ReportingAPIRateLimitedClientSide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function generateAttackEvent(): Event {
method: undefined,
ipAddress: undefined,
userAgent: undefined,
// @ts-expect-error Test
headers: undefined,
body: undefined,
source: "express",
Expand All @@ -32,6 +33,7 @@ function generateAttackEvent(): Event {
},
agent: {
version: "1.0.0",
library: "firewall-node",
dryMode: false,
hostname: "hostname",
packages: {},
Expand All @@ -47,6 +49,10 @@ function generateAttackEvent(): Event {
prototypePollution: {},
},
stack: [],
platform: {
version: "version",
arch: "arch",
},
},
};
}
Expand Down Expand Up @@ -100,6 +106,11 @@ function generateStartedEvent(): Event {
},
stack: [],
serverless: false,
library: "firewall-node",
platform: {
version: "version",
arch: "arch",
},
},
};
}
Expand Down Expand Up @@ -162,6 +173,11 @@ function generateHeartbeatEvent(): Event {
prototypePollution: {},
},
stack: [],
library: "firewall-node",
platform: {
version: "version",
arch: "arch",
},
},
hostnames: [],
routes: [],
Expand Down
5 changes: 5 additions & 0 deletions library/agent/api/ReportingAPIRateLimitedServerSide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function generateStartedEvent(): Event {
},
stack: [],
serverless: false,
library: "firewall-node",
platform: {
version: "version",
arch: "arch",
},
},
};
}
Expand Down
5 changes: 5 additions & 0 deletions library/agent/api/ReportingAPIThatValidatesToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ function generateStartedEvent(): Event {
},
stack: [],
serverless: false,
library: "firewall-node",
platform: {
version: "version",
arch: "arch",
},
},
};
}
Expand Down
1 change: 1 addition & 0 deletions library/agent/applyHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ t.test("it ignores route if force protection off is on", async (t) => {
method: "GET",
route: "/route",
forceProtectionOff: true,
// @ts-expect-error Test
rateLimiting: undefined,
},
],
Expand Down
7 changes: 4 additions & 3 deletions library/agent/realtime/pollForChanges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LoggerForTesting } from "../logger/LoggerForTesting";
import { LoggerNoop } from "../logger/LoggerNoop";
import { pollForChanges } from "./pollForChanges";
import * as FakeTimers from "@sinonjs/fake-timers";
import { Config } from "../Config";

t.test("it does not start interval if no token", async (t) => {
const logger = new LoggerForTesting();
Expand Down Expand Up @@ -44,7 +45,7 @@ t.test("it checks for config updates", async () => {
let configUpdatedAt = 0;

wrap(fetch, "fetch", function fetch() {
return async function fetch(params) {
return async function fetch(params: any) {
calls.push({
url: params.url.toString(),
method: params.method,
Expand Down Expand Up @@ -74,7 +75,7 @@ t.test("it checks for config updates", async () => {
};
});

const configUpdates = [];
const configUpdates: Config[] = [];

pollForChanges({
onConfigUpdate: (config) => {
Expand Down Expand Up @@ -206,7 +207,7 @@ t.test("it deals with API throwing errors", async () => {
};
});

const configUpdates = [];
const configUpdates: Config[] = [];

const logger = new LoggerForTesting();
pollForChanges({
Expand Down
3 changes: 3 additions & 0 deletions library/helpers/isPlainObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ t.test(
"should return `false` if the object is not created by the `Object` constructor.",
async (t) => {
function Foo() {
// @ts-expect-error Test
this.abc = {};
}

Expand All @@ -24,6 +25,7 @@ t.test(
t.notOk(isPlainObject(1));
t.notOk(isPlainObject(["foo", "bar"]));
t.notOk(isPlainObject([]));
// @ts-expect-error Test
t.notOk(isPlainObject(new Foo()));
t.notOk(isPlainObject(null));
}
Expand All @@ -35,6 +37,7 @@ t.test("should return `false` if prototype has been modified", async (t) => {
// Directly use a built-in type's prototype, for example, Array.prototype
CustomConstructor.prototype = Array.prototype;

// @ts-expect-error Test
const instance = new CustomConstructor();

t.same(isPlainObject(instance), false);
Expand Down
4 changes: 2 additions & 2 deletions library/helpers/percentiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { percentiles } from "./percentiles";

function generateArray(
length: number,
fn: (value: number, index: number) => number
fn: (value: unknown, index: number) => number
) {
return Array.from({ length: length }).map(fn);
}
Expand All @@ -29,7 +29,7 @@ const stubsSimple = [
{
percentile: 75,
list: shuffleArray(
[].concat(generateArraySimple(100), generateArraySimple(30))
generateArraySimple(100).concat(generateArraySimple(30))
),
result: 68,
},
Expand Down
33 changes: 33 additions & 0 deletions library/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@
"@types/aws-lambda": "^8.10.131",
"@types/cookie-parser": "^1.4.6",
"@types/express": "^4.17.21",
"@types/follow-redirects": "^1.14.4",
"@types/ip": "^1.1.3",
"@types/mysql": "^2.15.25",
"@types/needle": "^3.3.0",
"@types/node": "^20.11.5",
"@types/pg": "^8.11.0",
"@types/qs": "^6.9.11",
"@types/shell-quote": "^1.7.5",
"@types/sinonjs__fake-timers": "^8.1.5",
"@types/supertest": "^6.0.2",
"@types/xml2js": "^0.4.14",
"@typescript-eslint/eslint-plugin": "^8.4.0",
"@typescript-eslint/parser": "^8.4.0",
"aws-sdk": "^2.1595.0",
Expand Down Expand Up @@ -86,10 +89,10 @@
"scripts": {
"test": "node ../scripts/run-tap.js",
"test:ci": "CI=true node ../scripts/run-tap.js",
"build": "tsc",
"build": "tsc -p tsconfig.build.json",
"build:watch": "tsc --watch",
"lint": "npm run lint-eslint && npm run lint-tsc",
"lint-eslint": "eslint '**/*.ts'",
"lint-tsc": "tsc --noEmit"
"lint-tsc": "tsc"
}
}
4 changes: 2 additions & 2 deletions library/ratelimiting/shouldRateLimitRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { LoggerNoop } from "../agent/logger/LoggerNoop";
import { shouldRateLimitRequest } from "./shouldRateLimitRequest";

function createContext(
remoteAddress: string = undefined,
userId: string = undefined,
remoteAddress: string | undefined = undefined,
userId: string | undefined = undefined,
route: string = "/login",
method: string = "POST"
): Context {
Expand Down
Loading

0 comments on commit d9890c3

Please sign in to comment.