Skip to content

Commit d667cef

Browse files
committed
Extract to new test and assert logs
1 parent 2a7ca88 commit d667cef

File tree

2 files changed

+120
-34
lines changed

2 files changed

+120
-34
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
const t = require("tap");
2+
const { spawn } = require("child_process");
3+
const { resolve } = require("path");
4+
const timeout = require("../timeout");
5+
6+
const pathToApp = resolve(
7+
__dirname,
8+
"../../sample-apps/express-mongodb",
9+
"app.js"
10+
);
11+
12+
t.setTimeout(60000);
13+
14+
t.test("it blocks in blocking mode", (t) => {
15+
const server = spawn(`node`, [pathToApp, "4000"], {
16+
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCK: "true" },
17+
});
18+
19+
server.on("close", () => {
20+
t.end();
21+
});
22+
23+
server.on("error", (err) => {
24+
t.fail(err);
25+
});
26+
27+
let stdout = "";
28+
server.stdout.on("data", (data) => {
29+
stdout += data.toString();
30+
});
31+
32+
let stderr = "";
33+
server.stderr.on("data", (data) => {
34+
stderr += data.toString();
35+
});
36+
37+
// Wait for the server to start
38+
timeout(2000)
39+
.then(() => {
40+
return Promise.all([
41+
fetch("http://127.0.0.1:4000/hello/hans", {
42+
signal: AbortSignal.timeout(5000),
43+
}),
44+
fetch(`http://127.0.0.1:4000/hello/${encodeURIComponent(`hans" //`)}`, {
45+
signal: AbortSignal.timeout(5000),
46+
}),
47+
]);
48+
})
49+
.then(([safeName, unsafeName]) => {
50+
t.equal(safeName.status, 200);
51+
t.equal(unsafeName.status, 500);
52+
t.match(stdout, /Starting agent/);
53+
t.match(stdout, /Zen has blocked a JavaScript injection/);
54+
})
55+
.catch((error) => {
56+
t.fail(error);
57+
})
58+
.finally(() => {
59+
server.kill();
60+
});
61+
});
62+
63+
t.test("it does not block in dry mode", (t) => {
64+
const server = spawn(`node`, [pathToApp, "4001"], {
65+
env: { ...process.env, AIKIDO_DEBUG: "true" },
66+
});
67+
68+
server.on("close", () => {
69+
t.end();
70+
});
71+
72+
let stdout = "";
73+
server.stdout.on("data", (data) => {
74+
stdout += data.toString();
75+
});
76+
77+
let stderr = "";
78+
server.stderr.on("data", (data) => {
79+
stderr += data.toString();
80+
});
81+
82+
// Wait for the server to start
83+
timeout(2000)
84+
.then(() =>
85+
Promise.all([
86+
fetch("http://127.0.0.1:4001/hello/hans", {
87+
signal: AbortSignal.timeout(5000),
88+
}),
89+
fetch(`http://127.0.0.1:4001/hello/${encodeURIComponent(`hans" //`)}`, {
90+
signal: AbortSignal.timeout(5000),
91+
}),
92+
])
93+
)
94+
.then(([safeName, unsafeName]) => {
95+
t.equal(safeName.status, 200);
96+
t.equal(unsafeName.status, 200);
97+
t.match(stdout, /Starting agent/);
98+
t.match(stdout, /Zen has detected a JavaScript injection/);
99+
})
100+
.catch((error) => {
101+
t.fail(error);
102+
})
103+
.finally(() => {
104+
server.kill();
105+
});
106+
});

end2end/tests/express-mongodb.test.js

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,15 @@ t.test("it blocks in blocking mode", (t) => {
4747
fetch("http://127.0.0.1:4000/?search=title", {
4848
signal: AbortSignal.timeout(5000),
4949
}),
50-
fetch("http://127.0.0.1:4000/hello/hans", {
51-
signal: AbortSignal.timeout(5000),
52-
}),
53-
fetch(`http://127.0.0.1:4000/hello/${encodeURIComponent(`hans" //`)}`, {
54-
signal: AbortSignal.timeout(5000),
55-
}),
5650
]);
5751
})
58-
.then(
59-
([noSQLInjection, jsInjection, normalSearch, safeName, unsafeName]) => {
60-
t.equal(noSQLInjection.status, 500);
61-
t.equal(jsInjection.status, 500);
62-
t.equal(normalSearch.status, 200);
63-
t.equal(safeName.status, 200);
64-
t.equal(unsafeName.status, 500);
65-
t.match(stdout, /Starting agent/);
66-
t.match(stderr, /Zen has blocked a NoSQL injection/);
67-
}
68-
)
52+
.then(([noSQLInjection, jsInjection, normalSearch]) => {
53+
t.equal(noSQLInjection.status, 500);
54+
t.equal(jsInjection.status, 500);
55+
t.equal(normalSearch.status, 200);
56+
t.match(stdout, /Starting agent/);
57+
t.match(stderr, /Zen has blocked a NoSQL injection/);
58+
})
6959
.catch((error) => {
7060
t.fail(error);
7161
})
@@ -106,25 +96,15 @@ t.test("it does not block in dry mode", (t) => {
10696
fetch("http://127.0.0.1:4001/?search=title", {
10797
signal: AbortSignal.timeout(5000),
10898
}),
109-
fetch("http://127.0.0.1:4001/hello/hans", {
110-
signal: AbortSignal.timeout(5000),
111-
}),
112-
fetch(`http://127.0.0.1:4001/hello/${encodeURIComponent(`hans" //`)}`, {
113-
signal: AbortSignal.timeout(5000),
114-
}),
11599
])
116100
)
117-
.then(
118-
([noSQLInjection, jsInjection, normalSearch, safeName, unsafeName]) => {
119-
t.equal(noSQLInjection.status, 200);
120-
t.equal(jsInjection.status, 200);
121-
t.equal(normalSearch.status, 200);
122-
t.equal(safeName.status, 200);
123-
t.equal(unsafeName.status, 200);
124-
t.match(stdout, /Starting agent/);
125-
t.notMatch(stderr, /Zen has blocked a NoSQL injection/);
126-
}
127-
)
101+
.then(([noSQLInjection, jsInjection, normalSearch]) => {
102+
t.equal(noSQLInjection.status, 200);
103+
t.equal(jsInjection.status, 200);
104+
t.equal(normalSearch.status, 200);
105+
t.match(stdout, /Starting agent/);
106+
t.notMatch(stderr, /Zen has blocked a NoSQL injection/);
107+
})
128108
.catch((error) => {
129109
t.fail(error);
130110
})

0 commit comments

Comments
 (0)