From 75bea3397d2545fe055d211510b833444fc4bf5f Mon Sep 17 00:00:00 2001 From: michael-0acf4 Date: Thu, 19 Dec 2024 19:57:40 +0300 Subject: [PATCH] fix: missed tests --- .../docs/reference/policies/index.mdx | 4 +- src/typegate/src/runtimes/typegate.ts | 43 ++++++++++++------- .../__snapshots__/typegraph_test.ts.snap | 12 +++--- tests/utils/bindings_test.ts | 16 ++++--- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/docs/metatype.dev/docs/reference/policies/index.mdx b/docs/metatype.dev/docs/reference/policies/index.mdx index 63f0b7399..31374d872 100644 --- a/docs/metatype.dev/docs/reference/policies/index.mdx +++ b/docs/metatype.dev/docs/reference/policies/index.mdx @@ -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 @@ -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` diff --git a/src/typegate/src/runtimes/typegate.ts b/src/typegate/src/runtimes/typegate.ts index 56084f3b1..8d1a08703 100644 --- a/src/typegate/src/runtimes/typegate.ts +++ b/src/typegate/src/runtimes/typegate.ts @@ -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, (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, @@ -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, (value: number) => { - // if (value === null) { - // return null; - // } - // return tg.policy(value).name; - // }), - // ); - // }), + policies: getPolicies(node), }; } diff --git a/tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap b/tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap index 65a1bbec0..4f0039f62 100644 --- a/tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap +++ b/tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap @@ -413,7 +413,7 @@ snapshot[`typegraphs creation 2`] = ` `; snapshot[`typegraphs creation 3`] = ` -'[ +\`[ { "types": [ { @@ -574,7 +574,7 @@ snapshot[`typegraphs creation 3`] = ` "idempotent": true }, "data": { - "script": "var _my_lambda = () => false", + "script": "var _my_lambda = () => 'DENY'", "secrets": [] } }, @@ -666,7 +666,7 @@ snapshot[`typegraphs creation 3`] = ` } } } -]' +]\` `; snapshot[`typegraphs creation 4`] = ` @@ -1082,7 +1082,7 @@ snapshot[`typegraphs creation 5`] = ` `; snapshot[`typegraphs creation 6`] = ` -'[ +\`[ { "types": [ { @@ -1243,7 +1243,7 @@ snapshot[`typegraphs creation 6`] = ` "idempotent": true }, "data": { - "script": "var _my_lambda = () => false", + "script": "var _my_lambda = () => 'DENY'", "secrets": [] } }, @@ -1335,5 +1335,5 @@ snapshot[`typegraphs creation 6`] = ` } } } -]' +]\` `; diff --git a/tests/utils/bindings_test.ts b/tests/utils/bindings_test.ts index 42689515a..627a90d09 100644 --- a/tests/utils/bindings_test.ts +++ b/tests/utils/bindings_test.ts @@ -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,