Skip to content

Commit

Permalink
feat(identity): resolve DID from url and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaSelvaggini committed Aug 3, 2023
1 parent 806a5cc commit 8181d2b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ function initializeSingleContext(block:string):BlockContext{
export const addMiddlewares =
async (baseUrl: string, app: Express) => {
const mws = [
'timestamp', 'git', 'db', 'files', 'redis', 'influxdb', 'http',
'timestamp', 'identity', 'git', 'db', 'files', 'redis', 'influxdb', 'http',
'fabric', 'planetmint', 'sawroom', 'ethereum', 'logger', 'ui',

]
Expand Down
6 changes: 6 additions & 0 deletions packages/create-restroom/templates/restroom.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import zencode from "@restroom-mw/core";
{{#@restroom-mw/timestamp}}
import timestamp from "@restroom-mw/timestamp";
{{/@restroom-mw/timestamp}}
{{#@restroom-mw/identity}}
import identity from "@restroom-mw/identity";
{{/@restroom-mw/identity}}
{{#@restroom-mw/git}}
import git from "@restroom-mw/git";
{{/@restroom-mw/git}}
Expand Down Expand Up @@ -63,6 +66,9 @@ app.use(bodyParser.json());
app.use(morgan("dev"));
app.set("json spaces", 2);

{{#@restroom-mw/identity}}
app.use(identity.default);
{{/@restroom-mw/identity}}
{{#@restroom-mw/db}}
app.use(db.default);
{{/@restroom-mw/db}}
Expand Down
2 changes: 1 addition & 1 deletion packages/identity/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export enum Action {
* @param {string} myUrl - Name of the variable containing the url of the resolver
* @param {string} myOutput - Name of the variable where the output json will be stored
*/
RESOLVE_DID = "Given I resolve the did {} from {} and save the output into {}",
RESOLVE_DID = "resolve the did {} from {} and save the output into {}",
}
29 changes: 29 additions & 0 deletions packages/identity/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { Restroom } from "@restroom-mw/core";
import { ObjectLiteral } from "@restroom-mw/types";
import { Zencode } from "@restroom-mw/zencode";
import { NextFunction, Request, Response } from "express";
import axios from "axios";
import { Action } from "./actions";

export default (req: Request, res: Response, next: NextFunction) => {
const rr = new Restroom(req, res);
let content: ObjectLiteral = {};

rr.onBefore(
async (params: {
zencode: Zencode;
keys: ObjectLiteral;
data: ObjectLiteral;
}) => {
let { zencode, keys, data } = params;
content = rr.combineDataKeys(data, keys);

if (zencode.match(Action.RESOLVE_DID)) {
let [did, urlName, o] = zencode.paramsOf(Action.RESOLVE_DID);
let url = content[urlName]
if( url[-1] !== "/") {
url = url + "/";
}
const url_resolver = url + content[did];
const response = await axios.get(url_resolver);
data[o] = response.data;
}
}
)
next();
}
35 changes: 35 additions & 0 deletions packages/identity/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import test from "ava";
import request from "supertest";
import express from "express";
import bodyParser from "body-parser";

process.env.ZENCODE_DIR = "./test/identity";
const identity = require("..").default;
const zencode = require("../../core").default;

test("Resolve did",
async (t) => {
const app = express();
app.use(bodyParser.json());
app.use(identity);
app.use("/*", zencode);
const res = await request(app).post("/resolve-did");
t.true(
Object.keys(res.body).includes("myOutput"),
'could not find "myOutput " in response'
);
const output = res.body.myOutput;
t.true(
Object.keys(output).includes("didDocument"),
'could not find "didDocument" in myOutput'
);
t.true(
Object.keys(output).includes("didDocumentMetadata"),
'could not find "didDocumentMetadata" in myOutput'
);
t.true(
Object.keys(output).includes("@context"),
'could not find "@context" in myOutput'
);
t.is(res.status, 200);
});
2 changes: 1 addition & 1 deletion packages/identity/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
}, {
"path": "../types"
}],
"exclude": ["node_modules", "dist", "tests"]
"exclude": ["dist", "tests"]
}
4 changes: 4 additions & 0 deletions test/identity/resolve-did.keys
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"myUrl": "https://did.dyne.org/dids",
"myDid": "did:dyne:sandbox.zenswarm:HwSjX94eXVmVpqGnN6L5HD3rjSBwxJeNc9xByJKB83wB"
}
8 changes: 8 additions & 0 deletions test/identity/resolve-did.zen
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Rule caller restroom-mw

Given I have a 'string' named 'myDid'
Given I have a 'string' named 'myUrl'
Given I have a 'string dictionary' named 'myOutput'
Given I resolve the did 'myDid' from 'myUrl' and save the output into 'myOutput'

Then print all data

0 comments on commit 8181d2b

Please sign in to comment.