Skip to content

Commit

Permalink
fix: missed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Dec 19, 2024
1 parent a0e0f60 commit 75bea33
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/metatype.dev/docs/reference/policies/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Policies are hierarchical in the sense that the request starts with a denial, an
query={require("./policies.graphql")}
/>


## Composition rules

### Traversal order
Expand All @@ -48,12 +47,13 @@ The evaluation is as follows:
- `PASS` does not participate.

Or more concretely:

- `ALLOW` & Other -> Other
- `DENY` & Other -> `DENY`
- `PASS` & Other -> Other (`PASS` is a no-op)


Examples:

- `[DENY, DENY, ALLOW]` -> `DENY`
- `[ALLOW, PASS]` -> `ALLOW`
- `[PASS, PASS, PASS]` -> `PASS`
Expand Down
43 changes: 28 additions & 15 deletions src/typegate/src/runtimes/typegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,33 @@ function walkPath(
);
node = resNode;

const getPolicies = (node: TypeNode) => {
const fieldToPolicies = node.type == "object"
? Object.entries(node.policies ?? [])
: [];
const ret = [];
for (const [fieldName, indices] of fieldToPolicies) {
const fmtedIndices = indices.map((index) => {
if (typeof index === "number") {
return tg.policy(index).name;
}

return mapValues(index as Record<string, number>, (value: number) => {
if (value === null) {
return null;
}
return tg.policy(value).name;
});
});

ret.push(
{ fieldName, policies: JSON.stringify(fmtedIndices) },
);
}

return ret;
};

return {
optional: isOptional,
title: node.title,
Expand All @@ -563,20 +590,6 @@ function walkPath(
format: format ?? null,
fields: node.type == "object" ? collectObjectFields(tg, parent) : null,
// TODO enum type on typegraph typegate.py
// FIXME
policies: [],
// policies: node.policies.map((policy) => {
// if (typeof policy === "number") {
// return JSON.stringify(tg.policy(policy).name);
// }
// return JSON.stringify(
// mapValues(policy as Record<string, number>, (value: number) => {
// if (value === null) {
// return null;
// }
// return tg.policy(value).name;
// }),
// );
// }),
policies: getPolicies(node),
};
}
12 changes: 6 additions & 6 deletions tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ snapshot[`typegraphs creation 2`] = `
`;

snapshot[`typegraphs creation 3`] = `
'[
\`[
{
"types": [
{
Expand Down Expand Up @@ -574,7 +574,7 @@ snapshot[`typegraphs creation 3`] = `
"idempotent": true
},
"data": {
"script": "var _my_lambda = () => false",
"script": "var _my_lambda = () => 'DENY'",
"secrets": []
}
},
Expand Down Expand Up @@ -666,7 +666,7 @@ snapshot[`typegraphs creation 3`] = `
}
}
}
]'
]\`
`;

snapshot[`typegraphs creation 4`] = `
Expand Down Expand Up @@ -1082,7 +1082,7 @@ snapshot[`typegraphs creation 5`] = `
`;

snapshot[`typegraphs creation 6`] = `
'[
\`[
{
"types": [
{
Expand Down Expand Up @@ -1243,7 +1243,7 @@ snapshot[`typegraphs creation 6`] = `
"idempotent": true
},
"data": {
"script": "var _my_lambda = () => false",
"script": "var _my_lambda = () => 'DENY'",
"secrets": []
}
},
Expand Down Expand Up @@ -1335,5 +1335,5 @@ snapshot[`typegraphs creation 6`] = `
}
}
}
]'
]\`
`;
16 changes: 10 additions & 6 deletions tests/utils/bindings_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,27 @@ Deno.test("typegraphValidate", () => {
{
"type": "object",
"title": "introspection",
"policies": [],
"properties": {
"__type": 1,
"__schema": 64,
"__schema": 26
},
"id": [],
"required": [
"__type",
"__schema",
"__schema"
],
"policies": {
"__type": [
0
],
"__schema": [
0
]
}
},
{
"type": "function",
"title": "func_79",
"policies": [
0,
],
"input": 2,
"output": 4,
"runtimeConfig": null,
Expand Down

0 comments on commit 75bea33

Please sign in to comment.