@opensaas/stack-core 0.43.0 → 0.44.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2720 -0
- package/README.md +182 -115
- package/dist/.build-hash +1 -0
- package/dist/access/declared-dependencies.d.ts +102 -88
- package/dist/access/declared-dependencies.d.ts.map +1 -1
- package/dist/access/declared-dependencies.js +83 -71
- package/dist/access/declared-dependencies.js.map +1 -1
- package/dist/access/depth-limits.d.ts +4 -0
- package/dist/access/depth-limits.d.ts.map +1 -1
- package/dist/access/depth-limits.js +4 -0
- package/dist/access/depth-limits.js.map +1 -1
- package/dist/access/engine.d.ts +68 -20
- package/dist/access/engine.d.ts.map +1 -1
- package/dist/access/engine.js +151 -37
- package/dist/access/engine.js.map +1 -1
- package/dist/access/errors.d.ts +33 -48
- package/dist/access/errors.d.ts.map +1 -1
- package/dist/access/errors.js +73 -63
- package/dist/access/errors.js.map +1 -1
- package/dist/access/field-access.d.ts +76 -5
- package/dist/access/field-access.d.ts.map +1 -1
- package/dist/access/field-access.js +235 -61
- package/dist/access/field-access.js.map +1 -1
- package/dist/access/field-visibility.d.ts +50 -4
- package/dist/access/field-visibility.d.ts.map +1 -1
- package/dist/access/field-visibility.js +143 -160
- package/dist/access/field-visibility.js.map +1 -1
- package/dist/access/index.d.ts +5 -12
- package/dist/access/index.d.ts.map +1 -1
- package/dist/access/index.js +6 -19
- package/dist/access/index.js.map +1 -1
- package/dist/access/orm-client.d.ts +19 -0
- package/dist/access/orm-client.d.ts.map +1 -0
- package/dist/access/orm-client.js +37 -0
- package/dist/access/orm-client.js.map +1 -0
- package/dist/access/query-validation.d.ts +7 -129
- package/dist/access/query-validation.d.ts.map +1 -1
- package/dist/access/query-validation.js +12 -314
- package/dist/access/query-validation.js.map +1 -1
- package/dist/access/system-fields.d.ts +2 -0
- package/dist/access/system-fields.d.ts.map +1 -0
- package/dist/access/system-fields.js +7 -0
- package/dist/access/system-fields.js.map +1 -0
- package/dist/access/transaction-registry.d.ts +25 -2
- package/dist/access/transaction-registry.d.ts.map +1 -1
- package/dist/access/transaction-registry.js +34 -17
- package/dist/access/transaction-registry.js.map +1 -1
- package/dist/access/types.d.ts +217 -131
- package/dist/access/types.d.ts.map +1 -1
- package/dist/config/field-kind.d.ts +66 -0
- package/dist/config/field-kind.d.ts.map +1 -0
- package/dist/config/field-kind.js +63 -0
- package/dist/config/field-kind.js.map +1 -0
- package/dist/config/hook-only-fields.d.ts +3 -0
- package/dist/config/hook-only-fields.d.ts.map +1 -0
- package/dist/config/hook-only-fields.js +17 -0
- package/dist/config/hook-only-fields.js.map +1 -0
- package/dist/config/index.d.ts +3 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +6 -3
- package/dist/config/index.js.map +1 -1
- package/dist/config/mcp-tool-names.d.ts +3 -0
- package/dist/config/mcp-tool-names.d.ts.map +1 -0
- package/dist/config/mcp-tool-names.js +25 -0
- package/dist/config/mcp-tool-names.js.map +1 -0
- package/dist/config/nav-count.d.ts.map +1 -1
- package/dist/config/nav-count.js +4 -4
- package/dist/config/nav-count.js.map +1 -1
- package/dist/config/plugin-engine.d.ts +8 -2
- package/dist/config/plugin-engine.d.ts.map +1 -1
- package/dist/config/plugin-engine.js +102 -38
- package/dist/config/plugin-engine.js.map +1 -1
- package/dist/config/types.d.ts +966 -614
- package/dist/config/types.d.ts.map +1 -1
- package/dist/context/apply-defaults.d.ts +18 -1
- package/dist/context/apply-defaults.d.ts.map +1 -1
- package/dist/context/apply-defaults.js +30 -4
- package/dist/context/apply-defaults.js.map +1 -1
- package/dist/context/engine-context.d.ts +73 -0
- package/dist/context/engine-context.d.ts.map +1 -0
- package/dist/context/engine-context.js +67 -0
- package/dist/context/engine-context.js.map +1 -0
- package/dist/context/hook-pipeline.d.ts +9 -2
- package/dist/context/hook-pipeline.d.ts.map +1 -1
- package/dist/context/hook-pipeline.js +5 -4
- package/dist/context/hook-pipeline.js.map +1 -1
- package/dist/context/index.d.ts +98 -89
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +917 -722
- package/dist/context/index.js.map +1 -1
- package/dist/context/junction.d.ts +59 -0
- package/dist/context/junction.d.ts.map +1 -0
- package/dist/context/junction.js +126 -0
- package/dist/context/junction.js.map +1 -0
- package/dist/context/plugin-field-read.d.ts +33 -0
- package/dist/context/plugin-field-read.d.ts.map +1 -0
- package/dist/context/plugin-field-read.js +41 -0
- package/dist/context/plugin-field-read.js.map +1 -0
- package/dist/context/plugin-field-write.d.ts +104 -0
- package/dist/context/plugin-field-write.d.ts.map +1 -0
- package/dist/context/plugin-field-write.js +184 -0
- package/dist/context/plugin-field-write.js.map +1 -0
- package/dist/context/relationship-input.d.ts +146 -0
- package/dist/context/relationship-input.d.ts.map +1 -0
- package/dist/context/relationship-input.js +453 -0
- package/dist/context/relationship-input.js.map +1 -0
- package/dist/context/transaction-boundary.d.ts +54 -28
- package/dist/context/transaction-boundary.d.ts.map +1 -1
- package/dist/context/transaction-boundary.js +87 -169
- package/dist/context/transaction-boundary.js.map +1 -1
- package/dist/context/write-pipeline.d.ts +33 -51
- package/dist/context/write-pipeline.d.ts.map +1 -1
- package/dist/context/write-pipeline.js +372 -185
- package/dist/context/write-pipeline.js.map +1 -1
- package/dist/contract/dependencies.d.ts +88 -0
- package/dist/contract/dependencies.d.ts.map +1 -0
- package/dist/contract/dependencies.js +237 -0
- package/dist/contract/dependencies.js.map +1 -0
- package/dist/contract/derive.d.ts +49 -0
- package/dist/contract/derive.d.ts.map +1 -0
- package/dist/contract/derive.js +540 -0
- package/dist/contract/derive.js.map +1 -0
- package/dist/contract/fixtures/contract-configs.d.ts +64 -0
- package/dist/contract/fixtures/contract-configs.d.ts.map +1 -0
- package/dist/contract/fixtures/contract-configs.js +345 -0
- package/dist/contract/fixtures/contract-configs.js.map +1 -0
- package/dist/contract/id-boundary.d.ts +53 -0
- package/dist/contract/id-boundary.d.ts.map +1 -0
- package/dist/contract/id-boundary.js +76 -0
- package/dist/contract/id-boundary.js.map +1 -0
- package/dist/contract/index.d.ts +8 -0
- package/dist/contract/index.d.ts.map +1 -0
- package/dist/contract/index.js +11 -0
- package/dist/contract/index.js.map +1 -0
- package/dist/contract/prisma.d.ts +58 -0
- package/dist/contract/prisma.d.ts.map +1 -0
- package/dist/contract/prisma.js +282 -0
- package/dist/contract/prisma.js.map +1 -0
- package/dist/contract/relation-graph.d.ts +70 -0
- package/dist/contract/relation-graph.d.ts.map +1 -0
- package/dist/contract/relation-graph.js +113 -0
- package/dist/contract/relation-graph.js.map +1 -0
- package/dist/contract/types.d.ts +135 -0
- package/dist/contract/types.d.ts.map +1 -0
- package/dist/contract/types.js +2 -0
- package/dist/contract/types.js.map +1 -0
- package/dist/db/client.d.ts +55 -0
- package/dist/db/client.d.ts.map +1 -0
- package/dist/db/client.js +73 -0
- package/dist/db/client.js.map +1 -0
- package/dist/db/dev-database.d.ts +124 -0
- package/dist/db/dev-database.d.ts.map +1 -0
- package/dist/db/dev-database.js +291 -0
- package/dist/db/dev-database.js.map +1 -0
- package/dist/db/password-front.d.ts +10 -0
- package/dist/db/password-front.d.ts.map +1 -0
- package/dist/db/password-front.js +147 -0
- package/dist/db/password-front.js.map +1 -0
- package/dist/db/process-identity.d.ts +36 -0
- package/dist/db/process-identity.d.ts.map +1 -0
- package/dist/db/process-identity.js +114 -0
- package/dist/db/process-identity.js.map +1 -0
- package/dist/db/state-file.d.ts +54 -0
- package/dist/db/state-file.d.ts.map +1 -0
- package/dist/db/state-file.js +91 -0
- package/dist/db/state-file.js.map +1 -0
- package/dist/db/url.d.ts +89 -0
- package/dist/db/url.d.ts.map +1 -0
- package/dist/db/url.js +118 -0
- package/dist/db/url.js.map +1 -0
- package/dist/extend.d.ts +13 -2
- package/dist/extend.d.ts.map +1 -1
- package/dist/extend.js +24 -1
- package/dist/extend.js.map +1 -1
- package/dist/fields/index.d.ts +87 -42
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +457 -677
- package/dist/fields/index.js.map +1 -1
- package/dist/filter/collect.d.ts +3 -4
- package/dist/filter/collect.d.ts.map +1 -1
- package/dist/filter/collect.js +2 -2
- package/dist/filter/collect.js.map +1 -1
- package/dist/filter/index.d.ts +1 -2
- package/dist/filter/index.d.ts.map +1 -1
- package/dist/filter/index.js +0 -1
- package/dist/filter/index.js.map +1 -1
- package/dist/filter/map.d.ts +12 -5
- package/dist/filter/map.d.ts.map +1 -1
- package/dist/filter/map.js +47 -19
- package/dist/filter/map.js.map +1 -1
- package/dist/filter/types.d.ts +10 -24
- package/dist/filter/types.d.ts.map +1 -1
- package/dist/filter/types.js +2 -11
- package/dist/filter/types.js.map +1 -1
- package/dist/hooks/index.d.ts +22 -31
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +33 -37
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/transition-guard.d.ts +61 -0
- package/dist/hooks/transition-guard.d.ts.map +1 -0
- package/dist/hooks/transition-guard.js +93 -0
- package/dist/hooks/transition-guard.js.map +1 -0
- package/dist/index.d.ts +52 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +105 -51
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +4 -2
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +16 -0
- package/dist/internal.js.map +1 -1
- package/dist/lib/case-utils.d.ts +0 -1
- package/dist/lib/case-utils.d.ts.map +1 -1
- package/dist/lib/case-utils.js +0 -3
- package/dist/lib/case-utils.js.map +1 -1
- package/dist/lib/client-safe-error.d.ts +9 -0
- package/dist/lib/client-safe-error.d.ts.map +1 -0
- package/dist/lib/client-safe-error.js +23 -0
- package/dist/lib/client-safe-error.js.map +1 -0
- package/dist/lib/database-errors.d.ts +78 -0
- package/dist/lib/database-errors.d.ts.map +1 -0
- package/dist/lib/database-errors.js +148 -0
- package/dist/lib/database-errors.js.map +1 -0
- package/dist/lib/null-prototype-registry.d.ts +9 -0
- package/dist/lib/null-prototype-registry.d.ts.map +1 -0
- package/dist/lib/null-prototype-registry.js +11 -0
- package/dist/lib/null-prototype-registry.js.map +1 -0
- package/dist/lib/prisma-errors.d.ts +30 -32
- package/dist/lib/prisma-errors.d.ts.map +1 -1
- package/dist/lib/prisma-errors.js +86 -46
- package/dist/lib/prisma-errors.js.map +1 -1
- package/dist/lib/process-global.d.ts +23 -0
- package/dist/lib/process-global.d.ts.map +1 -0
- package/dist/lib/process-global.js +33 -0
- package/dist/lib/process-global.js.map +1 -0
- package/dist/lib/warn-once.d.ts +7 -0
- package/dist/lib/warn-once.d.ts.map +1 -0
- package/dist/lib/warn-once.js +13 -0
- package/dist/lib/warn-once.js.map +1 -0
- package/dist/mcp/advertise.d.ts +16 -0
- package/dist/mcp/advertise.d.ts.map +1 -0
- package/dist/mcp/advertise.js +25 -0
- package/dist/mcp/advertise.js.map +1 -0
- package/dist/mcp/arguments.d.ts +10 -0
- package/dist/mcp/arguments.d.ts.map +1 -0
- package/dist/mcp/arguments.js +143 -0
- package/dist/mcp/arguments.js.map +1 -0
- package/dist/mcp/field-schema.d.ts +50 -5
- package/dist/mcp/field-schema.d.ts.map +1 -1
- package/dist/mcp/field-schema.js +137 -14
- package/dist/mcp/field-schema.js.map +1 -1
- package/dist/mcp/handler.d.ts +16 -5
- package/dist/mcp/handler.d.ts.map +1 -1
- package/dist/mcp/handler.js +404 -126
- package/dist/mcp/handler.js.map +1 -1
- package/dist/mcp/index.d.ts +1 -0
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +1 -0
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/projection.d.ts +42 -34
- package/dist/mcp/projection.d.ts.map +1 -1
- package/dist/mcp/projection.js +246 -200
- package/dist/mcp/projection.js.map +1 -1
- package/dist/mcp/tool-error.d.ts +8 -0
- package/dist/mcp/tool-error.d.ts.map +1 -0
- package/dist/mcp/tool-error.js +11 -0
- package/dist/mcp/tool-error.js.map +1 -0
- package/dist/mcp/types.d.ts +3 -2
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/mcp/where-id-boundary.d.ts +28 -0
- package/dist/mcp/where-id-boundary.d.ts.map +1 -0
- package/dist/mcp/where-id-boundary.js +187 -0
- package/dist/mcp/where-id-boundary.js.map +1 -0
- package/dist/origin.d.ts +147 -0
- package/dist/origin.d.ts.map +1 -0
- package/dist/origin.js +191 -0
- package/dist/origin.js.map +1 -0
- package/dist/query/relationship-options.d.ts +35 -6
- package/dist/query/relationship-options.d.ts.map +1 -1
- package/dist/query/relationship-options.js +33 -23
- package/dist/query/relationship-options.js.map +1 -1
- package/dist/secured/aggregate.d.ts +32 -0
- package/dist/secured/aggregate.d.ts.map +1 -0
- package/dist/secured/aggregate.js +31 -0
- package/dist/secured/aggregate.js.map +1 -0
- package/dist/secured/include.d.ts +320 -0
- package/dist/secured/include.d.ts.map +1 -0
- package/dist/secured/include.js +650 -0
- package/dist/secured/include.js.map +1 -0
- package/dist/secured/lock.d.ts +127 -0
- package/dist/secured/lock.d.ts.map +1 -0
- package/dist/secured/lock.js +275 -0
- package/dist/secured/lock.js.map +1 -0
- package/dist/secured/lower.d.ts +82 -0
- package/dist/secured/lower.d.ts.map +1 -0
- package/dist/secured/lower.js +162 -0
- package/dist/secured/lower.js.map +1 -0
- package/dist/secured/operators.d.ts +7 -0
- package/dist/secured/operators.d.ts.map +1 -0
- package/dist/secured/operators.js +18 -0
- package/dist/secured/operators.js.map +1 -0
- package/dist/secured/read.d.ts +208 -0
- package/dist/secured/read.d.ts.map +1 -0
- package/dist/secured/read.js +1245 -0
- package/dist/secured/read.js.map +1 -0
- package/dist/secured/select.d.ts +87 -0
- package/dist/secured/select.d.ts.map +1 -0
- package/dist/secured/select.js +250 -0
- package/dist/secured/select.js.map +1 -0
- package/dist/secured/vector.d.ts +58 -0
- package/dist/secured/vector.d.ts.map +1 -0
- package/dist/secured/vector.js +113 -0
- package/dist/secured/vector.js.map +1 -0
- package/dist/secured/vocabulary.d.ts +227 -0
- package/dist/secured/vocabulary.d.ts.map +1 -0
- package/dist/secured/vocabulary.js +578 -0
- package/dist/secured/vocabulary.js.map +1 -0
- package/dist/secured/write.d.ts +58 -0
- package/dist/secured/write.d.ts.map +1 -0
- package/dist/secured/write.js +87 -0
- package/dist/secured/write.js.map +1 -0
- package/dist/testing/context.d.ts +236 -0
- package/dist/testing/context.d.ts.map +1 -0
- package/dist/testing/context.js +457 -0
- package/dist/testing/context.js.map +1 -0
- package/dist/testing/escape.d.ts +83 -0
- package/dist/testing/escape.d.ts.map +1 -0
- package/dist/testing/escape.js +148 -0
- package/dist/testing/escape.js.map +1 -0
- package/dist/testing/extensions.d.ts +48 -0
- package/dist/testing/extensions.d.ts.map +1 -0
- package/dist/testing/extensions.js +107 -0
- package/dist/testing/extensions.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +11 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/plans.d.ts +64 -0
- package/dist/testing/plans.d.ts.map +1 -0
- package/dist/testing/plans.js +47 -0
- package/dist/testing/plans.js.map +1 -0
- package/dist/types/context.d.ts +110 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/contract.d.ts +198 -0
- package/dist/types/contract.d.ts.map +1 -0
- package/dist/types/contract.js +2 -0
- package/dist/types/contract.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +11 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/inputs.d.ts +83 -0
- package/dist/types/inputs.d.ts.map +1 -0
- package/dist/types/inputs.js +2 -0
- package/dist/types/inputs.js.map +1 -0
- package/dist/types/rows.d.ts +76 -0
- package/dist/types/rows.d.ts.map +1 -0
- package/dist/types/rows.js +2 -0
- package/dist/types/rows.js.map +1 -0
- package/dist/types/secured-list.d.ts +396 -0
- package/dist/types/secured-list.d.ts.map +1 -0
- package/dist/types/secured-list.js +2 -0
- package/dist/types/secured-list.js.map +1 -0
- package/dist/unsafe.d.ts +162 -0
- package/dist/unsafe.d.ts.map +1 -0
- package/dist/unsafe.js +177 -0
- package/dist/unsafe.js.map +1 -0
- package/dist/validation/config-refusal.d.ts +16 -0
- package/dist/validation/config-refusal.d.ts.map +1 -0
- package/dist/validation/config-refusal.js +2 -0
- package/dist/validation/config-refusal.js.map +1 -0
- package/dist/validation/database-config.d.ts +19 -0
- package/dist/validation/database-config.d.ts.map +1 -0
- package/dist/validation/database-config.js +221 -0
- package/dist/validation/database-config.js.map +1 -0
- package/dist/validation/default-value.d.ts +21 -0
- package/dist/validation/default-value.d.ts.map +1 -0
- package/dist/validation/default-value.js +72 -0
- package/dist/validation/default-value.js.map +1 -0
- package/dist/validation/extension-packs.d.ts +11 -0
- package/dist/validation/extension-packs.d.ts.map +1 -0
- package/dist/validation/extension-packs.js +53 -0
- package/dist/validation/extension-packs.js.map +1 -0
- package/dist/validation/field-config.d.ts +35 -21
- package/dist/validation/field-config.d.ts.map +1 -1
- package/dist/validation/field-config.js +51 -41
- package/dist/validation/field-config.js.map +1 -1
- package/dist/validation/field-names.d.ts +15 -0
- package/dist/validation/field-names.d.ts.map +1 -0
- package/dist/validation/field-names.js +155 -0
- package/dist/validation/field-names.js.map +1 -0
- package/dist/validation/identifier-names.d.ts +4 -0
- package/dist/validation/identifier-names.d.ts.map +1 -0
- package/dist/validation/identifier-names.js +39 -0
- package/dist/validation/identifier-names.js.map +1 -0
- package/dist/validation/needs-closure.d.ts +20 -30
- package/dist/validation/needs-closure.d.ts.map +1 -1
- package/dist/validation/needs-closure.js +29 -105
- package/dist/validation/needs-closure.js.map +1 -1
- package/dist/validation/relations.d.ts +17 -0
- package/dist/validation/relations.d.ts.map +1 -0
- package/dist/validation/relations.js +268 -0
- package/dist/validation/relations.js.map +1 -0
- package/dist/validation/schema.d.ts.map +1 -1
- package/dist/validation/schema.js +1 -3
- package/dist/validation/schema.js.map +1 -1
- package/dist/where/like.d.ts +32 -0
- package/dist/where/like.d.ts.map +1 -0
- package/dist/where/like.js +42 -0
- package/dist/where/like.js.map +1 -0
- package/package.json +70 -8
- package/.turbo/turbo-build.log +0 -4
- package/CLAUDE.md +0 -609
- package/dist/access/access-filter.d.ts +0 -241
- package/dist/access/access-filter.d.ts.map +0 -1
- package/dist/access/access-filter.js +0 -692
- package/dist/access/access-filter.js.map +0 -1
- package/dist/access/access-filter.test.d.ts +0 -2
- package/dist/access/access-filter.test.d.ts.map +0 -1
- package/dist/access/access-filter.test.js +0 -1256
- package/dist/access/access-filter.test.js.map +0 -1
- package/dist/access/declared-dependencies.test.d.ts +0 -2
- package/dist/access/declared-dependencies.test.d.ts.map +0 -1
- package/dist/access/declared-dependencies.test.js +0 -226
- package/dist/access/declared-dependencies.test.js.map +0 -1
- package/dist/access/denied-relation-visibility.test.d.ts +0 -2
- package/dist/access/denied-relation-visibility.test.d.ts.map +0 -1
- package/dist/access/denied-relation-visibility.test.js +0 -114
- package/dist/access/denied-relation-visibility.test.js.map +0 -1
- package/dist/access/engine.test.d.ts +0 -2
- package/dist/access/engine.test.d.ts.map +0 -1
- package/dist/access/engine.test.js +0 -56
- package/dist/access/engine.test.js.map +0 -1
- package/dist/access/field-access.test.d.ts +0 -2
- package/dist/access/field-access.test.d.ts.map +0 -1
- package/dist/access/field-access.test.js +0 -635
- package/dist/access/field-access.test.js.map +0 -1
- package/dist/access/multi-column-read-write.test.d.ts +0 -2
- package/dist/access/multi-column-read-write.test.d.ts.map +0 -1
- package/dist/access/multi-column-read-write.test.js +0 -183
- package/dist/access/multi-column-read-write.test.js.map +0 -1
- package/dist/access/relationship-count.d.ts +0 -98
- package/dist/access/relationship-count.d.ts.map +0 -1
- package/dist/access/relationship-count.js +0 -262
- package/dist/access/relationship-count.js.map +0 -1
- package/dist/access/relationship-count.test.d.ts +0 -2
- package/dist/access/relationship-count.test.d.ts.map +0 -1
- package/dist/access/relationship-count.test.js +0 -247
- package/dist/access/relationship-count.test.js.map +0 -1
- package/dist/access/relationship-label-filter.d.ts +0 -45
- package/dist/access/relationship-label-filter.d.ts.map +0 -1
- package/dist/access/relationship-label-filter.js +0 -22
- package/dist/access/relationship-label-filter.js.map +0 -1
- package/dist/access/relationship-label-filter.test.d.ts +0 -2
- package/dist/access/relationship-label-filter.test.d.ts.map +0 -1
- package/dist/access/relationship-label-filter.test.js +0 -89
- package/dist/access/relationship-label-filter.test.js.map +0 -1
- package/dist/access/synthetic-include-read.test.d.ts +0 -2
- package/dist/access/synthetic-include-read.test.d.ts.map +0 -1
- package/dist/access/synthetic-include-read.test.js +0 -121
- package/dist/access/synthetic-include-read.test.js.map +0 -1
- package/dist/context/nested-operations.d.ts +0 -74
- package/dist/context/nested-operations.d.ts.map +0 -1
- package/dist/context/nested-operations.js +0 -847
- package/dist/context/nested-operations.js.map +0 -1
- package/dist/fields/calendar-day.test.d.ts +0 -2
- package/dist/fields/calendar-day.test.d.ts.map +0 -1
- package/dist/fields/calendar-day.test.js +0 -246
- package/dist/fields/calendar-day.test.js.map +0 -1
- package/dist/fields/format-prisma-default.d.ts +0 -35
- package/dist/fields/format-prisma-default.d.ts.map +0 -1
- package/dist/fields/format-prisma-default.js +0 -49
- package/dist/fields/format-prisma-default.js.map +0 -1
- package/dist/fields/format-prisma-default.test.d.ts +0 -2
- package/dist/fields/format-prisma-default.test.d.ts.map +0 -1
- package/dist/fields/format-prisma-default.test.js +0 -54
- package/dist/fields/format-prisma-default.test.js.map +0 -1
- package/dist/fields/relationship-foreign-key.test.d.ts +0 -2
- package/dist/fields/relationship-foreign-key.test.d.ts.map +0 -1
- package/dist/fields/relationship-foreign-key.test.js +0 -18
- package/dist/fields/relationship-foreign-key.test.js.map +0 -1
- package/dist/fields/select.test.d.ts +0 -2
- package/dist/fields/select.test.d.ts.map +0 -1
- package/dist/fields/select.test.js +0 -279
- package/dist/fields/select.test.js.map +0 -1
- package/dist/fields/text-keystone-compat.test.d.ts +0 -2
- package/dist/fields/text-keystone-compat.test.d.ts.map +0 -1
- package/dist/fields/text-keystone-compat.test.js +0 -93
- package/dist/fields/text-keystone-compat.test.js.map +0 -1
- package/dist/filter/filter.test.d.ts +0 -2
- package/dist/filter/filter.test.d.ts.map +0 -1
- package/dist/filter/filter.test.js +0 -431
- package/dist/filter/filter.test.js.map +0 -1
- package/dist/filter/serialize.test.d.ts +0 -2
- package/dist/filter/serialize.test.d.ts.map +0 -1
- package/dist/filter/serialize.test.js +0 -83
- package/dist/filter/serialize.test.js.map +0 -1
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.d.ts.map +0 -1
- package/dist/index.test.js +0 -33
- package/dist/index.test.js.map +0 -1
- package/dist/lib/case-utils.test.d.ts +0 -2
- package/dist/lib/case-utils.test.d.ts.map +0 -1
- package/dist/lib/case-utils.test.js +0 -123
- package/dist/lib/case-utils.test.js.map +0 -1
- package/dist/query/index.d.ts +0 -127
- package/dist/query/index.d.ts.map +0 -1
- package/dist/query/index.js +0 -179
- package/dist/query/index.js.map +0 -1
- package/dist/query/index.test.d.ts +0 -2
- package/dist/query/index.test.d.ts.map +0 -1
- package/dist/query/index.test.js +0 -632
- package/dist/query/index.test.js.map +0 -1
- package/dist/query/relationship-options.test.d.ts +0 -2
- package/dist/query/relationship-options.test.d.ts.map +0 -1
- package/dist/query/relationship-options.test.js +0 -150
- package/dist/query/relationship-options.test.js.map +0 -1
- package/dist/validation/field-config.test.d.ts +0 -2
- package/dist/validation/field-config.test.d.ts.map +0 -1
- package/dist/validation/field-config.test.js +0 -159
- package/dist/validation/field-config.test.js.map +0 -1
- package/dist/validation/schema.test.d.ts +0 -2
- package/dist/validation/schema.test.d.ts.map +0 -1
- package/dist/validation/schema.test.js +0 -409
- package/dist/validation/schema.test.js.map +0 -1
- package/docs/type-distribution-fix.md +0 -136
- package/src/access/access-filter.test.ts +0 -1919
- package/src/access/access-filter.ts +0 -1041
- package/src/access/declared-dependencies.test.ts +0 -277
- package/src/access/declared-dependencies.ts +0 -220
- package/src/access/denied-relation-visibility.test.ts +0 -173
- package/src/access/depth-limits.ts +0 -27
- package/src/access/engine.test.ts +0 -67
- package/src/access/engine.ts +0 -193
- package/src/access/errors.ts +0 -242
- package/src/access/field-access.test.ts +0 -792
- package/src/access/field-access.ts +0 -353
- package/src/access/field-transforms.ts +0 -68
- package/src/access/field-visibility.ts +0 -569
- package/src/access/index.ts +0 -82
- package/src/access/multi-column-read-write.test.ts +0 -305
- package/src/access/query-validation.ts +0 -486
- package/src/access/relationship-count.test.ts +0 -344
- package/src/access/relationship-count.ts +0 -346
- package/src/access/relationship-label-filter.test.ts +0 -128
- package/src/access/relationship-label-filter.ts +0 -63
- package/src/access/synthetic-include-read.test.ts +0 -179
- package/src/access/transaction-registry.ts +0 -58
- package/src/access/types.ts +0 -391
- package/src/config/index.ts +0 -177
- package/src/config/label.ts +0 -51
- package/src/config/nav-count.ts +0 -77
- package/src/config/plugin-engine.ts +0 -261
- package/src/config/types.ts +0 -3270
- package/src/context/apply-defaults.ts +0 -47
- package/src/context/hook-pipeline.ts +0 -137
- package/src/context/index.ts +0 -1737
- package/src/context/nested-operations.ts +0 -1470
- package/src/context/transaction-boundary.ts +0 -485
- package/src/context/write-pipeline.ts +0 -732
- package/src/extend.ts +0 -28
- package/src/fields/calendar-day.test.ts +0 -291
- package/src/fields/format-prisma-default.test.ts +0 -64
- package/src/fields/format-prisma-default.ts +0 -64
- package/src/fields/index.ts +0 -1901
- package/src/fields/relationship-foreign-key.test.ts +0 -29
- package/src/fields/select.test.ts +0 -336
- package/src/fields/text-keystone-compat.test.ts +0 -126
- package/src/filter/collect.ts +0 -108
- package/src/filter/filter.test.ts +0 -538
- package/src/filter/index.ts +0 -26
- package/src/filter/map.ts +0 -63
- package/src/filter/parse.ts +0 -99
- package/src/filter/serialize.test.ts +0 -115
- package/src/filter/serialize.ts +0 -73
- package/src/filter/types.ts +0 -120
- package/src/hooks/index.ts +0 -752
- package/src/index.test.ts +0 -50
- package/src/index.ts +0 -192
- package/src/internal.ts +0 -61
- package/src/lib/case-utils.test.ts +0 -159
- package/src/lib/case-utils.ts +0 -68
- package/src/lib/prisma-errors.ts +0 -86
- package/src/mcp/constants.ts +0 -24
- package/src/mcp/field-schema.ts +0 -84
- package/src/mcp/handler.ts +0 -633
- package/src/mcp/index.ts +0 -7
- package/src/mcp/projection.ts +0 -497
- package/src/mcp/types.ts +0 -18
- package/src/query/index.test.ts +0 -830
- package/src/query/index.ts +0 -352
- package/src/query/relationship-options.test.ts +0 -194
- package/src/query/relationship-options.ts +0 -78
- package/src/utils/password.ts +0 -80
- package/src/validation/field-config.test.ts +0 -199
- package/src/validation/field-config.ts +0 -139
- package/src/validation/needs-closure.ts +0 -188
- package/src/validation/schema.test.ts +0 -487
- package/src/validation/schema.ts +0 -51
- package/tests/access-relationships.test.ts +0 -646
- package/tests/access.test.ts +0 -590
- package/tests/apply-defaults.test.ts +0 -119
- package/tests/bare-read-scalars.test.ts +0 -147
- package/tests/computed-field-selective-evaluation.test.ts +0 -418
- package/tests/config.test.ts +0 -242
- package/tests/context.test.ts +0 -3146
- package/tests/default-value-create.test.ts +0 -299
- package/tests/field-hooks.test.ts +0 -410
- package/tests/field-types.test.ts +0 -1291
- package/tests/hook-context-secured.test.ts +0 -424
- package/tests/hook-pipeline.test.ts +0 -233
- package/tests/hooks.test.ts +0 -481
- package/tests/interactive-transaction.test.ts +0 -444
- package/tests/label.test.ts +0 -135
- package/tests/mcp-fields-projection-access.test.ts +0 -250
- package/tests/mcp-handler.test.ts +0 -1411
- package/tests/multi-column-validation-ordering.test.ts +0 -305
- package/tests/nav-count.test.ts +0 -189
- package/tests/needs-declared-dependencies.test.ts +0 -503
- package/tests/nested-access-and-hooks.test.ts +0 -2566
- package/tests/nested-operation-registry.test.ts +0 -317
- package/tests/nested-write-hooks.test.ts +0 -864
- package/tests/nested-write-synthetic-relation.test.ts +0 -437
- package/tests/password-type-distribution.test.ts +0 -154
- package/tests/password-types.test.ts +0 -174
- package/tests/password.test.ts +0 -263
- package/tests/plugin-engine.test.ts +0 -1155
- package/tests/prisma-errors.test.ts +0 -141
- package/tests/resolve-chain.test.ts +0 -411
- package/tests/singleton.test.ts +0 -421
- package/tests/sudo.test.ts +0 -676
- package/tests/to-one-include-access-filter.test.ts +0 -172
- package/tests/transaction-boundary-hooks.test.ts +0 -710
- package/tests/transaction-boundary-outermost.test.ts +0 -530
- package/tests/virtual-fields.test.ts +0 -421
- package/tests/with-session.test.ts +0 -243
- package/tests/write-pipeline.test.ts +0 -617
- package/tsconfig.json +0 -12
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -69
|
@@ -1,2566 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
|
-
import { getContext } from '../src/context/index.js'
|
|
3
|
-
import { config, list } from '../src/config/index.js'
|
|
4
|
-
import { text, relationship } from '../src/fields/index.js'
|
|
5
|
-
import { checkFieldAccess } from '../src/access/field-access.js'
|
|
6
|
-
import { InvalidCreateAccessResultError } from '../src/access/errors.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Mock Prisma Client for testing
|
|
10
|
-
*/
|
|
11
|
-
function createMockPrisma() {
|
|
12
|
-
const db = {
|
|
13
|
-
post: {
|
|
14
|
-
findFirst: vi.fn(),
|
|
15
|
-
findMany: vi.fn(),
|
|
16
|
-
findUnique: vi.fn(),
|
|
17
|
-
create: vi.fn(),
|
|
18
|
-
update: vi.fn(),
|
|
19
|
-
delete: vi.fn(),
|
|
20
|
-
count: vi.fn(),
|
|
21
|
-
},
|
|
22
|
-
user: {
|
|
23
|
-
findFirst: vi.fn(),
|
|
24
|
-
findMany: vi.fn(),
|
|
25
|
-
findUnique: vi.fn(),
|
|
26
|
-
create: vi.fn(),
|
|
27
|
-
update: vi.fn(),
|
|
28
|
-
delete: vi.fn(),
|
|
29
|
-
count: vi.fn(),
|
|
30
|
-
},
|
|
31
|
-
comment: {
|
|
32
|
-
findFirst: vi.fn(),
|
|
33
|
-
findMany: vi.fn(),
|
|
34
|
-
findUnique: vi.fn(),
|
|
35
|
-
create: vi.fn(),
|
|
36
|
-
update: vi.fn(),
|
|
37
|
-
delete: vi.fn(),
|
|
38
|
-
count: vi.fn(),
|
|
39
|
-
},
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return db
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
describe('Nested Operations - Access Control and Hooks', () => {
|
|
46
|
-
let mockPrisma: ReturnType<typeof createMockPrisma>
|
|
47
|
-
|
|
48
|
-
beforeEach(() => {
|
|
49
|
-
mockPrisma = createMockPrisma()
|
|
50
|
-
vi.clearAllMocks()
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
describe('Nested Create Operations', () => {
|
|
54
|
-
it('should run hooks and access control for nested create', async () => {
|
|
55
|
-
const userResolveInputHook = vi.fn(async ({ resolvedData, fieldKey }) => {
|
|
56
|
-
const value = resolvedData[fieldKey]
|
|
57
|
-
return typeof value === 'string' ? value.toUpperCase() : value
|
|
58
|
-
})
|
|
59
|
-
const userListResolveInputHook = vi.fn(async ({ resolvedData }) => resolvedData)
|
|
60
|
-
const userValidateInputHook = vi.fn(async () => {})
|
|
61
|
-
const postResolveInputHook = vi.fn(async ({ resolvedData }) => resolvedData)
|
|
62
|
-
|
|
63
|
-
const testConfig = config({
|
|
64
|
-
db: {
|
|
65
|
-
provider: 'postgresql',
|
|
66
|
-
url: 'postgresql://localhost:5432/test',
|
|
67
|
-
},
|
|
68
|
-
lists: {
|
|
69
|
-
User: list({
|
|
70
|
-
fields: {
|
|
71
|
-
name: text({
|
|
72
|
-
hooks: {
|
|
73
|
-
resolveInput: userResolveInputHook,
|
|
74
|
-
},
|
|
75
|
-
}),
|
|
76
|
-
email: text(),
|
|
77
|
-
},
|
|
78
|
-
access: {
|
|
79
|
-
operation: {
|
|
80
|
-
query: () => true,
|
|
81
|
-
create: () => true,
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
hooks: {
|
|
85
|
-
resolveInput: userListResolveInputHook,
|
|
86
|
-
validateInput: userValidateInputHook,
|
|
87
|
-
},
|
|
88
|
-
}),
|
|
89
|
-
Post: list({
|
|
90
|
-
fields: {
|
|
91
|
-
title: text(),
|
|
92
|
-
author: relationship({ ref: 'User.posts' }),
|
|
93
|
-
},
|
|
94
|
-
access: {
|
|
95
|
-
operation: {
|
|
96
|
-
query: () => true,
|
|
97
|
-
update: () => true,
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
hooks: {
|
|
101
|
-
resolveInput: postResolveInputHook,
|
|
102
|
-
},
|
|
103
|
-
}),
|
|
104
|
-
},
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
// Mock the database to return existing post
|
|
108
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
109
|
-
id: '1',
|
|
110
|
-
title: 'Original Title',
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// Mock the update to return the updated post
|
|
114
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
115
|
-
id: '1',
|
|
116
|
-
title: 'Updated Title',
|
|
117
|
-
authorId: '2',
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
const context = await getContext(testConfig, mockPrisma, { userId: '1' })
|
|
121
|
-
|
|
122
|
-
await context.db.post.update({
|
|
123
|
-
where: { id: '1' },
|
|
124
|
-
data: {
|
|
125
|
-
title: 'Updated Title',
|
|
126
|
-
author: {
|
|
127
|
-
create: {
|
|
128
|
-
name: 'john',
|
|
129
|
-
email: 'john@example.com',
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
// Verify User hooks were called
|
|
136
|
-
expect(userListResolveInputHook).toHaveBeenCalledWith(
|
|
137
|
-
expect.objectContaining({
|
|
138
|
-
operation: 'create',
|
|
139
|
-
resolvedData: expect.objectContaining({
|
|
140
|
-
name: 'john',
|
|
141
|
-
email: 'john@example.com',
|
|
142
|
-
}),
|
|
143
|
-
}),
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
expect(userResolveInputHook).toHaveBeenCalledWith(
|
|
147
|
-
expect.objectContaining({
|
|
148
|
-
fieldKey: 'name',
|
|
149
|
-
operation: 'create',
|
|
150
|
-
resolvedData: expect.objectContaining({
|
|
151
|
-
name: 'john',
|
|
152
|
-
}),
|
|
153
|
-
}),
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
expect(userValidateInputHook).toHaveBeenCalled()
|
|
157
|
-
|
|
158
|
-
// Verify Post hooks were called
|
|
159
|
-
expect(postResolveInputHook).toHaveBeenCalled()
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
it('should enforce create access control on nested create', async () => {
|
|
163
|
-
const testConfig = config({
|
|
164
|
-
db: {
|
|
165
|
-
provider: 'postgresql',
|
|
166
|
-
url: 'postgresql://localhost:5432/test',
|
|
167
|
-
},
|
|
168
|
-
lists: {
|
|
169
|
-
User: list({
|
|
170
|
-
fields: {
|
|
171
|
-
name: text(),
|
|
172
|
-
email: text(),
|
|
173
|
-
},
|
|
174
|
-
access: {
|
|
175
|
-
operation: {
|
|
176
|
-
query: () => true,
|
|
177
|
-
create: () => false, // Deny create
|
|
178
|
-
},
|
|
179
|
-
},
|
|
180
|
-
}),
|
|
181
|
-
Post: list({
|
|
182
|
-
fields: {
|
|
183
|
-
title: text(),
|
|
184
|
-
author: relationship({ ref: 'User.posts' }),
|
|
185
|
-
},
|
|
186
|
-
access: {
|
|
187
|
-
operation: {
|
|
188
|
-
query: () => true,
|
|
189
|
-
update: () => true,
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
}),
|
|
193
|
-
},
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
197
|
-
id: '1',
|
|
198
|
-
title: 'Original Title',
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
202
|
-
|
|
203
|
-
await expect(
|
|
204
|
-
context.db.post.update({
|
|
205
|
-
where: { id: '1' },
|
|
206
|
-
data: {
|
|
207
|
-
title: 'Updated Title',
|
|
208
|
-
author: {
|
|
209
|
-
create: {
|
|
210
|
-
name: 'John',
|
|
211
|
-
email: 'john@example.com',
|
|
212
|
-
},
|
|
213
|
-
},
|
|
214
|
-
},
|
|
215
|
-
}),
|
|
216
|
-
).rejects.toThrow('Access denied: Cannot create related item')
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
// #1009: a nested `create` rule returning a filter reads as though it
|
|
220
|
-
// scopes the create, but the nested-create path (like the top-level one)
|
|
221
|
-
// has no existing row to re-check that filter against. Before this fix
|
|
222
|
-
// the check only tested `=== false`, so a filter fell through the nested
|
|
223
|
-
// path exactly like the top-level one and was silently treated as allow.
|
|
224
|
-
it('should throw when nested create access control returns a Prisma filter', async () => {
|
|
225
|
-
const testConfig = config({
|
|
226
|
-
db: {
|
|
227
|
-
provider: 'postgresql',
|
|
228
|
-
url: 'postgresql://localhost:5432/test',
|
|
229
|
-
},
|
|
230
|
-
lists: {
|
|
231
|
-
User: list({
|
|
232
|
-
fields: {
|
|
233
|
-
name: text(),
|
|
234
|
-
email: text(),
|
|
235
|
-
},
|
|
236
|
-
access: {
|
|
237
|
-
operation: {
|
|
238
|
-
query: () => true,
|
|
239
|
-
// Looks like it scopes the nested create by owner. Cannot be
|
|
240
|
-
// honoured here — there is no row yet.
|
|
241
|
-
create: () => ({ ownerId: { equals: 'someone' } }),
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
}),
|
|
245
|
-
Post: list({
|
|
246
|
-
fields: {
|
|
247
|
-
title: text(),
|
|
248
|
-
author: relationship({ ref: 'User.posts' }),
|
|
249
|
-
},
|
|
250
|
-
access: {
|
|
251
|
-
operation: {
|
|
252
|
-
query: () => true,
|
|
253
|
-
update: () => true,
|
|
254
|
-
},
|
|
255
|
-
},
|
|
256
|
-
}),
|
|
257
|
-
},
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
261
|
-
id: '1',
|
|
262
|
-
title: 'Original Title',
|
|
263
|
-
})
|
|
264
|
-
|
|
265
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
266
|
-
|
|
267
|
-
await expect(
|
|
268
|
-
context.db.post.update({
|
|
269
|
-
where: { id: '1' },
|
|
270
|
-
data: {
|
|
271
|
-
title: 'Updated Title',
|
|
272
|
-
author: {
|
|
273
|
-
create: {
|
|
274
|
-
name: 'John',
|
|
275
|
-
email: 'john@example.com',
|
|
276
|
-
},
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
}),
|
|
280
|
-
).rejects.toThrow(InvalidCreateAccessResultError)
|
|
281
|
-
})
|
|
282
|
-
|
|
283
|
-
it('should apply field-level access control to nested create', async () => {
|
|
284
|
-
const testConfig = config({
|
|
285
|
-
db: {
|
|
286
|
-
provider: 'postgresql',
|
|
287
|
-
url: 'postgresql://localhost:5432/test',
|
|
288
|
-
},
|
|
289
|
-
lists: {
|
|
290
|
-
User: list({
|
|
291
|
-
fields: {
|
|
292
|
-
name: text(),
|
|
293
|
-
email: text(),
|
|
294
|
-
role: text({
|
|
295
|
-
access: {
|
|
296
|
-
create: () => false, // Cannot set role on create
|
|
297
|
-
},
|
|
298
|
-
}),
|
|
299
|
-
},
|
|
300
|
-
access: {
|
|
301
|
-
operation: {
|
|
302
|
-
query: () => true,
|
|
303
|
-
create: () => true,
|
|
304
|
-
},
|
|
305
|
-
},
|
|
306
|
-
}),
|
|
307
|
-
Post: list({
|
|
308
|
-
fields: {
|
|
309
|
-
title: text(),
|
|
310
|
-
author: relationship({ ref: 'User.posts' }),
|
|
311
|
-
},
|
|
312
|
-
access: {
|
|
313
|
-
operation: {
|
|
314
|
-
query: () => true,
|
|
315
|
-
update: () => true,
|
|
316
|
-
},
|
|
317
|
-
},
|
|
318
|
-
}),
|
|
319
|
-
},
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
323
|
-
id: '1',
|
|
324
|
-
title: 'Original Title',
|
|
325
|
-
})
|
|
326
|
-
|
|
327
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
328
|
-
id: '1',
|
|
329
|
-
title: 'Updated Title',
|
|
330
|
-
authorId: '2',
|
|
331
|
-
})
|
|
332
|
-
|
|
333
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
334
|
-
|
|
335
|
-
// #568: a nested-create field denied by field-level access now THROWS
|
|
336
|
-
// (Keystone fail-loud parity) instead of being silently stripped. Nested
|
|
337
|
-
// writes flow through the same `filterWritableFields` gate, so the denied
|
|
338
|
-
// `role` field rejects the whole write.
|
|
339
|
-
await expect(
|
|
340
|
-
context.db.post.update({
|
|
341
|
-
where: { id: '1' },
|
|
342
|
-
data: {
|
|
343
|
-
title: 'Updated Title',
|
|
344
|
-
author: {
|
|
345
|
-
create: {
|
|
346
|
-
name: 'John',
|
|
347
|
-
email: 'john@example.com',
|
|
348
|
-
role: 'admin', // Denied on create -> throws
|
|
349
|
-
},
|
|
350
|
-
},
|
|
351
|
-
},
|
|
352
|
-
}),
|
|
353
|
-
).rejects.toThrow(/role/)
|
|
354
|
-
|
|
355
|
-
// The denied field must abort the write, not let it proceed.
|
|
356
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
357
|
-
})
|
|
358
|
-
|
|
359
|
-
it('should run field validation on nested create', async () => {
|
|
360
|
-
const testConfig = config({
|
|
361
|
-
db: {
|
|
362
|
-
provider: 'postgresql',
|
|
363
|
-
url: 'postgresql://localhost:5432/test',
|
|
364
|
-
},
|
|
365
|
-
lists: {
|
|
366
|
-
User: list({
|
|
367
|
-
fields: {
|
|
368
|
-
name: text({ validation: { isRequired: true } }),
|
|
369
|
-
email: text({ validation: { isRequired: true } }),
|
|
370
|
-
},
|
|
371
|
-
access: {
|
|
372
|
-
operation: {
|
|
373
|
-
query: () => true,
|
|
374
|
-
create: () => true,
|
|
375
|
-
},
|
|
376
|
-
},
|
|
377
|
-
}),
|
|
378
|
-
Post: list({
|
|
379
|
-
fields: {
|
|
380
|
-
title: text(),
|
|
381
|
-
author: relationship({ ref: 'User.posts' }),
|
|
382
|
-
},
|
|
383
|
-
access: {
|
|
384
|
-
operation: {
|
|
385
|
-
query: () => true,
|
|
386
|
-
update: () => true,
|
|
387
|
-
},
|
|
388
|
-
},
|
|
389
|
-
}),
|
|
390
|
-
},
|
|
391
|
-
})
|
|
392
|
-
|
|
393
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
394
|
-
id: '1',
|
|
395
|
-
title: 'Original Title',
|
|
396
|
-
})
|
|
397
|
-
|
|
398
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
399
|
-
|
|
400
|
-
await expect(
|
|
401
|
-
context.db.post.update({
|
|
402
|
-
where: { id: '1' },
|
|
403
|
-
data: {
|
|
404
|
-
title: 'Updated Title',
|
|
405
|
-
author: {
|
|
406
|
-
create: {
|
|
407
|
-
name: '', // Empty required field
|
|
408
|
-
email: 'john@example.com',
|
|
409
|
-
},
|
|
410
|
-
},
|
|
411
|
-
},
|
|
412
|
-
}),
|
|
413
|
-
).rejects.toThrow('Name is required')
|
|
414
|
-
})
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
describe('Nested Read Operations with Includes', () => {
|
|
418
|
-
it('should apply query access control to relationships', async () => {
|
|
419
|
-
const testConfig = config({
|
|
420
|
-
db: {
|
|
421
|
-
provider: 'postgresql',
|
|
422
|
-
url: 'postgresql://localhost:5432/test',
|
|
423
|
-
},
|
|
424
|
-
lists: {
|
|
425
|
-
User: list({
|
|
426
|
-
fields: {
|
|
427
|
-
name: text(),
|
|
428
|
-
email: text(),
|
|
429
|
-
posts: relationship({ ref: 'Post.author', many: true }),
|
|
430
|
-
},
|
|
431
|
-
access: {
|
|
432
|
-
operation: {
|
|
433
|
-
query: () => true,
|
|
434
|
-
},
|
|
435
|
-
},
|
|
436
|
-
}),
|
|
437
|
-
Post: list({
|
|
438
|
-
fields: {
|
|
439
|
-
title: text(),
|
|
440
|
-
status: text(),
|
|
441
|
-
author: relationship({ ref: 'User.posts' }),
|
|
442
|
-
},
|
|
443
|
-
access: {
|
|
444
|
-
operation: {
|
|
445
|
-
// Only show published posts
|
|
446
|
-
query: () => ({ status: { equals: 'published' } }),
|
|
447
|
-
},
|
|
448
|
-
},
|
|
449
|
-
}),
|
|
450
|
-
},
|
|
451
|
-
})
|
|
452
|
-
|
|
453
|
-
mockPrisma.user.findFirst.mockResolvedValue({
|
|
454
|
-
id: '1',
|
|
455
|
-
name: 'John Doe',
|
|
456
|
-
email: 'john@example.com',
|
|
457
|
-
posts: [
|
|
458
|
-
{ id: '1', title: 'Published Post', status: 'published' },
|
|
459
|
-
{ id: '2', title: 'Draft Post', status: 'draft' },
|
|
460
|
-
],
|
|
461
|
-
})
|
|
462
|
-
|
|
463
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
464
|
-
|
|
465
|
-
// A caller-supplied `include` is required to fetch `posts` at all — a
|
|
466
|
-
// bare read returns scalars only (ADR-0024) — so name it explicitly to
|
|
467
|
-
// exercise the per-relation access `where` merge.
|
|
468
|
-
await context.db.user.findUnique({
|
|
469
|
-
where: { id: '1' },
|
|
470
|
-
include: { posts: true },
|
|
471
|
-
})
|
|
472
|
-
|
|
473
|
-
// Verify findFirst was called with access filter
|
|
474
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalledWith(
|
|
475
|
-
expect.objectContaining({
|
|
476
|
-
include: expect.objectContaining({
|
|
477
|
-
posts: expect.objectContaining({
|
|
478
|
-
where: { status: { equals: 'published' } },
|
|
479
|
-
}),
|
|
480
|
-
}),
|
|
481
|
-
}),
|
|
482
|
-
)
|
|
483
|
-
})
|
|
484
|
-
|
|
485
|
-
it('should apply field-level read access to nested relationships', async () => {
|
|
486
|
-
const userResolveOutputHook = vi.fn(({ value }) => `***${value}***`)
|
|
487
|
-
|
|
488
|
-
const testConfig = config({
|
|
489
|
-
db: {
|
|
490
|
-
provider: 'postgresql',
|
|
491
|
-
url: 'postgresql://localhost:5432/test',
|
|
492
|
-
},
|
|
493
|
-
lists: {
|
|
494
|
-
User: list({
|
|
495
|
-
fields: {
|
|
496
|
-
name: text(),
|
|
497
|
-
email: text({
|
|
498
|
-
access: {
|
|
499
|
-
read: () => false, // Hide email
|
|
500
|
-
},
|
|
501
|
-
}),
|
|
502
|
-
secretField: text({
|
|
503
|
-
hooks: {
|
|
504
|
-
resolveOutput: userResolveOutputHook,
|
|
505
|
-
},
|
|
506
|
-
}),
|
|
507
|
-
},
|
|
508
|
-
access: {
|
|
509
|
-
operation: {
|
|
510
|
-
query: () => true,
|
|
511
|
-
},
|
|
512
|
-
},
|
|
513
|
-
}),
|
|
514
|
-
Post: list({
|
|
515
|
-
fields: {
|
|
516
|
-
title: text(),
|
|
517
|
-
author: relationship({ ref: 'User.posts' }),
|
|
518
|
-
},
|
|
519
|
-
access: {
|
|
520
|
-
operation: {
|
|
521
|
-
query: () => true,
|
|
522
|
-
},
|
|
523
|
-
},
|
|
524
|
-
}),
|
|
525
|
-
},
|
|
526
|
-
})
|
|
527
|
-
|
|
528
|
-
mockPrisma.post.findFirst.mockResolvedValue({
|
|
529
|
-
id: '1',
|
|
530
|
-
title: 'Test Post',
|
|
531
|
-
author: {
|
|
532
|
-
id: '1',
|
|
533
|
-
name: 'John Doe',
|
|
534
|
-
email: 'john@example.com',
|
|
535
|
-
secretField: 'secret',
|
|
536
|
-
},
|
|
537
|
-
})
|
|
538
|
-
|
|
539
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
540
|
-
|
|
541
|
-
// `author` must be named explicitly — a bare read returns scalars only
|
|
542
|
-
// (ADR-0024).
|
|
543
|
-
const result = await context.db.post.findUnique({
|
|
544
|
-
where: { id: '1' },
|
|
545
|
-
include: { author: true },
|
|
546
|
-
})
|
|
547
|
-
|
|
548
|
-
// Email should be filtered out
|
|
549
|
-
expect(result?.author?.email).toBeUndefined()
|
|
550
|
-
expect(result?.author?.name).toBe('John Doe')
|
|
551
|
-
|
|
552
|
-
// resolveOutput hook should have been applied
|
|
553
|
-
expect(result?.author?.secretField).toBe('***secret***')
|
|
554
|
-
expect(userResolveOutputHook).toHaveBeenCalled()
|
|
555
|
-
})
|
|
556
|
-
|
|
557
|
-
it('should deny access to relationships when query access is false', async () => {
|
|
558
|
-
const testConfig = config({
|
|
559
|
-
db: {
|
|
560
|
-
provider: 'postgresql',
|
|
561
|
-
url: 'postgresql://localhost:5432/test',
|
|
562
|
-
},
|
|
563
|
-
lists: {
|
|
564
|
-
User: list({
|
|
565
|
-
fields: {
|
|
566
|
-
name: text(),
|
|
567
|
-
email: text(),
|
|
568
|
-
},
|
|
569
|
-
access: {
|
|
570
|
-
operation: {
|
|
571
|
-
query: () => false, // Deny access to users
|
|
572
|
-
},
|
|
573
|
-
},
|
|
574
|
-
}),
|
|
575
|
-
Post: list({
|
|
576
|
-
fields: {
|
|
577
|
-
title: text(),
|
|
578
|
-
author: relationship({ ref: 'User.posts' }),
|
|
579
|
-
},
|
|
580
|
-
access: {
|
|
581
|
-
operation: {
|
|
582
|
-
query: () => true,
|
|
583
|
-
},
|
|
584
|
-
},
|
|
585
|
-
}),
|
|
586
|
-
},
|
|
587
|
-
})
|
|
588
|
-
|
|
589
|
-
mockPrisma.post.findFirst.mockResolvedValue({
|
|
590
|
-
id: '1',
|
|
591
|
-
title: 'Test Post',
|
|
592
|
-
// Author should not be included due to access control
|
|
593
|
-
})
|
|
594
|
-
|
|
595
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
596
|
-
|
|
597
|
-
// `author` must be named explicitly to exercise the access-denied drop —
|
|
598
|
-
// a bare read never requests it at all (ADR-0024).
|
|
599
|
-
await context.db.post.findUnique({
|
|
600
|
-
where: { id: '1' },
|
|
601
|
-
include: { author: true },
|
|
602
|
-
})
|
|
603
|
-
|
|
604
|
-
// Verify include does NOT include author (access denied)
|
|
605
|
-
expect(mockPrisma.post.findFirst).toHaveBeenCalledWith(
|
|
606
|
-
expect.objectContaining({
|
|
607
|
-
include: expect.not.objectContaining({
|
|
608
|
-
author: expect.anything(),
|
|
609
|
-
}),
|
|
610
|
-
}),
|
|
611
|
-
)
|
|
612
|
-
})
|
|
613
|
-
})
|
|
614
|
-
|
|
615
|
-
describe('Update Return Values with Field Access', () => {
|
|
616
|
-
it('should filter return fields based on field-level read access', async () => {
|
|
617
|
-
const testConfig = config({
|
|
618
|
-
db: {
|
|
619
|
-
provider: 'postgresql',
|
|
620
|
-
url: 'postgresql://localhost:5432/test',
|
|
621
|
-
},
|
|
622
|
-
lists: {
|
|
623
|
-
Post: list({
|
|
624
|
-
fields: {
|
|
625
|
-
title: text(),
|
|
626
|
-
content: text(),
|
|
627
|
-
internalNotes: text({
|
|
628
|
-
access: {
|
|
629
|
-
read: () => false, // Hide from read
|
|
630
|
-
},
|
|
631
|
-
}),
|
|
632
|
-
},
|
|
633
|
-
access: {
|
|
634
|
-
operation: {
|
|
635
|
-
query: () => true,
|
|
636
|
-
update: () => true,
|
|
637
|
-
},
|
|
638
|
-
},
|
|
639
|
-
}),
|
|
640
|
-
},
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
644
|
-
id: '1',
|
|
645
|
-
title: 'Original Title',
|
|
646
|
-
content: 'Original Content',
|
|
647
|
-
internalNotes: 'Old notes',
|
|
648
|
-
})
|
|
649
|
-
|
|
650
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
651
|
-
id: '1',
|
|
652
|
-
title: 'Updated Title',
|
|
653
|
-
content: 'Updated Content',
|
|
654
|
-
internalNotes: 'New secret notes',
|
|
655
|
-
})
|
|
656
|
-
|
|
657
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
658
|
-
|
|
659
|
-
const result = await context.db.post.update({
|
|
660
|
-
where: { id: '1' },
|
|
661
|
-
data: {
|
|
662
|
-
title: 'Updated Title',
|
|
663
|
-
content: 'Updated Content',
|
|
664
|
-
internalNotes: 'New secret notes',
|
|
665
|
-
},
|
|
666
|
-
})
|
|
667
|
-
|
|
668
|
-
// internalNotes should be filtered out from response
|
|
669
|
-
expect(result?.title).toBe('Updated Title')
|
|
670
|
-
expect(result?.content).toBe('Updated Content')
|
|
671
|
-
expect(result?.internalNotes).toBeUndefined()
|
|
672
|
-
})
|
|
673
|
-
|
|
674
|
-
it('should apply resolveOutput hooks to update return value', async () => {
|
|
675
|
-
const titleResolveOutputHook = vi.fn(({ value }) => value.toUpperCase())
|
|
676
|
-
|
|
677
|
-
const testConfig = config({
|
|
678
|
-
db: {
|
|
679
|
-
provider: 'postgresql',
|
|
680
|
-
url: 'postgresql://localhost:5432/test',
|
|
681
|
-
},
|
|
682
|
-
lists: {
|
|
683
|
-
Post: list({
|
|
684
|
-
fields: {
|
|
685
|
-
title: text({
|
|
686
|
-
hooks: {
|
|
687
|
-
resolveOutput: titleResolveOutputHook,
|
|
688
|
-
},
|
|
689
|
-
}),
|
|
690
|
-
},
|
|
691
|
-
access: {
|
|
692
|
-
operation: {
|
|
693
|
-
query: () => true,
|
|
694
|
-
update: () => true,
|
|
695
|
-
},
|
|
696
|
-
},
|
|
697
|
-
}),
|
|
698
|
-
},
|
|
699
|
-
})
|
|
700
|
-
|
|
701
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
702
|
-
id: '1',
|
|
703
|
-
title: 'Original Title',
|
|
704
|
-
})
|
|
705
|
-
|
|
706
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
707
|
-
id: '1',
|
|
708
|
-
title: 'updated title',
|
|
709
|
-
})
|
|
710
|
-
|
|
711
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
712
|
-
|
|
713
|
-
const result = await context.db.post.update({
|
|
714
|
-
where: { id: '1' },
|
|
715
|
-
data: {
|
|
716
|
-
title: 'updated title',
|
|
717
|
-
},
|
|
718
|
-
})
|
|
719
|
-
|
|
720
|
-
// resolveOutput hook should transform the return value
|
|
721
|
-
expect(result?.title).toBe('UPDATED TITLE')
|
|
722
|
-
expect(titleResolveOutputHook).toHaveBeenCalled()
|
|
723
|
-
})
|
|
724
|
-
|
|
725
|
-
it('should filter nested relationships in update return value', async () => {
|
|
726
|
-
const testConfig = config({
|
|
727
|
-
db: {
|
|
728
|
-
provider: 'postgresql',
|
|
729
|
-
url: 'postgresql://localhost:5432/test',
|
|
730
|
-
},
|
|
731
|
-
lists: {
|
|
732
|
-
User: list({
|
|
733
|
-
fields: {
|
|
734
|
-
name: text(),
|
|
735
|
-
email: text({
|
|
736
|
-
access: {
|
|
737
|
-
read: () => false, // Hide email
|
|
738
|
-
},
|
|
739
|
-
}),
|
|
740
|
-
},
|
|
741
|
-
access: {
|
|
742
|
-
operation: {
|
|
743
|
-
query: () => true,
|
|
744
|
-
create: () => true,
|
|
745
|
-
},
|
|
746
|
-
},
|
|
747
|
-
}),
|
|
748
|
-
Post: list({
|
|
749
|
-
fields: {
|
|
750
|
-
title: text(),
|
|
751
|
-
author: relationship({ ref: 'User.posts' }),
|
|
752
|
-
},
|
|
753
|
-
access: {
|
|
754
|
-
operation: {
|
|
755
|
-
query: () => true,
|
|
756
|
-
update: () => true,
|
|
757
|
-
},
|
|
758
|
-
},
|
|
759
|
-
}),
|
|
760
|
-
},
|
|
761
|
-
})
|
|
762
|
-
|
|
763
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
764
|
-
id: '1',
|
|
765
|
-
title: 'Original Title',
|
|
766
|
-
})
|
|
767
|
-
|
|
768
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
769
|
-
id: '1',
|
|
770
|
-
title: 'Updated Title',
|
|
771
|
-
authorId: '2',
|
|
772
|
-
})
|
|
773
|
-
|
|
774
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
775
|
-
|
|
776
|
-
const result = await context.db.post.update({
|
|
777
|
-
where: { id: '1' },
|
|
778
|
-
data: {
|
|
779
|
-
title: 'Updated Title',
|
|
780
|
-
author: {
|
|
781
|
-
create: {
|
|
782
|
-
name: 'John',
|
|
783
|
-
email: 'john@example.com',
|
|
784
|
-
},
|
|
785
|
-
},
|
|
786
|
-
},
|
|
787
|
-
})
|
|
788
|
-
|
|
789
|
-
// Verify result exists
|
|
790
|
-
expect(result).toBeDefined()
|
|
791
|
-
expect(result?.title).toBe('Updated Title')
|
|
792
|
-
})
|
|
793
|
-
})
|
|
794
|
-
|
|
795
|
-
describe('Access Denial Scenarios', () => {
|
|
796
|
-
it('should deny nested connect when read (query) access scopes out the target row', async () => {
|
|
797
|
-
const testConfig = config({
|
|
798
|
-
db: {
|
|
799
|
-
provider: 'postgresql',
|
|
800
|
-
url: 'postgresql://localhost:5432/test',
|
|
801
|
-
},
|
|
802
|
-
lists: {
|
|
803
|
-
User: list({
|
|
804
|
-
fields: {
|
|
805
|
-
name: text(),
|
|
806
|
-
},
|
|
807
|
-
access: {
|
|
808
|
-
operation: {
|
|
809
|
-
// Only allow reading own profile (a scalar filter)
|
|
810
|
-
query: ({ session }) => ({ id: { equals: session?.userId } }),
|
|
811
|
-
update: () => true,
|
|
812
|
-
},
|
|
813
|
-
},
|
|
814
|
-
}),
|
|
815
|
-
Post: list({
|
|
816
|
-
fields: {
|
|
817
|
-
title: text(),
|
|
818
|
-
author: relationship({ ref: 'User.posts' }),
|
|
819
|
-
},
|
|
820
|
-
access: {
|
|
821
|
-
operation: {
|
|
822
|
-
query: () => true,
|
|
823
|
-
update: () => true,
|
|
824
|
-
},
|
|
825
|
-
},
|
|
826
|
-
}),
|
|
827
|
-
},
|
|
828
|
-
})
|
|
829
|
-
|
|
830
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
831
|
-
id: '1',
|
|
832
|
-
title: 'Original Title',
|
|
833
|
-
})
|
|
834
|
-
|
|
835
|
-
// User 2 is NOT reachable under { id: { equals: '1' } } → findFirst returns null.
|
|
836
|
-
mockPrisma.user.findFirst.mockResolvedValue(null)
|
|
837
|
-
|
|
838
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
839
|
-
|
|
840
|
-
await expect(
|
|
841
|
-
context.db.post.update({
|
|
842
|
-
where: { id: '1' },
|
|
843
|
-
data: {
|
|
844
|
-
author: {
|
|
845
|
-
connect: { id: '2' }, // Connecting to user 2, but session can only read user 1
|
|
846
|
-
},
|
|
847
|
-
},
|
|
848
|
-
}),
|
|
849
|
-
).rejects.toThrow('Access denied: Cannot connect to this item')
|
|
850
|
-
|
|
851
|
-
// Reachability must be evaluated in the DB, AND-combining connection + filter.
|
|
852
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalledWith({
|
|
853
|
-
where: { AND: [{ id: '2' }, { id: { equals: '1' } }] },
|
|
854
|
-
})
|
|
855
|
-
})
|
|
856
|
-
|
|
857
|
-
it('should allow nested connect when read (query) access is granted (boolean true)', async () => {
|
|
858
|
-
const testConfig = config({
|
|
859
|
-
db: {
|
|
860
|
-
provider: 'postgresql',
|
|
861
|
-
url: 'postgresql://localhost:5432/test',
|
|
862
|
-
},
|
|
863
|
-
lists: {
|
|
864
|
-
User: list({
|
|
865
|
-
fields: {
|
|
866
|
-
name: text(),
|
|
867
|
-
},
|
|
868
|
-
access: {
|
|
869
|
-
operation: {
|
|
870
|
-
query: () => true, // Allow reading all users
|
|
871
|
-
update: () => true,
|
|
872
|
-
},
|
|
873
|
-
},
|
|
874
|
-
}),
|
|
875
|
-
Post: list({
|
|
876
|
-
fields: {
|
|
877
|
-
title: text(),
|
|
878
|
-
author: relationship({ ref: 'User.posts' }),
|
|
879
|
-
},
|
|
880
|
-
access: {
|
|
881
|
-
operation: {
|
|
882
|
-
query: () => true,
|
|
883
|
-
update: () => true,
|
|
884
|
-
},
|
|
885
|
-
},
|
|
886
|
-
}),
|
|
887
|
-
},
|
|
888
|
-
})
|
|
889
|
-
|
|
890
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
891
|
-
id: '1',
|
|
892
|
-
title: 'Original Title',
|
|
893
|
-
})
|
|
894
|
-
|
|
895
|
-
mockPrisma.user.findUnique.mockResolvedValue({
|
|
896
|
-
id: '2',
|
|
897
|
-
name: 'John Doe',
|
|
898
|
-
})
|
|
899
|
-
|
|
900
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
901
|
-
id: '1',
|
|
902
|
-
title: 'Original Title',
|
|
903
|
-
authorId: '2',
|
|
904
|
-
})
|
|
905
|
-
|
|
906
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
907
|
-
|
|
908
|
-
const result = await context.db.post.update({
|
|
909
|
-
where: { id: '1' },
|
|
910
|
-
data: {
|
|
911
|
-
author: {
|
|
912
|
-
connect: { id: '2' },
|
|
913
|
-
},
|
|
914
|
-
},
|
|
915
|
-
})
|
|
916
|
-
|
|
917
|
-
expect(result).toBeDefined()
|
|
918
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
919
|
-
// Boolean-true access must not run a reachability re-check.
|
|
920
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
921
|
-
})
|
|
922
|
-
|
|
923
|
-
it('connect requires READ access, not target UPDATE (read-not-update repro)', async () => {
|
|
924
|
-
// Caller can READ the target (query=allowAll) but CANNOT update it
|
|
925
|
-
// (update=admin-only). Under the old behaviour this denied the connect;
|
|
926
|
-
// it must now succeed because connect only references the row.
|
|
927
|
-
const testConfig = config({
|
|
928
|
-
db: {
|
|
929
|
-
provider: 'postgresql',
|
|
930
|
-
url: 'postgresql://localhost:5432/test',
|
|
931
|
-
},
|
|
932
|
-
lists: {
|
|
933
|
-
LessonTerm: list({
|
|
934
|
-
fields: {
|
|
935
|
-
name: text(),
|
|
936
|
-
},
|
|
937
|
-
access: {
|
|
938
|
-
operation: {
|
|
939
|
-
query: () => true, // anyone can read
|
|
940
|
-
update: () => false, // nobody (non-admin) can update
|
|
941
|
-
},
|
|
942
|
-
},
|
|
943
|
-
}),
|
|
944
|
-
Enrolment: list({
|
|
945
|
-
fields: {
|
|
946
|
-
title: text(),
|
|
947
|
-
term: relationship({ ref: 'LessonTerm' }),
|
|
948
|
-
},
|
|
949
|
-
access: {
|
|
950
|
-
operation: {
|
|
951
|
-
query: () => true,
|
|
952
|
-
update: () => true,
|
|
953
|
-
},
|
|
954
|
-
},
|
|
955
|
-
}),
|
|
956
|
-
},
|
|
957
|
-
})
|
|
958
|
-
|
|
959
|
-
mockPrisma.enrolment = {
|
|
960
|
-
findFirst: vi.fn(),
|
|
961
|
-
findMany: vi.fn(),
|
|
962
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'e1', title: 'Term 1 Enrolment' }),
|
|
963
|
-
create: vi.fn(),
|
|
964
|
-
update: vi.fn().mockResolvedValue({ id: 'e1', title: 'Term 1 Enrolment', termId: 't1' }),
|
|
965
|
-
delete: vi.fn(),
|
|
966
|
-
count: vi.fn(),
|
|
967
|
-
}
|
|
968
|
-
mockPrisma.lessonTerm = {
|
|
969
|
-
findFirst: vi.fn(),
|
|
970
|
-
findMany: vi.fn(),
|
|
971
|
-
findUnique: vi.fn().mockResolvedValue({ id: 't1', name: 'Term 1' }),
|
|
972
|
-
create: vi.fn(),
|
|
973
|
-
update: vi.fn(),
|
|
974
|
-
delete: vi.fn(),
|
|
975
|
-
count: vi.fn(),
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'student-1' })
|
|
979
|
-
|
|
980
|
-
const result = await context.db.enrolment.update({
|
|
981
|
-
where: { id: 'e1' },
|
|
982
|
-
data: {
|
|
983
|
-
term: {
|
|
984
|
-
connect: { id: 't1' },
|
|
985
|
-
},
|
|
986
|
-
},
|
|
987
|
-
})
|
|
988
|
-
|
|
989
|
-
expect(result).toBeDefined()
|
|
990
|
-
expect(mockPrisma.enrolment.update).toHaveBeenCalled()
|
|
991
|
-
// query=true → existence check via findUnique, no reachability re-check.
|
|
992
|
-
expect(mockPrisma.lessonTerm.findUnique).toHaveBeenCalledWith({ where: { id: 't1' } })
|
|
993
|
-
expect(mockPrisma.lessonTerm.findFirst).not.toHaveBeenCalled()
|
|
994
|
-
})
|
|
995
|
-
|
|
996
|
-
it('allows nested connect when a NESTED-RELATION filter is reachable (account-holder self-connect)', async () => {
|
|
997
|
-
// Account's row filter is a nested-relation filter:
|
|
998
|
-
// { user: { id: { equals: session.userId } } }
|
|
999
|
-
// The old in-memory matcher could not evaluate this and always denied.
|
|
1000
|
-
const testConfig = config({
|
|
1001
|
-
db: {
|
|
1002
|
-
provider: 'postgresql',
|
|
1003
|
-
url: 'postgresql://localhost:5432/test',
|
|
1004
|
-
},
|
|
1005
|
-
lists: {
|
|
1006
|
-
Account: list({
|
|
1007
|
-
fields: {
|
|
1008
|
-
name: text(),
|
|
1009
|
-
},
|
|
1010
|
-
access: {
|
|
1011
|
-
operation: {
|
|
1012
|
-
query: ({ session }) => ({ user: { id: { equals: session?.userId } } }),
|
|
1013
|
-
update: () => true,
|
|
1014
|
-
},
|
|
1015
|
-
},
|
|
1016
|
-
}),
|
|
1017
|
-
Student: list({
|
|
1018
|
-
fields: {
|
|
1019
|
-
name: text(),
|
|
1020
|
-
account: relationship({ ref: 'Account' }),
|
|
1021
|
-
},
|
|
1022
|
-
access: {
|
|
1023
|
-
operation: {
|
|
1024
|
-
query: () => true,
|
|
1025
|
-
create: () => true,
|
|
1026
|
-
},
|
|
1027
|
-
},
|
|
1028
|
-
}),
|
|
1029
|
-
},
|
|
1030
|
-
})
|
|
1031
|
-
|
|
1032
|
-
mockPrisma.account = {
|
|
1033
|
-
findFirst: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
1034
|
-
findMany: vi.fn(),
|
|
1035
|
-
findUnique: vi.fn(),
|
|
1036
|
-
create: vi.fn(),
|
|
1037
|
-
update: vi.fn(),
|
|
1038
|
-
delete: vi.fn(),
|
|
1039
|
-
count: vi.fn(),
|
|
1040
|
-
}
|
|
1041
|
-
mockPrisma.student = {
|
|
1042
|
-
findFirst: vi.fn(),
|
|
1043
|
-
findMany: vi.fn(),
|
|
1044
|
-
findUnique: vi.fn(),
|
|
1045
|
-
create: vi.fn().mockResolvedValue({ id: 's1', name: 'Kid', accountId: 'acc-1' }),
|
|
1046
|
-
update: vi.fn(),
|
|
1047
|
-
delete: vi.fn(),
|
|
1048
|
-
count: vi.fn(),
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
1052
|
-
|
|
1053
|
-
const result = await context.db.student.create({
|
|
1054
|
-
data: {
|
|
1055
|
-
name: 'Kid',
|
|
1056
|
-
account: { connect: { id: 'acc-1' } },
|
|
1057
|
-
},
|
|
1058
|
-
})
|
|
1059
|
-
|
|
1060
|
-
expect(result).toBeDefined()
|
|
1061
|
-
expect(mockPrisma.student.create).toHaveBeenCalled()
|
|
1062
|
-
// Reachability check evaluated the nested-relation filter in the DB.
|
|
1063
|
-
expect(mockPrisma.account.findFirst).toHaveBeenCalledWith({
|
|
1064
|
-
where: {
|
|
1065
|
-
AND: [{ id: 'acc-1' }, { user: { id: { equals: 'user-1' } } }],
|
|
1066
|
-
},
|
|
1067
|
-
})
|
|
1068
|
-
})
|
|
1069
|
-
|
|
1070
|
-
it('denies nested connect when a NESTED-RELATION filter is NOT reachable', async () => {
|
|
1071
|
-
const testConfig = config({
|
|
1072
|
-
db: {
|
|
1073
|
-
provider: 'postgresql',
|
|
1074
|
-
url: 'postgresql://localhost:5432/test',
|
|
1075
|
-
},
|
|
1076
|
-
lists: {
|
|
1077
|
-
Account: list({
|
|
1078
|
-
fields: {
|
|
1079
|
-
name: text(),
|
|
1080
|
-
},
|
|
1081
|
-
access: {
|
|
1082
|
-
operation: {
|
|
1083
|
-
query: ({ session }) => ({ user: { id: { equals: session?.userId } } }),
|
|
1084
|
-
update: () => true,
|
|
1085
|
-
},
|
|
1086
|
-
},
|
|
1087
|
-
}),
|
|
1088
|
-
Student: list({
|
|
1089
|
-
fields: {
|
|
1090
|
-
name: text(),
|
|
1091
|
-
account: relationship({ ref: 'Account' }),
|
|
1092
|
-
},
|
|
1093
|
-
access: {
|
|
1094
|
-
operation: {
|
|
1095
|
-
query: () => true,
|
|
1096
|
-
create: () => true,
|
|
1097
|
-
},
|
|
1098
|
-
},
|
|
1099
|
-
}),
|
|
1100
|
-
},
|
|
1101
|
-
})
|
|
1102
|
-
|
|
1103
|
-
mockPrisma.account = {
|
|
1104
|
-
// Not reachable for this caller → findFirst returns null.
|
|
1105
|
-
findFirst: vi.fn().mockResolvedValue(null),
|
|
1106
|
-
findMany: vi.fn(),
|
|
1107
|
-
findUnique: vi.fn(),
|
|
1108
|
-
create: vi.fn(),
|
|
1109
|
-
update: vi.fn(),
|
|
1110
|
-
delete: vi.fn(),
|
|
1111
|
-
count: vi.fn(),
|
|
1112
|
-
}
|
|
1113
|
-
mockPrisma.student = {
|
|
1114
|
-
findFirst: vi.fn(),
|
|
1115
|
-
findMany: vi.fn(),
|
|
1116
|
-
findUnique: vi.fn(),
|
|
1117
|
-
create: vi.fn(),
|
|
1118
|
-
update: vi.fn(),
|
|
1119
|
-
delete: vi.fn(),
|
|
1120
|
-
count: vi.fn(),
|
|
1121
|
-
}
|
|
1122
|
-
|
|
1123
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'attacker' })
|
|
1124
|
-
|
|
1125
|
-
await expect(
|
|
1126
|
-
context.db.student.create({
|
|
1127
|
-
data: {
|
|
1128
|
-
name: 'Kid',
|
|
1129
|
-
account: { connect: { id: 'someone-elses-account' } },
|
|
1130
|
-
},
|
|
1131
|
-
}),
|
|
1132
|
-
).rejects.toThrow('Access denied: Cannot connect to this item')
|
|
1133
|
-
expect(mockPrisma.student.create).not.toHaveBeenCalled()
|
|
1134
|
-
})
|
|
1135
|
-
|
|
1136
|
-
it('evaluates AND/OR/some/none/not boolean-combinator filters via DB reachability', async () => {
|
|
1137
|
-
const complexFilter = {
|
|
1138
|
-
OR: [
|
|
1139
|
-
{ AND: [{ status: { equals: 'active' } }, { NOT: { archived: { equals: true } } }] },
|
|
1140
|
-
{ members: { some: { userId: { equals: 'user-1' } } } },
|
|
1141
|
-
],
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
const testConfig = config({
|
|
1145
|
-
db: {
|
|
1146
|
-
provider: 'postgresql',
|
|
1147
|
-
url: 'postgresql://localhost:5432/test',
|
|
1148
|
-
},
|
|
1149
|
-
lists: {
|
|
1150
|
-
Team: list({
|
|
1151
|
-
fields: {
|
|
1152
|
-
name: text(),
|
|
1153
|
-
},
|
|
1154
|
-
access: {
|
|
1155
|
-
operation: {
|
|
1156
|
-
query: () => complexFilter,
|
|
1157
|
-
update: () => true,
|
|
1158
|
-
},
|
|
1159
|
-
},
|
|
1160
|
-
}),
|
|
1161
|
-
Project: list({
|
|
1162
|
-
fields: {
|
|
1163
|
-
title: text(),
|
|
1164
|
-
team: relationship({ ref: 'Team' }),
|
|
1165
|
-
},
|
|
1166
|
-
access: {
|
|
1167
|
-
operation: {
|
|
1168
|
-
query: () => true,
|
|
1169
|
-
create: () => true,
|
|
1170
|
-
},
|
|
1171
|
-
},
|
|
1172
|
-
}),
|
|
1173
|
-
},
|
|
1174
|
-
})
|
|
1175
|
-
|
|
1176
|
-
mockPrisma.team = {
|
|
1177
|
-
findFirst: vi.fn().mockResolvedValue({ id: 'team-1', name: 'Team A' }),
|
|
1178
|
-
findMany: vi.fn(),
|
|
1179
|
-
findUnique: vi.fn(),
|
|
1180
|
-
create: vi.fn(),
|
|
1181
|
-
update: vi.fn(),
|
|
1182
|
-
delete: vi.fn(),
|
|
1183
|
-
count: vi.fn(),
|
|
1184
|
-
}
|
|
1185
|
-
mockPrisma.project = {
|
|
1186
|
-
findFirst: vi.fn(),
|
|
1187
|
-
findMany: vi.fn(),
|
|
1188
|
-
findUnique: vi.fn(),
|
|
1189
|
-
create: vi.fn().mockResolvedValue({ id: 'p1', title: 'P1', teamId: 'team-1' }),
|
|
1190
|
-
update: vi.fn(),
|
|
1191
|
-
delete: vi.fn(),
|
|
1192
|
-
count: vi.fn(),
|
|
1193
|
-
}
|
|
1194
|
-
|
|
1195
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
1196
|
-
|
|
1197
|
-
const result = await context.db.project.create({
|
|
1198
|
-
data: {
|
|
1199
|
-
title: 'P1',
|
|
1200
|
-
team: { connect: { id: 'team-1' } },
|
|
1201
|
-
},
|
|
1202
|
-
})
|
|
1203
|
-
|
|
1204
|
-
expect(result).toBeDefined()
|
|
1205
|
-
// The full boolean-combinator filter is AND-combined with the connection
|
|
1206
|
-
// and handed to the DB — no in-memory walk, no false denial.
|
|
1207
|
-
expect(mockPrisma.team.findFirst).toHaveBeenCalledWith({
|
|
1208
|
-
where: { AND: [{ id: 'team-1' }, complexFilter] },
|
|
1209
|
-
})
|
|
1210
|
-
})
|
|
1211
|
-
|
|
1212
|
-
it('sudo connect bypasses the access check entirely', async () => {
|
|
1213
|
-
const queryAccess = vi.fn(() => false as const)
|
|
1214
|
-
|
|
1215
|
-
const testConfig = config({
|
|
1216
|
-
db: {
|
|
1217
|
-
provider: 'postgresql',
|
|
1218
|
-
url: 'postgresql://localhost:5432/test',
|
|
1219
|
-
},
|
|
1220
|
-
lists: {
|
|
1221
|
-
User: list({
|
|
1222
|
-
fields: {
|
|
1223
|
-
name: text(),
|
|
1224
|
-
},
|
|
1225
|
-
access: {
|
|
1226
|
-
operation: {
|
|
1227
|
-
query: queryAccess, // would deny everything
|
|
1228
|
-
update: () => false,
|
|
1229
|
-
},
|
|
1230
|
-
},
|
|
1231
|
-
}),
|
|
1232
|
-
Post: list({
|
|
1233
|
-
fields: {
|
|
1234
|
-
title: text(),
|
|
1235
|
-
author: relationship({ ref: 'User.posts' }),
|
|
1236
|
-
},
|
|
1237
|
-
access: {
|
|
1238
|
-
operation: {
|
|
1239
|
-
query: () => true,
|
|
1240
|
-
update: () => true,
|
|
1241
|
-
},
|
|
1242
|
-
},
|
|
1243
|
-
}),
|
|
1244
|
-
},
|
|
1245
|
-
})
|
|
1246
|
-
|
|
1247
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1248
|
-
mockPrisma.post.update.mockResolvedValue({ id: '1', title: 'Original Title', authorId: '2' })
|
|
1249
|
-
|
|
1250
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' }).sudo()
|
|
1251
|
-
|
|
1252
|
-
const result = await context.db.post.update({
|
|
1253
|
-
where: { id: '1' },
|
|
1254
|
-
data: {
|
|
1255
|
-
author: { connect: { id: '2' } },
|
|
1256
|
-
},
|
|
1257
|
-
})
|
|
1258
|
-
|
|
1259
|
-
expect(result).toBeDefined()
|
|
1260
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
1261
|
-
// Sudo skips access evaluation: neither the access fn nor reachability run.
|
|
1262
|
-
expect(queryAccess).not.toHaveBeenCalled()
|
|
1263
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
1264
|
-
expect(mockPrisma.user.findUnique).not.toHaveBeenCalled()
|
|
1265
|
-
})
|
|
1266
|
-
|
|
1267
|
-
it("connectOrCreate's connect branch uses read-access + DB reachability and denies unreachable rows", async () => {
|
|
1268
|
-
const testConfig = config({
|
|
1269
|
-
db: {
|
|
1270
|
-
provider: 'postgresql',
|
|
1271
|
-
url: 'postgresql://localhost:5432/test',
|
|
1272
|
-
},
|
|
1273
|
-
lists: {
|
|
1274
|
-
Account: list({
|
|
1275
|
-
fields: {
|
|
1276
|
-
name: text(),
|
|
1277
|
-
},
|
|
1278
|
-
access: {
|
|
1279
|
-
operation: {
|
|
1280
|
-
query: ({ session }) => ({ user: { id: { equals: session?.userId } } }),
|
|
1281
|
-
create: () => true,
|
|
1282
|
-
update: () => true,
|
|
1283
|
-
},
|
|
1284
|
-
},
|
|
1285
|
-
}),
|
|
1286
|
-
Student: list({
|
|
1287
|
-
fields: {
|
|
1288
|
-
name: text(),
|
|
1289
|
-
account: relationship({ ref: 'Account' }),
|
|
1290
|
-
},
|
|
1291
|
-
access: {
|
|
1292
|
-
operation: {
|
|
1293
|
-
query: () => true,
|
|
1294
|
-
create: () => true,
|
|
1295
|
-
},
|
|
1296
|
-
},
|
|
1297
|
-
}),
|
|
1298
|
-
},
|
|
1299
|
-
})
|
|
1300
|
-
|
|
1301
|
-
mockPrisma.account = {
|
|
1302
|
-
// Row exists ...
|
|
1303
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'acc-x', name: 'Other Account' }),
|
|
1304
|
-
// ... but is NOT reachable under the access filter for this caller.
|
|
1305
|
-
findFirst: vi.fn().mockResolvedValue(null),
|
|
1306
|
-
findMany: vi.fn(),
|
|
1307
|
-
create: vi.fn(),
|
|
1308
|
-
update: vi.fn(),
|
|
1309
|
-
delete: vi.fn(),
|
|
1310
|
-
count: vi.fn(),
|
|
1311
|
-
}
|
|
1312
|
-
mockPrisma.student = {
|
|
1313
|
-
findFirst: vi.fn(),
|
|
1314
|
-
findMany: vi.fn(),
|
|
1315
|
-
findUnique: vi.fn(),
|
|
1316
|
-
create: vi.fn(),
|
|
1317
|
-
update: vi.fn(),
|
|
1318
|
-
delete: vi.fn(),
|
|
1319
|
-
count: vi.fn(),
|
|
1320
|
-
}
|
|
1321
|
-
|
|
1322
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
1323
|
-
|
|
1324
|
-
await expect(
|
|
1325
|
-
context.db.student.create({
|
|
1326
|
-
data: {
|
|
1327
|
-
name: 'Kid',
|
|
1328
|
-
account: {
|
|
1329
|
-
connectOrCreate: {
|
|
1330
|
-
where: { id: 'acc-x' },
|
|
1331
|
-
create: { name: 'New Account' },
|
|
1332
|
-
},
|
|
1333
|
-
},
|
|
1334
|
-
},
|
|
1335
|
-
}),
|
|
1336
|
-
).rejects.toThrow('Access denied: Cannot connect to existing item')
|
|
1337
|
-
|
|
1338
|
-
expect(mockPrisma.account.findFirst).toHaveBeenCalledWith({
|
|
1339
|
-
where: { AND: [{ id: 'acc-x' }, { user: { id: { equals: 'user-1' } } }] },
|
|
1340
|
-
})
|
|
1341
|
-
expect(mockPrisma.student.create).not.toHaveBeenCalled()
|
|
1342
|
-
})
|
|
1343
|
-
|
|
1344
|
-
it("connectOrCreate's connect branch allows a reachable existing row", async () => {
|
|
1345
|
-
const testConfig = config({
|
|
1346
|
-
db: {
|
|
1347
|
-
provider: 'postgresql',
|
|
1348
|
-
url: 'postgresql://localhost:5432/test',
|
|
1349
|
-
},
|
|
1350
|
-
lists: {
|
|
1351
|
-
Account: list({
|
|
1352
|
-
fields: {
|
|
1353
|
-
name: text(),
|
|
1354
|
-
},
|
|
1355
|
-
access: {
|
|
1356
|
-
operation: {
|
|
1357
|
-
query: ({ session }) => ({ user: { id: { equals: session?.userId } } }),
|
|
1358
|
-
create: () => true,
|
|
1359
|
-
update: () => true,
|
|
1360
|
-
},
|
|
1361
|
-
},
|
|
1362
|
-
}),
|
|
1363
|
-
Student: list({
|
|
1364
|
-
fields: {
|
|
1365
|
-
name: text(),
|
|
1366
|
-
account: relationship({ ref: 'Account' }),
|
|
1367
|
-
},
|
|
1368
|
-
access: {
|
|
1369
|
-
operation: {
|
|
1370
|
-
query: () => true,
|
|
1371
|
-
create: () => true,
|
|
1372
|
-
},
|
|
1373
|
-
},
|
|
1374
|
-
}),
|
|
1375
|
-
},
|
|
1376
|
-
})
|
|
1377
|
-
|
|
1378
|
-
mockPrisma.account = {
|
|
1379
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
1380
|
-
findFirst: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
1381
|
-
findMany: vi.fn(),
|
|
1382
|
-
create: vi.fn(),
|
|
1383
|
-
update: vi.fn(),
|
|
1384
|
-
delete: vi.fn(),
|
|
1385
|
-
count: vi.fn(),
|
|
1386
|
-
}
|
|
1387
|
-
mockPrisma.student = {
|
|
1388
|
-
findFirst: vi.fn(),
|
|
1389
|
-
findMany: vi.fn(),
|
|
1390
|
-
findUnique: vi.fn(),
|
|
1391
|
-
create: vi.fn().mockResolvedValue({ id: 's1', name: 'Kid', accountId: 'acc-1' }),
|
|
1392
|
-
update: vi.fn(),
|
|
1393
|
-
delete: vi.fn(),
|
|
1394
|
-
count: vi.fn(),
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
1398
|
-
|
|
1399
|
-
const result = await context.db.student.create({
|
|
1400
|
-
data: {
|
|
1401
|
-
name: 'Kid',
|
|
1402
|
-
account: {
|
|
1403
|
-
connectOrCreate: {
|
|
1404
|
-
where: { id: 'acc-1' },
|
|
1405
|
-
create: { name: 'New Account' },
|
|
1406
|
-
},
|
|
1407
|
-
},
|
|
1408
|
-
},
|
|
1409
|
-
})
|
|
1410
|
-
|
|
1411
|
-
expect(result).toBeDefined()
|
|
1412
|
-
expect(mockPrisma.student.create).toHaveBeenCalled()
|
|
1413
|
-
})
|
|
1414
|
-
|
|
1415
|
-
it('should deny nested update when update access is denied on related item', async () => {
|
|
1416
|
-
const testConfig = config({
|
|
1417
|
-
db: {
|
|
1418
|
-
provider: 'postgresql',
|
|
1419
|
-
url: 'postgresql://localhost:5432/test',
|
|
1420
|
-
},
|
|
1421
|
-
lists: {
|
|
1422
|
-
User: list({
|
|
1423
|
-
fields: {
|
|
1424
|
-
name: text(),
|
|
1425
|
-
},
|
|
1426
|
-
access: {
|
|
1427
|
-
operation: {
|
|
1428
|
-
query: () => true,
|
|
1429
|
-
update: () => false, // Deny all updates
|
|
1430
|
-
},
|
|
1431
|
-
},
|
|
1432
|
-
}),
|
|
1433
|
-
Post: list({
|
|
1434
|
-
fields: {
|
|
1435
|
-
title: text(),
|
|
1436
|
-
author: relationship({ ref: 'User.posts' }),
|
|
1437
|
-
},
|
|
1438
|
-
access: {
|
|
1439
|
-
operation: {
|
|
1440
|
-
query: () => true,
|
|
1441
|
-
update: () => true,
|
|
1442
|
-
},
|
|
1443
|
-
},
|
|
1444
|
-
}),
|
|
1445
|
-
},
|
|
1446
|
-
})
|
|
1447
|
-
|
|
1448
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
1449
|
-
id: '1',
|
|
1450
|
-
title: 'Original Title',
|
|
1451
|
-
})
|
|
1452
|
-
|
|
1453
|
-
mockPrisma.user.findUnique.mockResolvedValue({
|
|
1454
|
-
id: '2',
|
|
1455
|
-
name: 'John Doe',
|
|
1456
|
-
})
|
|
1457
|
-
|
|
1458
|
-
const context = getContext(await testConfig, mockPrisma, null)
|
|
1459
|
-
|
|
1460
|
-
await expect(
|
|
1461
|
-
context.db.post.update({
|
|
1462
|
-
where: { id: '1' },
|
|
1463
|
-
data: {
|
|
1464
|
-
author: {
|
|
1465
|
-
update: {
|
|
1466
|
-
where: { id: '2' },
|
|
1467
|
-
data: { name: 'Jane Doe' },
|
|
1468
|
-
},
|
|
1469
|
-
},
|
|
1470
|
-
},
|
|
1471
|
-
}),
|
|
1472
|
-
).rejects.toThrow('Access denied: Cannot update related item')
|
|
1473
|
-
})
|
|
1474
|
-
|
|
1475
|
-
// #1081: a nested update/delete access rule returning a Prisma filter (the
|
|
1476
|
-
// common `gate ? filter : false` composition) must be re-checked against
|
|
1477
|
-
// the target row in the DATABASE, mirroring the top-level write pipeline's
|
|
1478
|
-
// `resolveExistingTarget` and the nested-connect reachability check (#578)
|
|
1479
|
-
// — not treated as an unconditional allow just because it isn't `false`.
|
|
1480
|
-
describe('Nested update/delete honour a filter-result access control (#1081)', () => {
|
|
1481
|
-
it('denies nested update (to-one) when the update access filter does not match the target row', async () => {
|
|
1482
|
-
const userResolveInput = vi.fn(async ({ resolvedData }) => resolvedData)
|
|
1483
|
-
|
|
1484
|
-
const testConfig = config({
|
|
1485
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1486
|
-
lists: {
|
|
1487
|
-
User: list({
|
|
1488
|
-
fields: { name: text() },
|
|
1489
|
-
access: {
|
|
1490
|
-
operation: {
|
|
1491
|
-
query: () => true,
|
|
1492
|
-
update: ({ session }) => ({ id: { equals: session?.userId } }),
|
|
1493
|
-
},
|
|
1494
|
-
},
|
|
1495
|
-
hooks: { resolveInput: userResolveInput },
|
|
1496
|
-
}),
|
|
1497
|
-
Post: list({
|
|
1498
|
-
fields: {
|
|
1499
|
-
title: text(),
|
|
1500
|
-
author: relationship({ ref: 'User.posts' }),
|
|
1501
|
-
},
|
|
1502
|
-
access: {
|
|
1503
|
-
operation: { query: () => true, update: () => true },
|
|
1504
|
-
},
|
|
1505
|
-
}),
|
|
1506
|
-
},
|
|
1507
|
-
})
|
|
1508
|
-
|
|
1509
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1510
|
-
// The target row exists ...
|
|
1511
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: '2', name: 'John Doe' })
|
|
1512
|
-
// ... but is NOT reachable under { id: { equals: '1' } } for this caller.
|
|
1513
|
-
mockPrisma.user.findFirst.mockResolvedValue(null)
|
|
1514
|
-
|
|
1515
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1516
|
-
|
|
1517
|
-
await expect(
|
|
1518
|
-
context.db.post.update({
|
|
1519
|
-
where: { id: '1' },
|
|
1520
|
-
data: {
|
|
1521
|
-
author: {
|
|
1522
|
-
update: { where: { id: '2' }, data: { name: 'Jane Doe' } },
|
|
1523
|
-
},
|
|
1524
|
-
},
|
|
1525
|
-
}),
|
|
1526
|
-
).rejects.toThrow('Access denied: Cannot update related item')
|
|
1527
|
-
|
|
1528
|
-
// Reachability re-checked in the DB, AND-combining the target `where`
|
|
1529
|
-
// with the returned filter — not an in-memory test.
|
|
1530
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalledWith({
|
|
1531
|
-
where: { AND: [{ id: { equals: '1' } }, { id: '2' }] },
|
|
1532
|
-
})
|
|
1533
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
1534
|
-
expect(mockPrisma.user.update).not.toHaveBeenCalled()
|
|
1535
|
-
// Denied before any of the target list's hooks fire.
|
|
1536
|
-
expect(userResolveInput).not.toHaveBeenCalled()
|
|
1537
|
-
})
|
|
1538
|
-
|
|
1539
|
-
it('allows nested update (to-one) when the update access filter matches the target row', async () => {
|
|
1540
|
-
const testConfig = config({
|
|
1541
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1542
|
-
lists: {
|
|
1543
|
-
User: list({
|
|
1544
|
-
fields: { name: text() },
|
|
1545
|
-
access: {
|
|
1546
|
-
operation: {
|
|
1547
|
-
query: () => true,
|
|
1548
|
-
update: ({ session }) => ({ id: { equals: session?.userId } }),
|
|
1549
|
-
},
|
|
1550
|
-
},
|
|
1551
|
-
}),
|
|
1552
|
-
Post: list({
|
|
1553
|
-
fields: {
|
|
1554
|
-
title: text(),
|
|
1555
|
-
author: relationship({ ref: 'User.posts' }),
|
|
1556
|
-
},
|
|
1557
|
-
access: {
|
|
1558
|
-
operation: { query: () => true, update: () => true },
|
|
1559
|
-
},
|
|
1560
|
-
}),
|
|
1561
|
-
},
|
|
1562
|
-
})
|
|
1563
|
-
|
|
1564
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1565
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: '1', name: 'John Doe' })
|
|
1566
|
-
// Reachable: the target IS the caller's own row.
|
|
1567
|
-
mockPrisma.user.findFirst.mockResolvedValue({ id: '1', name: 'John Doe' })
|
|
1568
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
1569
|
-
id: '1',
|
|
1570
|
-
title: 'Original Title',
|
|
1571
|
-
authorId: '1',
|
|
1572
|
-
})
|
|
1573
|
-
|
|
1574
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1575
|
-
|
|
1576
|
-
const result = await context.db.post.update({
|
|
1577
|
-
where: { id: '1' },
|
|
1578
|
-
data: {
|
|
1579
|
-
author: {
|
|
1580
|
-
update: { where: { id: '1' }, data: { name: 'Jane Doe' } },
|
|
1581
|
-
},
|
|
1582
|
-
},
|
|
1583
|
-
})
|
|
1584
|
-
|
|
1585
|
-
expect(result).toBeDefined()
|
|
1586
|
-
// The re-check matched, so the nested write reaches Prisma's own
|
|
1587
|
-
// nested-update syntax on the PARENT's `update` call (Prisma persists
|
|
1588
|
-
// it, not a separate `user.update` call).
|
|
1589
|
-
expect(mockPrisma.post.update).toHaveBeenCalledWith(
|
|
1590
|
-
expect.objectContaining({
|
|
1591
|
-
data: expect.objectContaining({
|
|
1592
|
-
author: { update: { where: { id: '1' }, data: { name: 'Jane Doe' } } },
|
|
1593
|
-
}),
|
|
1594
|
-
}),
|
|
1595
|
-
)
|
|
1596
|
-
})
|
|
1597
|
-
|
|
1598
|
-
it('denies nested update (to-many array) when the update access filter does not match the target row', async () => {
|
|
1599
|
-
const testConfig = config({
|
|
1600
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1601
|
-
lists: {
|
|
1602
|
-
Comment: list({
|
|
1603
|
-
fields: { body: text() },
|
|
1604
|
-
access: {
|
|
1605
|
-
operation: {
|
|
1606
|
-
query: () => true,
|
|
1607
|
-
update: ({ session }) => ({ authorId: { equals: session?.userId } }),
|
|
1608
|
-
},
|
|
1609
|
-
},
|
|
1610
|
-
}),
|
|
1611
|
-
Post: list({
|
|
1612
|
-
fields: {
|
|
1613
|
-
title: text(),
|
|
1614
|
-
comments: relationship({ ref: 'Comment', many: true }),
|
|
1615
|
-
},
|
|
1616
|
-
access: {
|
|
1617
|
-
operation: { query: () => true, update: () => true },
|
|
1618
|
-
},
|
|
1619
|
-
}),
|
|
1620
|
-
},
|
|
1621
|
-
})
|
|
1622
|
-
|
|
1623
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1624
|
-
mockPrisma.comment.findUnique.mockResolvedValue({
|
|
1625
|
-
id: 'c1',
|
|
1626
|
-
body: 'hi',
|
|
1627
|
-
authorId: 'someone-else',
|
|
1628
|
-
})
|
|
1629
|
-
mockPrisma.comment.findFirst.mockResolvedValue(null)
|
|
1630
|
-
|
|
1631
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1632
|
-
|
|
1633
|
-
await expect(
|
|
1634
|
-
context.db.post.update({
|
|
1635
|
-
where: { id: '1' },
|
|
1636
|
-
data: {
|
|
1637
|
-
comments: {
|
|
1638
|
-
update: [{ where: { id: 'c1' }, data: { body: 'edited' } }],
|
|
1639
|
-
},
|
|
1640
|
-
},
|
|
1641
|
-
}),
|
|
1642
|
-
).rejects.toThrow('Access denied: Cannot update related item')
|
|
1643
|
-
|
|
1644
|
-
expect(mockPrisma.comment.findFirst).toHaveBeenCalledWith({
|
|
1645
|
-
where: { AND: [{ authorId: { equals: '1' } }, { id: 'c1' }] },
|
|
1646
|
-
})
|
|
1647
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
1648
|
-
})
|
|
1649
|
-
|
|
1650
|
-
it('denies nested delete when the delete access filter does not match the target row', async () => {
|
|
1651
|
-
const commentBeforeOperation = vi.fn(async () => {})
|
|
1652
|
-
|
|
1653
|
-
const testConfig = config({
|
|
1654
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1655
|
-
lists: {
|
|
1656
|
-
Comment: list({
|
|
1657
|
-
fields: { body: text() },
|
|
1658
|
-
access: {
|
|
1659
|
-
operation: {
|
|
1660
|
-
query: () => true,
|
|
1661
|
-
delete: ({ session }) => ({ authorId: { equals: session?.userId } }),
|
|
1662
|
-
},
|
|
1663
|
-
},
|
|
1664
|
-
hooks: { beforeOperation: commentBeforeOperation },
|
|
1665
|
-
}),
|
|
1666
|
-
Post: list({
|
|
1667
|
-
fields: {
|
|
1668
|
-
title: text(),
|
|
1669
|
-
comments: relationship({ ref: 'Comment', many: true }),
|
|
1670
|
-
},
|
|
1671
|
-
access: {
|
|
1672
|
-
operation: { query: () => true, update: () => true },
|
|
1673
|
-
},
|
|
1674
|
-
}),
|
|
1675
|
-
},
|
|
1676
|
-
})
|
|
1677
|
-
|
|
1678
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1679
|
-
mockPrisma.comment.findUnique.mockResolvedValue({
|
|
1680
|
-
id: 'c1',
|
|
1681
|
-
body: 'hi',
|
|
1682
|
-
authorId: 'someone-else',
|
|
1683
|
-
})
|
|
1684
|
-
mockPrisma.comment.findFirst.mockResolvedValue(null)
|
|
1685
|
-
|
|
1686
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1687
|
-
|
|
1688
|
-
await expect(
|
|
1689
|
-
context.db.post.update({
|
|
1690
|
-
where: { id: '1' },
|
|
1691
|
-
data: {
|
|
1692
|
-
comments: { delete: { id: 'c1' } },
|
|
1693
|
-
},
|
|
1694
|
-
}),
|
|
1695
|
-
).rejects.toThrow('Access denied: Cannot delete related item')
|
|
1696
|
-
|
|
1697
|
-
expect(mockPrisma.comment.findFirst).toHaveBeenCalledWith({
|
|
1698
|
-
where: { AND: [{ authorId: { equals: '1' } }, { id: 'c1' }] },
|
|
1699
|
-
})
|
|
1700
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
1701
|
-
expect(commentBeforeOperation).not.toHaveBeenCalled()
|
|
1702
|
-
})
|
|
1703
|
-
|
|
1704
|
-
it('allows nested delete when the delete access filter matches the target row', async () => {
|
|
1705
|
-
const testConfig = config({
|
|
1706
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1707
|
-
lists: {
|
|
1708
|
-
Comment: list({
|
|
1709
|
-
fields: { body: text() },
|
|
1710
|
-
access: {
|
|
1711
|
-
operation: {
|
|
1712
|
-
query: () => true,
|
|
1713
|
-
delete: ({ session }) => ({ authorId: { equals: session?.userId } }),
|
|
1714
|
-
},
|
|
1715
|
-
},
|
|
1716
|
-
}),
|
|
1717
|
-
Post: list({
|
|
1718
|
-
fields: {
|
|
1719
|
-
title: text(),
|
|
1720
|
-
comments: relationship({ ref: 'Comment', many: true }),
|
|
1721
|
-
},
|
|
1722
|
-
access: {
|
|
1723
|
-
operation: { query: () => true, update: () => true },
|
|
1724
|
-
},
|
|
1725
|
-
}),
|
|
1726
|
-
},
|
|
1727
|
-
})
|
|
1728
|
-
|
|
1729
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1730
|
-
mockPrisma.comment.findUnique.mockResolvedValue({ id: 'c1', body: 'hi', authorId: '1' })
|
|
1731
|
-
mockPrisma.comment.findFirst.mockResolvedValue({ id: 'c1', body: 'hi', authorId: '1' })
|
|
1732
|
-
mockPrisma.post.update.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1733
|
-
|
|
1734
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1735
|
-
|
|
1736
|
-
const result = await context.db.post.update({
|
|
1737
|
-
where: { id: '1' },
|
|
1738
|
-
data: {
|
|
1739
|
-
comments: { delete: { id: 'c1' } },
|
|
1740
|
-
},
|
|
1741
|
-
})
|
|
1742
|
-
|
|
1743
|
-
expect(result).toBeDefined()
|
|
1744
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
1745
|
-
})
|
|
1746
|
-
|
|
1747
|
-
it('sudo bypasses the reachability re-check for both nested update and nested delete', async () => {
|
|
1748
|
-
const updateAccess = vi.fn(() => ({ id: { equals: 'nobody' } }))
|
|
1749
|
-
const deleteAccess = vi.fn(() => ({ id: { equals: 'nobody' } }))
|
|
1750
|
-
|
|
1751
|
-
const testConfig = config({
|
|
1752
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1753
|
-
lists: {
|
|
1754
|
-
User: list({
|
|
1755
|
-
fields: { name: text() },
|
|
1756
|
-
access: { operation: { query: () => true, update: updateAccess } },
|
|
1757
|
-
}),
|
|
1758
|
-
Comment: list({
|
|
1759
|
-
fields: { body: text() },
|
|
1760
|
-
access: { operation: { query: () => true, delete: deleteAccess } },
|
|
1761
|
-
}),
|
|
1762
|
-
Post: list({
|
|
1763
|
-
fields: {
|
|
1764
|
-
title: text(),
|
|
1765
|
-
author: relationship({ ref: 'User.posts' }),
|
|
1766
|
-
comments: relationship({ ref: 'Comment', many: true }),
|
|
1767
|
-
},
|
|
1768
|
-
access: {
|
|
1769
|
-
operation: { query: () => true, update: () => true },
|
|
1770
|
-
},
|
|
1771
|
-
}),
|
|
1772
|
-
},
|
|
1773
|
-
})
|
|
1774
|
-
|
|
1775
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1776
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: '2', name: 'John Doe' })
|
|
1777
|
-
mockPrisma.comment.findUnique.mockResolvedValue({ id: 'c1', body: 'hi' })
|
|
1778
|
-
mockPrisma.post.update.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1779
|
-
|
|
1780
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' }).sudo()
|
|
1781
|
-
|
|
1782
|
-
const result = await context.db.post.update({
|
|
1783
|
-
where: { id: '1' },
|
|
1784
|
-
data: {
|
|
1785
|
-
author: { update: { where: { id: '2' }, data: { name: 'Jane Doe' } } },
|
|
1786
|
-
comments: { delete: { id: 'c1' } },
|
|
1787
|
-
},
|
|
1788
|
-
})
|
|
1789
|
-
|
|
1790
|
-
expect(result).toBeDefined()
|
|
1791
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
1792
|
-
expect(updateAccess).not.toHaveBeenCalled()
|
|
1793
|
-
expect(deleteAccess).not.toHaveBeenCalled()
|
|
1794
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
1795
|
-
expect(mockPrisma.comment.findFirst).not.toHaveBeenCalled()
|
|
1796
|
-
})
|
|
1797
|
-
})
|
|
1798
|
-
})
|
|
1799
|
-
|
|
1800
|
-
// #588 — nested connect must ALSO be gated by the OWNING relationship field's
|
|
1801
|
-
// field-level access (the `access` on the relationship field of the list being
|
|
1802
|
-
// written, e.g. `Post.author`), in addition to the target read-access + DB
|
|
1803
|
-
// reachability check from #578. A deny on the owning field denies the connect
|
|
1804
|
-
// even when the target row is fully readable/reachable.
|
|
1805
|
-
//
|
|
1806
|
-
// NOTE: the owning relationship field's create/update access is enforced at two
|
|
1807
|
-
// points for a write that names the relationship key: (1) the parent write's
|
|
1808
|
-
// own `filterWritableFields` gate (#568), which fires FIRST and fail-louds with
|
|
1809
|
-
// a "Cannot create/update <field>" ValidationError, and (2) the connect-site
|
|
1810
|
-
// gate added here (#588), which fail-louds with "Access denied: Cannot
|
|
1811
|
-
// connect…". For the standard nested-connect payload #568 short-circuits, so
|
|
1812
|
-
// these deny tests assert the write is denied fail-loud (and never persisted)
|
|
1813
|
-
// rather than pinning the exact layer. The ALLOW + sudo tests below exercise
|
|
1814
|
-
// the connect-site gate's pass-through behaviour directly.
|
|
1815
|
-
const OWNING_FIELD_DENIES = /field-level access denied|Cannot connect/
|
|
1816
|
-
|
|
1817
|
-
describe('Owning-field field-level access gate on nested connect (#588)', () => {
|
|
1818
|
-
it('denies connect (enclosing UPDATE) when the owning field update access is denied, even though target is readable/reachable', async () => {
|
|
1819
|
-
const testConfig = config({
|
|
1820
|
-
db: {
|
|
1821
|
-
provider: 'postgresql',
|
|
1822
|
-
url: 'postgresql://localhost:5432/test',
|
|
1823
|
-
},
|
|
1824
|
-
lists: {
|
|
1825
|
-
User: list({
|
|
1826
|
-
fields: {
|
|
1827
|
-
name: text(),
|
|
1828
|
-
},
|
|
1829
|
-
access: {
|
|
1830
|
-
operation: {
|
|
1831
|
-
query: () => true, // target fully readable
|
|
1832
|
-
update: () => true,
|
|
1833
|
-
},
|
|
1834
|
-
},
|
|
1835
|
-
}),
|
|
1836
|
-
Post: list({
|
|
1837
|
-
fields: {
|
|
1838
|
-
title: text(),
|
|
1839
|
-
// Owning relationship field denies update-time writes.
|
|
1840
|
-
author: relationship({
|
|
1841
|
-
ref: 'User.posts',
|
|
1842
|
-
access: {
|
|
1843
|
-
update: () => false,
|
|
1844
|
-
},
|
|
1845
|
-
}),
|
|
1846
|
-
},
|
|
1847
|
-
access: {
|
|
1848
|
-
operation: {
|
|
1849
|
-
query: () => true,
|
|
1850
|
-
update: () => true,
|
|
1851
|
-
},
|
|
1852
|
-
},
|
|
1853
|
-
}),
|
|
1854
|
-
},
|
|
1855
|
-
})
|
|
1856
|
-
|
|
1857
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1858
|
-
// Target row is readable (query=true) and exists.
|
|
1859
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: '2', name: 'John Doe' })
|
|
1860
|
-
|
|
1861
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1862
|
-
|
|
1863
|
-
await expect(
|
|
1864
|
-
context.db.post.update({
|
|
1865
|
-
where: { id: '1' },
|
|
1866
|
-
data: {
|
|
1867
|
-
author: { connect: { id: '2' } },
|
|
1868
|
-
},
|
|
1869
|
-
}),
|
|
1870
|
-
).rejects.toThrow(OWNING_FIELD_DENIES)
|
|
1871
|
-
|
|
1872
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
1873
|
-
// Denied owning field short-circuits before the target row is touched.
|
|
1874
|
-
expect(mockPrisma.user.findUnique).not.toHaveBeenCalled()
|
|
1875
|
-
})
|
|
1876
|
-
|
|
1877
|
-
it('denies connect (enclosing CREATE) when the owning field create access is denied, even though target is readable/reachable', async () => {
|
|
1878
|
-
const testConfig = config({
|
|
1879
|
-
db: {
|
|
1880
|
-
provider: 'postgresql',
|
|
1881
|
-
url: 'postgresql://localhost:5432/test',
|
|
1882
|
-
},
|
|
1883
|
-
lists: {
|
|
1884
|
-
Account: list({
|
|
1885
|
-
fields: {
|
|
1886
|
-
name: text(),
|
|
1887
|
-
},
|
|
1888
|
-
access: {
|
|
1889
|
-
operation: {
|
|
1890
|
-
query: () => true, // target fully readable
|
|
1891
|
-
create: () => true,
|
|
1892
|
-
},
|
|
1893
|
-
},
|
|
1894
|
-
}),
|
|
1895
|
-
Student: list({
|
|
1896
|
-
fields: {
|
|
1897
|
-
name: text(),
|
|
1898
|
-
// Owning relationship field denies create-time writes.
|
|
1899
|
-
account: relationship({
|
|
1900
|
-
ref: 'Account',
|
|
1901
|
-
access: {
|
|
1902
|
-
create: () => false,
|
|
1903
|
-
},
|
|
1904
|
-
}),
|
|
1905
|
-
},
|
|
1906
|
-
access: {
|
|
1907
|
-
operation: {
|
|
1908
|
-
query: () => true,
|
|
1909
|
-
create: () => true,
|
|
1910
|
-
},
|
|
1911
|
-
},
|
|
1912
|
-
}),
|
|
1913
|
-
},
|
|
1914
|
-
})
|
|
1915
|
-
|
|
1916
|
-
mockPrisma.account = {
|
|
1917
|
-
findFirst: vi.fn(),
|
|
1918
|
-
findMany: vi.fn(),
|
|
1919
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
1920
|
-
create: vi.fn(),
|
|
1921
|
-
update: vi.fn(),
|
|
1922
|
-
delete: vi.fn(),
|
|
1923
|
-
count: vi.fn(),
|
|
1924
|
-
}
|
|
1925
|
-
mockPrisma.student = {
|
|
1926
|
-
findFirst: vi.fn(),
|
|
1927
|
-
findMany: vi.fn(),
|
|
1928
|
-
findUnique: vi.fn(),
|
|
1929
|
-
create: vi.fn(),
|
|
1930
|
-
update: vi.fn(),
|
|
1931
|
-
delete: vi.fn(),
|
|
1932
|
-
count: vi.fn(),
|
|
1933
|
-
}
|
|
1934
|
-
|
|
1935
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
1936
|
-
|
|
1937
|
-
await expect(
|
|
1938
|
-
context.db.student.create({
|
|
1939
|
-
data: {
|
|
1940
|
-
name: 'Kid',
|
|
1941
|
-
account: { connect: { id: 'acc-1' } },
|
|
1942
|
-
},
|
|
1943
|
-
}),
|
|
1944
|
-
).rejects.toThrow(OWNING_FIELD_DENIES)
|
|
1945
|
-
|
|
1946
|
-
expect(mockPrisma.student.create).not.toHaveBeenCalled()
|
|
1947
|
-
expect(mockPrisma.account.findUnique).not.toHaveBeenCalled()
|
|
1948
|
-
})
|
|
1949
|
-
|
|
1950
|
-
it('allows connect when the owning field access permits and the target row is reachable (enclosing UPDATE)', async () => {
|
|
1951
|
-
const testConfig = config({
|
|
1952
|
-
db: {
|
|
1953
|
-
provider: 'postgresql',
|
|
1954
|
-
url: 'postgresql://localhost:5432/test',
|
|
1955
|
-
},
|
|
1956
|
-
lists: {
|
|
1957
|
-
User: list({
|
|
1958
|
-
fields: {
|
|
1959
|
-
name: text(),
|
|
1960
|
-
},
|
|
1961
|
-
access: {
|
|
1962
|
-
operation: {
|
|
1963
|
-
query: () => true,
|
|
1964
|
-
update: () => true,
|
|
1965
|
-
},
|
|
1966
|
-
},
|
|
1967
|
-
}),
|
|
1968
|
-
Post: list({
|
|
1969
|
-
fields: {
|
|
1970
|
-
title: text(),
|
|
1971
|
-
author: relationship({
|
|
1972
|
-
ref: 'User.posts',
|
|
1973
|
-
access: {
|
|
1974
|
-
update: () => true, // owning field permits
|
|
1975
|
-
},
|
|
1976
|
-
}),
|
|
1977
|
-
},
|
|
1978
|
-
access: {
|
|
1979
|
-
operation: {
|
|
1980
|
-
query: () => true,
|
|
1981
|
-
update: () => true,
|
|
1982
|
-
},
|
|
1983
|
-
},
|
|
1984
|
-
}),
|
|
1985
|
-
},
|
|
1986
|
-
})
|
|
1987
|
-
|
|
1988
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
1989
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: '2', name: 'John Doe' })
|
|
1990
|
-
mockPrisma.post.update.mockResolvedValue({ id: '1', title: 'Original Title', authorId: '2' })
|
|
1991
|
-
|
|
1992
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
1993
|
-
|
|
1994
|
-
const result = await context.db.post.update({
|
|
1995
|
-
where: { id: '1' },
|
|
1996
|
-
data: {
|
|
1997
|
-
author: { connect: { id: '2' } },
|
|
1998
|
-
},
|
|
1999
|
-
})
|
|
2000
|
-
|
|
2001
|
-
expect(result).toBeDefined()
|
|
2002
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
2003
|
-
// Owning field allowed → fall through to the #578 target existence check.
|
|
2004
|
-
expect(mockPrisma.user.findUnique).toHaveBeenCalledWith({ where: { id: '2' } })
|
|
2005
|
-
})
|
|
2006
|
-
|
|
2007
|
-
it('sudo bypasses the owning-field gate (denied owning field, connect still succeeds)', async () => {
|
|
2008
|
-
const owningFieldUpdate = vi.fn(() => false as const)
|
|
2009
|
-
|
|
2010
|
-
const testConfig = config({
|
|
2011
|
-
db: {
|
|
2012
|
-
provider: 'postgresql',
|
|
2013
|
-
url: 'postgresql://localhost:5432/test',
|
|
2014
|
-
},
|
|
2015
|
-
lists: {
|
|
2016
|
-
User: list({
|
|
2017
|
-
fields: {
|
|
2018
|
-
name: text(),
|
|
2019
|
-
},
|
|
2020
|
-
access: {
|
|
2021
|
-
operation: {
|
|
2022
|
-
query: () => false,
|
|
2023
|
-
update: () => false,
|
|
2024
|
-
},
|
|
2025
|
-
},
|
|
2026
|
-
}),
|
|
2027
|
-
Post: list({
|
|
2028
|
-
fields: {
|
|
2029
|
-
title: text(),
|
|
2030
|
-
author: relationship({
|
|
2031
|
-
ref: 'User.posts',
|
|
2032
|
-
access: {
|
|
2033
|
-
update: owningFieldUpdate, // would deny
|
|
2034
|
-
},
|
|
2035
|
-
}),
|
|
2036
|
-
},
|
|
2037
|
-
access: {
|
|
2038
|
-
operation: {
|
|
2039
|
-
query: () => true,
|
|
2040
|
-
update: () => true,
|
|
2041
|
-
},
|
|
2042
|
-
},
|
|
2043
|
-
}),
|
|
2044
|
-
},
|
|
2045
|
-
})
|
|
2046
|
-
|
|
2047
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: '1', title: 'Original Title' })
|
|
2048
|
-
mockPrisma.post.update.mockResolvedValue({ id: '1', title: 'Original Title', authorId: '2' })
|
|
2049
|
-
|
|
2050
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' }).sudo()
|
|
2051
|
-
|
|
2052
|
-
const result = await context.db.post.update({
|
|
2053
|
-
where: { id: '1' },
|
|
2054
|
-
data: {
|
|
2055
|
-
author: { connect: { id: '2' } },
|
|
2056
|
-
},
|
|
2057
|
-
})
|
|
2058
|
-
|
|
2059
|
-
expect(result).toBeDefined()
|
|
2060
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
2061
|
-
// Sudo skips the whole connect check: neither the owning-field access fn
|
|
2062
|
-
// nor the target reachability/existence read run.
|
|
2063
|
-
expect(owningFieldUpdate).not.toHaveBeenCalled()
|
|
2064
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
2065
|
-
expect(mockPrisma.user.findUnique).not.toHaveBeenCalled()
|
|
2066
|
-
})
|
|
2067
|
-
|
|
2068
|
-
it("connectOrCreate's connect branch is gated by the owning field access (denied → throws, existing row)", async () => {
|
|
2069
|
-
const testConfig = config({
|
|
2070
|
-
db: {
|
|
2071
|
-
provider: 'postgresql',
|
|
2072
|
-
url: 'postgresql://localhost:5432/test',
|
|
2073
|
-
},
|
|
2074
|
-
lists: {
|
|
2075
|
-
Account: list({
|
|
2076
|
-
fields: {
|
|
2077
|
-
name: text(),
|
|
2078
|
-
},
|
|
2079
|
-
access: {
|
|
2080
|
-
operation: {
|
|
2081
|
-
query: () => true, // target readable
|
|
2082
|
-
create: () => true,
|
|
2083
|
-
update: () => true,
|
|
2084
|
-
},
|
|
2085
|
-
},
|
|
2086
|
-
}),
|
|
2087
|
-
Student: list({
|
|
2088
|
-
fields: {
|
|
2089
|
-
name: text(),
|
|
2090
|
-
account: relationship({
|
|
2091
|
-
ref: 'Account',
|
|
2092
|
-
access: {
|
|
2093
|
-
create: () => false, // owning field denies on create
|
|
2094
|
-
},
|
|
2095
|
-
}),
|
|
2096
|
-
},
|
|
2097
|
-
access: {
|
|
2098
|
-
operation: {
|
|
2099
|
-
query: () => true,
|
|
2100
|
-
create: () => true,
|
|
2101
|
-
},
|
|
2102
|
-
},
|
|
2103
|
-
}),
|
|
2104
|
-
},
|
|
2105
|
-
})
|
|
2106
|
-
|
|
2107
|
-
mockPrisma.account = {
|
|
2108
|
-
// Row exists and is reachable, but the owning field gate must still deny.
|
|
2109
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
2110
|
-
findFirst: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
2111
|
-
findMany: vi.fn(),
|
|
2112
|
-
create: vi.fn(),
|
|
2113
|
-
update: vi.fn(),
|
|
2114
|
-
delete: vi.fn(),
|
|
2115
|
-
count: vi.fn(),
|
|
2116
|
-
}
|
|
2117
|
-
mockPrisma.student = {
|
|
2118
|
-
findFirst: vi.fn(),
|
|
2119
|
-
findMany: vi.fn(),
|
|
2120
|
-
findUnique: vi.fn(),
|
|
2121
|
-
create: vi.fn(),
|
|
2122
|
-
update: vi.fn(),
|
|
2123
|
-
delete: vi.fn(),
|
|
2124
|
-
count: vi.fn(),
|
|
2125
|
-
}
|
|
2126
|
-
|
|
2127
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
2128
|
-
|
|
2129
|
-
await expect(
|
|
2130
|
-
context.db.student.create({
|
|
2131
|
-
data: {
|
|
2132
|
-
name: 'Kid',
|
|
2133
|
-
account: {
|
|
2134
|
-
connectOrCreate: {
|
|
2135
|
-
where: { id: 'acc-1' },
|
|
2136
|
-
create: { name: 'New Account' },
|
|
2137
|
-
},
|
|
2138
|
-
},
|
|
2139
|
-
},
|
|
2140
|
-
}),
|
|
2141
|
-
).rejects.toThrow(OWNING_FIELD_DENIES)
|
|
2142
|
-
|
|
2143
|
-
expect(mockPrisma.student.create).not.toHaveBeenCalled()
|
|
2144
|
-
// Denied owning field short-circuits before the target reachability re-check.
|
|
2145
|
-
expect(mockPrisma.account.findFirst).not.toHaveBeenCalled()
|
|
2146
|
-
})
|
|
2147
|
-
|
|
2148
|
-
it("connectOrCreate's connect branch succeeds when the owning field access permits a reachable existing row", async () => {
|
|
2149
|
-
const testConfig = config({
|
|
2150
|
-
db: {
|
|
2151
|
-
provider: 'postgresql',
|
|
2152
|
-
url: 'postgresql://localhost:5432/test',
|
|
2153
|
-
},
|
|
2154
|
-
lists: {
|
|
2155
|
-
Account: list({
|
|
2156
|
-
fields: {
|
|
2157
|
-
name: text(),
|
|
2158
|
-
},
|
|
2159
|
-
access: {
|
|
2160
|
-
operation: {
|
|
2161
|
-
query: () => true,
|
|
2162
|
-
create: () => true,
|
|
2163
|
-
update: () => true,
|
|
2164
|
-
},
|
|
2165
|
-
},
|
|
2166
|
-
}),
|
|
2167
|
-
Student: list({
|
|
2168
|
-
fields: {
|
|
2169
|
-
name: text(),
|
|
2170
|
-
account: relationship({
|
|
2171
|
-
ref: 'Account',
|
|
2172
|
-
access: {
|
|
2173
|
-
create: () => true, // owning field permits
|
|
2174
|
-
},
|
|
2175
|
-
}),
|
|
2176
|
-
},
|
|
2177
|
-
access: {
|
|
2178
|
-
operation: {
|
|
2179
|
-
query: () => true,
|
|
2180
|
-
create: () => true,
|
|
2181
|
-
},
|
|
2182
|
-
},
|
|
2183
|
-
}),
|
|
2184
|
-
},
|
|
2185
|
-
})
|
|
2186
|
-
|
|
2187
|
-
mockPrisma.account = {
|
|
2188
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
2189
|
-
findFirst: vi.fn().mockResolvedValue({ id: 'acc-1', name: 'My Account' }),
|
|
2190
|
-
findMany: vi.fn(),
|
|
2191
|
-
create: vi.fn(),
|
|
2192
|
-
update: vi.fn(),
|
|
2193
|
-
delete: vi.fn(),
|
|
2194
|
-
count: vi.fn(),
|
|
2195
|
-
}
|
|
2196
|
-
mockPrisma.student = {
|
|
2197
|
-
findFirst: vi.fn(),
|
|
2198
|
-
findMany: vi.fn(),
|
|
2199
|
-
findUnique: vi.fn(),
|
|
2200
|
-
create: vi.fn().mockResolvedValue({ id: 's1', name: 'Kid', accountId: 'acc-1' }),
|
|
2201
|
-
update: vi.fn(),
|
|
2202
|
-
delete: vi.fn(),
|
|
2203
|
-
count: vi.fn(),
|
|
2204
|
-
}
|
|
2205
|
-
|
|
2206
|
-
const context = getContext(await testConfig, mockPrisma, { userId: 'user-1' })
|
|
2207
|
-
|
|
2208
|
-
const result = await context.db.student.create({
|
|
2209
|
-
data: {
|
|
2210
|
-
name: 'Kid',
|
|
2211
|
-
account: {
|
|
2212
|
-
connectOrCreate: {
|
|
2213
|
-
where: { id: 'acc-1' },
|
|
2214
|
-
create: { name: 'New Account' },
|
|
2215
|
-
},
|
|
2216
|
-
},
|
|
2217
|
-
},
|
|
2218
|
-
})
|
|
2219
|
-
|
|
2220
|
-
expect(result).toBeDefined()
|
|
2221
|
-
expect(mockPrisma.student.create).toHaveBeenCalled()
|
|
2222
|
-
})
|
|
2223
|
-
|
|
2224
|
-
// #588 finding — the connect-site owning-field gate must receive the SAME
|
|
2225
|
-
// `item`/`inputData` the canonical Phase-5 `filterWritableFields` call passes,
|
|
2226
|
-
// so a field-access rule that depends on `item`/`inputData` cannot be ALLOWED
|
|
2227
|
-
// by Phase 5 yet DENIED at the connect site (a spurious denial). With those
|
|
2228
|
-
// values threaded, an item-/inputData-dependent rule that resolves to ALLOW at
|
|
2229
|
-
// Phase 5 also resolves to ALLOW at the connect site, so the legitimate connect
|
|
2230
|
-
// succeeds. (#568's Phase-5 gate is the first line of defense and evaluates the
|
|
2231
|
-
// identical rule with the identical values; this test pins that the connect-site
|
|
2232
|
-
// gate does not diverge from it.)
|
|
2233
|
-
it('does not spuriously deny: owning-field access depending on item/inputData allows the connect (enclosing UPDATE)', async () => {
|
|
2234
|
-
const owningUpdate = vi.fn(
|
|
2235
|
-
({ item, inputData }: { item?: { status?: string }; inputData?: { title?: string } }) =>
|
|
2236
|
-
// Depends on BOTH the existing row (`item`) and the write payload
|
|
2237
|
-
// (`inputData`). If the connect-site gate were called without these, it
|
|
2238
|
-
// would throw on `item.status`/`inputData.title` and the legitimate
|
|
2239
|
-
// connect would fail spuriously.
|
|
2240
|
-
item?.status === 'draft' && inputData?.title === 'Updated',
|
|
2241
|
-
)
|
|
2242
|
-
|
|
2243
|
-
const testConfig = config({
|
|
2244
|
-
db: {
|
|
2245
|
-
provider: 'postgresql',
|
|
2246
|
-
url: 'postgresql://localhost:5432/test',
|
|
2247
|
-
},
|
|
2248
|
-
lists: {
|
|
2249
|
-
User: list({
|
|
2250
|
-
fields: {
|
|
2251
|
-
name: text(),
|
|
2252
|
-
},
|
|
2253
|
-
access: {
|
|
2254
|
-
operation: {
|
|
2255
|
-
query: () => true,
|
|
2256
|
-
update: () => true,
|
|
2257
|
-
},
|
|
2258
|
-
},
|
|
2259
|
-
}),
|
|
2260
|
-
Post: list({
|
|
2261
|
-
fields: {
|
|
2262
|
-
title: text(),
|
|
2263
|
-
author: relationship({
|
|
2264
|
-
ref: 'User.posts',
|
|
2265
|
-
access: {
|
|
2266
|
-
// Item-/inputData-dependent: allowed only when editing a draft
|
|
2267
|
-
// and setting title to 'Updated'.
|
|
2268
|
-
update: owningUpdate,
|
|
2269
|
-
},
|
|
2270
|
-
}),
|
|
2271
|
-
},
|
|
2272
|
-
access: {
|
|
2273
|
-
operation: {
|
|
2274
|
-
query: () => true,
|
|
2275
|
-
update: () => true,
|
|
2276
|
-
},
|
|
2277
|
-
},
|
|
2278
|
-
}),
|
|
2279
|
-
},
|
|
2280
|
-
})
|
|
2281
|
-
|
|
2282
|
-
// Existing row is a draft → the item-dependent rule allows.
|
|
2283
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
2284
|
-
id: '1',
|
|
2285
|
-
title: 'Original Title',
|
|
2286
|
-
status: 'draft',
|
|
2287
|
-
})
|
|
2288
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: '2', name: 'John Doe' })
|
|
2289
|
-
mockPrisma.post.update.mockResolvedValue({ id: '1', title: 'Updated', authorId: '2' })
|
|
2290
|
-
|
|
2291
|
-
const context = getContext(await testConfig, mockPrisma, { userId: '1' })
|
|
2292
|
-
|
|
2293
|
-
const result = await context.db.post.update({
|
|
2294
|
-
where: { id: '1' },
|
|
2295
|
-
data: {
|
|
2296
|
-
title: 'Updated',
|
|
2297
|
-
author: { connect: { id: '2' } },
|
|
2298
|
-
},
|
|
2299
|
-
})
|
|
2300
|
-
|
|
2301
|
-
// No spurious denial: the connect succeeded.
|
|
2302
|
-
expect(result).toBeDefined()
|
|
2303
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
2304
|
-
// Owning field allowed → fall through to the #578 target existence check.
|
|
2305
|
-
expect(mockPrisma.user.findUnique).toHaveBeenCalledWith({ where: { id: '2' } })
|
|
2306
|
-
|
|
2307
|
-
// The gate was evaluated with the enclosing write's `item` (the draft
|
|
2308
|
-
// originalItem) and `inputData` (the update payload) — the same values
|
|
2309
|
-
// Phase-5 uses — proving the two evaluations cannot diverge.
|
|
2310
|
-
const sawItemAndInput = owningUpdate.mock.calls.some(
|
|
2311
|
-
([arg]) => arg?.item?.status === 'draft' && arg?.inputData?.title === 'Updated',
|
|
2312
|
-
)
|
|
2313
|
-
expect(sawItemAndInput).toBe(true)
|
|
2314
|
-
})
|
|
2315
|
-
})
|
|
2316
|
-
|
|
2317
|
-
// Focused unit test of the connect-site gate's evaluator (#588 finding).
|
|
2318
|
-
//
|
|
2319
|
-
// The connect-site gate calls the SHARED `checkFieldAccess` evaluator with the
|
|
2320
|
-
// enclosing write's `item`/`inputData`. #568 (Phase-5 `filterWritableFields`) is
|
|
2321
|
-
// the first line of defense and calls the same evaluator with the same values,
|
|
2322
|
-
// so an end-to-end sole-connect-site DENY is not separately constructible. This
|
|
2323
|
-
// test therefore pins the evaluator directly: with `item`/`inputData` provided
|
|
2324
|
-
// it honours an item-/inputData-dependent rule (DENY and ALLOW branches), which
|
|
2325
|
-
// is exactly the contract the connect-site gate now relies on.
|
|
2326
|
-
describe('Connect-site field gate evaluator honours item/inputData (#588)', () => {
|
|
2327
|
-
it('DENIES when the item-/inputData-dependent rule resolves false', async () => {
|
|
2328
|
-
const access = {
|
|
2329
|
-
update: ({
|
|
2330
|
-
item,
|
|
2331
|
-
inputData,
|
|
2332
|
-
}: {
|
|
2333
|
-
item?: { status?: string }
|
|
2334
|
-
inputData?: { title?: string }
|
|
2335
|
-
}) => item?.status === 'draft' && inputData?.title === 'Updated',
|
|
2336
|
-
}
|
|
2337
|
-
const ctx = getContext(
|
|
2338
|
-
await config({
|
|
2339
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
2340
|
-
lists: { User: list({ fields: { name: text() } }) },
|
|
2341
|
-
}),
|
|
2342
|
-
mockPrisma,
|
|
2343
|
-
{ userId: '1' },
|
|
2344
|
-
)
|
|
2345
|
-
|
|
2346
|
-
const allowed = await checkFieldAccess(access, 'update', {
|
|
2347
|
-
session: ctx.session,
|
|
2348
|
-
item: { status: 'published' }, // not a draft → rule is false
|
|
2349
|
-
inputData: { title: 'Updated' },
|
|
2350
|
-
context: ctx,
|
|
2351
|
-
})
|
|
2352
|
-
expect(allowed).toBe(false)
|
|
2353
|
-
})
|
|
2354
|
-
|
|
2355
|
-
it('ALLOWS when the item-/inputData-dependent rule resolves true', async () => {
|
|
2356
|
-
const access = {
|
|
2357
|
-
update: ({
|
|
2358
|
-
item,
|
|
2359
|
-
inputData,
|
|
2360
|
-
}: {
|
|
2361
|
-
item?: { status?: string }
|
|
2362
|
-
inputData?: { title?: string }
|
|
2363
|
-
}) => item?.status === 'draft' && inputData?.title === 'Updated',
|
|
2364
|
-
}
|
|
2365
|
-
const ctx = getContext(
|
|
2366
|
-
await config({
|
|
2367
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
2368
|
-
lists: { User: list({ fields: { name: text() } }) },
|
|
2369
|
-
}),
|
|
2370
|
-
mockPrisma,
|
|
2371
|
-
{ userId: '1' },
|
|
2372
|
-
)
|
|
2373
|
-
|
|
2374
|
-
const allowed = await checkFieldAccess(access, 'update', {
|
|
2375
|
-
session: ctx.session,
|
|
2376
|
-
item: { status: 'draft' },
|
|
2377
|
-
inputData: { title: 'Updated' },
|
|
2378
|
-
context: ctx,
|
|
2379
|
-
})
|
|
2380
|
-
expect(allowed).toBe(true)
|
|
2381
|
-
})
|
|
2382
|
-
})
|
|
2383
|
-
|
|
2384
|
-
describe('Async resolveOutput Hooks', () => {
|
|
2385
|
-
it('should await async resolveOutput hooks for regular fields', async () => {
|
|
2386
|
-
const asyncResolveOutputHook = vi.fn(async ({ value }) => {
|
|
2387
|
-
// Simulate async operation (e.g., external API call, database lookup)
|
|
2388
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
2389
|
-
return `async:${value}`
|
|
2390
|
-
})
|
|
2391
|
-
|
|
2392
|
-
const testConfig = config({
|
|
2393
|
-
db: {
|
|
2394
|
-
provider: 'postgresql',
|
|
2395
|
-
url: 'postgresql://localhost:5432/test',
|
|
2396
|
-
},
|
|
2397
|
-
lists: {
|
|
2398
|
-
Post: list({
|
|
2399
|
-
fields: {
|
|
2400
|
-
title: text({
|
|
2401
|
-
hooks: {
|
|
2402
|
-
resolveOutput: asyncResolveOutputHook,
|
|
2403
|
-
},
|
|
2404
|
-
}),
|
|
2405
|
-
},
|
|
2406
|
-
access: {
|
|
2407
|
-
operation: {
|
|
2408
|
-
query: () => true,
|
|
2409
|
-
},
|
|
2410
|
-
},
|
|
2411
|
-
}),
|
|
2412
|
-
},
|
|
2413
|
-
})
|
|
2414
|
-
|
|
2415
|
-
mockPrisma.post.findMany.mockResolvedValue([
|
|
2416
|
-
{ id: '1', title: 'Hello World', createdAt: new Date(), updatedAt: new Date() },
|
|
2417
|
-
])
|
|
2418
|
-
|
|
2419
|
-
const context = getContext(testConfig, mockPrisma, null)
|
|
2420
|
-
const results = await context.db.post.findMany()
|
|
2421
|
-
|
|
2422
|
-
// The resolved value should be the async result, not a Promise object
|
|
2423
|
-
expect(results[0].title).toBe('async:Hello World')
|
|
2424
|
-
expect(results[0].title).not.toBeInstanceOf(Promise)
|
|
2425
|
-
expect(asyncResolveOutputHook).toHaveBeenCalled()
|
|
2426
|
-
})
|
|
2427
|
-
|
|
2428
|
-
it('should await async resolveOutput hooks for virtual fields', async () => {
|
|
2429
|
-
const asyncVirtualHook = vi.fn(async ({ item }) => {
|
|
2430
|
-
// Simulate async operation (e.g., compute from related data)
|
|
2431
|
-
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
2432
|
-
return `computed:${item.firstName}-${item.lastName}`
|
|
2433
|
-
})
|
|
2434
|
-
|
|
2435
|
-
const testConfig = config({
|
|
2436
|
-
db: {
|
|
2437
|
-
provider: 'postgresql',
|
|
2438
|
-
url: 'postgresql://localhost:5432/test',
|
|
2439
|
-
},
|
|
2440
|
-
lists: {
|
|
2441
|
-
User: list({
|
|
2442
|
-
fields: {
|
|
2443
|
-
firstName: text(),
|
|
2444
|
-
lastName: text(),
|
|
2445
|
-
fullName: {
|
|
2446
|
-
type: 'virtual',
|
|
2447
|
-
virtual: true,
|
|
2448
|
-
outputType: 'string',
|
|
2449
|
-
hooks: {
|
|
2450
|
-
resolveOutput: asyncVirtualHook,
|
|
2451
|
-
},
|
|
2452
|
-
},
|
|
2453
|
-
},
|
|
2454
|
-
access: {
|
|
2455
|
-
operation: {
|
|
2456
|
-
query: () => true,
|
|
2457
|
-
},
|
|
2458
|
-
},
|
|
2459
|
-
}),
|
|
2460
|
-
},
|
|
2461
|
-
})
|
|
2462
|
-
|
|
2463
|
-
mockPrisma.user.findMany.mockResolvedValue([
|
|
2464
|
-
{
|
|
2465
|
-
id: '1',
|
|
2466
|
-
firstName: 'John',
|
|
2467
|
-
lastName: 'Doe',
|
|
2468
|
-
createdAt: new Date(),
|
|
2469
|
-
updatedAt: new Date(),
|
|
2470
|
-
},
|
|
2471
|
-
])
|
|
2472
|
-
|
|
2473
|
-
const context = getContext(testConfig, mockPrisma, null)
|
|
2474
|
-
const results = await context.db.user.findMany()
|
|
2475
|
-
|
|
2476
|
-
// The resolved value should be the async result, not a Promise object
|
|
2477
|
-
expect(results[0].fullName).toBe('computed:John-Doe')
|
|
2478
|
-
expect(results[0].fullName).not.toBeInstanceOf(Promise)
|
|
2479
|
-
expect(asyncVirtualHook).toHaveBeenCalled()
|
|
2480
|
-
})
|
|
2481
|
-
|
|
2482
|
-
it('should still work with sync resolveOutput hooks (backwards compatibility)', async () => {
|
|
2483
|
-
const syncResolveOutputHook = vi.fn(({ value }) => {
|
|
2484
|
-
// Synchronous transformation
|
|
2485
|
-
return value.toUpperCase()
|
|
2486
|
-
})
|
|
2487
|
-
|
|
2488
|
-
const testConfig = config({
|
|
2489
|
-
db: {
|
|
2490
|
-
provider: 'postgresql',
|
|
2491
|
-
url: 'postgresql://localhost:5432/test',
|
|
2492
|
-
},
|
|
2493
|
-
lists: {
|
|
2494
|
-
Post: list({
|
|
2495
|
-
fields: {
|
|
2496
|
-
title: text({
|
|
2497
|
-
hooks: {
|
|
2498
|
-
resolveOutput: syncResolveOutputHook,
|
|
2499
|
-
},
|
|
2500
|
-
}),
|
|
2501
|
-
},
|
|
2502
|
-
access: {
|
|
2503
|
-
operation: {
|
|
2504
|
-
query: () => true,
|
|
2505
|
-
},
|
|
2506
|
-
},
|
|
2507
|
-
}),
|
|
2508
|
-
},
|
|
2509
|
-
})
|
|
2510
|
-
|
|
2511
|
-
mockPrisma.post.findMany.mockResolvedValue([
|
|
2512
|
-
{ id: '1', title: 'hello world', createdAt: new Date(), updatedAt: new Date() },
|
|
2513
|
-
])
|
|
2514
|
-
|
|
2515
|
-
const context = getContext(testConfig, mockPrisma, null)
|
|
2516
|
-
const results = await context.db.post.findMany()
|
|
2517
|
-
|
|
2518
|
-
// Sync hooks should still work as before
|
|
2519
|
-
expect(results[0].title).toBe('HELLO WORLD')
|
|
2520
|
-
expect(syncResolveOutputHook).toHaveBeenCalled()
|
|
2521
|
-
})
|
|
2522
|
-
|
|
2523
|
-
it('should pass context to async resolveOutput hooks', async () => {
|
|
2524
|
-
const asyncHookWithContext = vi.fn(async ({ value, context }) => {
|
|
2525
|
-
// Verify context is passed correctly
|
|
2526
|
-
expect(context).toBeDefined()
|
|
2527
|
-
expect(context.session).toEqual({ userId: 'user-123' })
|
|
2528
|
-
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
2529
|
-
return `${value}:${context.session?.userId}`
|
|
2530
|
-
})
|
|
2531
|
-
|
|
2532
|
-
const testConfig = config({
|
|
2533
|
-
db: {
|
|
2534
|
-
provider: 'postgresql',
|
|
2535
|
-
url: 'postgresql://localhost:5432/test',
|
|
2536
|
-
},
|
|
2537
|
-
lists: {
|
|
2538
|
-
Post: list({
|
|
2539
|
-
fields: {
|
|
2540
|
-
title: text({
|
|
2541
|
-
hooks: {
|
|
2542
|
-
resolveOutput: asyncHookWithContext,
|
|
2543
|
-
},
|
|
2544
|
-
}),
|
|
2545
|
-
},
|
|
2546
|
-
access: {
|
|
2547
|
-
operation: {
|
|
2548
|
-
query: () => true,
|
|
2549
|
-
},
|
|
2550
|
-
},
|
|
2551
|
-
}),
|
|
2552
|
-
},
|
|
2553
|
-
})
|
|
2554
|
-
|
|
2555
|
-
mockPrisma.post.findMany.mockResolvedValue([
|
|
2556
|
-
{ id: '1', title: 'Test', createdAt: new Date(), updatedAt: new Date() },
|
|
2557
|
-
])
|
|
2558
|
-
|
|
2559
|
-
const context = getContext(testConfig, mockPrisma, { userId: 'user-123' })
|
|
2560
|
-
const results = await context.db.post.findMany()
|
|
2561
|
-
|
|
2562
|
-
expect(results[0].title).toBe('Test:user-123')
|
|
2563
|
-
expect(asyncHookWithContext).toHaveBeenCalled()
|
|
2564
|
-
})
|
|
2565
|
-
})
|
|
2566
|
-
})
|