Skip to content

Commit

Permalink
Merge pull request #105 from alexolivier/test-output
Browse files Browse the repository at this point in the history
Add support for test output formats
  • Loading branch information
ogazitt committed May 30, 2024
2 parents dc14e9a + 55588b4 commit 293d3c9
Show file tree
Hide file tree
Showing 6 changed files with 700 additions and 437 deletions.
16 changes: 16 additions & 0 deletions interop/authzen-todo-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
## Setup

### Install dependencies

To install the application dependencies, run the following command:

```shell
yarn
```

### Set up the `.env` file

Rename the `.env.example` file to `.env` and update the `AUTHZEN_PDP_URL` variable. The authorization middleware will send AuthZEN requests to `${AUTHZEN_PDP_URL}/access/v1/evaluations`.

Optionally, set the `AUTHZEN_PDP_API_KEY` variable if your authorizer needs an API key. You should prefix it with `basic` or `Bearer` as appropriate. If set, the authorization middleware will add the `authorization: ${AUTHZEN_PDP_API_KEY}` header to every authorization request.
Expand All @@ -35,3 +37,17 @@ yarn dev
yarn build
yarn start
```

## Run tests against a PDP

Output to console:

```shell
yarn test https://authorizer.domain.com console
```

Output as markdown:

```shell
yarn test https://authorizer.domain.com markdown
```
10 changes: 7 additions & 3 deletions interop/authzen-todo-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,37 @@
"main": "src/index.ts",
"scripts": {
"build": "rm -rf ./build && tsc",
"start": "node build/index.js",
"start": "node build/src/index.js",
"dev": "nodemon --ignore db/* ./src/index.ts",
"docker-build": "scripts/docker-build.sh",
"docker-run": "scripts/docker-run.sh",
"tsc": "tsc --noEmit --incremental",
"release": "npm version patch && git push --follow-tags",
"test": "./scripts/test.sh"
"test": "npm run build && node build/test/runner.js"
},
"keywords": [],
"author": "Omri Gazitt <ogazitt@gmail.com>",
"license": "MIT",
"dependencies": {
"array-to-table": "^1.0.1",
"axios": "^1.6.7",
"cli-color": "^2.0.4",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"dotenv-expand": "^9.0.0",
"express": "^4.19.2",
"express-jwt": "8.4.1",
"jwks-rsa": "3.0.0",
"lodash": "^4.17.21",
"node-fetch": "^3.3.2",
"sqlite": "^4.1.2",
"sqlite3": "^5.0.8",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/cli-color": "^2.0.6",
"@types/express": "^4.17.13",
"@types/node": "^17.0.21",
"@types/node": "^17.0.45",
"@types/sqlite3": "^3.1.8",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
Expand Down
23 changes: 0 additions & 23 deletions interop/authzen-todo-backend/scripts/test.sh

This file was deleted.

103 changes: 103 additions & 0 deletions interop/authzen-todo-backend/test/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import arrayToTable from "array-to-table";
import clc from "cli-color";

import { decisions } from "./decisions.json";

const AUTHZEN_PDP_URL =
process.argv[2] || "https://authzen-proxy.demo.aserto.com";
const AUTHZEN_PDP_API_KEY = process.env.AUTHZEN_PDP_API_KEY;

enum OutputTypes {
MARKDOWN,
CONSOLE,
}

const FORMAT =
process.argv[3] === "markdown" ? OutputTypes.MARKDOWN : OutputTypes.CONSOLE;

interface Result {
request: (typeof decisions)[number]["request"];
expected: (typeof decisions)[0]["expected"];
response?: boolean;
status: "PASS" | "FAIL" | "ERROR";
error?: string;
}

async function main() {
const results: Result[] = [];

for (const decision of decisions) {
const REQ = decision.request;
const EXP = decision.expected;
try {
const response = await fetch(`${AUTHZEN_PDP_URL}/access/v1/evaluation`, {
method: "POST",
headers: {
Authorization: AUTHZEN_PDP_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(REQ),
});

const data = await response.json();
const RSP = data.decision || false;

const result: Result = {
request: REQ,
response: RSP,
expected: EXP,
status: JSON.stringify(EXP) === JSON.stringify(RSP) ? "PASS" : "FAIL",
};

results.push(result);
if (FORMAT === OutputTypes.CONSOLE) logResult(result);
} catch (error) {
const result: Result = {
request: REQ,
expected: EXP,
status: "ERROR",
error: error.message,
};

results.push(result);
if (FORMAT === OutputTypes.CONSOLE) logResult(result);
}
}

if (FORMAT === OutputTypes.MARKDOWN) {
console.log(
arrayToTable(
results.map((d) => {
return {
result: d.status,
request: JSON.stringify(d.request),
};
})
)
);
}
}

function logResult(result: Result) {
switch (result.status) {
case "PASS":
console.log(clc.green("PASS"), "REQ:", JSON.stringify(result.request));
break;

case "FAIL":
console.log(clc.red("FAIL"), "REQ:", JSON.stringify(result.request));
break;

default:
console.log(
clc.yellow("ERROR"),
"REQ:",
JSON.stringify(result.request),
"Error:",
result.error
);
break;
}
}

main();
33 changes: 16 additions & 17 deletions interop/authzen-todo-backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"compilerOptions": {
"target": "esnext",
"module": "CommonJS",
"moduleResolution": "node",
"baseUrl": "src",
"outDir": "./build",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src"],
"exclude": ["node_modules"]
}

"compilerOptions": {
"target": "esnext",
"module": "CommonJS",
"moduleResolution": "node",
"baseUrl": "src",
"outDir": "./build",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 293d3c9

Please sign in to comment.