Skip to content

Commit

Permalink
expose /magic endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
d98762625 committed Jun 26, 2020
1 parent d21fc88 commit 9a54a76
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 33 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import errorHandler from "./lib/errorHandler.js";
import helmet from "helmet";

import bakeRouter from "./routes/bake";
import magicRouter from "./routes/magic";

const app = express();
app.disable("x-powered-by");
Expand Down Expand Up @@ -37,6 +38,7 @@ const swaggerFile = fs.readFileSync("./swagger.yml", "utf8");

// Routes
app.use("/bake", bakeRouter);
app.use("/magic", magicRouter);


// Default route
Expand Down
41 changes: 11 additions & 30 deletions package-lock.json

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

22 changes: 22 additions & 0 deletions routes/magic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Router } from "express";
const router = Router();
import { magic } from "cyberchef/src/node/index.mjs";

/**
* magicPost
*/
router.post("/", async function magicPost(req, res, next) {
try {
if (!req.body.input) {
throw new TypeError("'input' property is required in request body");
}

const dish = await magic(req.body.input, req.body.args);
res.send(dish);

} catch (e) {
next(e);
}
});

export default router;
6 changes: 3 additions & 3 deletions test/bake.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe("POST /bake", function() {
.expect("Invalid data type string. No matching enum.", done);
});

it("should perform MAGIC", (done) => {
it("should not perform MAGIC via /bake", (done) => {
request(app)
.post("/bake")
.set("Content-Type", "application/json")
Expand All @@ -203,8 +203,8 @@ describe("POST /bake", function() {
"Magic"
]
})
.expect(200)
.expect("something", done);
.expect(400)
.expect("flowControl operations like Magic are not currently allowed in recipes for chef.bake in the Node API", done);
});

});
76 changes: 76 additions & 0 deletions test/magic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import assert from "assert";
import request from "supertest";
import app from "../app";

describe("POST /magic", function() {
it("should error helpfully if there's no `input` property in request body", (done) => {
request(app)
.post("/magic")
.send({})
.expect(400)
.expect("'input' property is required in request body", done);
});

it("should return JSON when given some input, without content-type header", (done) => {
request(app)
.post("/magic")
.send({input: "WUagwsiae6mP8gNtCCLUFpCpCB26RmBDoDD8PacdAmzAzBVjkK2QstFXaKhpC6iUS7RHqXrJtFisoRSgoJ4whjm1arm864qaNq4RcfUmLHrcsAaZc5TXCYifNdgS83gDeejGX46gaiMyuBV6EskHt1scgJ88x2tNSotQDwbGY1mmCob2ARGFvCKYNqiN9ipMq1ZU1mgkdbNuGcb76aRtYWhCGUc8g93UJudhb8htsheZnwTpgqhx83SVJSZXMXUjJT2zmpC7uXWtumqokbdSi88YtkWDAc1Toouh2oH4D4ddmNKJWUDpMwmngUmK14xwmomccPQE9hM172APnSqwxdKQ172RkcAsysnmj5gGtRmVNNh2s359wr6mS2QRP"})
.expect(200)
.expect(response => {
assert.ok(response);
assert.ok(response.body);
assert.ok(response.body.value);
assert.ok(response.body.type);
assert.deepEqual(response.body.type, 6); // 6 is Dish.JSON enum value
})
.end((err, res) => {
if (err) {
return done(err);
}
return done();
});
});

it("should apply optional arguments provided as an object", (done) => {
request(app)
.post("/magic")
.send({
input: "WUagwsiae6mP8gNtCCLUFpCpCB26RmBDoDD8PacdAmzAzBVjkK2QstFXaKhpC6iUS7RHqXrJtFisoRSgoJ4whjm1arm864qaNq4RcfUmLHrcsAaZc5TXCYifNdgS83gDeejGX46gaiMyuBV6EskHt1scgJ88x2tNSotQDwbGY1mmCob2ARGFvCKYNqiN9ipMq1ZU1mgkdbNuGcb76aRtYWhCGUc8g93UJudhb8htsheZnwTpgqhx83SVJSZXMXUjJT2zmpC7uXWtumqokbdSi88YtkWDAc1Toouh2oH4D4ddmNKJWUDpMwmngUmK14xwmomccPQE9hM172APnSqwxdKQ172RkcAsysnmj5gGtRmVNNh2s359wr6mS2QRP",
args: {
depth: 1,
},
})
.expect(200)
.expect(response => {
assert.ok(Array.isArray(response.body.value));
assert.strictEqual(response.body.value.length, 2); // This would be longer if depth was default 3
})
.end((err, result) => {
if (err) {
return done(err);
}
done();
});
});

it("should apply optional arguments provided as an array", (done) => {
request(app)
.post("/magic")
.send({
input: "WUagwsiae6mP8gNtCCLUFpCpCB26RmBDoDD8PacdAmzAzBVjkK2QstFXaKhpC6iUS7RHqXrJtFisoRSgoJ4whjm1arm864qaNq4RcfUmLHrcsAaZc5TXCYifNdgS83gDeejGX46gaiMyuBV6EskHt1scgJ88x2tNSotQDwbGY1mmCob2ARGFvCKYNqiN9ipMq1ZU1mgkdbNuGcb76aRtYWhCGUc8g93UJudhb8htsheZnwTpgqhx83SVJSZXMXUjJT2zmpC7uXWtumqokbdSi88YtkWDAc1Toouh2oH4D4ddmNKJWUDpMwmngUmK14xwmomccPQE9hM172APnSqwxdKQ172RkcAsysnmj5gGtRmVNNh2s359wr6mS2QRP",
args: [1, true, false, ""],
})
.expect(200)
.expect(response => {
assert.ok(Array.isArray(response.body.value));
assert.strictEqual(response.body.value.length, 24); // intensive language support true => lots of suggestions
})
.end((err, result) => {
if (err) {
return done(err);
}
done();
});
});

});

0 comments on commit 9a54a76

Please sign in to comment.