@opensaas/stack-core 0.43.0 → 0.44.0-alpha.2
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 +2753 -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 +1035 -611
- 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 +629 -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 +294 -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 +153 -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 +462 -678
- 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 +237 -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
package/tests/context.test.ts
DELETED
|
@@ -1,3146 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import { getContext } from '../src/context/index.js'
|
|
3
|
-
import { defineFragment } from '../src/query/index.js'
|
|
4
|
-
import { virtual } from '../src/fields/index.js'
|
|
5
|
-
import {
|
|
6
|
-
AccessScopeDepthExceededError,
|
|
7
|
-
InvalidFieldAccessResultError,
|
|
8
|
-
} from '../src/access/index.js'
|
|
9
|
-
import { READ_INCLUDE_MAX_DEPTH } from '../src/access/depth-limits.js'
|
|
10
|
-
import type { OpenSaasConfig } from '../src/config/types.js'
|
|
11
|
-
|
|
12
|
-
describe('getContext', () => {
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
let mockPrisma: any
|
|
15
|
-
let config: OpenSaasConfig
|
|
16
|
-
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
// Mock Prisma client with all methods needed by context
|
|
19
|
-
mockPrisma = {
|
|
20
|
-
user: {
|
|
21
|
-
findFirst: vi.fn(),
|
|
22
|
-
findUnique: vi.fn(),
|
|
23
|
-
findMany: vi.fn(),
|
|
24
|
-
create: vi.fn(),
|
|
25
|
-
update: vi.fn(),
|
|
26
|
-
delete: vi.fn(),
|
|
27
|
-
count: vi.fn(),
|
|
28
|
-
},
|
|
29
|
-
post: {
|
|
30
|
-
findFirst: vi.fn(),
|
|
31
|
-
findUnique: vi.fn(),
|
|
32
|
-
findMany: vi.fn(),
|
|
33
|
-
create: vi.fn(),
|
|
34
|
-
update: vi.fn(),
|
|
35
|
-
delete: vi.fn(),
|
|
36
|
-
count: vi.fn(),
|
|
37
|
-
},
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Sample config with open access (no access control to simplify tests)
|
|
41
|
-
config = {
|
|
42
|
-
db: {
|
|
43
|
-
provider: 'postgresql',
|
|
44
|
-
url: 'postgresql://localhost:5432/test',
|
|
45
|
-
},
|
|
46
|
-
lists: {
|
|
47
|
-
User: {
|
|
48
|
-
fields: {
|
|
49
|
-
name: { type: 'text' },
|
|
50
|
-
email: { type: 'text', isIndexed: 'unique' },
|
|
51
|
-
posts: { type: 'relationship', ref: 'Post.author', many: true },
|
|
52
|
-
},
|
|
53
|
-
access: {
|
|
54
|
-
operation: {
|
|
55
|
-
query: () => true,
|
|
56
|
-
create: () => true,
|
|
57
|
-
update: () => true,
|
|
58
|
-
delete: () => true,
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
Post: {
|
|
63
|
-
fields: {
|
|
64
|
-
title: { type: 'text' },
|
|
65
|
-
content: { type: 'text' },
|
|
66
|
-
author: { type: 'relationship', ref: 'User.posts' },
|
|
67
|
-
},
|
|
68
|
-
access: {
|
|
69
|
-
operation: {
|
|
70
|
-
query: () => true,
|
|
71
|
-
create: () => true,
|
|
72
|
-
update: () => true,
|
|
73
|
-
delete: () => true,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it('should create context with lowercase db keys', async () => {
|
|
82
|
-
const context = await getContext(config, mockPrisma, null)
|
|
83
|
-
|
|
84
|
-
expect(context.db).toBeDefined()
|
|
85
|
-
expect(context.db.user).toBeDefined()
|
|
86
|
-
expect(context.db.post).toBeDefined()
|
|
87
|
-
expect(context.session).toBeNull()
|
|
88
|
-
expect(context.prisma).toBe(mockPrisma)
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('should include session when provided', async () => {
|
|
92
|
-
const session = { userId: '123', role: 'admin' }
|
|
93
|
-
const context = await getContext(config, mockPrisma, session)
|
|
94
|
-
|
|
95
|
-
expect(context.session).toEqual(session)
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
describe('serverAction', () => {
|
|
99
|
-
it('should create an item', async () => {
|
|
100
|
-
const mockCreatedUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
101
|
-
mockPrisma.user.create.mockResolvedValue(mockCreatedUser)
|
|
102
|
-
|
|
103
|
-
const context = await getContext(config, mockPrisma, null)
|
|
104
|
-
const result = await context.serverAction({
|
|
105
|
-
listKey: 'User',
|
|
106
|
-
action: 'create',
|
|
107
|
-
data: { name: 'John', email: 'john@example.com' },
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
expect(mockPrisma.user.create).toHaveBeenCalledWith({
|
|
111
|
-
data: { name: 'John', email: 'john@example.com' },
|
|
112
|
-
})
|
|
113
|
-
expect(result).toEqual({ success: true, data: mockCreatedUser })
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it('should update an item', async () => {
|
|
117
|
-
const existingUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
118
|
-
const mockUpdatedUser = { id: '1', name: 'John Updated', email: 'john@example.com' }
|
|
119
|
-
// Update operation first fetches the existing item
|
|
120
|
-
mockPrisma.user.findUnique.mockResolvedValue(existingUser)
|
|
121
|
-
mockPrisma.user.update.mockResolvedValue(mockUpdatedUser)
|
|
122
|
-
|
|
123
|
-
const context = await getContext(config, mockPrisma, null)
|
|
124
|
-
const result = await context.serverAction({
|
|
125
|
-
listKey: 'User',
|
|
126
|
-
action: 'update',
|
|
127
|
-
id: '1',
|
|
128
|
-
data: { name: 'John Updated' },
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
expect(mockPrisma.user.findUnique).toHaveBeenCalled()
|
|
132
|
-
expect(mockPrisma.user.update).toHaveBeenCalled()
|
|
133
|
-
expect(result).toEqual({ success: true, data: mockUpdatedUser })
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
it('should delete an item', async () => {
|
|
137
|
-
const mockDeletedUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
138
|
-
// Delete operation first fetches the existing item
|
|
139
|
-
mockPrisma.user.findUnique.mockResolvedValue(mockDeletedUser)
|
|
140
|
-
mockPrisma.user.delete.mockResolvedValue(mockDeletedUser)
|
|
141
|
-
|
|
142
|
-
const context = await getContext(config, mockPrisma, null)
|
|
143
|
-
const result = await context.serverAction({
|
|
144
|
-
listKey: 'User',
|
|
145
|
-
action: 'delete',
|
|
146
|
-
id: '1',
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
expect(mockPrisma.user.findUnique).toHaveBeenCalled()
|
|
150
|
-
expect(mockPrisma.user.delete).toHaveBeenCalled()
|
|
151
|
-
expect(result).toEqual({ success: true, data: mockDeletedUser })
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
it('should convert listKey to lowercase for db operations', async () => {
|
|
155
|
-
const mockCreatedPost = { id: '1', title: 'Test Post' }
|
|
156
|
-
mockPrisma.post.create.mockResolvedValue(mockCreatedPost)
|
|
157
|
-
|
|
158
|
-
const context = await getContext(config, mockPrisma, null)
|
|
159
|
-
const result = await context.serverAction({
|
|
160
|
-
listKey: 'Post',
|
|
161
|
-
action: 'create',
|
|
162
|
-
data: { title: 'Test Post' },
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
expect(mockPrisma.post.create).toHaveBeenCalled()
|
|
166
|
-
expect(result).toEqual({ success: true, data: mockCreatedPost })
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
it('should return error for unknown action', async () => {
|
|
170
|
-
const context = await getContext(config, mockPrisma, null)
|
|
171
|
-
const result = await context.serverAction({
|
|
172
|
-
listKey: 'User',
|
|
173
|
-
action: 'unknown' as unknown as 'create',
|
|
174
|
-
data: {},
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
expect(result).toEqual({ success: false, error: 'Access denied or operation failed' })
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
it('should return error for unknown list', async () => {
|
|
181
|
-
const context = await getContext(config, mockPrisma, null)
|
|
182
|
-
const result = await context.serverAction({
|
|
183
|
-
listKey: 'UnknownList',
|
|
184
|
-
action: 'create',
|
|
185
|
-
data: {},
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
expect(result).toEqual({
|
|
189
|
-
success: false,
|
|
190
|
-
error: 'List "UnknownList" not found in configuration',
|
|
191
|
-
})
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
it('should handle database errors', async () => {
|
|
195
|
-
const dbError = new Error('Database connection failed')
|
|
196
|
-
mockPrisma.user.create.mockRejectedValue(dbError)
|
|
197
|
-
|
|
198
|
-
const context = await getContext(config, mockPrisma, null)
|
|
199
|
-
const result = await context.serverAction({
|
|
200
|
-
listKey: 'User',
|
|
201
|
-
action: 'create',
|
|
202
|
-
data: { name: 'John', email: 'john@example.com' },
|
|
203
|
-
})
|
|
204
|
-
|
|
205
|
-
expect(result).toMatchObject({
|
|
206
|
-
success: false,
|
|
207
|
-
error: 'Database connection failed',
|
|
208
|
-
})
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
describe('removeRelated (relationship-table row removal)', () => {
|
|
212
|
-
it('unlinks a to-one back-reference via an update on the related list', async () => {
|
|
213
|
-
const existing = { id: 'p1', title: 'T', content: 'c', authorId: 'u1' }
|
|
214
|
-
mockPrisma.post.findUnique.mockResolvedValue(existing)
|
|
215
|
-
mockPrisma.post.update.mockResolvedValue({ id: 'p1', title: 'T', authorId: null })
|
|
216
|
-
|
|
217
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
218
|
-
const result = await context.serverAction({
|
|
219
|
-
listKey: 'Post',
|
|
220
|
-
action: 'removeRelated',
|
|
221
|
-
mode: 'disconnect',
|
|
222
|
-
id: 'p1',
|
|
223
|
-
field: 'author',
|
|
224
|
-
parentId: 'u1',
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
// Disconnect is an UPDATE on the related list nulling the back-reference
|
|
228
|
-
// (a to-one back-ref disconnects with `true`), never a delete. The
|
|
229
|
-
// distinct `{ removed }` shape avoids a redirect-on-success wrapper.
|
|
230
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
231
|
-
const updateArg = mockPrisma.post.update.mock.calls[0][0]
|
|
232
|
-
expect(updateArg.where).toEqual({ id: 'p1' })
|
|
233
|
-
expect(updateArg.data.author).toEqual({ disconnect: true })
|
|
234
|
-
expect(mockPrisma.post.delete).not.toHaveBeenCalled()
|
|
235
|
-
expect(result).toEqual({ removed: true })
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
it('deletes the related row when mode is delete', async () => {
|
|
239
|
-
mockPrisma.post.findUnique.mockResolvedValue({ id: 'p1', title: 'T', authorId: 'u1' })
|
|
240
|
-
mockPrisma.post.delete.mockResolvedValue({ id: 'p1', title: 'T' })
|
|
241
|
-
|
|
242
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
243
|
-
const result = await context.serverAction({
|
|
244
|
-
listKey: 'Post',
|
|
245
|
-
action: 'removeRelated',
|
|
246
|
-
mode: 'delete',
|
|
247
|
-
id: 'p1',
|
|
248
|
-
})
|
|
249
|
-
|
|
250
|
-
expect(mockPrisma.post.delete).toHaveBeenCalled()
|
|
251
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
252
|
-
expect(result).toEqual({ removed: true })
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
it('disconnects a to-many back-reference by parent id (many-to-many)', async () => {
|
|
256
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
257
|
-
const m2mPrisma: any = {
|
|
258
|
-
lesson: {
|
|
259
|
-
findUnique: vi.fn().mockResolvedValue({ id: 'l1', title: 'L' }),
|
|
260
|
-
update: vi.fn().mockResolvedValue({ id: 'l1', title: 'L' }),
|
|
261
|
-
delete: vi.fn(),
|
|
262
|
-
},
|
|
263
|
-
// The disconnect target-row form now verifies the target is reachable
|
|
264
|
-
// under the caller's own query access (#1384) before disconnecting it.
|
|
265
|
-
teacher: {
|
|
266
|
-
findUnique: vi.fn().mockResolvedValue({ id: 't1', name: 'T' }),
|
|
267
|
-
},
|
|
268
|
-
}
|
|
269
|
-
const m2mConfig: OpenSaasConfig = {
|
|
270
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
271
|
-
lists: {
|
|
272
|
-
Lesson: {
|
|
273
|
-
fields: {
|
|
274
|
-
title: { type: 'text' },
|
|
275
|
-
teachers: { type: 'relationship', ref: 'Teacher.lessons', many: true },
|
|
276
|
-
},
|
|
277
|
-
access: {
|
|
278
|
-
operation: {
|
|
279
|
-
query: () => true,
|
|
280
|
-
create: () => true,
|
|
281
|
-
update: () => true,
|
|
282
|
-
delete: () => true,
|
|
283
|
-
},
|
|
284
|
-
},
|
|
285
|
-
},
|
|
286
|
-
Teacher: {
|
|
287
|
-
fields: {
|
|
288
|
-
name: { type: 'text' },
|
|
289
|
-
lessons: { type: 'relationship', ref: 'Lesson.teachers', many: true },
|
|
290
|
-
},
|
|
291
|
-
access: {
|
|
292
|
-
operation: {
|
|
293
|
-
query: () => true,
|
|
294
|
-
create: () => true,
|
|
295
|
-
update: () => true,
|
|
296
|
-
delete: () => true,
|
|
297
|
-
},
|
|
298
|
-
},
|
|
299
|
-
},
|
|
300
|
-
},
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
const context = await getContext(m2mConfig, m2mPrisma, { userId: 'u1' })
|
|
304
|
-
await context.serverAction({
|
|
305
|
-
listKey: 'Lesson',
|
|
306
|
-
action: 'removeRelated',
|
|
307
|
-
mode: 'disconnect',
|
|
308
|
-
id: 'l1',
|
|
309
|
-
field: 'teachers',
|
|
310
|
-
parentId: 't1',
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
const updateArg = m2mPrisma.lesson.update.mock.calls[0][0]
|
|
314
|
-
// A to-many back-reference disconnects the specific parent by id.
|
|
315
|
-
expect(updateArg.data.teachers).toEqual({ disconnect: { id: 't1' } })
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
it('returns a generic error (Silent failure) when the update is access-denied', async () => {
|
|
319
|
-
const deniedConfig: OpenSaasConfig = {
|
|
320
|
-
...config,
|
|
321
|
-
lists: {
|
|
322
|
-
...config.lists,
|
|
323
|
-
Post: {
|
|
324
|
-
...config.lists.Post,
|
|
325
|
-
access: {
|
|
326
|
-
operation: {
|
|
327
|
-
query: () => true,
|
|
328
|
-
create: () => true,
|
|
329
|
-
update: () => false, // no update access → disconnect denied
|
|
330
|
-
delete: () => true,
|
|
331
|
-
},
|
|
332
|
-
},
|
|
333
|
-
},
|
|
334
|
-
},
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const context = await getContext(deniedConfig, mockPrisma, { userId: 'u1' })
|
|
338
|
-
const result = await context.serverAction({
|
|
339
|
-
listKey: 'Post',
|
|
340
|
-
action: 'removeRelated',
|
|
341
|
-
mode: 'disconnect',
|
|
342
|
-
id: 'p1',
|
|
343
|
-
field: 'author',
|
|
344
|
-
parentId: 'u1',
|
|
345
|
-
})
|
|
346
|
-
|
|
347
|
-
// Denied: reported as not removed with a generic reason (no leak).
|
|
348
|
-
expect(result).toEqual({ removed: false, error: 'Access denied or operation failed' })
|
|
349
|
-
})
|
|
350
|
-
})
|
|
351
|
-
|
|
352
|
-
describe('bulkAction (custom list bulk actions, #736)', () => {
|
|
353
|
-
// A config whose Post list declares a custom bulk action that runs each
|
|
354
|
-
// selected id through the SECURED context (`context.db.post.update`), so
|
|
355
|
-
// access control + hooks apply per id.
|
|
356
|
-
function configWithPublishAction(
|
|
357
|
-
overrides?: Partial<import('../src/config/types.js').BulkAction>,
|
|
358
|
-
): OpenSaasConfig {
|
|
359
|
-
return {
|
|
360
|
-
...config,
|
|
361
|
-
lists: {
|
|
362
|
-
...config.lists,
|
|
363
|
-
Post: {
|
|
364
|
-
...config.lists.Post,
|
|
365
|
-
fields: {
|
|
366
|
-
...config.lists.Post.fields,
|
|
367
|
-
status: { type: 'text' },
|
|
368
|
-
},
|
|
369
|
-
ui: {
|
|
370
|
-
listView: {
|
|
371
|
-
bulkActions: [
|
|
372
|
-
{
|
|
373
|
-
key: 'publish',
|
|
374
|
-
label: 'Publish',
|
|
375
|
-
handler: async ({ ids, context }) => {
|
|
376
|
-
let n = 0
|
|
377
|
-
for (const id of ids) {
|
|
378
|
-
const r = await context.db.post.update({
|
|
379
|
-
where: { id },
|
|
380
|
-
data: { status: 'published' },
|
|
381
|
-
})
|
|
382
|
-
if (r) n++
|
|
383
|
-
}
|
|
384
|
-
return { message: `Published ${n} of ${ids.length}` }
|
|
385
|
-
},
|
|
386
|
-
...overrides,
|
|
387
|
-
},
|
|
388
|
-
],
|
|
389
|
-
},
|
|
390
|
-
},
|
|
391
|
-
},
|
|
392
|
-
},
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
it('runs the handler over the selected ids through the secured context', async () => {
|
|
397
|
-
mockPrisma.post.findUnique.mockImplementation(({ where }: { where: { id: string } }) =>
|
|
398
|
-
Promise.resolve({ id: where.id, title: 'T', content: 'c' }),
|
|
399
|
-
)
|
|
400
|
-
mockPrisma.post.update.mockImplementation(({ where }: { where: { id: string } }) =>
|
|
401
|
-
Promise.resolve({ id: where.id, status: 'published' }),
|
|
402
|
-
)
|
|
403
|
-
|
|
404
|
-
const context = await getContext(configWithPublishAction(), mockPrisma, { userId: 'u1' })
|
|
405
|
-
const result = await context.serverAction({
|
|
406
|
-
listKey: 'Post',
|
|
407
|
-
action: 'bulkAction',
|
|
408
|
-
key: 'publish',
|
|
409
|
-
ids: ['p1', 'p2'],
|
|
410
|
-
})
|
|
411
|
-
|
|
412
|
-
// Each id went through the secured update delegate, not a raw call.
|
|
413
|
-
expect(mockPrisma.post.update).toHaveBeenCalledTimes(2)
|
|
414
|
-
expect(result).toEqual({ bulkAction: true, message: 'Published 2 of 2' })
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
it('absorbs a per-id Silent failure into the handler count (no leak)', async () => {
|
|
418
|
-
// Update access scopes writes to id p1 only → p2 is denied and returns
|
|
419
|
-
// null through the secured delegate. The handler simply does not count
|
|
420
|
-
// it; the outcome never reveals which id was denied.
|
|
421
|
-
const scopedConfig = configWithPublishAction()
|
|
422
|
-
scopedConfig.lists.Post.access = {
|
|
423
|
-
operation: {
|
|
424
|
-
query: () => true,
|
|
425
|
-
create: () => true,
|
|
426
|
-
update: () => ({ id: { equals: 'p1' } }),
|
|
427
|
-
delete: () => true,
|
|
428
|
-
},
|
|
429
|
-
}
|
|
430
|
-
// The access filter merges into the existence pre-check: only p1 is
|
|
431
|
-
// visible, so p2's update is denied (Silent failure → null) before it
|
|
432
|
-
// ever reaches the update delegate.
|
|
433
|
-
mockPrisma.post.findUnique.mockImplementation(({ where }: { where: { id: string } }) =>
|
|
434
|
-
where.id === 'p1'
|
|
435
|
-
? Promise.resolve({ id: 'p1', title: 'T', content: 'c' })
|
|
436
|
-
: Promise.resolve(null),
|
|
437
|
-
)
|
|
438
|
-
// The filter-match re-check (mergeFilters) only ever runs for the row
|
|
439
|
-
// that passed the existence pre-check (p1), so returning it is enough.
|
|
440
|
-
mockPrisma.post.findFirst.mockResolvedValue({ id: 'p1', title: 'T', content: 'c' })
|
|
441
|
-
mockPrisma.post.update.mockResolvedValue({ id: 'p1', status: 'published' })
|
|
442
|
-
|
|
443
|
-
const context = await getContext(scopedConfig, mockPrisma, { userId: 'u1' })
|
|
444
|
-
const result = await context.serverAction({
|
|
445
|
-
listKey: 'Post',
|
|
446
|
-
action: 'bulkAction',
|
|
447
|
-
key: 'publish',
|
|
448
|
-
ids: ['p1', 'p2'],
|
|
449
|
-
})
|
|
450
|
-
|
|
451
|
-
expect(result).toEqual({ bulkAction: true, message: 'Published 1 of 2' })
|
|
452
|
-
})
|
|
453
|
-
|
|
454
|
-
it('returns an error for an unknown action key', async () => {
|
|
455
|
-
const context = await getContext(configWithPublishAction(), mockPrisma, { userId: 'u1' })
|
|
456
|
-
const result = await context.serverAction({
|
|
457
|
-
listKey: 'Post',
|
|
458
|
-
action: 'bulkAction',
|
|
459
|
-
key: 'nope',
|
|
460
|
-
ids: ['p1'],
|
|
461
|
-
})
|
|
462
|
-
|
|
463
|
-
expect(result).toMatchObject({ bulkAction: false })
|
|
464
|
-
})
|
|
465
|
-
|
|
466
|
-
it('re-checks hasAccess server-side so a hidden action cannot be invoked', async () => {
|
|
467
|
-
const context = await getContext(
|
|
468
|
-
configWithPublishAction({ hasAccess: () => false }),
|
|
469
|
-
mockPrisma,
|
|
470
|
-
{ userId: 'u1' },
|
|
471
|
-
)
|
|
472
|
-
const result = await context.serverAction({
|
|
473
|
-
listKey: 'Post',
|
|
474
|
-
action: 'bulkAction',
|
|
475
|
-
key: 'publish',
|
|
476
|
-
ids: ['p1', 'p2'],
|
|
477
|
-
})
|
|
478
|
-
|
|
479
|
-
expect(result).toEqual({ bulkAction: false, error: 'Access denied' })
|
|
480
|
-
// The handler never ran.
|
|
481
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
482
|
-
})
|
|
483
|
-
|
|
484
|
-
it('returns a generic message (not the internal text) and logs when the handler throws a plain Error', async () => {
|
|
485
|
-
// An unexpected, non-Prisma, non-known-error bug inside the handler must
|
|
486
|
-
// not surface its internal message to the client (#761).
|
|
487
|
-
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
488
|
-
try {
|
|
489
|
-
const context = await getContext(
|
|
490
|
-
configWithPublishAction({
|
|
491
|
-
handler: async () => {
|
|
492
|
-
throw new Error('secret internal detail: connection string leaked')
|
|
493
|
-
},
|
|
494
|
-
}),
|
|
495
|
-
mockPrisma,
|
|
496
|
-
{ userId: 'u1' },
|
|
497
|
-
)
|
|
498
|
-
const result = await context.serverAction({
|
|
499
|
-
listKey: 'Post',
|
|
500
|
-
action: 'bulkAction',
|
|
501
|
-
key: 'publish',
|
|
502
|
-
ids: ['p1', 'p2'],
|
|
503
|
-
})
|
|
504
|
-
|
|
505
|
-
// Client sees the generic fallback — never the internal message.
|
|
506
|
-
expect(result).toEqual({ bulkAction: false, error: 'Action failed' })
|
|
507
|
-
if ('error' in result) {
|
|
508
|
-
expect(result.error).not.toContain('secret internal detail')
|
|
509
|
-
}
|
|
510
|
-
// The real error is logged server-side for the operator.
|
|
511
|
-
expect(consoleError).toHaveBeenCalled()
|
|
512
|
-
const loggedArgs = consoleError.mock.calls[0]
|
|
513
|
-
expect(loggedArgs.some((arg) => arg instanceof Error)).toBe(true)
|
|
514
|
-
} finally {
|
|
515
|
-
consoleError.mockRestore()
|
|
516
|
-
}
|
|
517
|
-
})
|
|
518
|
-
})
|
|
519
|
-
|
|
520
|
-
describe('updateRelated (relationship-table inline cell edit)', () => {
|
|
521
|
-
it('updates a single field on the related row through the secured context', async () => {
|
|
522
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
523
|
-
id: 'p1',
|
|
524
|
-
title: 'Old',
|
|
525
|
-
content: 'c',
|
|
526
|
-
authorId: 'u1',
|
|
527
|
-
})
|
|
528
|
-
mockPrisma.post.update.mockResolvedValue({
|
|
529
|
-
id: 'p1',
|
|
530
|
-
title: 'New',
|
|
531
|
-
content: 'c',
|
|
532
|
-
authorId: 'u1',
|
|
533
|
-
})
|
|
534
|
-
|
|
535
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
536
|
-
const result = await context.serverAction({
|
|
537
|
-
listKey: 'Post',
|
|
538
|
-
action: 'updateRelated',
|
|
539
|
-
id: 'p1',
|
|
540
|
-
field: 'title',
|
|
541
|
-
value: 'New',
|
|
542
|
-
})
|
|
543
|
-
|
|
544
|
-
// The edit is an UPDATE on the RELATED list, one field only. The distinct
|
|
545
|
-
// `{ updated }` shape avoids a redirect-on-success wrapper.
|
|
546
|
-
expect(mockPrisma.post.update).toHaveBeenCalled()
|
|
547
|
-
const updateArg = mockPrisma.post.update.mock.calls[0][0]
|
|
548
|
-
expect(updateArg.where).toEqual({ id: 'p1' })
|
|
549
|
-
expect(updateArg.data.title).toBe('New')
|
|
550
|
-
expect(mockPrisma.post.delete).not.toHaveBeenCalled()
|
|
551
|
-
expect(result).toEqual({ updated: true })
|
|
552
|
-
})
|
|
553
|
-
|
|
554
|
-
it('returns a generic error (Silent failure) when the update is access-denied', async () => {
|
|
555
|
-
const deniedConfig: OpenSaasConfig = {
|
|
556
|
-
...config,
|
|
557
|
-
lists: {
|
|
558
|
-
...config.lists,
|
|
559
|
-
Post: {
|
|
560
|
-
...config.lists.Post,
|
|
561
|
-
access: {
|
|
562
|
-
operation: {
|
|
563
|
-
query: () => true,
|
|
564
|
-
create: () => true,
|
|
565
|
-
update: () => false, // no update access → inline edit denied
|
|
566
|
-
delete: () => true,
|
|
567
|
-
},
|
|
568
|
-
},
|
|
569
|
-
},
|
|
570
|
-
},
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
const context = await getContext(deniedConfig, mockPrisma, { userId: 'u1' })
|
|
574
|
-
const result = await context.serverAction({
|
|
575
|
-
listKey: 'Post',
|
|
576
|
-
action: 'updateRelated',
|
|
577
|
-
id: 'p1',
|
|
578
|
-
field: 'title',
|
|
579
|
-
value: 'New',
|
|
580
|
-
})
|
|
581
|
-
|
|
582
|
-
// Denied: reported as not updated with a generic reason (no denied-vs-absent leak).
|
|
583
|
-
expect(result).toEqual({ updated: false, error: 'Access denied or operation failed' })
|
|
584
|
-
})
|
|
585
|
-
|
|
586
|
-
it('surfaces a validation error so the cell can revert with a reason', async () => {
|
|
587
|
-
const validationConfig: OpenSaasConfig = {
|
|
588
|
-
...config,
|
|
589
|
-
lists: {
|
|
590
|
-
...config.lists,
|
|
591
|
-
Post: {
|
|
592
|
-
...config.lists.Post,
|
|
593
|
-
hooks: {
|
|
594
|
-
validateInput: async (args) => {
|
|
595
|
-
if (args.operation === 'delete') return
|
|
596
|
-
const { resolvedData, addValidationError } = args
|
|
597
|
-
if (resolvedData.title === 'spam') {
|
|
598
|
-
addValidationError('Title cannot contain the word "spam"')
|
|
599
|
-
}
|
|
600
|
-
},
|
|
601
|
-
},
|
|
602
|
-
},
|
|
603
|
-
},
|
|
604
|
-
}
|
|
605
|
-
mockPrisma.post.findUnique.mockResolvedValue({
|
|
606
|
-
id: 'p1',
|
|
607
|
-
title: 'Old',
|
|
608
|
-
content: 'c',
|
|
609
|
-
authorId: 'u1',
|
|
610
|
-
})
|
|
611
|
-
|
|
612
|
-
const context = await getContext(validationConfig, mockPrisma, { userId: 'u1' })
|
|
613
|
-
const result = await context.serverAction({
|
|
614
|
-
listKey: 'Post',
|
|
615
|
-
action: 'updateRelated',
|
|
616
|
-
id: 'p1',
|
|
617
|
-
field: 'title',
|
|
618
|
-
value: 'spam',
|
|
619
|
-
})
|
|
620
|
-
|
|
621
|
-
// A validation error becomes { updated: false } with the reason; the write
|
|
622
|
-
// never reaches the database.
|
|
623
|
-
expect(mockPrisma.post.update).not.toHaveBeenCalled()
|
|
624
|
-
expect(result).toMatchObject({ updated: false })
|
|
625
|
-
const failure = result as { updated: boolean; error?: string }
|
|
626
|
-
expect(failure.error).toContain('spam')
|
|
627
|
-
})
|
|
628
|
-
})
|
|
629
|
-
|
|
630
|
-
describe('createRelated (relationship-table pre-linked create)', () => {
|
|
631
|
-
it('creates a row on the related list with the to-one back-reference preset to the parent', async () => {
|
|
632
|
-
const created = { id: 'p1', title: 'New', content: 'c', authorId: 'u1' }
|
|
633
|
-
mockPrisma.post.create.mockResolvedValue(created)
|
|
634
|
-
// The connect target must be reachable (the related list's read access).
|
|
635
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
636
|
-
|
|
637
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
638
|
-
const result = await context.serverAction({
|
|
639
|
-
listKey: 'Post',
|
|
640
|
-
action: 'createRelated',
|
|
641
|
-
data: { title: 'New', content: 'c' },
|
|
642
|
-
field: 'author',
|
|
643
|
-
parentId: 'u1',
|
|
644
|
-
})
|
|
645
|
-
|
|
646
|
-
// The back-reference is set on the SERVER (a to-one back-ref connects a
|
|
647
|
-
// single parent), so the new row links to exactly the parent being edited.
|
|
648
|
-
expect(mockPrisma.post.create).toHaveBeenCalled()
|
|
649
|
-
const createArg = mockPrisma.post.create.mock.calls[0][0]
|
|
650
|
-
expect(createArg.data.title).toBe('New')
|
|
651
|
-
expect(createArg.data.author).toEqual({ connect: { id: 'u1' } })
|
|
652
|
-
// Distinct `{ created }` shape (never a single-op `success`).
|
|
653
|
-
expect(result).toEqual({ created: true, id: 'p1' })
|
|
654
|
-
})
|
|
655
|
-
|
|
656
|
-
it('connects a to-many back-reference by parent id (many-to-many)', async () => {
|
|
657
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
658
|
-
const m2mPrisma: any = {
|
|
659
|
-
lesson: {
|
|
660
|
-
findUnique: vi.fn(),
|
|
661
|
-
update: vi.fn(),
|
|
662
|
-
delete: vi.fn(),
|
|
663
|
-
create: vi.fn().mockResolvedValue({ id: 'l1', title: 'L' }),
|
|
664
|
-
},
|
|
665
|
-
teacher: {
|
|
666
|
-
findUnique: vi.fn().mockResolvedValue({ id: 't1' }),
|
|
667
|
-
update: vi.fn(),
|
|
668
|
-
delete: vi.fn(),
|
|
669
|
-
create: vi.fn(),
|
|
670
|
-
},
|
|
671
|
-
}
|
|
672
|
-
const m2mConfig: OpenSaasConfig = {
|
|
673
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
674
|
-
lists: {
|
|
675
|
-
Lesson: {
|
|
676
|
-
fields: {
|
|
677
|
-
title: { type: 'text' },
|
|
678
|
-
teachers: { type: 'relationship', ref: 'Teacher.lessons', many: true },
|
|
679
|
-
},
|
|
680
|
-
access: {
|
|
681
|
-
operation: {
|
|
682
|
-
query: () => true,
|
|
683
|
-
create: () => true,
|
|
684
|
-
update: () => true,
|
|
685
|
-
delete: () => true,
|
|
686
|
-
},
|
|
687
|
-
},
|
|
688
|
-
},
|
|
689
|
-
Teacher: {
|
|
690
|
-
fields: {
|
|
691
|
-
name: { type: 'text' },
|
|
692
|
-
lessons: { type: 'relationship', ref: 'Lesson.teachers', many: true },
|
|
693
|
-
},
|
|
694
|
-
access: {
|
|
695
|
-
operation: {
|
|
696
|
-
query: () => true,
|
|
697
|
-
create: () => true,
|
|
698
|
-
update: () => true,
|
|
699
|
-
delete: () => true,
|
|
700
|
-
},
|
|
701
|
-
},
|
|
702
|
-
},
|
|
703
|
-
},
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
const context = await getContext(m2mConfig, m2mPrisma, { userId: 'u1' })
|
|
707
|
-
await context.serverAction({
|
|
708
|
-
listKey: 'Lesson',
|
|
709
|
-
action: 'createRelated',
|
|
710
|
-
data: { title: 'L' },
|
|
711
|
-
field: 'teachers',
|
|
712
|
-
parentId: 't1',
|
|
713
|
-
})
|
|
714
|
-
|
|
715
|
-
const createArg = m2mPrisma.lesson.create.mock.calls[0][0]
|
|
716
|
-
// A to-many back-reference connects the parent by id.
|
|
717
|
-
expect(createArg.data.teachers).toEqual({ connect: [{ id: 't1' }] })
|
|
718
|
-
})
|
|
719
|
-
|
|
720
|
-
// Security-critical invariant (#758): the server sets the back-reference by
|
|
721
|
-
// spreading client `data` and THEN overwriting `data[field]` from the
|
|
722
|
-
// trusted `parentId`, so a HOSTILE client-supplied `data[field]` can never
|
|
723
|
-
// re-target the link to a different parent. These lock that overwrite for
|
|
724
|
-
// both the to-one and the to-many back-reference shapes.
|
|
725
|
-
it('overwrites a hostile client-supplied to-one back-reference with the trusted parentId', async () => {
|
|
726
|
-
const created = { id: 'p1', title: 'New', authorId: 'u1' }
|
|
727
|
-
mockPrisma.post.create.mockResolvedValue(created)
|
|
728
|
-
// The connect target must be reachable (the related list's read access).
|
|
729
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
730
|
-
|
|
731
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
732
|
-
await context.serverAction({
|
|
733
|
-
listKey: 'Post',
|
|
734
|
-
action: 'createRelated',
|
|
735
|
-
// Hostile: the client tries to re-target the link to a different parent
|
|
736
|
-
// by supplying the back-reference field itself.
|
|
737
|
-
data: { title: 'New', author: 'evil-id' },
|
|
738
|
-
field: 'author',
|
|
739
|
-
parentId: 'u1',
|
|
740
|
-
})
|
|
741
|
-
|
|
742
|
-
// The server OVERWRITES the spread-in hostile value with a connect to the
|
|
743
|
-
// trusted parentId — the evil id never reaches the secured create.
|
|
744
|
-
const createArg = mockPrisma.post.create.mock.calls[0][0]
|
|
745
|
-
expect(createArg.data.author).toEqual({ connect: { id: 'u1' } })
|
|
746
|
-
expect(createArg.data.author).not.toBe('evil-id')
|
|
747
|
-
})
|
|
748
|
-
|
|
749
|
-
it('overwrites a hostile client-supplied to-many back-reference with the trusted parentId', async () => {
|
|
750
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
751
|
-
const m2mPrisma: any = {
|
|
752
|
-
lesson: {
|
|
753
|
-
findUnique: vi.fn(),
|
|
754
|
-
update: vi.fn(),
|
|
755
|
-
delete: vi.fn(),
|
|
756
|
-
create: vi.fn().mockResolvedValue({ id: 'l1', title: 'L' }),
|
|
757
|
-
},
|
|
758
|
-
teacher: {
|
|
759
|
-
findUnique: vi.fn().mockResolvedValue({ id: 't1' }),
|
|
760
|
-
update: vi.fn(),
|
|
761
|
-
delete: vi.fn(),
|
|
762
|
-
create: vi.fn(),
|
|
763
|
-
},
|
|
764
|
-
}
|
|
765
|
-
const m2mConfig: OpenSaasConfig = {
|
|
766
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
767
|
-
lists: {
|
|
768
|
-
Lesson: {
|
|
769
|
-
fields: {
|
|
770
|
-
title: { type: 'text' },
|
|
771
|
-
teachers: { type: 'relationship', ref: 'Teacher.lessons', many: true },
|
|
772
|
-
},
|
|
773
|
-
access: {
|
|
774
|
-
operation: {
|
|
775
|
-
query: () => true,
|
|
776
|
-
create: () => true,
|
|
777
|
-
update: () => true,
|
|
778
|
-
delete: () => true,
|
|
779
|
-
},
|
|
780
|
-
},
|
|
781
|
-
},
|
|
782
|
-
Teacher: {
|
|
783
|
-
fields: {
|
|
784
|
-
name: { type: 'text' },
|
|
785
|
-
lessons: { type: 'relationship', ref: 'Lesson.teachers', many: true },
|
|
786
|
-
},
|
|
787
|
-
access: {
|
|
788
|
-
operation: {
|
|
789
|
-
query: () => true,
|
|
790
|
-
create: () => true,
|
|
791
|
-
update: () => true,
|
|
792
|
-
delete: () => true,
|
|
793
|
-
},
|
|
794
|
-
},
|
|
795
|
-
},
|
|
796
|
-
},
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
const context = await getContext(m2mConfig, m2mPrisma, { userId: 'u1' })
|
|
800
|
-
await context.serverAction({
|
|
801
|
-
listKey: 'Lesson',
|
|
802
|
-
action: 'createRelated',
|
|
803
|
-
// Hostile: the client tries to connect a different teacher.
|
|
804
|
-
data: { title: 'L', teachers: { connect: [{ id: 'evil-id' }] } },
|
|
805
|
-
field: 'teachers',
|
|
806
|
-
parentId: 't1',
|
|
807
|
-
})
|
|
808
|
-
|
|
809
|
-
// The to-many back-reference is overwritten to connect exactly the trusted
|
|
810
|
-
// parent by id — the hostile connect is discarded.
|
|
811
|
-
const createArg = m2mPrisma.lesson.create.mock.calls[0][0]
|
|
812
|
-
expect(createArg.data.teachers).toEqual({ connect: [{ id: 't1' }] })
|
|
813
|
-
})
|
|
814
|
-
|
|
815
|
-
// Defensive guard (#758): malformed direct calls (unreachable from the
|
|
816
|
-
// drawer, which always sends a valid relationship back-reference plus both
|
|
817
|
-
// field and parentId) are rejected rather than degrading to an unguarded
|
|
818
|
-
// create.
|
|
819
|
-
it('rejects a malformed call supplying only one of field/parentId (defensive guard)', async () => {
|
|
820
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
821
|
-
const result = await context.serverAction({
|
|
822
|
-
listKey: 'Post',
|
|
823
|
-
action: 'createRelated',
|
|
824
|
-
data: { title: 'New', author: 'evil-id' },
|
|
825
|
-
field: 'author',
|
|
826
|
-
// parentId omitted — a lone back-reference field is malformed.
|
|
827
|
-
})
|
|
828
|
-
|
|
829
|
-
expect(result).toEqual({
|
|
830
|
-
created: false,
|
|
831
|
-
error: 'createRelated requires both field and parentId, or neither',
|
|
832
|
-
})
|
|
833
|
-
// The unguarded create is never attempted.
|
|
834
|
-
expect(mockPrisma.post.create).not.toHaveBeenCalled()
|
|
835
|
-
})
|
|
836
|
-
|
|
837
|
-
it('rejects a back-reference that does not name a relationship field (defensive guard)', async () => {
|
|
838
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
839
|
-
const result = await context.serverAction({
|
|
840
|
-
listKey: 'Post',
|
|
841
|
-
action: 'createRelated',
|
|
842
|
-
data: { title: 'New' },
|
|
843
|
-
field: 'title', // a scalar field, not a relationship
|
|
844
|
-
parentId: 'u1',
|
|
845
|
-
})
|
|
846
|
-
|
|
847
|
-
expect(result).toMatchObject({ created: false })
|
|
848
|
-
expect(mockPrisma.post.create).not.toHaveBeenCalled()
|
|
849
|
-
})
|
|
850
|
-
|
|
851
|
-
it('returns a generic error (Silent failure) when the create is access-denied', async () => {
|
|
852
|
-
const deniedConfig: OpenSaasConfig = {
|
|
853
|
-
...config,
|
|
854
|
-
lists: {
|
|
855
|
-
...config.lists,
|
|
856
|
-
Post: {
|
|
857
|
-
...config.lists.Post,
|
|
858
|
-
access: {
|
|
859
|
-
operation: {
|
|
860
|
-
query: () => true,
|
|
861
|
-
create: () => false, // no create access → create denied
|
|
862
|
-
update: () => true,
|
|
863
|
-
delete: () => true,
|
|
864
|
-
},
|
|
865
|
-
},
|
|
866
|
-
},
|
|
867
|
-
},
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
const context = await getContext(deniedConfig, mockPrisma, { userId: 'u1' })
|
|
871
|
-
const result = await context.serverAction({
|
|
872
|
-
listKey: 'Post',
|
|
873
|
-
action: 'createRelated',
|
|
874
|
-
data: { title: 'New' },
|
|
875
|
-
field: 'author',
|
|
876
|
-
parentId: 'u1',
|
|
877
|
-
})
|
|
878
|
-
|
|
879
|
-
// Denied: reported as not created with a generic reason (no leak). The
|
|
880
|
-
// create is never attempted at the Prisma layer.
|
|
881
|
-
expect(result).toEqual({ created: false, error: 'Access denied or operation failed' })
|
|
882
|
-
expect(mockPrisma.post.create).not.toHaveBeenCalled()
|
|
883
|
-
})
|
|
884
|
-
|
|
885
|
-
it('surfaces field errors from the create (e.g. a unique constraint) in the drawer shape', async () => {
|
|
886
|
-
mockPrisma.post.create.mockRejectedValue({ code: 'P2002', meta: { target: ['title'] } })
|
|
887
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
888
|
-
|
|
889
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
890
|
-
const result = await context.serverAction({
|
|
891
|
-
listKey: 'Post',
|
|
892
|
-
action: 'createRelated',
|
|
893
|
-
data: { title: 'Dup' },
|
|
894
|
-
field: 'author',
|
|
895
|
-
parentId: 'u1',
|
|
896
|
-
})
|
|
897
|
-
|
|
898
|
-
// Parsed to a distinct { created: false } with per-field errors the drawer
|
|
899
|
-
// renders — never a single-op `success` envelope.
|
|
900
|
-
expect(result).toMatchObject({ created: false })
|
|
901
|
-
const created = result as { created: boolean; fieldErrors?: Record<string, string> }
|
|
902
|
-
expect(created.fieldErrors?.title).toBeDefined()
|
|
903
|
-
})
|
|
904
|
-
|
|
905
|
-
// Prisma 7 driver adapters (verified against @prisma/adapter-pg and PGlite,
|
|
906
|
-
// issue #979) leave `meta.target` undefined and put the equivalent data at
|
|
907
|
-
// `meta.driverAdapterError.cause` instead, as free-text-adjacent structured
|
|
908
|
-
// data rather than the documented shape.
|
|
909
|
-
describe('P2002 under Prisma 7 driver adapters (issue #979)', () => {
|
|
910
|
-
it('produces per-field errors for a composite unique violation', async () => {
|
|
911
|
-
mockPrisma.post.create.mockRejectedValue({
|
|
912
|
-
code: 'P2002',
|
|
913
|
-
meta: {
|
|
914
|
-
modelName: 'Post',
|
|
915
|
-
driverAdapterError: {
|
|
916
|
-
name: 'DriverAdapterError',
|
|
917
|
-
cause: {
|
|
918
|
-
originalCode: '23505',
|
|
919
|
-
originalMessage:
|
|
920
|
-
'duplicate key value violates unique constraint "post_title_content_key"',
|
|
921
|
-
kind: 'UniqueConstraintViolation',
|
|
922
|
-
constraint: { fields: ['title', 'content'] },
|
|
923
|
-
},
|
|
924
|
-
},
|
|
925
|
-
},
|
|
926
|
-
})
|
|
927
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
928
|
-
|
|
929
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
930
|
-
const result = await context.serverAction({
|
|
931
|
-
listKey: 'Post',
|
|
932
|
-
action: 'createRelated',
|
|
933
|
-
data: { title: 'Dup', content: 'Dup' },
|
|
934
|
-
field: 'author',
|
|
935
|
-
parentId: 'u1',
|
|
936
|
-
})
|
|
937
|
-
|
|
938
|
-
expect(result).toMatchObject({ created: false })
|
|
939
|
-
const created = result as { created: boolean; fieldErrors?: Record<string, string> }
|
|
940
|
-
expect(created.fieldErrors?.title).toBeDefined()
|
|
941
|
-
expect(created.fieldErrors?.content).toBeDefined()
|
|
942
|
-
})
|
|
943
|
-
|
|
944
|
-
it('produces a field error for a single-column unique violation', async () => {
|
|
945
|
-
mockPrisma.post.create.mockRejectedValue({
|
|
946
|
-
code: 'P2002',
|
|
947
|
-
meta: {
|
|
948
|
-
modelName: 'Post',
|
|
949
|
-
driverAdapterError: {
|
|
950
|
-
cause: {
|
|
951
|
-
originalMessage:
|
|
952
|
-
'duplicate key value violates unique constraint "post_title_key"',
|
|
953
|
-
constraint: { fields: ['title'] },
|
|
954
|
-
},
|
|
955
|
-
},
|
|
956
|
-
},
|
|
957
|
-
})
|
|
958
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
959
|
-
|
|
960
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
961
|
-
const result = await context.serverAction({
|
|
962
|
-
listKey: 'Post',
|
|
963
|
-
action: 'createRelated',
|
|
964
|
-
data: { title: 'Dup' },
|
|
965
|
-
field: 'author',
|
|
966
|
-
parentId: 'u1',
|
|
967
|
-
})
|
|
968
|
-
|
|
969
|
-
const created = result as { created: boolean; fieldErrors?: Record<string, string> }
|
|
970
|
-
expect(created.fieldErrors?.title).toBeDefined()
|
|
971
|
-
})
|
|
972
|
-
|
|
973
|
-
it('strips quotes so a camelCase column is keyed correctly (not left quoted)', async () => {
|
|
974
|
-
// Postgres quotes an identifier in `constraint.fields` only when it
|
|
975
|
-
// needed quoting — a camelCase column arrives as `"authorId"`. A naive
|
|
976
|
-
// fix that skips stripping would key fieldErrors by the literal string
|
|
977
|
-
// `"authorId"` (quotes included), missing the real field name.
|
|
978
|
-
mockPrisma.post.create.mockRejectedValue({
|
|
979
|
-
code: 'P2002',
|
|
980
|
-
meta: {
|
|
981
|
-
driverAdapterError: {
|
|
982
|
-
cause: {
|
|
983
|
-
originalMessage:
|
|
984
|
-
'duplicate key value violates unique constraint "post_authorId_key"',
|
|
985
|
-
constraint: { fields: ['"authorId"'] },
|
|
986
|
-
},
|
|
987
|
-
},
|
|
988
|
-
},
|
|
989
|
-
})
|
|
990
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
991
|
-
|
|
992
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
993
|
-
const result = await context.serverAction({
|
|
994
|
-
listKey: 'Post',
|
|
995
|
-
action: 'createRelated',
|
|
996
|
-
data: { title: 'Dup' },
|
|
997
|
-
field: 'author',
|
|
998
|
-
parentId: 'u1',
|
|
999
|
-
})
|
|
1000
|
-
|
|
1001
|
-
const created = result as { created: boolean; fieldErrors?: Record<string, string> }
|
|
1002
|
-
expect(created.fieldErrors).toEqual({ authorId: 'This value is already in use' })
|
|
1003
|
-
})
|
|
1004
|
-
|
|
1005
|
-
it('humanizes a camelCase field name in the message instead of running words together', async () => {
|
|
1006
|
-
// Before this fix, `target` was always empty under a driver adapter, so
|
|
1007
|
-
// this label-formatting path never ran for a camelCase column. Recovering
|
|
1008
|
-
// real column names makes it reachable — verify it reads as words, not
|
|
1009
|
-
// "tenantid".
|
|
1010
|
-
const camelCaseConfig: OpenSaasConfig = {
|
|
1011
|
-
...config,
|
|
1012
|
-
lists: {
|
|
1013
|
-
...config.lists,
|
|
1014
|
-
Post: {
|
|
1015
|
-
...config.lists.Post,
|
|
1016
|
-
fields: { ...config.lists.Post.fields, tenantSlug: { type: 'text' } },
|
|
1017
|
-
},
|
|
1018
|
-
},
|
|
1019
|
-
}
|
|
1020
|
-
mockPrisma.post.create.mockRejectedValue({
|
|
1021
|
-
code: 'P2002',
|
|
1022
|
-
meta: {
|
|
1023
|
-
driverAdapterError: {
|
|
1024
|
-
cause: {
|
|
1025
|
-
originalMessage:
|
|
1026
|
-
'duplicate key value violates unique constraint "post_tenantSlug_key"',
|
|
1027
|
-
constraint: { fields: ['"tenantSlug"'] },
|
|
1028
|
-
},
|
|
1029
|
-
},
|
|
1030
|
-
},
|
|
1031
|
-
})
|
|
1032
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
1033
|
-
|
|
1034
|
-
const context = await getContext(camelCaseConfig, mockPrisma, { userId: 'u1' })
|
|
1035
|
-
const result = await context.serverAction({
|
|
1036
|
-
listKey: 'Post',
|
|
1037
|
-
action: 'createRelated',
|
|
1038
|
-
data: { title: 'Dup' },
|
|
1039
|
-
field: 'author',
|
|
1040
|
-
parentId: 'u1',
|
|
1041
|
-
})
|
|
1042
|
-
|
|
1043
|
-
const created = result as { created: boolean; fieldErrors?: Record<string, string> }
|
|
1044
|
-
expect(created.fieldErrors).toEqual({
|
|
1045
|
-
tenantSlug: 'This tenant slug is already in use',
|
|
1046
|
-
})
|
|
1047
|
-
})
|
|
1048
|
-
|
|
1049
|
-
it('leaves an already-populated meta.target unaffected (existing path still wins)', async () => {
|
|
1050
|
-
mockPrisma.post.create.mockRejectedValue({
|
|
1051
|
-
code: 'P2002',
|
|
1052
|
-
meta: {
|
|
1053
|
-
target: ['title'],
|
|
1054
|
-
// Present but must be ignored — meta.target already answers the question.
|
|
1055
|
-
driverAdapterError: {
|
|
1056
|
-
cause: {
|
|
1057
|
-
originalMessage: 'duplicate key value violates unique constraint "other_key"',
|
|
1058
|
-
constraint: { fields: ['content'] },
|
|
1059
|
-
},
|
|
1060
|
-
},
|
|
1061
|
-
},
|
|
1062
|
-
})
|
|
1063
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
1064
|
-
|
|
1065
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
1066
|
-
const result = await context.serverAction({
|
|
1067
|
-
listKey: 'Post',
|
|
1068
|
-
action: 'createRelated',
|
|
1069
|
-
data: { title: 'Dup' },
|
|
1070
|
-
field: 'author',
|
|
1071
|
-
parentId: 'u1',
|
|
1072
|
-
})
|
|
1073
|
-
|
|
1074
|
-
const created = result as { created: boolean; fieldErrors?: Record<string, string> }
|
|
1075
|
-
expect(created.fieldErrors).toEqual({ title: 'This title is already in use' })
|
|
1076
|
-
})
|
|
1077
|
-
|
|
1078
|
-
it('falls back to the generic message (not a throw) when nothing is recoverable', async () => {
|
|
1079
|
-
mockPrisma.post.create.mockRejectedValue({ code: 'P2002', meta: {} })
|
|
1080
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
1081
|
-
|
|
1082
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
1083
|
-
const result = await context.serverAction({
|
|
1084
|
-
listKey: 'Post',
|
|
1085
|
-
action: 'createRelated',
|
|
1086
|
-
data: { title: 'Dup' },
|
|
1087
|
-
field: 'author',
|
|
1088
|
-
parentId: 'u1',
|
|
1089
|
-
})
|
|
1090
|
-
|
|
1091
|
-
expect(result).toEqual({
|
|
1092
|
-
created: false,
|
|
1093
|
-
error: 'A record with this value already exists',
|
|
1094
|
-
fieldErrors: {},
|
|
1095
|
-
})
|
|
1096
|
-
})
|
|
1097
|
-
|
|
1098
|
-
it('leaves a non-P2002 Prisma error unaffected', async () => {
|
|
1099
|
-
mockPrisma.post.create.mockRejectedValue({
|
|
1100
|
-
code: 'P2025',
|
|
1101
|
-
meta: { driverAdapterError: { cause: { constraint: { fields: ['title'] } } } },
|
|
1102
|
-
message: 'Record to update not found',
|
|
1103
|
-
})
|
|
1104
|
-
mockPrisma.user.findUnique.mockResolvedValue({ id: 'u1' })
|
|
1105
|
-
|
|
1106
|
-
const context = await getContext(config, mockPrisma, { userId: 'u1' })
|
|
1107
|
-
const result = await context.serverAction({
|
|
1108
|
-
listKey: 'Post',
|
|
1109
|
-
action: 'createRelated',
|
|
1110
|
-
data: { title: 'Dup' },
|
|
1111
|
-
field: 'author',
|
|
1112
|
-
parentId: 'u1',
|
|
1113
|
-
})
|
|
1114
|
-
|
|
1115
|
-
expect(result).toEqual({
|
|
1116
|
-
created: false,
|
|
1117
|
-
error: 'Record to update not found',
|
|
1118
|
-
fieldErrors: {},
|
|
1119
|
-
})
|
|
1120
|
-
})
|
|
1121
|
-
})
|
|
1122
|
-
})
|
|
1123
|
-
|
|
1124
|
-
describe('relationshipOptions', () => {
|
|
1125
|
-
it('returns { id, label }[] for the related list, unfiltered/unincluded', async () => {
|
|
1126
|
-
mockPrisma.user.findMany.mockResolvedValue([
|
|
1127
|
-
{ id: 'u1', name: 'Ada' },
|
|
1128
|
-
{ id: 'u2', name: 'Alan' },
|
|
1129
|
-
])
|
|
1130
|
-
|
|
1131
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1132
|
-
const result = await context.serverAction({
|
|
1133
|
-
listKey: 'Post',
|
|
1134
|
-
action: 'relationshipOptions',
|
|
1135
|
-
field: 'author',
|
|
1136
|
-
})
|
|
1137
|
-
|
|
1138
|
-
expect(result).toEqual({
|
|
1139
|
-
success: true,
|
|
1140
|
-
data: [
|
|
1141
|
-
{ id: 'u1', label: 'Ada' },
|
|
1142
|
-
{ id: 'u2', label: 'Alan' },
|
|
1143
|
-
],
|
|
1144
|
-
})
|
|
1145
|
-
const call = mockPrisma.user.findMany.mock.calls[0][0]
|
|
1146
|
-
expect(call.include).toBeUndefined()
|
|
1147
|
-
})
|
|
1148
|
-
|
|
1149
|
-
it('bounds the query by take', async () => {
|
|
1150
|
-
mockPrisma.user.findMany.mockResolvedValue([{ id: 'u1', name: 'Ada' }])
|
|
1151
|
-
|
|
1152
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1153
|
-
await context.serverAction({
|
|
1154
|
-
listKey: 'Post',
|
|
1155
|
-
action: 'relationshipOptions',
|
|
1156
|
-
field: 'author',
|
|
1157
|
-
take: 1,
|
|
1158
|
-
})
|
|
1159
|
-
|
|
1160
|
-
expect(mockPrisma.user.findMany).toHaveBeenCalledWith(expect.objectContaining({ take: 1 }))
|
|
1161
|
-
})
|
|
1162
|
-
|
|
1163
|
-
it('unions selectedIds beyond the take/search window', async () => {
|
|
1164
|
-
mockPrisma.user.findMany.mockResolvedValueOnce([{ id: 'u1', name: 'Ada' }])
|
|
1165
|
-
mockPrisma.user.findMany.mockResolvedValueOnce([{ id: 'u9', name: 'Zed' }])
|
|
1166
|
-
|
|
1167
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1168
|
-
const result = await context.serverAction({
|
|
1169
|
-
listKey: 'Post',
|
|
1170
|
-
action: 'relationshipOptions',
|
|
1171
|
-
field: 'author',
|
|
1172
|
-
take: 1,
|
|
1173
|
-
selectedIds: ['u9'],
|
|
1174
|
-
})
|
|
1175
|
-
|
|
1176
|
-
expect(result).toEqual({
|
|
1177
|
-
success: true,
|
|
1178
|
-
data: [
|
|
1179
|
-
{ id: 'u1', label: 'Ada' },
|
|
1180
|
-
{ id: 'u9', label: 'Zed' },
|
|
1181
|
-
],
|
|
1182
|
-
})
|
|
1183
|
-
})
|
|
1184
|
-
|
|
1185
|
-
it('returns [] data when the related list query access is denied', async () => {
|
|
1186
|
-
const deniedConfig: OpenSaasConfig = {
|
|
1187
|
-
...config,
|
|
1188
|
-
lists: {
|
|
1189
|
-
...config.lists,
|
|
1190
|
-
User: {
|
|
1191
|
-
...config.lists.User,
|
|
1192
|
-
access: { operation: { query: () => false } },
|
|
1193
|
-
},
|
|
1194
|
-
},
|
|
1195
|
-
}
|
|
1196
|
-
mockPrisma.user.findMany.mockResolvedValue([{ id: 'u1', name: 'Ada' }])
|
|
1197
|
-
|
|
1198
|
-
const context = await getContext(deniedConfig, mockPrisma, null)
|
|
1199
|
-
const result = await context.serverAction({
|
|
1200
|
-
listKey: 'Post',
|
|
1201
|
-
action: 'relationshipOptions',
|
|
1202
|
-
field: 'author',
|
|
1203
|
-
})
|
|
1204
|
-
|
|
1205
|
-
expect(result).toEqual({ success: true, data: [] })
|
|
1206
|
-
})
|
|
1207
|
-
|
|
1208
|
-
it('returns an error when the field is not a relationship field', async () => {
|
|
1209
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1210
|
-
const result = await context.serverAction({
|
|
1211
|
-
listKey: 'Post',
|
|
1212
|
-
action: 'relationshipOptions',
|
|
1213
|
-
field: 'title',
|
|
1214
|
-
})
|
|
1215
|
-
|
|
1216
|
-
expect(result).toEqual({
|
|
1217
|
-
success: false,
|
|
1218
|
-
error: 'Field "title" on list "Post" is not a relationship field',
|
|
1219
|
-
})
|
|
1220
|
-
})
|
|
1221
|
-
|
|
1222
|
-
it('returns an error when the field does not exist', async () => {
|
|
1223
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1224
|
-
const result = await context.serverAction({
|
|
1225
|
-
listKey: 'Post',
|
|
1226
|
-
action: 'relationshipOptions',
|
|
1227
|
-
field: 'nonexistent',
|
|
1228
|
-
})
|
|
1229
|
-
|
|
1230
|
-
expect(result).toEqual({
|
|
1231
|
-
success: false,
|
|
1232
|
-
error: 'Field "nonexistent" on list "Post" is not a relationship field',
|
|
1233
|
-
})
|
|
1234
|
-
})
|
|
1235
|
-
})
|
|
1236
|
-
})
|
|
1237
|
-
|
|
1238
|
-
describe('db operations', () => {
|
|
1239
|
-
it('should delegate findUnique to prisma with access control', async () => {
|
|
1240
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
1241
|
-
mockPrisma.user.findFirst.mockResolvedValue(mockUser)
|
|
1242
|
-
|
|
1243
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1244
|
-
const result = await context.db.user.findUnique({ where: { id: '1' } })
|
|
1245
|
-
|
|
1246
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalled()
|
|
1247
|
-
expect(result).toEqual(mockUser)
|
|
1248
|
-
})
|
|
1249
|
-
|
|
1250
|
-
describe('findUnique unique-where enforcement (#567)', () => {
|
|
1251
|
-
it('accepts a valid unique where (id) and keeps access + include intact', async () => {
|
|
1252
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
1253
|
-
mockPrisma.user.findFirst.mockResolvedValue(mockUser)
|
|
1254
|
-
|
|
1255
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1256
|
-
const result = await context.db.user.findUnique({
|
|
1257
|
-
where: { id: '1' },
|
|
1258
|
-
include: { posts: true },
|
|
1259
|
-
})
|
|
1260
|
-
|
|
1261
|
-
// Access control still runs and the underlying delegate is invoked with
|
|
1262
|
-
// the merged where + include (proving access + include path is intact).
|
|
1263
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalledWith(
|
|
1264
|
-
expect.objectContaining({
|
|
1265
|
-
where: expect.objectContaining({ id: '1' }),
|
|
1266
|
-
include: { posts: true },
|
|
1267
|
-
}),
|
|
1268
|
-
)
|
|
1269
|
-
expect(result).toEqual(mockUser)
|
|
1270
|
-
})
|
|
1271
|
-
|
|
1272
|
-
it('accepts a configured-unique field (email) as the unique where', async () => {
|
|
1273
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
1274
|
-
mockPrisma.user.findFirst.mockResolvedValue(mockUser)
|
|
1275
|
-
|
|
1276
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1277
|
-
const result = await context.db.user.findUnique({
|
|
1278
|
-
where: { email: 'john@example.com' },
|
|
1279
|
-
})
|
|
1280
|
-
|
|
1281
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalled()
|
|
1282
|
-
expect(result).toEqual(mockUser)
|
|
1283
|
-
})
|
|
1284
|
-
|
|
1285
|
-
it('narrows the result through a query fragment with a unique where', async () => {
|
|
1286
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
1287
|
-
mockPrisma.user.findFirst.mockResolvedValue(mockUser)
|
|
1288
|
-
|
|
1289
|
-
const fragment = defineFragment<{ id: string; name: string; email: string }>()({
|
|
1290
|
-
id: true,
|
|
1291
|
-
name: true,
|
|
1292
|
-
} as const)
|
|
1293
|
-
|
|
1294
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1295
|
-
const result = await context.db.user.findUnique({ where: { id: '1' }, query: fragment })
|
|
1296
|
-
|
|
1297
|
-
// Fragment narrows the result to only the requested fields (email omitted)
|
|
1298
|
-
expect(result).toEqual({ id: '1', name: 'John' })
|
|
1299
|
-
})
|
|
1300
|
-
|
|
1301
|
-
it('THROWS on a non-unique where (caller-shape error, not a silent null)', async () => {
|
|
1302
|
-
mockPrisma.user.findFirst.mockResolvedValue({ id: '1', name: 'John' })
|
|
1303
|
-
|
|
1304
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1305
|
-
|
|
1306
|
-
// `name` is not a unique key — this is misuse and must throw, not return null.
|
|
1307
|
-
await expect(context.db.user.findUnique({ where: { name: 'John' } })).rejects.toThrow(
|
|
1308
|
-
/requires a unique `where`/,
|
|
1309
|
-
)
|
|
1310
|
-
// The error guides the caller toward findFirst (the non-unique escape hatch).
|
|
1311
|
-
await expect(context.db.user.findUnique({ where: { name: 'John' } })).rejects.toThrow(
|
|
1312
|
-
/findFirst/,
|
|
1313
|
-
)
|
|
1314
|
-
// Guard runs before any DB access.
|
|
1315
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
1316
|
-
})
|
|
1317
|
-
|
|
1318
|
-
it('THROWS when a unique key is mixed with extra non-unique keys', async () => {
|
|
1319
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1320
|
-
|
|
1321
|
-
await expect(
|
|
1322
|
-
context.db.user.findUnique({ where: { id: '1', name: 'John' } }),
|
|
1323
|
-
).rejects.toThrow(/requires a unique `where`/)
|
|
1324
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
1325
|
-
})
|
|
1326
|
-
|
|
1327
|
-
it('THROWS on an empty where', async () => {
|
|
1328
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1329
|
-
|
|
1330
|
-
await expect(context.db.user.findUnique({ where: {} })).rejects.toThrow(
|
|
1331
|
-
/requires a unique `where`/,
|
|
1332
|
-
)
|
|
1333
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
1334
|
-
})
|
|
1335
|
-
|
|
1336
|
-
it('returns null on access denial (silent-failure contract preserved)', async () => {
|
|
1337
|
-
const deniedConfig: OpenSaasConfig = {
|
|
1338
|
-
...config,
|
|
1339
|
-
lists: {
|
|
1340
|
-
...config.lists,
|
|
1341
|
-
User: {
|
|
1342
|
-
...config.lists.User,
|
|
1343
|
-
access: {
|
|
1344
|
-
operation: {
|
|
1345
|
-
query: () => false,
|
|
1346
|
-
create: () => true,
|
|
1347
|
-
update: () => true,
|
|
1348
|
-
delete: () => true,
|
|
1349
|
-
},
|
|
1350
|
-
},
|
|
1351
|
-
},
|
|
1352
|
-
},
|
|
1353
|
-
}
|
|
1354
|
-
mockPrisma.user.findFirst.mockResolvedValue({ id: '1', name: 'John' })
|
|
1355
|
-
|
|
1356
|
-
const context = await getContext(deniedConfig, mockPrisma, null)
|
|
1357
|
-
const result = await context.db.user.findUnique({ where: { id: '1' } })
|
|
1358
|
-
|
|
1359
|
-
// Access denied -> null (not a throw), and the DB is never queried.
|
|
1360
|
-
expect(result).toBeNull()
|
|
1361
|
-
expect(mockPrisma.user.findFirst).not.toHaveBeenCalled()
|
|
1362
|
-
})
|
|
1363
|
-
|
|
1364
|
-
it('returns null when no record matches a valid unique where', async () => {
|
|
1365
|
-
mockPrisma.user.findFirst.mockResolvedValue(null)
|
|
1366
|
-
|
|
1367
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1368
|
-
const result = await context.db.user.findUnique({ where: { id: 'missing' } })
|
|
1369
|
-
|
|
1370
|
-
expect(result).toBeNull()
|
|
1371
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalled()
|
|
1372
|
-
})
|
|
1373
|
-
})
|
|
1374
|
-
|
|
1375
|
-
it('should delegate findMany to prisma with access control', async () => {
|
|
1376
|
-
const mockUsers = [
|
|
1377
|
-
{ id: '1', name: 'John' },
|
|
1378
|
-
{ id: '2', name: 'Jane' },
|
|
1379
|
-
]
|
|
1380
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
1381
|
-
|
|
1382
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1383
|
-
const result = await context.db.user.findMany()
|
|
1384
|
-
|
|
1385
|
-
expect(mockPrisma.user.findMany).toHaveBeenCalled()
|
|
1386
|
-
expect(result).toEqual(mockUsers)
|
|
1387
|
-
})
|
|
1388
|
-
|
|
1389
|
-
describe('findFirst', () => {
|
|
1390
|
-
it('should return the first matching row', async () => {
|
|
1391
|
-
const mockUsers = [
|
|
1392
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
1393
|
-
{ id: '2', name: 'Jane', email: 'jane@example.com' },
|
|
1394
|
-
]
|
|
1395
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
1396
|
-
|
|
1397
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1398
|
-
const result = await context.db.user.findFirst()
|
|
1399
|
-
|
|
1400
|
-
// Delegates to the access-controlled findMany with take: 1
|
|
1401
|
-
expect(mockPrisma.user.findMany).toHaveBeenCalledWith(expect.objectContaining({ take: 1 }))
|
|
1402
|
-
expect(result).toEqual(mockUsers[0])
|
|
1403
|
-
})
|
|
1404
|
-
|
|
1405
|
-
it('should return null (not undefined, not throw) when nothing matches', async () => {
|
|
1406
|
-
mockPrisma.user.findMany.mockResolvedValue([])
|
|
1407
|
-
|
|
1408
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1409
|
-
const result = await context.db.user.findFirst({ where: { name: 'Nobody' } })
|
|
1410
|
-
|
|
1411
|
-
expect(result).toBeNull()
|
|
1412
|
-
expect(result).not.toBeUndefined()
|
|
1413
|
-
})
|
|
1414
|
-
|
|
1415
|
-
it('should respect where and orderBy', async () => {
|
|
1416
|
-
const mockUser = { id: '2', name: 'Jane', email: 'jane@example.com' }
|
|
1417
|
-
mockPrisma.user.findMany.mockResolvedValue([mockUser])
|
|
1418
|
-
|
|
1419
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1420
|
-
const result = await context.db.user.findFirst({
|
|
1421
|
-
where: { name: 'Jane' },
|
|
1422
|
-
orderBy: { name: 'asc' },
|
|
1423
|
-
})
|
|
1424
|
-
|
|
1425
|
-
expect(mockPrisma.user.findMany).toHaveBeenCalledWith(
|
|
1426
|
-
expect.objectContaining({
|
|
1427
|
-
where: expect.objectContaining({ name: 'Jane' }),
|
|
1428
|
-
orderBy: { name: 'asc' },
|
|
1429
|
-
take: 1,
|
|
1430
|
-
}),
|
|
1431
|
-
)
|
|
1432
|
-
expect(result).toEqual(mockUser)
|
|
1433
|
-
})
|
|
1434
|
-
|
|
1435
|
-
it('should honour query-access denial the same as findMany (denied -> null)', async () => {
|
|
1436
|
-
const deniedConfig: OpenSaasConfig = {
|
|
1437
|
-
...config,
|
|
1438
|
-
lists: {
|
|
1439
|
-
...config.lists,
|
|
1440
|
-
User: {
|
|
1441
|
-
...config.lists.User,
|
|
1442
|
-
access: {
|
|
1443
|
-
operation: {
|
|
1444
|
-
query: () => false,
|
|
1445
|
-
create: () => true,
|
|
1446
|
-
update: () => true,
|
|
1447
|
-
delete: () => true,
|
|
1448
|
-
},
|
|
1449
|
-
},
|
|
1450
|
-
},
|
|
1451
|
-
},
|
|
1452
|
-
}
|
|
1453
|
-
mockPrisma.user.findMany.mockResolvedValue([
|
|
1454
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
1455
|
-
])
|
|
1456
|
-
|
|
1457
|
-
const context = await getContext(deniedConfig, mockPrisma, null)
|
|
1458
|
-
const result = await context.db.user.findFirst()
|
|
1459
|
-
|
|
1460
|
-
// Denied query short-circuits before hitting prisma — exactly like findMany
|
|
1461
|
-
expect(result).toBeNull()
|
|
1462
|
-
expect(mockPrisma.user.findMany).not.toHaveBeenCalled()
|
|
1463
|
-
})
|
|
1464
|
-
|
|
1465
|
-
it('should respect a query fragment, narrowing the returned single result', async () => {
|
|
1466
|
-
const mockUsers = [
|
|
1467
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
1468
|
-
{ id: '2', name: 'Jane', email: 'jane@example.com' },
|
|
1469
|
-
]
|
|
1470
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
1471
|
-
|
|
1472
|
-
const fragment = defineFragment<{ id: string; name: string; email: string }>()({
|
|
1473
|
-
id: true,
|
|
1474
|
-
name: true,
|
|
1475
|
-
} as const)
|
|
1476
|
-
|
|
1477
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1478
|
-
const result = await context.db.user.findFirst({ query: fragment })
|
|
1479
|
-
|
|
1480
|
-
// Fragment narrows the result to only the requested fields (email omitted)
|
|
1481
|
-
expect(result).toEqual({ id: '1', name: 'John' })
|
|
1482
|
-
})
|
|
1483
|
-
})
|
|
1484
|
-
|
|
1485
|
-
describe('read-path key validation (#912)', () => {
|
|
1486
|
-
it('throws on an undeclared `where` key on findMany, naming the list and the key', async () => {
|
|
1487
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1488
|
-
|
|
1489
|
-
await expect(context.db.post.findMany({ where: { bogusKey: 'x' } })).rejects.toThrow(/Post/)
|
|
1490
|
-
await expect(context.db.post.findMany({ where: { bogusKey: 'x' } })).rejects.toThrow(
|
|
1491
|
-
/bogusKey/,
|
|
1492
|
-
)
|
|
1493
|
-
expect(mockPrisma.post.findMany).not.toHaveBeenCalled()
|
|
1494
|
-
})
|
|
1495
|
-
|
|
1496
|
-
it('throws on an undeclared `where` key on count', async () => {
|
|
1497
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1498
|
-
|
|
1499
|
-
await expect(context.db.user.count({ where: { bogusKey: 'x' } })).rejects.toThrow(
|
|
1500
|
-
/bogusKey/,
|
|
1501
|
-
)
|
|
1502
|
-
expect(mockPrisma.user.count).not.toHaveBeenCalled()
|
|
1503
|
-
})
|
|
1504
|
-
|
|
1505
|
-
it('throws on an undeclared `orderBy` key on findMany', async () => {
|
|
1506
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1507
|
-
|
|
1508
|
-
await expect(context.db.post.findMany({ orderBy: { bogusKey: 'asc' } })).rejects.toThrow(
|
|
1509
|
-
/bogusKey/,
|
|
1510
|
-
)
|
|
1511
|
-
expect(mockPrisma.post.findMany).not.toHaveBeenCalled()
|
|
1512
|
-
})
|
|
1513
|
-
|
|
1514
|
-
it('throws on an undeclared key nested inside a logical operator (AND/OR/NOT)', async () => {
|
|
1515
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1516
|
-
|
|
1517
|
-
await expect(
|
|
1518
|
-
context.db.post.findMany({
|
|
1519
|
-
where: { OR: [{ title: { contains: 'a' } }, { bogusKey: 'x' }] },
|
|
1520
|
-
}),
|
|
1521
|
-
).rejects.toThrow(/bogusKey/)
|
|
1522
|
-
|
|
1523
|
-
await expect(
|
|
1524
|
-
context.db.post.findMany({
|
|
1525
|
-
where: { AND: [{ NOT: { bogusKey: 'x' } }] },
|
|
1526
|
-
}),
|
|
1527
|
-
).rejects.toThrow(/bogusKey/)
|
|
1528
|
-
})
|
|
1529
|
-
|
|
1530
|
-
it('throws on an undeclared key nested inside a relation filter, naming the RELATED list', async () => {
|
|
1531
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1532
|
-
|
|
1533
|
-
await expect(
|
|
1534
|
-
context.db.post.findMany({ where: { author: { is: { bogusKey: 'x' } } } }),
|
|
1535
|
-
).rejects.toThrow(/User/)
|
|
1536
|
-
await expect(
|
|
1537
|
-
context.db.post.findMany({ where: { author: { is: { bogusKey: 'x' } } } }),
|
|
1538
|
-
).rejects.toThrow(/bogusKey/)
|
|
1539
|
-
})
|
|
1540
|
-
|
|
1541
|
-
it("throws on an undeclared key reached via Prisma's direct-nesting to-one filter (no `is` wrapper)", async () => {
|
|
1542
|
-
// Prisma's documented default for filtering a to-one relation nests the
|
|
1543
|
-
// related list's fields directly, with no `is`/`isNot` wrapper at all —
|
|
1544
|
-
// e.g. `{ author: { email: { contains: '...' } } }`. An undeclared key
|
|
1545
|
-
// reached this way (one hop through a to-one relation, rather than at
|
|
1546
|
-
// the root) must be rejected exactly like the wrapped `is` form.
|
|
1547
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1548
|
-
|
|
1549
|
-
await expect(
|
|
1550
|
-
context.db.post.findMany({ where: { author: { bogusKey: 'x' } } }),
|
|
1551
|
-
).rejects.toThrow(/User/)
|
|
1552
|
-
await expect(
|
|
1553
|
-
context.db.post.findMany({ where: { author: { bogusKey: 'x' } } }),
|
|
1554
|
-
).rejects.toThrow(/bogusKey/)
|
|
1555
|
-
expect(mockPrisma.post.findMany).not.toHaveBeenCalled()
|
|
1556
|
-
})
|
|
1557
|
-
|
|
1558
|
-
it('accepts a declared field on the direct-nesting to-one filter form', async () => {
|
|
1559
|
-
mockPrisma.post.findMany.mockResolvedValue([])
|
|
1560
|
-
|
|
1561
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1562
|
-
const where = { author: { name: { equals: 'John' } } }
|
|
1563
|
-
await context.db.post.findMany({ where })
|
|
1564
|
-
|
|
1565
|
-
expect(mockPrisma.post.findMany).toHaveBeenCalledWith(expect.objectContaining({ where }))
|
|
1566
|
-
})
|
|
1567
|
-
|
|
1568
|
-
it('rejects a Prisma-generated back-relation the list config never declares (the regression that matters most)', async () => {
|
|
1569
|
-
// Organisation declares no relationship back to Document — mirrors a
|
|
1570
|
-
// list whose Prisma model gets a `from_Document_organisation`
|
|
1571
|
-
// back-relation purely because Document holds an FK pointing at it.
|
|
1572
|
-
const backRelationPrisma = {
|
|
1573
|
-
organisation: { findMany: vi.fn(), count: vi.fn() },
|
|
1574
|
-
}
|
|
1575
|
-
const backRelationConfig: OpenSaasConfig = {
|
|
1576
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1577
|
-
lists: {
|
|
1578
|
-
Organisation: {
|
|
1579
|
-
fields: { name: { type: 'text' } },
|
|
1580
|
-
access: { operation: { query: () => true } },
|
|
1581
|
-
},
|
|
1582
|
-
},
|
|
1583
|
-
}
|
|
1584
|
-
|
|
1585
|
-
const context = await getContext(backRelationConfig, backRelationPrisma, null)
|
|
1586
|
-
|
|
1587
|
-
await expect(
|
|
1588
|
-
context.db.organisation.findMany({
|
|
1589
|
-
where: { from_Document_organisation: { some: {} } },
|
|
1590
|
-
}),
|
|
1591
|
-
).rejects.toThrow(/from_Document_organisation/)
|
|
1592
|
-
await expect(
|
|
1593
|
-
context.db.organisation.count({
|
|
1594
|
-
where: { from_Document_organisation: { some: {} } },
|
|
1595
|
-
}),
|
|
1596
|
-
).rejects.toThrow(/from_Document_organisation/)
|
|
1597
|
-
|
|
1598
|
-
expect(backRelationPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
1599
|
-
expect(backRelationPrisma.organisation.count).not.toHaveBeenCalled()
|
|
1600
|
-
})
|
|
1601
|
-
|
|
1602
|
-
it('never mistakes a Prisma filter operator (equals/contains/startsWith/in/is/isNot) for a field name', async () => {
|
|
1603
|
-
mockPrisma.post.findMany.mockResolvedValue([
|
|
1604
|
-
{ id: '1', title: 'Test Post', content: 'x', authorId: 'u1' },
|
|
1605
|
-
])
|
|
1606
|
-
|
|
1607
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1608
|
-
const where = {
|
|
1609
|
-
title: { equals: 'Test Post', contains: 'Test', startsWith: 'T', in: ['Test Post'] },
|
|
1610
|
-
author: { is: { name: 'John' }, isNot: null },
|
|
1611
|
-
}
|
|
1612
|
-
|
|
1613
|
-
await context.db.post.findMany({ where })
|
|
1614
|
-
|
|
1615
|
-
expect(mockPrisma.post.findMany).toHaveBeenCalledWith(expect.objectContaining({ where }))
|
|
1616
|
-
})
|
|
1617
|
-
|
|
1618
|
-
it('passes all of the above through under sudo (the single trusted bypass)', async () => {
|
|
1619
|
-
mockPrisma.post.findMany.mockResolvedValue([])
|
|
1620
|
-
|
|
1621
|
-
const context = (await getContext(config, mockPrisma, null)).sudo()
|
|
1622
|
-
await context.db.post.findMany({
|
|
1623
|
-
where: { bogusKey: 'x', OR: [{ alsoBogus: 'y' }] },
|
|
1624
|
-
orderBy: { alsoOrderByBogus: 'asc' },
|
|
1625
|
-
})
|
|
1626
|
-
|
|
1627
|
-
expect(mockPrisma.post.findMany).toHaveBeenCalledWith(
|
|
1628
|
-
expect.objectContaining({
|
|
1629
|
-
where: { bogusKey: 'x', OR: [{ alsoBogus: 'y' }] },
|
|
1630
|
-
orderBy: { alsoOrderByBogus: 'asc' },
|
|
1631
|
-
}),
|
|
1632
|
-
)
|
|
1633
|
-
})
|
|
1634
|
-
|
|
1635
|
-
it('keeps an implied foreign-key scalar filterable (authorId for author: relationship(...))', async () => {
|
|
1636
|
-
mockPrisma.post.findMany.mockResolvedValue([])
|
|
1637
|
-
|
|
1638
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1639
|
-
await context.db.post.findMany({ where: { authorId: 'user-1' } })
|
|
1640
|
-
|
|
1641
|
-
expect(mockPrisma.post.findMany).toHaveBeenCalledWith(
|
|
1642
|
-
expect.objectContaining({ where: { authorId: 'user-1' } }),
|
|
1643
|
-
)
|
|
1644
|
-
})
|
|
1645
|
-
|
|
1646
|
-
it('findFirst inherits the check through findMany, with no second copy', async () => {
|
|
1647
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1648
|
-
|
|
1649
|
-
await expect(context.db.post.findFirst({ where: { bogusKey: 'x' } })).rejects.toThrow(
|
|
1650
|
-
/bogusKey/,
|
|
1651
|
-
)
|
|
1652
|
-
expect(mockPrisma.post.findMany).not.toHaveBeenCalled()
|
|
1653
|
-
})
|
|
1654
|
-
|
|
1655
|
-
it('updateMany inherits the check through findMany, with no second copy', async () => {
|
|
1656
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1657
|
-
|
|
1658
|
-
await expect(
|
|
1659
|
-
context.db.user.updateMany({ where: { bogusKey: 'x' }, data: { name: 'y' } }),
|
|
1660
|
-
).rejects.toThrow(/bogusKey/)
|
|
1661
|
-
expect(mockPrisma.user.findMany).not.toHaveBeenCalled()
|
|
1662
|
-
expect(mockPrisma.user.update).not.toHaveBeenCalled()
|
|
1663
|
-
})
|
|
1664
|
-
|
|
1665
|
-
it('an ordinary read using only declared keys is unaffected', async () => {
|
|
1666
|
-
const mockUsers = [{ id: '1', name: 'John', email: 'john@example.com' }]
|
|
1667
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
1668
|
-
|
|
1669
|
-
const context = await getContext(config, mockPrisma, null)
|
|
1670
|
-
const result = await context.db.user.findMany({
|
|
1671
|
-
where: { name: 'John' },
|
|
1672
|
-
orderBy: { name: 'asc' },
|
|
1673
|
-
})
|
|
1674
|
-
|
|
1675
|
-
expect(result).toEqual(mockUsers)
|
|
1676
|
-
})
|
|
1677
|
-
})
|
|
1678
|
-
|
|
1679
|
-
describe('predicate-time field-read access (#915)', () => {
|
|
1680
|
-
// The exact shape from the issue: a public `query` gate with one
|
|
1681
|
-
// field-level `read` gate. Without this fix, `billingAddress`'s withheld
|
|
1682
|
-
// value can be recovered one character at a time via `count()`, and its
|
|
1683
|
-
// relative order leaked via `orderBy`, despite no session ever being
|
|
1684
|
-
// allowed to READ it.
|
|
1685
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1686
|
-
let orgPrisma: any
|
|
1687
|
-
let orgConfig: OpenSaasConfig
|
|
1688
|
-
|
|
1689
|
-
beforeEach(() => {
|
|
1690
|
-
orgPrisma = {
|
|
1691
|
-
organisation: { findMany: vi.fn(), count: vi.fn() },
|
|
1692
|
-
}
|
|
1693
|
-
orgConfig = {
|
|
1694
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1695
|
-
lists: {
|
|
1696
|
-
Organisation: {
|
|
1697
|
-
fields: {
|
|
1698
|
-
name: { type: 'text' },
|
|
1699
|
-
billingAddress: {
|
|
1700
|
-
type: 'text',
|
|
1701
|
-
access: { read: () => false },
|
|
1702
|
-
},
|
|
1703
|
-
},
|
|
1704
|
-
access: { operation: { query: () => true } },
|
|
1705
|
-
},
|
|
1706
|
-
},
|
|
1707
|
-
}
|
|
1708
|
-
})
|
|
1709
|
-
|
|
1710
|
-
it('denies a `where` filter naming a read-denied field, on both findMany and count', async () => {
|
|
1711
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
1712
|
-
|
|
1713
|
-
await expect(
|
|
1714
|
-
context.db.organisation.findMany({
|
|
1715
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1716
|
-
}),
|
|
1717
|
-
).rejects.toThrow(/billingAddress/)
|
|
1718
|
-
await expect(
|
|
1719
|
-
context.db.organisation.count({
|
|
1720
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1721
|
-
}),
|
|
1722
|
-
).rejects.toThrow(/billingAddress/)
|
|
1723
|
-
|
|
1724
|
-
expect(orgPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
1725
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
1726
|
-
})
|
|
1727
|
-
|
|
1728
|
-
it('a count probe answers identically for a matching and a non-matching prefix (no oracle)', async () => {
|
|
1729
|
-
// Before the fix, these two counts would differ (1 vs 0), letting the
|
|
1730
|
-
// withheld value be recovered one character at a time. After the fix,
|
|
1731
|
-
// both throw the SAME denial — there is no distinguishing signal left.
|
|
1732
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
1733
|
-
|
|
1734
|
-
const matching = context.db.organisation
|
|
1735
|
-
.count({ where: { billingAddress: { startsWith: '12 ' } } })
|
|
1736
|
-
.catch((err: Error) => err.message)
|
|
1737
|
-
const nonMatching = context.db.organisation
|
|
1738
|
-
.count({ where: { billingAddress: { startsWith: '99 ' } } })
|
|
1739
|
-
.catch((err: Error) => err.message)
|
|
1740
|
-
|
|
1741
|
-
expect(await matching).toEqual(await nonMatching)
|
|
1742
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
1743
|
-
})
|
|
1744
|
-
|
|
1745
|
-
it('denies ordering by a read-denied field', async () => {
|
|
1746
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
1747
|
-
|
|
1748
|
-
await expect(
|
|
1749
|
-
context.db.organisation.findMany({ orderBy: { billingAddress: 'asc' } }),
|
|
1750
|
-
).rejects.toThrow(/billingAddress/)
|
|
1751
|
-
expect(orgPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
1752
|
-
})
|
|
1753
|
-
|
|
1754
|
-
it('checks keys nested inside logical operators (AND/OR/NOT)', async () => {
|
|
1755
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
1756
|
-
|
|
1757
|
-
await expect(
|
|
1758
|
-
context.db.organisation.findMany({
|
|
1759
|
-
where: { OR: [{ name: { contains: 'a' } }, { billingAddress: { contains: 'x' } }] },
|
|
1760
|
-
}),
|
|
1761
|
-
).rejects.toThrow(/billingAddress/)
|
|
1762
|
-
await expect(
|
|
1763
|
-
context.db.organisation.findMany({
|
|
1764
|
-
where: { AND: [{ NOT: { billingAddress: { contains: 'x' } } }] },
|
|
1765
|
-
}),
|
|
1766
|
-
).rejects.toThrow(/billingAddress/)
|
|
1767
|
-
})
|
|
1768
|
-
|
|
1769
|
-
it('a row-dependent read rule resolves to a denial, not a skipped check', async () => {
|
|
1770
|
-
// `item` cannot be evaluated before the query runs — there is no row
|
|
1771
|
-
// yet — so a rule that depends on it (the shape `FieldAccess['read']`
|
|
1772
|
-
// documents as the norm) must deny here even though, post-query, the
|
|
1773
|
-
// very same rule might have allowed this session to read the field.
|
|
1774
|
-
const rowDependentConfig: OpenSaasConfig = {
|
|
1775
|
-
...orgConfig,
|
|
1776
|
-
lists: {
|
|
1777
|
-
Organisation: {
|
|
1778
|
-
...orgConfig.lists.Organisation,
|
|
1779
|
-
fields: {
|
|
1780
|
-
...orgConfig.lists.Organisation.fields,
|
|
1781
|
-
billingAddress: {
|
|
1782
|
-
type: 'text',
|
|
1783
|
-
access: {
|
|
1784
|
-
read: ({ item, session }: { item: { ownerId: string }; session: unknown }) =>
|
|
1785
|
-
item.ownerId === (session as { userId?: string } | null)?.userId,
|
|
1786
|
-
},
|
|
1787
|
-
},
|
|
1788
|
-
},
|
|
1789
|
-
},
|
|
1790
|
-
},
|
|
1791
|
-
}
|
|
1792
|
-
|
|
1793
|
-
const context = await getContext(rowDependentConfig, orgPrisma, { userId: 'user-1' })
|
|
1794
|
-
|
|
1795
|
-
await expect(
|
|
1796
|
-
context.db.organisation.findMany({
|
|
1797
|
-
where: { billingAddress: { contains: 'x' } },
|
|
1798
|
-
}),
|
|
1799
|
-
).rejects.toThrow(/billingAddress/)
|
|
1800
|
-
expect(orgPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
1801
|
-
})
|
|
1802
|
-
|
|
1803
|
-
it('propagates InvalidFieldAccessResultError (#913), never folding it into a plain denial', async () => {
|
|
1804
|
-
const filterReturningConfig: OpenSaasConfig = {
|
|
1805
|
-
...orgConfig,
|
|
1806
|
-
lists: {
|
|
1807
|
-
Organisation: {
|
|
1808
|
-
...orgConfig.lists.Organisation,
|
|
1809
|
-
fields: {
|
|
1810
|
-
...orgConfig.lists.Organisation.fields,
|
|
1811
|
-
billingAddress: {
|
|
1812
|
-
type: 'text',
|
|
1813
|
-
// Bypasses the FieldAccessControl boolean-only type on purpose —
|
|
1814
|
-
// this is the exact misconfiguration #913 closed.
|
|
1815
|
-
access: { read: (() => ({ ownerId: 'x' })) as unknown as () => boolean },
|
|
1816
|
-
},
|
|
1817
|
-
},
|
|
1818
|
-
},
|
|
1819
|
-
},
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
const context = await getContext(filterReturningConfig, orgPrisma, null)
|
|
1823
|
-
|
|
1824
|
-
await expect(
|
|
1825
|
-
context.db.organisation.findMany({
|
|
1826
|
-
where: { billingAddress: { contains: 'x' } },
|
|
1827
|
-
}),
|
|
1828
|
-
).rejects.toThrow(InvalidFieldAccessResultError)
|
|
1829
|
-
})
|
|
1830
|
-
|
|
1831
|
-
it('leaves a readable field filterable and sortable (no regression)', async () => {
|
|
1832
|
-
orgPrisma.organisation.findMany.mockResolvedValue([])
|
|
1833
|
-
|
|
1834
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
1835
|
-
await context.db.organisation.findMany({
|
|
1836
|
-
where: { name: { contains: 'Acme' } },
|
|
1837
|
-
orderBy: { name: 'asc' },
|
|
1838
|
-
})
|
|
1839
|
-
|
|
1840
|
-
expect(orgPrisma.organisation.findMany).toHaveBeenCalledWith(
|
|
1841
|
-
expect.objectContaining({
|
|
1842
|
-
where: { name: { contains: 'Acme' } },
|
|
1843
|
-
orderBy: { name: 'asc' },
|
|
1844
|
-
}),
|
|
1845
|
-
)
|
|
1846
|
-
})
|
|
1847
|
-
|
|
1848
|
-
it('sudo bypasses the check entirely, matching #912', async () => {
|
|
1849
|
-
orgPrisma.organisation.findMany.mockResolvedValue([])
|
|
1850
|
-
orgPrisma.organisation.count.mockResolvedValue(3)
|
|
1851
|
-
|
|
1852
|
-
const context = (await getContext(orgConfig, orgPrisma, null)).sudo()
|
|
1853
|
-
await context.db.organisation.findMany({
|
|
1854
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1855
|
-
orderBy: { billingAddress: 'asc' },
|
|
1856
|
-
})
|
|
1857
|
-
await context.db.organisation.count({
|
|
1858
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1859
|
-
})
|
|
1860
|
-
|
|
1861
|
-
expect(orgPrisma.organisation.findMany).toHaveBeenCalledWith(
|
|
1862
|
-
expect.objectContaining({
|
|
1863
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1864
|
-
orderBy: { billingAddress: 'asc' },
|
|
1865
|
-
}),
|
|
1866
|
-
)
|
|
1867
|
-
expect(orgPrisma.organisation.count).toHaveBeenCalledWith(
|
|
1868
|
-
expect.objectContaining({ where: { billingAddress: { startsWith: '12 ' } } }),
|
|
1869
|
-
)
|
|
1870
|
-
})
|
|
1871
|
-
|
|
1872
|
-
it('a caller with zero list access gets the ordinary silent denial, not a validation error naming the field (found in review of #925)', async () => {
|
|
1873
|
-
// Operation-level `query` access is fully denied for this session.
|
|
1874
|
-
// The #912/#915 where/orderBy validation must never run in that case
|
|
1875
|
-
// — the thrown ValidationError names the offending key, which would
|
|
1876
|
-
// let a caller with NO access to the list at all learn a field's
|
|
1877
|
-
// name and its read-gating status purely from the error message.
|
|
1878
|
-
// That is itself the kind of oracle #915 exists to close, just
|
|
1879
|
-
// pointed at "does this field exist and is it read-gated" instead of
|
|
1880
|
-
// "what is this field's value" — so a fully denied caller must see
|
|
1881
|
-
// the SAME silent []/0 whether or not the where/orderBy names an
|
|
1882
|
-
// undeclared or read-denied key.
|
|
1883
|
-
const deniedConfig: OpenSaasConfig = {
|
|
1884
|
-
...orgConfig,
|
|
1885
|
-
lists: {
|
|
1886
|
-
Organisation: {
|
|
1887
|
-
...orgConfig.lists.Organisation,
|
|
1888
|
-
access: { operation: { query: () => false } },
|
|
1889
|
-
},
|
|
1890
|
-
},
|
|
1891
|
-
}
|
|
1892
|
-
const context = await getContext(deniedConfig, orgPrisma, null)
|
|
1893
|
-
|
|
1894
|
-
await expect(
|
|
1895
|
-
context.db.organisation.findMany({
|
|
1896
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1897
|
-
}),
|
|
1898
|
-
).resolves.toEqual([])
|
|
1899
|
-
await expect(
|
|
1900
|
-
context.db.organisation.findMany({ where: { bogusKey: 'x' } }),
|
|
1901
|
-
).resolves.toEqual([])
|
|
1902
|
-
await expect(
|
|
1903
|
-
context.db.organisation.count({
|
|
1904
|
-
where: { billingAddress: { startsWith: '12 ' } },
|
|
1905
|
-
}),
|
|
1906
|
-
).resolves.toBe(0)
|
|
1907
|
-
await expect(context.db.organisation.count({ where: { bogusKey: 'x' } })).resolves.toBe(0)
|
|
1908
|
-
|
|
1909
|
-
expect(orgPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
1910
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
1911
|
-
})
|
|
1912
|
-
|
|
1913
|
-
it('#915 itself does not recurse into a related list nested inside a relation filter, but #916 now does', async () => {
|
|
1914
|
-
// #915 (`validateQueryFieldReadAccess`) checks whether THIS list's
|
|
1915
|
-
// relationship field may be named at all — it does not by itself
|
|
1916
|
-
// reject a field on the RELATED list reached through it. Once #916
|
|
1917
|
-
// landed, that gap is closed by a SEPARATE pass
|
|
1918
|
-
// (`buildAccessScopedWhere`) that scopes relation filters and applies
|
|
1919
|
-
// the related list's own field-read access to keys nested inside
|
|
1920
|
-
// them — so the read-denied `User.secret` field is still rejected
|
|
1921
|
-
// overall, just not by #915's own top-level-only check.
|
|
1922
|
-
const relPrisma = {
|
|
1923
|
-
post: { findMany: vi.fn(), count: vi.fn() },
|
|
1924
|
-
}
|
|
1925
|
-
const relConfig: OpenSaasConfig = {
|
|
1926
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1927
|
-
lists: {
|
|
1928
|
-
User: {
|
|
1929
|
-
fields: {
|
|
1930
|
-
name: { type: 'text' },
|
|
1931
|
-
secret: { type: 'text', access: { read: () => false } },
|
|
1932
|
-
},
|
|
1933
|
-
access: { operation: { query: () => true } },
|
|
1934
|
-
},
|
|
1935
|
-
Post: {
|
|
1936
|
-
fields: {
|
|
1937
|
-
title: { type: 'text' },
|
|
1938
|
-
author: { type: 'relationship', ref: 'User.posts' },
|
|
1939
|
-
},
|
|
1940
|
-
access: { operation: { query: () => true } },
|
|
1941
|
-
},
|
|
1942
|
-
},
|
|
1943
|
-
}
|
|
1944
|
-
relPrisma.post.findMany.mockResolvedValue([])
|
|
1945
|
-
|
|
1946
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
1947
|
-
// Names a field on the RELATED list (User.secret) nested inside the
|
|
1948
|
-
// relation filter — not a key of Post itself, so #915's own check
|
|
1949
|
-
// (which only inspects Post's own fields) does not deny it, but
|
|
1950
|
-
// #916's relation-filter scoping (which checks the RELATED list's
|
|
1951
|
-
// field-read access) now does.
|
|
1952
|
-
await expect(
|
|
1953
|
-
context.db.post.findMany({ where: { author: { is: { secret: { contains: 'x' } } } } }),
|
|
1954
|
-
).rejects.toThrow(/secret/)
|
|
1955
|
-
|
|
1956
|
-
expect(relPrisma.post.findMany).not.toHaveBeenCalled()
|
|
1957
|
-
})
|
|
1958
|
-
})
|
|
1959
|
-
|
|
1960
|
-
describe('relation filter access scoping (#916)', () => {
|
|
1961
|
-
// The exact shape from the issue: a publicly-queryable Organisation,
|
|
1962
|
-
// reachable Document/Membership/User lists each gated differently, and
|
|
1963
|
-
// a two-hop chain (Organisation -> members -> user -> email) that lets
|
|
1964
|
-
// an anonymous caller binary-search a field it could never read
|
|
1965
|
-
// directly, unless the relation filter itself is scoped.
|
|
1966
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1967
|
-
let orgPrisma: any
|
|
1968
|
-
let orgConfig: OpenSaasConfig
|
|
1969
|
-
|
|
1970
|
-
beforeEach(() => {
|
|
1971
|
-
orgPrisma = {
|
|
1972
|
-
organisation: { findMany: vi.fn(), count: vi.fn() },
|
|
1973
|
-
}
|
|
1974
|
-
orgConfig = {
|
|
1975
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
1976
|
-
lists: {
|
|
1977
|
-
Organisation: {
|
|
1978
|
-
fields: {
|
|
1979
|
-
name: { type: 'text' },
|
|
1980
|
-
documents: { type: 'relationship', ref: 'Document.organisation', many: true },
|
|
1981
|
-
members: { type: 'relationship', ref: 'Membership.organisation', many: true },
|
|
1982
|
-
},
|
|
1983
|
-
access: { operation: { query: () => true } },
|
|
1984
|
-
},
|
|
1985
|
-
// Membership-scoped: `query` denies everything for an anonymous
|
|
1986
|
-
// session (no session means no membership filter can match).
|
|
1987
|
-
Document: {
|
|
1988
|
-
fields: {
|
|
1989
|
-
organisation: { type: 'relationship', ref: 'Organisation.documents' },
|
|
1990
|
-
billingAddress: { type: 'text' },
|
|
1991
|
-
},
|
|
1992
|
-
access: { operation: { query: () => false } },
|
|
1993
|
-
},
|
|
1994
|
-
Membership: {
|
|
1995
|
-
fields: {
|
|
1996
|
-
organisation: { type: 'relationship', ref: 'Organisation.members' },
|
|
1997
|
-
user: { type: 'relationship', ref: 'User.memberships' },
|
|
1998
|
-
},
|
|
1999
|
-
// Readable by anyone — the hop itself isn't the leak, the
|
|
2000
|
-
// identity list at the end of the chain is.
|
|
2001
|
-
access: { operation: { query: () => true } },
|
|
2002
|
-
},
|
|
2003
|
-
User: {
|
|
2004
|
-
fields: {
|
|
2005
|
-
memberships: { type: 'relationship', ref: 'Membership.user', many: true },
|
|
2006
|
-
email: { type: 'text' },
|
|
2007
|
-
},
|
|
2008
|
-
// Not queryable at all by an anonymous session.
|
|
2009
|
-
access: { operation: { query: () => false } },
|
|
2010
|
-
},
|
|
2011
|
-
},
|
|
2012
|
-
}
|
|
2013
|
-
})
|
|
2014
|
-
|
|
2015
|
-
it('denies a single-hop relation filter through a fully denied related list, on both findMany and count', async () => {
|
|
2016
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
2017
|
-
|
|
2018
|
-
await expect(
|
|
2019
|
-
context.db.organisation.findMany({
|
|
2020
|
-
where: { documents: { some: { billingAddress: { startsWith: '12 ' } } } },
|
|
2021
|
-
}),
|
|
2022
|
-
).rejects.toThrow(/Document/)
|
|
2023
|
-
await expect(
|
|
2024
|
-
context.db.organisation.count({
|
|
2025
|
-
where: { documents: { some: { billingAddress: { startsWith: '12 ' } } } },
|
|
2026
|
-
}),
|
|
2027
|
-
).rejects.toThrow(/Document/)
|
|
2028
|
-
|
|
2029
|
-
expect(orgPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
2030
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
2031
|
-
})
|
|
2032
|
-
|
|
2033
|
-
it('a count probe through a denied relation answers identically for a matching and non-matching value (no oracle)', async () => {
|
|
2034
|
-
// Before the fix these counts could differ (1 vs 0), letting a caller
|
|
2035
|
-
// recover `billingAddress` one character at a time. After the fix
|
|
2036
|
-
// both throw the SAME denial.
|
|
2037
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
2038
|
-
|
|
2039
|
-
const matching = context.db.organisation
|
|
2040
|
-
.count({ where: { documents: { some: { billingAddress: { startsWith: '12 ' } } } } })
|
|
2041
|
-
.catch((err: Error) => err.message)
|
|
2042
|
-
const nonMatching = context.db.organisation
|
|
2043
|
-
.count({ where: { documents: { some: { billingAddress: { startsWith: '99 ' } } } } })
|
|
2044
|
-
.catch((err: Error) => err.message)
|
|
2045
|
-
|
|
2046
|
-
expect(await matching).toEqual(await nonMatching)
|
|
2047
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
2048
|
-
})
|
|
2049
|
-
|
|
2050
|
-
it('denies a two-hop chain reaching a denied identity list, the exact probe from the issue', async () => {
|
|
2051
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
2052
|
-
|
|
2053
|
-
await expect(
|
|
2054
|
-
context.db.organisation.count({
|
|
2055
|
-
where: {
|
|
2056
|
-
members: { some: { user: { is: { email: { equals: 'ada@example.com' } } } } },
|
|
2057
|
-
},
|
|
2058
|
-
}),
|
|
2059
|
-
).rejects.toThrow(/User/)
|
|
2060
|
-
|
|
2061
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
2062
|
-
})
|
|
2063
|
-
|
|
2064
|
-
it('a two-hop count probe answers identically for a real and a fabricated email (no identity-confirmation oracle)', async () => {
|
|
2065
|
-
const context = await getContext(orgConfig, orgPrisma, null)
|
|
2066
|
-
|
|
2067
|
-
const real = context.db.organisation
|
|
2068
|
-
.count({
|
|
2069
|
-
where: {
|
|
2070
|
-
members: { some: { user: { is: { email: { equals: 'real@example.com' } } } } },
|
|
2071
|
-
},
|
|
2072
|
-
})
|
|
2073
|
-
.catch((err: Error) => err.message)
|
|
2074
|
-
const fabricated = context.db.organisation
|
|
2075
|
-
.count({
|
|
2076
|
-
where: {
|
|
2077
|
-
members: { some: { user: { is: { email: { equals: 'nobody@example.com' } } } } },
|
|
2078
|
-
},
|
|
2079
|
-
})
|
|
2080
|
-
.catch((err: Error) => err.message)
|
|
2081
|
-
|
|
2082
|
-
expect(await real).toEqual(await fabricated)
|
|
2083
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
2084
|
-
})
|
|
2085
|
-
|
|
2086
|
-
it("folds the related list's access filter into a relation filter rather than denying or passing it through unscoped", async () => {
|
|
2087
|
-
orgPrisma.organisation.findMany.mockResolvedValue([])
|
|
2088
|
-
const scopedConfig: OpenSaasConfig = {
|
|
2089
|
-
...orgConfig,
|
|
2090
|
-
lists: {
|
|
2091
|
-
...orgConfig.lists,
|
|
2092
|
-
Document: {
|
|
2093
|
-
...orgConfig.lists.Document,
|
|
2094
|
-
// A filter, not a boolean: only this org's own published docs.
|
|
2095
|
-
access: { operation: { query: () => ({ published: { equals: true } }) } },
|
|
2096
|
-
},
|
|
2097
|
-
},
|
|
2098
|
-
}
|
|
2099
|
-
|
|
2100
|
-
const context = await getContext(scopedConfig, orgPrisma, null)
|
|
2101
|
-
await context.db.organisation.findMany({
|
|
2102
|
-
where: { documents: { some: { billingAddress: { startsWith: '12 ' } } } },
|
|
2103
|
-
})
|
|
2104
|
-
|
|
2105
|
-
expect(orgPrisma.organisation.findMany).toHaveBeenCalledWith(
|
|
2106
|
-
expect.objectContaining({
|
|
2107
|
-
where: {
|
|
2108
|
-
documents: {
|
|
2109
|
-
some: {
|
|
2110
|
-
AND: [{ published: { equals: true } }, { billingAddress: { startsWith: '12 ' } }],
|
|
2111
|
-
},
|
|
2112
|
-
},
|
|
2113
|
-
},
|
|
2114
|
-
}),
|
|
2115
|
-
)
|
|
2116
|
-
})
|
|
2117
|
-
|
|
2118
|
-
it('leaves a relation filter through a fully readable related list unwrapped (no perturbation)', async () => {
|
|
2119
|
-
orgPrisma.organisation.findMany.mockResolvedValue([])
|
|
2120
|
-
const openConfig: OpenSaasConfig = {
|
|
2121
|
-
...orgConfig,
|
|
2122
|
-
lists: {
|
|
2123
|
-
...orgConfig.lists,
|
|
2124
|
-
Membership: {
|
|
2125
|
-
...orgConfig.lists.Membership,
|
|
2126
|
-
access: { operation: { query: () => true } },
|
|
2127
|
-
},
|
|
2128
|
-
},
|
|
2129
|
-
}
|
|
2130
|
-
const context = await getContext(openConfig, orgPrisma, null)
|
|
2131
|
-
const where = { members: { some: { organisation: { is: { name: { equals: 'Acme' } } } } } }
|
|
2132
|
-
|
|
2133
|
-
await context.db.organisation.findMany({ where })
|
|
2134
|
-
|
|
2135
|
-
expect(orgPrisma.organisation.findMany).toHaveBeenCalledWith(
|
|
2136
|
-
expect.objectContaining({ where }),
|
|
2137
|
-
)
|
|
2138
|
-
})
|
|
2139
|
-
|
|
2140
|
-
it('sudo bypasses relation-filter scoping entirely (#916 unaffected)', async () => {
|
|
2141
|
-
orgPrisma.organisation.findMany.mockResolvedValue([])
|
|
2142
|
-
|
|
2143
|
-
const context = (await getContext(orgConfig, orgPrisma, null)).sudo()
|
|
2144
|
-
const where = { documents: { some: { billingAddress: { startsWith: '12 ' } } } }
|
|
2145
|
-
await context.db.organisation.findMany({ where })
|
|
2146
|
-
|
|
2147
|
-
expect(orgPrisma.organisation.findMany).toHaveBeenCalledWith(
|
|
2148
|
-
expect.objectContaining({ where }),
|
|
2149
|
-
)
|
|
2150
|
-
})
|
|
2151
|
-
|
|
2152
|
-
it('a caller with zero list access gets the ordinary silent denial, never the relation-filter error', async () => {
|
|
2153
|
-
const deniedConfig: OpenSaasConfig = {
|
|
2154
|
-
...orgConfig,
|
|
2155
|
-
lists: {
|
|
2156
|
-
...orgConfig.lists,
|
|
2157
|
-
Organisation: {
|
|
2158
|
-
...orgConfig.lists.Organisation,
|
|
2159
|
-
access: { operation: { query: () => false } },
|
|
2160
|
-
},
|
|
2161
|
-
},
|
|
2162
|
-
}
|
|
2163
|
-
const context = await getContext(deniedConfig, orgPrisma, null)
|
|
2164
|
-
|
|
2165
|
-
await expect(
|
|
2166
|
-
context.db.organisation.findMany({
|
|
2167
|
-
where: { documents: { some: { billingAddress: { startsWith: '12 ' } } } },
|
|
2168
|
-
}),
|
|
2169
|
-
).resolves.toEqual([])
|
|
2170
|
-
await expect(
|
|
2171
|
-
context.db.organisation.count({
|
|
2172
|
-
where: { documents: { some: { billingAddress: { startsWith: '12 ' } } } },
|
|
2173
|
-
}),
|
|
2174
|
-
).resolves.toBe(0)
|
|
2175
|
-
|
|
2176
|
-
expect(orgPrisma.organisation.findMany).not.toHaveBeenCalled()
|
|
2177
|
-
expect(orgPrisma.organisation.count).not.toHaveBeenCalled()
|
|
2178
|
-
})
|
|
2179
|
-
})
|
|
2180
|
-
|
|
2181
|
-
describe('explicit include merges with access control (#566)', () => {
|
|
2182
|
-
// Author has many Posts; Post.query access scopes to published posts only.
|
|
2183
|
-
// A caller-supplied `include` must NOT bypass that per-relation filter.
|
|
2184
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2185
|
-
let relPrisma: any
|
|
2186
|
-
let relConfig: OpenSaasConfig
|
|
2187
|
-
|
|
2188
|
-
beforeEach(() => {
|
|
2189
|
-
relPrisma = {
|
|
2190
|
-
author: {
|
|
2191
|
-
findFirst: vi.fn(),
|
|
2192
|
-
findUnique: vi.fn(),
|
|
2193
|
-
findMany: vi.fn(),
|
|
2194
|
-
},
|
|
2195
|
-
post: {
|
|
2196
|
-
findFirst: vi.fn(),
|
|
2197
|
-
findUnique: vi.fn(),
|
|
2198
|
-
findMany: vi.fn(),
|
|
2199
|
-
},
|
|
2200
|
-
comment: {
|
|
2201
|
-
findFirst: vi.fn(),
|
|
2202
|
-
findMany: vi.fn(),
|
|
2203
|
-
},
|
|
2204
|
-
}
|
|
2205
|
-
|
|
2206
|
-
relConfig = {
|
|
2207
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
2208
|
-
lists: {
|
|
2209
|
-
Author: {
|
|
2210
|
-
fields: {
|
|
2211
|
-
name: { type: 'text' },
|
|
2212
|
-
posts: { type: 'relationship', ref: 'Post.author', many: true },
|
|
2213
|
-
},
|
|
2214
|
-
access: { operation: { query: () => true } },
|
|
2215
|
-
},
|
|
2216
|
-
Post: {
|
|
2217
|
-
fields: {
|
|
2218
|
-
title: { type: 'text' },
|
|
2219
|
-
author: { type: 'relationship', ref: 'Author.posts' },
|
|
2220
|
-
comments: { type: 'relationship', ref: 'Comment.post', many: true },
|
|
2221
|
-
},
|
|
2222
|
-
access: {
|
|
2223
|
-
// Row filter: only published posts are visible.
|
|
2224
|
-
operation: { query: () => ({ status: { equals: 'published' } }) },
|
|
2225
|
-
},
|
|
2226
|
-
},
|
|
2227
|
-
Comment: {
|
|
2228
|
-
fields: {
|
|
2229
|
-
body: { type: 'text' },
|
|
2230
|
-
post: { type: 'relationship', ref: 'Post.comments' },
|
|
2231
|
-
},
|
|
2232
|
-
access: {
|
|
2233
|
-
operation: { query: () => ({ approved: { equals: true } }) },
|
|
2234
|
-
},
|
|
2235
|
-
},
|
|
2236
|
-
Secret: {
|
|
2237
|
-
fields: {
|
|
2238
|
-
value: { type: 'text' },
|
|
2239
|
-
},
|
|
2240
|
-
// Fully denied list.
|
|
2241
|
-
access: { operation: { query: () => false } },
|
|
2242
|
-
},
|
|
2243
|
-
},
|
|
2244
|
-
}
|
|
2245
|
-
// Add a denied relation onto Author for the "drop denied relation" test.
|
|
2246
|
-
relConfig.lists.Author.fields.secrets = {
|
|
2247
|
-
type: 'relationship',
|
|
2248
|
-
ref: 'Secret',
|
|
2249
|
-
many: true,
|
|
2250
|
-
}
|
|
2251
|
-
})
|
|
2252
|
-
|
|
2253
|
-
it('findMany: caller include {posts:true} applies the relation access where (not bare true)', async () => {
|
|
2254
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2255
|
-
|
|
2256
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2257
|
-
await context.db.author.findMany({ include: { posts: true } })
|
|
2258
|
-
|
|
2259
|
-
// The relation is fetched WITH the Post query-access where (NOT bare true),
|
|
2260
|
-
// proving the row-level bypass is closed.
|
|
2261
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2262
|
-
expect(call.include.posts).not.toBe(true)
|
|
2263
|
-
expect(call.include.posts.where).toEqual({ status: { equals: 'published' } })
|
|
2264
|
-
})
|
|
2265
|
-
|
|
2266
|
-
it('findUnique: caller include {posts:true} applies the relation access where', async () => {
|
|
2267
|
-
relPrisma.author.findFirst.mockResolvedValue({ id: 'a1', name: 'Jo', posts: [] })
|
|
2268
|
-
|
|
2269
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2270
|
-
await context.db.author.findUnique({ where: { id: 'a1' }, include: { posts: true } })
|
|
2271
|
-
|
|
2272
|
-
const call = relPrisma.author.findFirst.mock.calls[0][0]
|
|
2273
|
-
expect(call.where).toEqual(expect.objectContaining({ id: 'a1' }))
|
|
2274
|
-
expect(call.include.posts).not.toBe(true)
|
|
2275
|
-
expect(call.include.posts.where).toEqual({ status: { equals: 'published' } })
|
|
2276
|
-
})
|
|
2277
|
-
|
|
2278
|
-
it('drops a relation whose query access is false when named in the caller include', async () => {
|
|
2279
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2280
|
-
|
|
2281
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2282
|
-
await context.db.author.findMany({ include: { secrets: true, posts: true } })
|
|
2283
|
-
|
|
2284
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2285
|
-
// Denied `secrets` relation is dropped; allowed `posts` keeps its filter.
|
|
2286
|
-
expect(call.include.secrets).toBeUndefined()
|
|
2287
|
-
expect(call.include.posts.where).toEqual({ status: { equals: 'published' } })
|
|
2288
|
-
})
|
|
2289
|
-
|
|
2290
|
-
it('caller `_count` folds the relation access where into the select (#1087)', async () => {
|
|
2291
|
-
relPrisma.author.findMany.mockResolvedValue([
|
|
2292
|
-
{ id: 'a1', name: 'Jo', _count: { posts: 3 } },
|
|
2293
|
-
])
|
|
2294
|
-
|
|
2295
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2296
|
-
const result = await context.db.author.findMany({
|
|
2297
|
-
include: { _count: { select: { posts: true } } },
|
|
2298
|
-
})
|
|
2299
|
-
|
|
2300
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2301
|
-
expect(call.include._count).toEqual({
|
|
2302
|
-
select: { posts: { where: { status: { equals: 'published' } } } },
|
|
2303
|
-
})
|
|
2304
|
-
expect(result[0]._count).toEqual({ posts: 3 })
|
|
2305
|
-
})
|
|
2306
|
-
|
|
2307
|
-
it('caller `_count` on a fully-denied relation is omitted from the select and returns 0 (#1087)', async () => {
|
|
2308
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2309
|
-
|
|
2310
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2311
|
-
const result = await context.db.author.findMany({
|
|
2312
|
-
include: { _count: { select: { posts: true, secrets: true } } },
|
|
2313
|
-
})
|
|
2314
|
-
|
|
2315
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2316
|
-
// The denied `Secret` list is never asked to be counted...
|
|
2317
|
-
expect(call.include._count).toEqual({
|
|
2318
|
-
select: { posts: { where: { status: { equals: 'published' } } } },
|
|
2319
|
-
})
|
|
2320
|
-
// ...but the caller still sees an explicit `0`, never an absent key —
|
|
2321
|
-
// a count is session-relative, and `0` is what "no visible rows" means.
|
|
2322
|
-
expect(result[0]._count).toEqual({ secrets: 0 })
|
|
2323
|
-
})
|
|
2324
|
-
|
|
2325
|
-
it('sudo `_count` is used as-is, unscoped (behaviour preserved)', async () => {
|
|
2326
|
-
relPrisma.author.findMany.mockResolvedValue([
|
|
2327
|
-
{ id: 'a1', name: 'Jo', _count: { posts: 3, secrets: 7 } },
|
|
2328
|
-
])
|
|
2329
|
-
|
|
2330
|
-
const context = await getContext(relConfig, relPrisma, null).sudo()
|
|
2331
|
-
const result = await context.db.author.findMany({
|
|
2332
|
-
include: { _count: { select: { posts: true, secrets: true } } },
|
|
2333
|
-
})
|
|
2334
|
-
|
|
2335
|
-
expect(relPrisma.author.findMany).toHaveBeenCalledWith(
|
|
2336
|
-
expect.objectContaining({
|
|
2337
|
-
include: { _count: { select: { posts: true, secrets: true } } },
|
|
2338
|
-
}),
|
|
2339
|
-
)
|
|
2340
|
-
expect(result[0]._count).toEqual({ posts: 3, secrets: 7 })
|
|
2341
|
-
})
|
|
2342
|
-
|
|
2343
|
-
it('AND-combines a caller nested where with the relation access where', async () => {
|
|
2344
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2345
|
-
|
|
2346
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2347
|
-
await context.db.author.findMany({
|
|
2348
|
-
include: { posts: { where: { title: { contains: 'hello' } } } },
|
|
2349
|
-
})
|
|
2350
|
-
|
|
2351
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2352
|
-
// Both the access where and the caller where are applied via AND.
|
|
2353
|
-
expect(call.include.posts.where).toEqual({
|
|
2354
|
-
AND: [{ status: { equals: 'published' } }, { title: { contains: 'hello' } }],
|
|
2355
|
-
})
|
|
2356
|
-
})
|
|
2357
|
-
|
|
2358
|
-
it('access-filters nested (2-level) caller includes at every level', async () => {
|
|
2359
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2360
|
-
|
|
2361
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2362
|
-
await context.db.author.findMany({
|
|
2363
|
-
include: { posts: { include: { comments: true } } },
|
|
2364
|
-
})
|
|
2365
|
-
|
|
2366
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2367
|
-
// Level 1 (posts) and level 2 (comments) both carry their access where.
|
|
2368
|
-
expect(call.include.posts.where).toEqual({ status: { equals: 'published' } })
|
|
2369
|
-
expect(call.include.posts.include.comments.where).toEqual({ approved: { equals: true } })
|
|
2370
|
-
})
|
|
2371
|
-
|
|
2372
|
-
it('sudo with explicit include returns the include unfiltered (behaviour preserved)', async () => {
|
|
2373
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2374
|
-
|
|
2375
|
-
const context = await getContext(relConfig, relPrisma, null).sudo()
|
|
2376
|
-
await context.db.author.findMany({ include: { posts: true, secrets: true } })
|
|
2377
|
-
|
|
2378
|
-
// Under sudo the caller include is used as-is: no filter, nothing dropped.
|
|
2379
|
-
expect(relPrisma.author.findMany).toHaveBeenCalledWith(
|
|
2380
|
-
expect.objectContaining({ include: { posts: true, secrets: true } }),
|
|
2381
|
-
)
|
|
2382
|
-
})
|
|
2383
|
-
|
|
2384
|
-
it('query fragment path carries the access filter, same as the include: path (#1088)', async () => {
|
|
2385
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2386
|
-
|
|
2387
|
-
const postsFragment = defineFragment<{ id: string; title: string }>()({
|
|
2388
|
-
title: true,
|
|
2389
|
-
} as const)
|
|
2390
|
-
const fragment = defineFragment<{ id: string; name: string; posts: unknown }>()({
|
|
2391
|
-
id: true,
|
|
2392
|
-
name: true,
|
|
2393
|
-
posts: postsFragment,
|
|
2394
|
-
} as const)
|
|
2395
|
-
|
|
2396
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2397
|
-
await context.db.author.findMany({ query: fragment })
|
|
2398
|
-
|
|
2399
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2400
|
-
// The fragment-built include now runs through the same scoping walk as
|
|
2401
|
-
// an explicit caller `include`, so `posts` carries Post's access where
|
|
2402
|
-
// (matching the `include: { posts: true }` test above) instead of a
|
|
2403
|
-
// bare, unfiltered `true`.
|
|
2404
|
-
expect(call.include).toEqual({ posts: { where: { status: { equals: 'published' } } } })
|
|
2405
|
-
})
|
|
2406
|
-
|
|
2407
|
-
it('drops a relation whose query access is false when named in a query fragment', async () => {
|
|
2408
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2409
|
-
|
|
2410
|
-
const secretsFragment = defineFragment<{ id: string; value: string }>()({
|
|
2411
|
-
value: true,
|
|
2412
|
-
} as const)
|
|
2413
|
-
const fragment = defineFragment<{ id: string; name: string; secrets: unknown }>()({
|
|
2414
|
-
id: true,
|
|
2415
|
-
name: true,
|
|
2416
|
-
secrets: secretsFragment,
|
|
2417
|
-
} as const)
|
|
2418
|
-
|
|
2419
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2420
|
-
await context.db.author.findMany({ query: fragment })
|
|
2421
|
-
|
|
2422
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2423
|
-
// `Secret`'s query access is `() => false` — the denied relation is
|
|
2424
|
-
// dropped from the include entirely, same as the `include:` path.
|
|
2425
|
-
expect(call.include.secrets).toBeUndefined()
|
|
2426
|
-
})
|
|
2427
|
-
|
|
2428
|
-
it('a fragment nesting past the depth cap raises the same depth error a caller include does', async () => {
|
|
2429
|
-
const chainLength = READ_INCLUDE_MAX_DEPTH + 2
|
|
2430
|
-
const chainConfig: OpenSaasConfig = {
|
|
2431
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
2432
|
-
lists: {},
|
|
2433
|
-
}
|
|
2434
|
-
for (let i = 0; i < chainLength; i++) {
|
|
2435
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- minimal config for unit test
|
|
2436
|
-
const fields: Record<string, any> = { name: { type: 'text' } }
|
|
2437
|
-
if (i < chainLength - 1) fields.next = { type: 'relationship', ref: `D${i + 1}.prev` }
|
|
2438
|
-
if (i > 0) fields.prev = { type: 'relationship', ref: `D${i - 1}.next` }
|
|
2439
|
-
chainConfig.lists[`D${i}`] = { fields, access: { operation: { query: () => true } } }
|
|
2440
|
-
}
|
|
2441
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2442
|
-
const chainPrisma: any = {}
|
|
2443
|
-
for (let i = 0; i < chainLength; i++) {
|
|
2444
|
-
chainPrisma[`d${i}`] = { findMany: vi.fn(), findFirst: vi.fn(), findUnique: vi.fn() }
|
|
2445
|
-
}
|
|
2446
|
-
|
|
2447
|
-
// A fragment selecting `next` recursively, `hops` levels deep.
|
|
2448
|
-
function nestedFragmentFields(hops: number): Record<string, unknown> {
|
|
2449
|
-
if (hops <= 0) return { name: true }
|
|
2450
|
-
return {
|
|
2451
|
-
name: true,
|
|
2452
|
-
next: defineFragment<Record<string, unknown>>()(nestedFragmentFields(hops - 1)),
|
|
2453
|
-
}
|
|
2454
|
-
}
|
|
2455
|
-
const deepFragment = defineFragment<Record<string, unknown>>()(
|
|
2456
|
-
nestedFragmentFields(READ_INCLUDE_MAX_DEPTH + 1),
|
|
2457
|
-
)
|
|
2458
|
-
|
|
2459
|
-
const context = await getContext(chainConfig, chainPrisma, null)
|
|
2460
|
-
|
|
2461
|
-
await expect(context.db.d0.findMany({ query: deepFragment })).rejects.toThrow(
|
|
2462
|
-
AccessScopeDepthExceededError,
|
|
2463
|
-
)
|
|
2464
|
-
expect(chainPrisma.d0.findMany).not.toHaveBeenCalled()
|
|
2465
|
-
})
|
|
2466
|
-
|
|
2467
|
-
it('sudo query fragment reads stay unscoped (behaviour preserved)', async () => {
|
|
2468
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2469
|
-
|
|
2470
|
-
const secretsFragment = defineFragment<{ id: string; value: string }>()({
|
|
2471
|
-
value: true,
|
|
2472
|
-
} as const)
|
|
2473
|
-
const fragment = defineFragment<{ id: string; name: string; secrets: unknown }>()({
|
|
2474
|
-
id: true,
|
|
2475
|
-
name: true,
|
|
2476
|
-
secrets: secretsFragment,
|
|
2477
|
-
} as const)
|
|
2478
|
-
|
|
2479
|
-
const context = await getContext(relConfig, relPrisma, null).sudo()
|
|
2480
|
-
await context.db.author.findMany({ query: fragment })
|
|
2481
|
-
|
|
2482
|
-
// Under sudo, the fragment-built include is used as-is: the denied
|
|
2483
|
-
// `Secret` relation is fetched unfiltered, matching sudo's existing
|
|
2484
|
-
// include: behaviour.
|
|
2485
|
-
expect(relPrisma.author.findMany).toHaveBeenCalledWith(
|
|
2486
|
-
expect.objectContaining({ include: { secrets: true } }),
|
|
2487
|
-
)
|
|
2488
|
-
})
|
|
2489
|
-
|
|
2490
|
-
it('a fragment read on a to-one relation nulls out a row its related list denies, matching include: (#974)', async () => {
|
|
2491
|
-
// `Comment.post` is a to-one relation onto `Post`, whose query access
|
|
2492
|
-
// is a row filter (`status: published`) rather than a plain boolean —
|
|
2493
|
-
// Prisma can't carry that as a `where` on a to-one include, so it's
|
|
2494
|
-
// resolved via the post-query existence check instead.
|
|
2495
|
-
const postFragment = defineFragment<{ id: string; title: string }>()({
|
|
2496
|
-
title: true,
|
|
2497
|
-
} as const)
|
|
2498
|
-
const fragment = defineFragment<{ id: string; body: string; post: unknown }>()({
|
|
2499
|
-
id: true,
|
|
2500
|
-
body: true,
|
|
2501
|
-
post: postFragment,
|
|
2502
|
-
} as const)
|
|
2503
|
-
|
|
2504
|
-
relPrisma.comment.findMany.mockResolvedValue([
|
|
2505
|
-
{ id: 'c1', body: 'hi', post: { id: 'p1', title: 'Draft' } },
|
|
2506
|
-
])
|
|
2507
|
-
// The batched existence check queries the raw Post model directly;
|
|
2508
|
-
// an empty result means `p1` does not satisfy Post's access filter.
|
|
2509
|
-
relPrisma.post.findMany.mockResolvedValue([])
|
|
2510
|
-
|
|
2511
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2512
|
-
const result = await context.db.comment.findMany({ query: fragment })
|
|
2513
|
-
|
|
2514
|
-
expect(result[0].post).toBeNull()
|
|
2515
|
-
})
|
|
2516
|
-
|
|
2517
|
-
it('a fragment read and an equivalent include: read produce the same access-scoped include', async () => {
|
|
2518
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2519
|
-
|
|
2520
|
-
const postsFragment = defineFragment<{ id: string; title: string }>()({
|
|
2521
|
-
title: true,
|
|
2522
|
-
} as const)
|
|
2523
|
-
const fragment = defineFragment<{ id: string; name: string; posts: unknown }>()({
|
|
2524
|
-
id: true,
|
|
2525
|
-
name: true,
|
|
2526
|
-
posts: postsFragment,
|
|
2527
|
-
} as const)
|
|
2528
|
-
|
|
2529
|
-
const context = await getContext(relConfig, relPrisma, null)
|
|
2530
|
-
await context.db.author.findMany({ query: fragment })
|
|
2531
|
-
const fragmentInclude = relPrisma.author.findMany.mock.calls[0][0].include
|
|
2532
|
-
|
|
2533
|
-
relPrisma.author.findMany.mockClear()
|
|
2534
|
-
await context.db.author.findMany({ include: { posts: true } })
|
|
2535
|
-
const callerInclude = relPrisma.author.findMany.mock.calls[0][0].include
|
|
2536
|
-
|
|
2537
|
-
expect(fragmentInclude).toEqual(callerInclude)
|
|
2538
|
-
})
|
|
2539
|
-
|
|
2540
|
-
// Core new guarantee introduced by #852 / ADR-0026: naming one relation
|
|
2541
|
-
// no longer walks (and access-checks) every other relationship of the
|
|
2542
|
-
// list. Before this fix, `include: { posts: true }` would ALSO evaluate
|
|
2543
|
-
// `Secret`'s query access (and, one hop further, `Comment`'s) even
|
|
2544
|
-
// though the caller never named `secrets` — wasted access calls the
|
|
2545
|
-
// caller never asked to pay for. Asserted directly on the access
|
|
2546
|
-
// function, not just on the resulting include shape.
|
|
2547
|
-
it('does not invoke query access on a relation the caller did not name (#852)', async () => {
|
|
2548
|
-
const postQuerySpy = vi.fn(() => ({ status: { equals: 'published' } }))
|
|
2549
|
-
const secretQuerySpy = vi.fn(() => false)
|
|
2550
|
-
const spiedConfig: OpenSaasConfig = {
|
|
2551
|
-
...relConfig,
|
|
2552
|
-
lists: {
|
|
2553
|
-
...relConfig.lists,
|
|
2554
|
-
Post: { ...relConfig.lists.Post, access: { operation: { query: postQuerySpy } } },
|
|
2555
|
-
Secret: { ...relConfig.lists.Secret, access: { operation: { query: secretQuerySpy } } },
|
|
2556
|
-
},
|
|
2557
|
-
}
|
|
2558
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2559
|
-
|
|
2560
|
-
const context = await getContext(spiedConfig, relPrisma, null)
|
|
2561
|
-
await context.db.author.findMany({ include: { posts: true } })
|
|
2562
|
-
|
|
2563
|
-
expect(postQuerySpy).toHaveBeenCalledTimes(1)
|
|
2564
|
-
expect(secretQuerySpy).not.toHaveBeenCalled()
|
|
2565
|
-
})
|
|
2566
|
-
|
|
2567
|
-
// Regression for issue #830: a read issued from inside a `resolveOutput`
|
|
2568
|
-
// hook used to lose relation row scoping ENTIRELY — `buildIncludeWithAccessControl`
|
|
2569
|
-
// returned a whole-object `undefined` for the inner read (any
|
|
2570
|
-
// `_resolveOutputChain.length > 0`), which `mergeIncludeWithAccessControl`
|
|
2571
|
-
// treated as "nothing to merge against" and passed the caller's include
|
|
2572
|
-
// through completely unscoped. The fix scopes each immediate relation with
|
|
2573
|
-
// its own access `where` while still not auto-EXPANDING into that
|
|
2574
|
-
// relation's own nested relations (preserving the original loop-prevention
|
|
2575
|
-
// — see the self-referential coverage in `access-filter.test.ts`).
|
|
2576
|
-
describe('scopes (without expanding) a caller include used inside a resolveOutput hook (#830)', () => {
|
|
2577
|
-
// Build an Author config with a virtual field whose resolveOutput issues a
|
|
2578
|
-
// read WITH an explicit include. While that hook runs,
|
|
2579
|
-
// _resolveOutputChain.length > 0.
|
|
2580
|
-
function configWithResolveOutputProbe(
|
|
2581
|
-
callerInclude: Record<string, unknown>,
|
|
2582
|
-
capture: (include: unknown) => void,
|
|
2583
|
-
): OpenSaasConfig {
|
|
2584
|
-
return {
|
|
2585
|
-
...relConfig,
|
|
2586
|
-
lists: {
|
|
2587
|
-
...relConfig.lists,
|
|
2588
|
-
Author: {
|
|
2589
|
-
...relConfig.lists.Author,
|
|
2590
|
-
fields: {
|
|
2591
|
-
...relConfig.lists.Author.fields,
|
|
2592
|
-
commentSummary: virtual({
|
|
2593
|
-
type: 'string',
|
|
2594
|
-
hooks: {
|
|
2595
|
-
resolveOutput: async ({ context }) => {
|
|
2596
|
-
await context.db.comment.findMany({ include: callerInclude })
|
|
2597
|
-
capture(relPrisma.comment.findMany.mock.calls[0][0].include)
|
|
2598
|
-
return 'summary'
|
|
2599
|
-
},
|
|
2600
|
-
},
|
|
2601
|
-
}),
|
|
2602
|
-
},
|
|
2603
|
-
},
|
|
2604
|
-
},
|
|
2605
|
-
}
|
|
2606
|
-
}
|
|
2607
|
-
|
|
2608
|
-
it('findUnique inside a resolveOutput hook still fetches the relation, unscoped at the Prisma level (#974)', async () => {
|
|
2609
|
-
let innerIncludeSeen: unknown
|
|
2610
|
-
const hookConfig = configWithResolveOutputProbe({ post: true }, (include) => {
|
|
2611
|
-
innerIncludeSeen = include
|
|
2612
|
-
})
|
|
2613
|
-
|
|
2614
|
-
relPrisma.author.findFirst.mockResolvedValue({ id: 'a1', name: 'Jo' })
|
|
2615
|
-
relPrisma.comment.findMany.mockResolvedValue([{ id: 'c1', body: 'hi', post: null }])
|
|
2616
|
-
|
|
2617
|
-
const context = await getContext(hookConfig, relPrisma, null)
|
|
2618
|
-
await context.db.author.findUnique({ where: { id: 'a1' } })
|
|
2619
|
-
|
|
2620
|
-
// `post` is still fetched (not dropped). It carries no `where` — Post
|
|
2621
|
-
// is a to-one relation here, and Prisma rejects a `where` on a to-one
|
|
2622
|
-
// include (#974); Post's own access filter is enforced afterward via
|
|
2623
|
-
// a batched existence check instead (see access-filter.test.ts).
|
|
2624
|
-
expect(innerIncludeSeen).toEqual({ post: true })
|
|
2625
|
-
})
|
|
2626
|
-
|
|
2627
|
-
it('findMany inside a resolveOutput hook scopes the relation but does not auto-expand its nested include', async () => {
|
|
2628
|
-
let innerIncludeSeen: unknown
|
|
2629
|
-
const hookConfig = configWithResolveOutputProbe(
|
|
2630
|
-
{ post: { include: { author: true } } },
|
|
2631
|
-
(include) => {
|
|
2632
|
-
innerIncludeSeen = include
|
|
2633
|
-
},
|
|
2634
|
-
)
|
|
2635
|
-
|
|
2636
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2637
|
-
relPrisma.comment.findMany.mockResolvedValue([{ id: 'c1', body: 'hi', post: null }])
|
|
2638
|
-
|
|
2639
|
-
const context = await getContext(hookConfig, relPrisma, null)
|
|
2640
|
-
await context.db.author.findMany()
|
|
2641
|
-
|
|
2642
|
-
// The caller's own nested selection (`post.include.author`) is honoured
|
|
2643
|
-
// as-is (access control does not auto-descend further here). `post`
|
|
2644
|
-
// itself carries no `where` — see the findUnique test above (#974).
|
|
2645
|
-
expect(innerIncludeSeen).toEqual({
|
|
2646
|
-
post: { include: { author: true } },
|
|
2647
|
-
})
|
|
2648
|
-
})
|
|
2649
|
-
})
|
|
2650
|
-
|
|
2651
|
-
// Regression for issue #830: a caller `include` nested deeper than the
|
|
2652
|
-
// Access Filter can scope used to be returned unscoped rather than
|
|
2653
|
-
// denied. This exercises the fix end-to-end through `context.db`,
|
|
2654
|
-
// matching the reproduction in the issue: a chain of lists deep enough
|
|
2655
|
-
// to cross `READ_INCLUDE_MAX_DEPTH`, read as a non-privileged session.
|
|
2656
|
-
describe('fail-closed at the read-include depth cap through context.db (#830)', () => {
|
|
2657
|
-
function chainListConfig(count: number): OpenSaasConfig['lists'] {
|
|
2658
|
-
const lists: OpenSaasConfig['lists'] = {}
|
|
2659
|
-
for (let i = 0; i < count; i++) {
|
|
2660
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- minimal config for unit test
|
|
2661
|
-
const fields: Record<string, any> = { name: { type: 'text' } }
|
|
2662
|
-
if (i < count - 1) fields.next = { type: 'relationship', ref: `C${i + 1}.prev` }
|
|
2663
|
-
if (i > 0) fields.prev = { type: 'relationship', ref: `C${i - 1}.next` }
|
|
2664
|
-
lists[`C${i}`] = {
|
|
2665
|
-
fields,
|
|
2666
|
-
access: {
|
|
2667
|
-
operation: { query: () => (i === 0 ? true : { ownerId: { equals: `C${i}` } }) },
|
|
2668
|
-
},
|
|
2669
|
-
}
|
|
2670
|
-
}
|
|
2671
|
-
return lists
|
|
2672
|
-
}
|
|
2673
|
-
|
|
2674
|
-
// A caller include selecting `next` `hops` more times, ending bare.
|
|
2675
|
-
function nestedCallerInclude(hops: number): Record<string, unknown> {
|
|
2676
|
-
if (hops <= 0) return true as unknown as Record<string, unknown>
|
|
2677
|
-
return { include: { next: nestedCallerInclude(hops - 1) } }
|
|
2678
|
-
}
|
|
2679
|
-
|
|
2680
|
-
it('throws AccessScopeDepthExceededError for a caller include one hop past the cap', async () => {
|
|
2681
|
-
const chainLength = READ_INCLUDE_MAX_DEPTH + 2
|
|
2682
|
-
const chainConfig: OpenSaasConfig = {
|
|
2683
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
2684
|
-
lists: chainListConfig(chainLength),
|
|
2685
|
-
}
|
|
2686
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2687
|
-
const chainPrisma: any = {}
|
|
2688
|
-
for (let i = 0; i < chainLength; i++) {
|
|
2689
|
-
chainPrisma[`c${i}`] = { findMany: vi.fn(), findFirst: vi.fn(), findUnique: vi.fn() }
|
|
2690
|
-
}
|
|
2691
|
-
|
|
2692
|
-
const context = await getContext(chainConfig, chainPrisma, null)
|
|
2693
|
-
|
|
2694
|
-
await expect(
|
|
2695
|
-
context.db.c0.findMany({
|
|
2696
|
-
include: { next: nestedCallerInclude(READ_INCLUDE_MAX_DEPTH) },
|
|
2697
|
-
}),
|
|
2698
|
-
).rejects.toThrow(AccessScopeDepthExceededError)
|
|
2699
|
-
|
|
2700
|
-
// The database is never even queried — the denial happens before the
|
|
2701
|
-
// Prisma call, not as a post-hoc filter on returned data.
|
|
2702
|
-
expect(chainPrisma.c0.findMany).not.toHaveBeenCalled()
|
|
2703
|
-
})
|
|
2704
|
-
|
|
2705
|
-
it('still applies row-scoping at the deepest hop for the same include one hop shallower', async () => {
|
|
2706
|
-
const chainLength = READ_INCLUDE_MAX_DEPTH + 1
|
|
2707
|
-
const chainConfig: OpenSaasConfig = {
|
|
2708
|
-
db: { provider: 'postgresql', url: 'postgresql://localhost:5432/test' },
|
|
2709
|
-
lists: chainListConfig(chainLength),
|
|
2710
|
-
}
|
|
2711
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2712
|
-
const chainPrisma: any = {}
|
|
2713
|
-
for (let i = 0; i < chainLength; i++) {
|
|
2714
|
-
chainPrisma[`c${i}`] = { findMany: vi.fn(), findFirst: vi.fn(), findUnique: vi.fn() }
|
|
2715
|
-
}
|
|
2716
|
-
|
|
2717
|
-
// Every hop here is to-one (no `many: true`), so none of them can
|
|
2718
|
-
// carry a `where` on the Prisma include (#974) — the chain comes
|
|
2719
|
-
// back unscoped at the Prisma level, one nested object per hop, and
|
|
2720
|
-
// row-scoping is enforced afterward via a batched existence check
|
|
2721
|
-
// per hop instead.
|
|
2722
|
-
let deepestItem: Record<string, unknown> = { id: `id${chainLength - 1}`, name: 'leaf' }
|
|
2723
|
-
for (let i = chainLength - 2; i >= 0; i--) {
|
|
2724
|
-
deepestItem = { id: `id${i}`, name: 'x', next: deepestItem }
|
|
2725
|
-
}
|
|
2726
|
-
chainPrisma.c0.findMany.mockResolvedValue([deepestItem])
|
|
2727
|
-
for (let i = 1; i < chainLength; i++) {
|
|
2728
|
-
chainPrisma[`c${i}`].findMany.mockResolvedValue([{ id: `id${i}` }])
|
|
2729
|
-
}
|
|
2730
|
-
|
|
2731
|
-
const context = await getContext(chainConfig, chainPrisma, null)
|
|
2732
|
-
|
|
2733
|
-
await context.db.c0.findMany({
|
|
2734
|
-
include: { next: nestedCallerInclude(READ_INCLUDE_MAX_DEPTH - 1) },
|
|
2735
|
-
})
|
|
2736
|
-
|
|
2737
|
-
// Walk the built include down to the last list — no hop carries a
|
|
2738
|
-
// `where` (they are all to-one).
|
|
2739
|
-
let entry = chainPrisma.c0.findMany.mock.calls[0][0].include.next
|
|
2740
|
-
for (let i = 1; i < READ_INCLUDE_MAX_DEPTH; i++) {
|
|
2741
|
-
expect(entry.where).toBeUndefined()
|
|
2742
|
-
entry = entry.include.next
|
|
2743
|
-
}
|
|
2744
|
-
expect(entry.where).toBeUndefined()
|
|
2745
|
-
|
|
2746
|
-
// The deepest hop's own existence check still carries that list's
|
|
2747
|
-
// access filter — row scoping reaches all the way down, just not
|
|
2748
|
-
// through the Prisma `include` itself.
|
|
2749
|
-
expect(chainPrisma[`c${READ_INCLUDE_MAX_DEPTH}`].findMany).toHaveBeenCalledWith({
|
|
2750
|
-
where: {
|
|
2751
|
-
AND: [
|
|
2752
|
-
{ ownerId: { equals: `C${READ_INCLUDE_MAX_DEPTH}` } },
|
|
2753
|
-
{ id: { in: [`id${READ_INCLUDE_MAX_DEPTH}`] } },
|
|
2754
|
-
],
|
|
2755
|
-
},
|
|
2756
|
-
select: { id: true },
|
|
2757
|
-
})
|
|
2758
|
-
})
|
|
2759
|
-
})
|
|
2760
|
-
|
|
2761
|
-
// Regression for #628: a virtual field named in `include` used to be
|
|
2762
|
-
// forwarded straight through to Prisma, which throws "Unknown field"
|
|
2763
|
-
// because virtual fields have no database column. The runtime must
|
|
2764
|
-
// strip virtual keys from the include payload while still computing
|
|
2765
|
-
// the virtual value (via resolveOutput) and leaving real relationship
|
|
2766
|
-
// includes — access-controlled or not — untouched.
|
|
2767
|
-
describe('virtual fields named in include no longer reach Prisma (#628)', () => {
|
|
2768
|
-
function configWithVirtualDisplayName(): OpenSaasConfig {
|
|
2769
|
-
return {
|
|
2770
|
-
...relConfig,
|
|
2771
|
-
lists: {
|
|
2772
|
-
...relConfig.lists,
|
|
2773
|
-
Author: {
|
|
2774
|
-
...relConfig.lists.Author,
|
|
2775
|
-
fields: {
|
|
2776
|
-
...relConfig.lists.Author.fields,
|
|
2777
|
-
displayName: virtual({
|
|
2778
|
-
type: 'string',
|
|
2779
|
-
hooks: {
|
|
2780
|
-
resolveOutput: ({ item }) => `Author: ${item.name}`,
|
|
2781
|
-
},
|
|
2782
|
-
}),
|
|
2783
|
-
},
|
|
2784
|
-
},
|
|
2785
|
-
},
|
|
2786
|
-
}
|
|
2787
|
-
}
|
|
2788
|
-
|
|
2789
|
-
it('findMany: strips the virtual key from the Prisma include but keeps the real relationship include, and computes the virtual value', async () => {
|
|
2790
|
-
const vConfig = configWithVirtualDisplayName()
|
|
2791
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo', posts: [] }])
|
|
2792
|
-
|
|
2793
|
-
const context = await getContext(vConfig, relPrisma, null)
|
|
2794
|
-
const result = await context.db.author.findMany({
|
|
2795
|
-
include: { displayName: true, posts: true },
|
|
2796
|
-
})
|
|
2797
|
-
|
|
2798
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2799
|
-
expect(call.include).not.toHaveProperty('displayName')
|
|
2800
|
-
expect(call.include.posts).toBeDefined()
|
|
2801
|
-
expect(result[0].displayName).toBe('Author: Jo')
|
|
2802
|
-
})
|
|
2803
|
-
|
|
2804
|
-
it('findUnique: strips the virtual key from the Prisma include but keeps the real relationship include, and computes the virtual value', async () => {
|
|
2805
|
-
const vConfig = configWithVirtualDisplayName()
|
|
2806
|
-
relPrisma.author.findFirst.mockResolvedValue({ id: 'a1', name: 'Jo', posts: [] })
|
|
2807
|
-
|
|
2808
|
-
const context = await getContext(vConfig, relPrisma, null)
|
|
2809
|
-
const result = await context.db.author.findUnique({
|
|
2810
|
-
where: { id: 'a1' },
|
|
2811
|
-
include: { displayName: true, posts: true },
|
|
2812
|
-
})
|
|
2813
|
-
|
|
2814
|
-
const call = relPrisma.author.findFirst.mock.calls[0][0]
|
|
2815
|
-
expect(call.include).not.toHaveProperty('displayName')
|
|
2816
|
-
expect(call.include.posts).toBeDefined()
|
|
2817
|
-
expect(result.displayName).toBe('Author: Jo')
|
|
2818
|
-
})
|
|
2819
|
-
|
|
2820
|
-
it('the virtual value is populated even when omitted from include', async () => {
|
|
2821
|
-
const vConfig = configWithVirtualDisplayName()
|
|
2822
|
-
relPrisma.author.findFirst.mockResolvedValue({ id: 'a1', name: 'Jo' })
|
|
2823
|
-
|
|
2824
|
-
const context = await getContext(vConfig, relPrisma, null)
|
|
2825
|
-
const result = await context.db.author.findUnique({ where: { id: 'a1' } })
|
|
2826
|
-
|
|
2827
|
-
expect(result.displayName).toBe('Author: Jo')
|
|
2828
|
-
})
|
|
2829
|
-
|
|
2830
|
-
it('sudo: the virtual key is stripped from the Prisma include even though it bypasses access control', async () => {
|
|
2831
|
-
const vConfig = configWithVirtualDisplayName()
|
|
2832
|
-
relPrisma.author.findMany.mockResolvedValue([{ id: 'a1', name: 'Jo' }])
|
|
2833
|
-
|
|
2834
|
-
const context = await getContext(vConfig, relPrisma, null).sudo()
|
|
2835
|
-
const result = await context.db.author.findMany({
|
|
2836
|
-
include: { displayName: true, posts: true },
|
|
2837
|
-
})
|
|
2838
|
-
|
|
2839
|
-
const call = relPrisma.author.findMany.mock.calls[0][0]
|
|
2840
|
-
expect(call.include).not.toHaveProperty('displayName')
|
|
2841
|
-
// The real relationship is unaffected — sudo still passes it through as-is.
|
|
2842
|
-
expect(call.include.posts).toBe(true)
|
|
2843
|
-
expect(result[0].displayName).toBe('Author: Jo')
|
|
2844
|
-
})
|
|
2845
|
-
|
|
2846
|
-
it('read access control on the virtual field is still enforced (denied field is omitted)', async () => {
|
|
2847
|
-
const vConfig = configWithVirtualDisplayName()
|
|
2848
|
-
vConfig.lists.Author.fields.displayName = virtual({
|
|
2849
|
-
type: 'string',
|
|
2850
|
-
access: { read: () => false },
|
|
2851
|
-
hooks: {
|
|
2852
|
-
resolveOutput: ({ item }) => `Author: ${item.name}`,
|
|
2853
|
-
},
|
|
2854
|
-
})
|
|
2855
|
-
relPrisma.author.findFirst.mockResolvedValue({ id: 'a1', name: 'Jo' })
|
|
2856
|
-
|
|
2857
|
-
const context = await getContext(vConfig, relPrisma, null)
|
|
2858
|
-
const result = await context.db.author.findUnique({
|
|
2859
|
-
where: { id: 'a1' },
|
|
2860
|
-
include: { displayName: true },
|
|
2861
|
-
})
|
|
2862
|
-
|
|
2863
|
-
expect(result).not.toHaveProperty('displayName')
|
|
2864
|
-
})
|
|
2865
|
-
})
|
|
2866
|
-
})
|
|
2867
|
-
|
|
2868
|
-
it('should delegate create to prisma with access control and hooks', async () => {
|
|
2869
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
2870
|
-
mockPrisma.user.create.mockResolvedValue(mockUser)
|
|
2871
|
-
|
|
2872
|
-
const context = await getContext(config, mockPrisma, null)
|
|
2873
|
-
const result = await context.db.user.create({
|
|
2874
|
-
data: { name: 'John', email: 'john@example.com' },
|
|
2875
|
-
})
|
|
2876
|
-
|
|
2877
|
-
expect(mockPrisma.user.create).toHaveBeenCalled()
|
|
2878
|
-
expect(result).toEqual(mockUser)
|
|
2879
|
-
})
|
|
2880
|
-
|
|
2881
|
-
it('should delegate update to prisma with access control and hooks', async () => {
|
|
2882
|
-
const existingUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
2883
|
-
const updatedUser = { id: '1', name: 'John Updated', email: 'john@example.com' }
|
|
2884
|
-
mockPrisma.user.findUnique.mockResolvedValue(existingUser)
|
|
2885
|
-
mockPrisma.user.update.mockResolvedValue(updatedUser)
|
|
2886
|
-
|
|
2887
|
-
const context = await getContext(config, mockPrisma, null)
|
|
2888
|
-
const result = await context.db.user.update({
|
|
2889
|
-
where: { id: '1' },
|
|
2890
|
-
data: { name: 'John Updated' },
|
|
2891
|
-
})
|
|
2892
|
-
|
|
2893
|
-
expect(mockPrisma.user.update).toHaveBeenCalled()
|
|
2894
|
-
expect(result).toEqual(updatedUser)
|
|
2895
|
-
})
|
|
2896
|
-
|
|
2897
|
-
it('should delegate delete to prisma with access control and hooks', async () => {
|
|
2898
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
2899
|
-
mockPrisma.user.findUnique.mockResolvedValue(mockUser)
|
|
2900
|
-
mockPrisma.user.delete.mockResolvedValue(mockUser)
|
|
2901
|
-
|
|
2902
|
-
const context = await getContext(config, mockPrisma, null)
|
|
2903
|
-
const result = await context.db.user.delete({ where: { id: '1' } })
|
|
2904
|
-
|
|
2905
|
-
expect(mockPrisma.user.delete).toHaveBeenCalled()
|
|
2906
|
-
expect(result).toEqual(mockUser)
|
|
2907
|
-
})
|
|
2908
|
-
|
|
2909
|
-
it('should delegate count to prisma with access control', async () => {
|
|
2910
|
-
mockPrisma.user.count.mockResolvedValue(5)
|
|
2911
|
-
|
|
2912
|
-
const context = await getContext(config, mockPrisma, null)
|
|
2913
|
-
const result = await context.db.user.count()
|
|
2914
|
-
|
|
2915
|
-
expect(mockPrisma.user.count).toHaveBeenCalled()
|
|
2916
|
-
expect(result).toBe(5)
|
|
2917
|
-
})
|
|
2918
|
-
|
|
2919
|
-
it('should batch create items via createMany', async () => {
|
|
2920
|
-
const mockUsers = [
|
|
2921
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
2922
|
-
{ id: '2', name: 'Jane', email: 'jane@example.com' },
|
|
2923
|
-
{ id: '3', name: 'Bob', email: 'bob@example.com' },
|
|
2924
|
-
]
|
|
2925
|
-
|
|
2926
|
-
// Mock create to return each user in sequence
|
|
2927
|
-
mockPrisma.user.create
|
|
2928
|
-
.mockResolvedValueOnce(mockUsers[0])
|
|
2929
|
-
.mockResolvedValueOnce(mockUsers[1])
|
|
2930
|
-
.mockResolvedValueOnce(mockUsers[2])
|
|
2931
|
-
|
|
2932
|
-
const context = await getContext(config, mockPrisma, null)
|
|
2933
|
-
const result = await context.db.user.createMany({
|
|
2934
|
-
data: [
|
|
2935
|
-
{ name: 'John', email: 'john@example.com' },
|
|
2936
|
-
{ name: 'Jane', email: 'jane@example.com' },
|
|
2937
|
-
{ name: 'Bob', email: 'bob@example.com' },
|
|
2938
|
-
],
|
|
2939
|
-
})
|
|
2940
|
-
|
|
2941
|
-
// Should call create 3 times (once for each item)
|
|
2942
|
-
expect(mockPrisma.user.create).toHaveBeenCalledTimes(3)
|
|
2943
|
-
expect(result).toEqual(mockUsers)
|
|
2944
|
-
})
|
|
2945
|
-
|
|
2946
|
-
it('should batch update items via updateMany', async () => {
|
|
2947
|
-
const mockUsers = [
|
|
2948
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
2949
|
-
{ id: '2', name: 'Jane', email: 'jane@example.com' },
|
|
2950
|
-
]
|
|
2951
|
-
|
|
2952
|
-
const updatedUsers = [
|
|
2953
|
-
{ id: '1', name: 'John Updated', email: 'john@example.com' },
|
|
2954
|
-
{ id: '2', name: 'Jane Updated', email: 'jane@example.com' },
|
|
2955
|
-
]
|
|
2956
|
-
|
|
2957
|
-
// Mock findMany to return the users
|
|
2958
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
2959
|
-
|
|
2960
|
-
// Mock findUnique for each update's access check
|
|
2961
|
-
mockPrisma.user.findUnique
|
|
2962
|
-
.mockResolvedValueOnce(mockUsers[0])
|
|
2963
|
-
.mockResolvedValueOnce(mockUsers[1])
|
|
2964
|
-
|
|
2965
|
-
// Mock update to return updated users
|
|
2966
|
-
mockPrisma.user.update
|
|
2967
|
-
.mockResolvedValueOnce(updatedUsers[0])
|
|
2968
|
-
.mockResolvedValueOnce(updatedUsers[1])
|
|
2969
|
-
|
|
2970
|
-
const context = await getContext(config, mockPrisma, null)
|
|
2971
|
-
const result = await context.db.user.updateMany({
|
|
2972
|
-
where: { id: { in: ['1', '2'] } },
|
|
2973
|
-
data: { name: 'Updated' },
|
|
2974
|
-
})
|
|
2975
|
-
|
|
2976
|
-
// Should call findMany once to get records
|
|
2977
|
-
expect(mockPrisma.user.findMany).toHaveBeenCalledWith({
|
|
2978
|
-
where: { id: { in: ['1', '2'] } },
|
|
2979
|
-
take: undefined,
|
|
2980
|
-
skip: undefined,
|
|
2981
|
-
include: undefined,
|
|
2982
|
-
})
|
|
2983
|
-
|
|
2984
|
-
// Should call update twice (once for each item)
|
|
2985
|
-
expect(mockPrisma.user.update).toHaveBeenCalledTimes(2)
|
|
2986
|
-
expect(result).toEqual(updatedUsers)
|
|
2987
|
-
})
|
|
2988
|
-
|
|
2989
|
-
it('should run hooks and access control for each item in createMany', async () => {
|
|
2990
|
-
// Test that hooks are called for each item
|
|
2991
|
-
const mockUsers = [
|
|
2992
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
2993
|
-
{ id: '2', name: 'Jane', email: 'jane@example.com' },
|
|
2994
|
-
]
|
|
2995
|
-
|
|
2996
|
-
mockPrisma.user.create.mockResolvedValueOnce(mockUsers[0]).mockResolvedValueOnce(mockUsers[1])
|
|
2997
|
-
|
|
2998
|
-
// Config with hook
|
|
2999
|
-
const configWithHook: OpenSaasConfig = {
|
|
3000
|
-
...config,
|
|
3001
|
-
lists: {
|
|
3002
|
-
...config.lists,
|
|
3003
|
-
User: {
|
|
3004
|
-
...config.lists.User,
|
|
3005
|
-
hooks: {
|
|
3006
|
-
resolveInput: vi.fn(async ({ resolvedData }) => resolvedData),
|
|
3007
|
-
},
|
|
3008
|
-
},
|
|
3009
|
-
},
|
|
3010
|
-
}
|
|
3011
|
-
|
|
3012
|
-
const context = await getContext(configWithHook, mockPrisma, null)
|
|
3013
|
-
await context.db.user.createMany({
|
|
3014
|
-
data: [
|
|
3015
|
-
{ name: 'John', email: 'john@example.com' },
|
|
3016
|
-
{ name: 'Jane', email: 'jane@example.com' },
|
|
3017
|
-
],
|
|
3018
|
-
})
|
|
3019
|
-
|
|
3020
|
-
// Hook should be called twice (once for each item)
|
|
3021
|
-
expect(configWithHook.lists.User.hooks?.resolveInput).toHaveBeenCalledTimes(2)
|
|
3022
|
-
})
|
|
3023
|
-
|
|
3024
|
-
it('should run hooks and access control for each item in updateMany', async () => {
|
|
3025
|
-
const mockUsers = [
|
|
3026
|
-
{ id: '1', name: 'John', email: 'john@example.com' },
|
|
3027
|
-
{ id: '2', name: 'Jane', email: 'jane@example.com' },
|
|
3028
|
-
]
|
|
3029
|
-
|
|
3030
|
-
const updatedUsers = [
|
|
3031
|
-
{ id: '1', name: 'John Updated', email: 'john@example.com' },
|
|
3032
|
-
{ id: '2', name: 'Jane Updated', email: 'jane@example.com' },
|
|
3033
|
-
]
|
|
3034
|
-
|
|
3035
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
3036
|
-
mockPrisma.user.findUnique
|
|
3037
|
-
.mockResolvedValueOnce(mockUsers[0])
|
|
3038
|
-
.mockResolvedValueOnce(mockUsers[1])
|
|
3039
|
-
mockPrisma.user.update
|
|
3040
|
-
.mockResolvedValueOnce(updatedUsers[0])
|
|
3041
|
-
.mockResolvedValueOnce(updatedUsers[1])
|
|
3042
|
-
|
|
3043
|
-
// Config with hook
|
|
3044
|
-
const configWithHook: OpenSaasConfig = {
|
|
3045
|
-
...config,
|
|
3046
|
-
lists: {
|
|
3047
|
-
...config.lists,
|
|
3048
|
-
User: {
|
|
3049
|
-
...config.lists.User,
|
|
3050
|
-
hooks: {
|
|
3051
|
-
resolveInput: vi.fn(async ({ resolvedData }) => resolvedData),
|
|
3052
|
-
},
|
|
3053
|
-
},
|
|
3054
|
-
},
|
|
3055
|
-
}
|
|
3056
|
-
|
|
3057
|
-
const context = await getContext(configWithHook, mockPrisma, null)
|
|
3058
|
-
await context.db.user.updateMany({
|
|
3059
|
-
where: { id: { in: ['1', '2'] } },
|
|
3060
|
-
data: { name: 'Updated' },
|
|
3061
|
-
})
|
|
3062
|
-
|
|
3063
|
-
// Hook should be called twice (once for each item)
|
|
3064
|
-
expect(configWithHook.lists.User.hooks?.resolveInput).toHaveBeenCalledTimes(2)
|
|
3065
|
-
})
|
|
3066
|
-
})
|
|
3067
|
-
|
|
3068
|
-
// `select` is not honoured by context.db reads — it is a visible no-op: the
|
|
3069
|
-
// op warns (once per list+operation) and still returns the full, access-
|
|
3070
|
-
// filtered result. Each test re-imports getContext via vi.resetModules() so
|
|
3071
|
-
// the module-level warn-once cache starts empty.
|
|
3072
|
-
describe('select no-op warning', () => {
|
|
3073
|
-
let warnSpy: ReturnType<typeof vi.spyOn>
|
|
3074
|
-
let freshGetContext: typeof getContext
|
|
3075
|
-
|
|
3076
|
-
beforeEach(async () => {
|
|
3077
|
-
vi.resetModules()
|
|
3078
|
-
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
3079
|
-
const mod = await import('../src/context/index.js')
|
|
3080
|
-
freshGetContext = mod.getContext
|
|
3081
|
-
})
|
|
3082
|
-
|
|
3083
|
-
afterEach(() => {
|
|
3084
|
-
warnSpy.mockRestore()
|
|
3085
|
-
})
|
|
3086
|
-
|
|
3087
|
-
it('warns AND still returns the row when findUnique is passed a select', async () => {
|
|
3088
|
-
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
|
|
3089
|
-
mockPrisma.user.findFirst.mockResolvedValue(mockUser)
|
|
3090
|
-
|
|
3091
|
-
const context = await freshGetContext(config, mockPrisma, null)
|
|
3092
|
-
const result = await context.db.user.findUnique({
|
|
3093
|
-
where: { id: '1' },
|
|
3094
|
-
select: { name: true },
|
|
3095
|
-
})
|
|
3096
|
-
|
|
3097
|
-
expect(warnSpy).toHaveBeenCalledTimes(1)
|
|
3098
|
-
expect(warnSpy.mock.calls[0][0]).toContain('`select` is ignored')
|
|
3099
|
-
expect(warnSpy.mock.calls[0][0]).toContain('findUnique')
|
|
3100
|
-
// Behaviour unchanged: the op still runs and returns the full row.
|
|
3101
|
-
expect(mockPrisma.user.findFirst).toHaveBeenCalled()
|
|
3102
|
-
expect(result).toEqual(mockUser)
|
|
3103
|
-
})
|
|
3104
|
-
|
|
3105
|
-
it('warns AND still returns the rows when findMany is passed a select', async () => {
|
|
3106
|
-
const mockUsers = [
|
|
3107
|
-
{ id: '1', name: 'John' },
|
|
3108
|
-
{ id: '2', name: 'Jane' },
|
|
3109
|
-
]
|
|
3110
|
-
mockPrisma.user.findMany.mockResolvedValue(mockUsers)
|
|
3111
|
-
|
|
3112
|
-
const context = await freshGetContext(config, mockPrisma, null)
|
|
3113
|
-
const result = await context.db.user.findMany({ select: { name: true } })
|
|
3114
|
-
|
|
3115
|
-
expect(warnSpy).toHaveBeenCalledTimes(1)
|
|
3116
|
-
expect(warnSpy.mock.calls[0][0]).toContain('`select` is ignored')
|
|
3117
|
-
expect(warnSpy.mock.calls[0][0]).toContain('findMany')
|
|
3118
|
-
expect(mockPrisma.user.findMany).toHaveBeenCalled()
|
|
3119
|
-
expect(result).toEqual(mockUsers)
|
|
3120
|
-
})
|
|
3121
|
-
|
|
3122
|
-
it('warns only once per list+operation across repeated calls', async () => {
|
|
3123
|
-
mockPrisma.user.findMany.mockResolvedValue([])
|
|
3124
|
-
|
|
3125
|
-
const context = await freshGetContext(config, mockPrisma, null)
|
|
3126
|
-
await context.db.user.findMany({ select: { name: true } })
|
|
3127
|
-
await context.db.user.findMany({ select: { name: true } })
|
|
3128
|
-
await context.db.user.findMany({ select: { email: true } })
|
|
3129
|
-
|
|
3130
|
-
expect(warnSpy).toHaveBeenCalledTimes(1)
|
|
3131
|
-
})
|
|
3132
|
-
|
|
3133
|
-
it('does NOT warn for findUnique/findMany using only include or query', async () => {
|
|
3134
|
-
const mockUser = { id: '1', name: 'John' }
|
|
3135
|
-
mockPrisma.user.findFirst.mockResolvedValue(mockUser)
|
|
3136
|
-
mockPrisma.user.findMany.mockResolvedValue([mockUser])
|
|
3137
|
-
|
|
3138
|
-
const context = await freshGetContext(config, mockPrisma, null)
|
|
3139
|
-
await context.db.user.findUnique({ where: { id: '1' }, include: { posts: true } })
|
|
3140
|
-
await context.db.user.findMany({ include: { posts: true } })
|
|
3141
|
-
await context.db.user.findMany()
|
|
3142
|
-
|
|
3143
|
-
expect(warnSpy).not.toHaveBeenCalled()
|
|
3144
|
-
})
|
|
3145
|
-
})
|
|
3146
|
-
})
|