Skip to content

Commit

Permalink
Test if our own error are still thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 29, 2024
1 parent 8de8bf6 commit 4cd5b66
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
35 changes: 35 additions & 0 deletions library/src/agent/applyHooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as t from "tap";
import { Agent } from "./Agent";
import { setInstance } from "./AgentSingleton";
import { APIForTesting } from "./api/APIForTesting";
import { applyHooks } from "./applyHooks";
import { Hooks } from "./hooks/Hooks";
import { LoggerForTesting } from "./logger/LoggerForTesting";
import { LoggerNoop } from "./logger/LoggerNoop";

t.test("it ignores if package is not installed", async (t) => {
const hooks = new Hooks();
Expand Down Expand Up @@ -56,6 +61,12 @@ t.test("it ignores if version is not supported", async (t) => {
});
});

function removeStackTraceErrorMessage(error: string) {
const [msg] = error.split("\n");

return msg;
}

t.test("it adds try/catch around the wrapped method", async (t) => {
const hooks = new Hooks();
const connection = hooks
Expand All @@ -68,6 +79,9 @@ t.test("it adds try/catch around the wrapped method", async (t) => {
connection.modifyArguments("execute", () => {
throw new Error("THIS SHOULD BE CATCHED");
});
connection.inspect("ping", () => {
throw new Error("Aikido guard has blocked a SQL injection");
});

t.same(applyHooks(hooks), {
mysql2: {
Expand All @@ -92,5 +106,26 @@ t.test("it adds try/catch around the wrapped method", async (t) => {
const [executeRows] = await actualConnection.execute("SELECT 1 as number");
t.same(executeRows, [{ number: 1 }]);

const logger = new LoggerForTesting();
setInstance(
new Agent(false, logger, new APIForTesting(), undefined, false, {})
);

const [queryRows2] = await actualConnection.query("SELECT 1 as number");
t.same(queryRows2, [{ number: 1 }]);

const [executeRows2] = await actualConnection.execute("SELECT 1 as number");
t.same(executeRows2, [{ number: 1 }]);

const error = await t.rejects(() => actualConnection.ping());
if (error instanceof Error) {
t.equal(error.message, "Aikido guard has blocked a SQL injection");
}

t.same(logger.getMessages().map(removeStackTraceErrorMessage), [
'Internal error in module "mysql2" in method "query"',
'Internal error in module "mysql2" in method "execute"',
]);

await actualConnection.end();
});
4 changes: 3 additions & 1 deletion library/src/agent/logger/LoggerConsole.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class LoggerConsole {
import { Logger } from "./Logger";

export class LoggerConsole implements Logger {
/**
* Creates a terminal log with the "AIKIDO: " affix.
* @param message Message to be logged
Expand Down
13 changes: 13 additions & 0 deletions library/src/agent/logger/LoggerForTesting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Logger } from "./Logger";

export class LoggerForTesting implements Logger {
private readonly messages: string[] = [];

log(message: string) {
this.messages.push(message);
}

getMessages() {
return this.messages;
}
}
4 changes: 3 additions & 1 deletion library/src/agent/logger/LoggerNoop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class LoggerNoop {
import { Logger } from "./Logger";

export class LoggerNoop implements Logger {
log(message: string) {
// noop
}
Expand Down

0 comments on commit 4cd5b66

Please sign in to comment.