Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all the available sentences registered in Restroom-mws #206

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"supertest": "^6.1.3",
"ts-node": "^10.5.0",
"tslint": "^6.1.3",
"turbo": "^1.0.9",
"turbo": "^1.8.6",
"typescript": "^4.6.2"
},
"resolutions": {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isErrorResult,
isChainLastBlock,
SLASH,
getFile,
} from "./utils";
import { zencode_exec } from "zenroom";
import { Zencode } from "@restroom-mw/zencode";
Expand Down Expand Up @@ -58,6 +59,12 @@ const dispatch = async (req: Request, res: Response, next: NextFunction) => {
return;
}

if (req.params[0] === "") {
res.contentType('text/plain')
res.send(getFile("sids"));
return;
}

const runHook = (hook: string, args: any) => {
try {
return getHooks(hook, res, args);
Expand Down
12 changes: 11 additions & 1 deletion packages/core/src/restroom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ObjectLiteral } from "@restroom-mw/types";
import { DK } from "./types";
import { getFile } from "./utils";
import fs from "fs";

/**
*
Expand All @@ -16,9 +18,17 @@ import { DK } from "./types";
export class Restroom {
_req: any;
_res: any;
constructor(req: any, res: any) {
constructor(req: any, res: any, sids: string[]) {
this._req = req;
this._res = res;
const sidsFile = getFile("sids");
if (sidsFile === null) {
fs.appendFileSync(`${process.env.ZENCODE_DIR as string}/sids`, sids.join('\n'));
} else {
const registered = new Set (sidsFile.split("\n"));
sids.map(s => registered.add(s));
fs.writeFileSync(`${process.env.ZENCODE_DIR as string}/sids`, Array.from(registered).join('\n'));
}
}

_hook(name: string, promise: (params: Promise<void>) => Promise<void>) {
Expand Down
30 changes: 19 additions & 11 deletions packages/core/tests/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ test("Check that the middleware detects when zenfile and zencontent are missing
const res = await request(app).post("/api/missing-zenfile.chain");

t.true(
res.body.exception.includes("Neither zenFile nor zenContent are declared for block id"),
res.body.exception.includes(
"Neither zenFile nor zenContent are declared for block id"
),
res.body.exception
);
t.is(res.status, 500);
Expand All @@ -62,8 +64,10 @@ test("Check that the middleware provide context when debug is on for missing zen
test("Check that the middleware provide context when debug is on for gpp sawroom sample", async (t) => {
const app = express();
app.use("/api/*", core);
for(const url of ["gpp-sawroom-sample-debug",
"gpp-sawroom-sample-debug-inplace"]) {
for (const url of [
"gpp-sawroom-sample-debug",
"gpp-sawroom-sample-debug-inplace",
]) {
const res = await request(app).post(`/api/${url}.chain`);

t.is(res.body.context.debugEnabled, true);
Expand All @@ -74,11 +78,15 @@ test("Check that the middleware provide context when debug is on for gpp sawroom
test("Check that the middleware detects a duplicated mapping key in yaml blocks", async (t) => {
const app = express();
app.use("/api/*", core);
for(const url of ["duplicated-mapping-key",
"duplicated-mapping-key-inplace"]) {
for (const url of [
"duplicated-mapping-key",
"duplicated-mapping-key-inplace",
]) {
const res = await request(app).post(`/api/${url}.chain`);

t.true(res.body.exception.includes("YAMLException: duplicated mapping key"));
t.true(
res.body.exception.includes("YAMLException: duplicated mapping key")
);
t.is(res.status, 500);
}
});
Expand Down Expand Up @@ -126,11 +134,11 @@ test("Check that the middleware is able to execute for each chain with for each
app.use(bodyParser.json());
app.use("/api/*", core);

for(const url of ["gpp-sawroom-sample-foreach",
"gpp-sawroom-sample-foreach-inplace"]) {
const res = await request(app)
.post(`/api/${url}.chain`)
.send(_data);
for (const url of [
"gpp-sawroom-sample-foreach",
"gpp-sawroom-sample-foreach-inplace",
]) {
const res = await request(app).post(`/api/${url}.chain`).send(_data);

t.is(res.body.myUsers.Pippo.password, "p1pp0");
t.is(res.body.myUsers.Topolino.password, "t0p0l1n0");
Expand Down
10 changes: 8 additions & 2 deletions packages/core/tests/validations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ test("validateZen works correctly if forEachObject is undefined", (t) => {
validateZen(singleContext, block);
});

t.is(error.message, "Neither zenFile nor zenContent are declared for block id: currentBlock");
t.is(
error.message,
"Neither zenFile nor zenContent are declared for block id: currentBlock"
);
});

test("validateZen works correctly if I pass both zenContent and zenFile", (t) => {
Expand All @@ -128,7 +131,10 @@ test("validateZen works correctly if I pass both zenContent and zenFile", (t) =>
validateZen(singleContext, block);
});

t.is(error.message, "Cannot declare both zenContent and zenFile for block id: currentBlock");
t.is(
error.message,
"Cannot declare both zenContent and zenFile for block id: currentBlock"
);
});

test("validatePathsInYml works correctly if different paths are present", (t) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Result extends Model {
*
* @enum {number}
*/
const enum ACTIONS {
enum ACTIONS {
/**
* Given I have a database uri named {}
* @param {string} uri the key of the connection string
Expand Down Expand Up @@ -81,7 +81,7 @@ const enum ACTIONS {

export default (req: Request, res: Response, next: NextFunction) => {
try {
const rr = new Restroom(req, res);
const rr = new Restroom(req, res, Object.values(ACTIONS));
const parse = (o: string) => rr.safeJSONParse(o, `[DATABASE] Error in JSON format "${o}"`)
let content: ObjectLiteral = {};
let contentKeys: string[];
Expand Down
Loading