Skip to content

Commit

Permalink
This should do the trick
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 16, 2024
1 parent 7eb75e3 commit ec347eb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 19 deletions.
2 changes: 1 addition & 1 deletion end2end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"tap": "^18.7.0"
},
"scripts": {
"test": "tap tests/*.js"
"test": "tap tests/*.js --allow-empty-coverage"
}
}
21 changes: 3 additions & 18 deletions end2end/tests/express-mongodb.test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
const t = require("tap");
const { spawn } = require("node:child_process");
const { resolve } = require("node:path");
const timeout = require("../timeout");

const pathToApp = resolve(
__dirname,
"../../sample-apps/express-mongodb",
"app.js"
);

async function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

t.test("it blocks in blocking mode", (t) => {
const server = spawn(`node`, [pathToApp, "4000"]);

server.on("close", () => {
console.log("received close");
t.end();
});

server.on("error", (err) => {
console.log("received error");
t.fail(err.message);
});

let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
console.log("stdout", data.toString());
});

let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
console.log("stderr", data.toString());
});

// Wait for the server to start
timeout(2000)
.then(() => {
console.log("sending requests");
return Promise.all([
fetch("http://localhost:4000/?search[$ne]=null", {
signal: AbortSignal.timeout(5000),
Expand All @@ -51,27 +43,22 @@ t.test("it blocks in blocking mode", (t) => {
]);
})
.then(([noSQLInjection, normalSearch]) => {
console.log("noSQLInjection", noSQLInjection.status);
console.log("normalSearch", normalSearch.status);
t.equal(noSQLInjection.status, 500);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.match(stderr, /Aikido guard has blocked a NoSQL injection/);
})
.catch((error) => {
console.log("error", error.message);
t.fail(error.message);
})
.finally(() => {
console.log("killing server");
server.kill();
});
});

/*t.test("it does not block in dry mode", (t) => {
t.test("it does not block in dry mode", (t) => {
const server = spawn(`node`, [pathToApp, "4001"], {
env: { ...process.env, AIKIDO_NO_BLOCKING: "true" },
shell: true,
});

server.on("close", () => {
Expand All @@ -81,13 +68,11 @@ t.test("it blocks in blocking mode", (t) => {
let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
console.log("stdout", data.toString());
});

let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
console.log("stderr", data.toString());
});

// Wait for the server to start
Expand All @@ -114,4 +99,4 @@ t.test("it blocks in blocking mode", (t) => {
.finally(() => {
server.kill();
});
});*/
});
3 changes: 3 additions & 0 deletions end2end/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
};

0 comments on commit ec347eb

Please sign in to comment.