@terreno/api 57.0.0-beta.1 → 57.1.0

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.
Files changed (175) hide show
  1. package/.ai/skills/mongoose-schema-safety/SKILL.md +8 -0
  2. package/dist/actions.d.ts +7 -2
  3. package/dist/actions.js +60 -25
  4. package/dist/adminModelRouter.d.ts +14 -0
  5. package/dist/adminModelRouter.js +199 -0
  6. package/dist/adminTypes.d.ts +156 -0
  7. package/dist/adminTypes.js +5 -0
  8. package/dist/api.d.ts +44 -3
  9. package/dist/api.js +74 -26
  10. package/dist/auth.d.ts +18 -1
  11. package/dist/auth.js +122 -20
  12. package/dist/consentApp.d.ts +2 -0
  13. package/dist/consentApp.js +98 -0
  14. package/dist/index.d.ts +22 -1
  15. package/dist/index.js +51 -7
  16. package/dist/mcp/auth.d.ts +11 -0
  17. package/dist/mcp/auth.js +133 -0
  18. package/dist/mcp/createMCPRequest.d.ts +16 -0
  19. package/dist/mcp/createMCPRequest.js +26 -0
  20. package/dist/mcp/handlers.d.ts +8 -0
  21. package/dist/mcp/handlers.js +793 -0
  22. package/dist/mcp/query.d.ts +32 -0
  23. package/dist/mcp/query.js +232 -0
  24. package/dist/mcp/registry.d.ts +21 -0
  25. package/dist/mcp/registry.js +54 -0
  26. package/dist/mcp/schemaGenerator.d.ts +14 -0
  27. package/dist/mcp/schemaGenerator.js +379 -0
  28. package/dist/mcp/server.d.ts +8 -0
  29. package/dist/mcp/server.js +133 -0
  30. package/dist/mcp/toolGenerator.d.ts +14 -0
  31. package/dist/mcp/toolGenerator.js +246 -0
  32. package/dist/mcp/types.d.ts +85 -0
  33. package/dist/mcp/types.js +2 -0
  34. package/dist/openApi.d.ts +1 -1
  35. package/dist/openApi.js +26 -8
  36. package/dist/permissions.js +15 -15
  37. package/dist/rbac/access.d.ts +3 -0
  38. package/dist/rbac/access.js +294 -0
  39. package/dist/rbac/auditModel.d.ts +31 -0
  40. package/dist/rbac/auditModel.js +112 -0
  41. package/dist/rbac/backfillAdmins.d.ts +18 -0
  42. package/dist/rbac/backfillAdmins.js +139 -0
  43. package/dist/rbac/fieldViews.d.ts +3 -0
  44. package/dist/rbac/fieldViews.js +201 -0
  45. package/dist/rbac/middleware.d.ts +21 -0
  46. package/dist/rbac/middleware.js +108 -0
  47. package/dist/rbac/modelRouterAccess.d.ts +42 -0
  48. package/dist/rbac/modelRouterAccess.js +388 -0
  49. package/dist/rbac/permissionUtils.d.ts +8 -0
  50. package/dist/rbac/permissionUtils.js +209 -0
  51. package/dist/rbac/realtimeAccess.d.ts +18 -0
  52. package/dist/rbac/realtimeAccess.js +173 -0
  53. package/dist/rbac/resolve.d.ts +28 -0
  54. package/dist/rbac/resolve.js +387 -0
  55. package/dist/rbac/roleManager.d.ts +20 -0
  56. package/dist/rbac/roleManager.js +741 -0
  57. package/dist/rbac/roleModel.d.ts +42 -0
  58. package/dist/rbac/roleModel.js +267 -0
  59. package/dist/rbac/routes.d.ts +9 -0
  60. package/dist/rbac/routes.js +211 -0
  61. package/dist/rbac/scopes.d.ts +15 -0
  62. package/dist/rbac/scopes.js +155 -0
  63. package/dist/rbac/statements.d.ts +20 -0
  64. package/dist/rbac/statements.js +115 -0
  65. package/dist/rbac/types.d.ts +202 -0
  66. package/dist/rbac/types.js +2 -0
  67. package/dist/rbac/userPlugin.d.ts +12 -0
  68. package/dist/rbac/userPlugin.js +41 -0
  69. package/dist/realtime/changeStreamWatcher.d.ts +1 -1
  70. package/dist/realtime/changeStreamWatcher.js +48 -39
  71. package/dist/realtime/realtimeApp.js +4 -8
  72. package/dist/realtime/registry.d.ts +5 -0
  73. package/dist/realtime/registry.js +13 -1
  74. package/dist/scrubAdminFields.d.ts +17 -0
  75. package/dist/scrubAdminFields.js +192 -0
  76. package/dist/sync/executors.d.ts +1 -1
  77. package/dist/sync/executors.js +63 -40
  78. package/dist/sync/loadHarness.js +4 -5
  79. package/dist/sync/registry.d.ts +5 -0
  80. package/dist/sync/registry.js +13 -1
  81. package/dist/sync/routes.js +41 -36
  82. package/dist/sync/serialize.js +5 -6
  83. package/dist/terrenoApp.d.ts +18 -0
  84. package/dist/terrenoApp.js +46 -3
  85. package/dist/terrenoPlugin.d.ts +8 -1
  86. package/dist/tests/createTestData.js +8 -0
  87. package/dist/tests/models.d.ts +1 -0
  88. package/dist/tests/models.js +5 -0
  89. package/dist/tests/testHelper.js +1 -2
  90. package/dist/transformers.js +16 -1
  91. package/package.json +16 -12
  92. package/src/__snapshots__/openApi.test.ts.snap +192 -0
  93. package/src/actions.test.ts +194 -1
  94. package/src/actions.ts +65 -8
  95. package/src/adminModelRouter.ts +154 -0
  96. package/src/adminTypes.ts +183 -0
  97. package/src/api.test.ts +66 -1
  98. package/src/api.ts +131 -12
  99. package/src/auth.test.ts +135 -1
  100. package/src/auth.ts +81 -3
  101. package/src/consentApp.test.ts +24 -0
  102. package/src/consentApp.ts +100 -0
  103. package/src/index.ts +56 -0
  104. package/src/mcp/auth.test.ts +197 -0
  105. package/src/mcp/auth.ts +100 -0
  106. package/src/mcp/createMCPRequest.test.ts +47 -0
  107. package/src/mcp/createMCPRequest.ts +32 -0
  108. package/src/mcp/customTools.test.ts +54 -0
  109. package/src/mcp/handlers.ts +639 -0
  110. package/src/mcp/integration.test.ts +1493 -0
  111. package/src/mcp/query.test.ts +172 -0
  112. package/src/mcp/query.ts +179 -0
  113. package/src/mcp/registry.ts +70 -0
  114. package/src/mcp/schemaGenerator.test.ts +182 -0
  115. package/src/mcp/schemaGenerator.ts +354 -0
  116. package/src/mcp/server.test.ts +226 -0
  117. package/src/mcp/server.ts +86 -0
  118. package/src/mcp/toolGenerator.test.ts +284 -0
  119. package/src/mcp/toolGenerator.ts +199 -0
  120. package/src/mcp/types.ts +95 -0
  121. package/src/openApi.test.ts +107 -0
  122. package/src/openApi.ts +32 -1
  123. package/src/permissions.ts +3 -2
  124. package/src/rbac/access.test.ts +357 -0
  125. package/src/rbac/access.ts +201 -0
  126. package/src/rbac/auditModel.ts +103 -0
  127. package/src/rbac/backfillAdmins.ts +54 -0
  128. package/src/rbac/fieldViews.test.ts +39 -0
  129. package/src/rbac/fieldViews.ts +122 -0
  130. package/src/rbac/middleware.ts +55 -0
  131. package/src/rbac/modelRouterAccess.test.ts +336 -0
  132. package/src/rbac/modelRouterAccess.ts +331 -0
  133. package/src/rbac/permissionUtils.test.ts +112 -0
  134. package/src/rbac/permissionUtils.ts +83 -0
  135. package/src/rbac/realtimeAccess.test.ts +242 -0
  136. package/src/rbac/realtimeAccess.ts +138 -0
  137. package/src/rbac/resolve.test.ts +358 -0
  138. package/src/rbac/resolve.ts +285 -0
  139. package/src/rbac/roleManager.test.ts +915 -0
  140. package/src/rbac/roleManager.ts +440 -0
  141. package/src/rbac/roleModel.test.ts +114 -0
  142. package/src/rbac/roleModel.ts +196 -0
  143. package/src/rbac/routes.ts +170 -0
  144. package/src/rbac/scopes.test.ts +105 -0
  145. package/src/rbac/scopes.ts +81 -0
  146. package/src/rbac/statements.test.ts +65 -0
  147. package/src/rbac/statements.ts +62 -0
  148. package/src/rbac/types.ts +198 -0
  149. package/src/rbac/userPlugin.test.ts +55 -0
  150. package/src/rbac/userPlugin.ts +25 -0
  151. package/src/realtime/changeStreamWatcher.ts +20 -15
  152. package/src/realtime/realtime.test.ts +25 -1
  153. package/src/realtime/realtimeApp.ts +2 -5
  154. package/src/realtime/registry.ts +15 -0
  155. package/src/scrubAdminFields.test.ts +113 -0
  156. package/src/scrubAdminFields.ts +158 -0
  157. package/src/sync/executors.ts +31 -1
  158. package/src/sync/loadHarness.ts +8 -7
  159. package/src/sync/models.test.ts +262 -0
  160. package/src/sync/registry.ts +15 -0
  161. package/src/sync/routes.ts +25 -11
  162. package/src/sync/scripts/compactTombstones.test.ts +4 -6
  163. package/src/sync/serialize.ts +17 -9
  164. package/src/sync/sync.test.ts +52 -0
  165. package/src/sync/syncRoutes.test.ts +5 -2
  166. package/src/sync/syncSocket.test.ts +28 -0
  167. package/src/terrenoApp.test.ts +100 -0
  168. package/src/terrenoApp.ts +65 -3
  169. package/src/terrenoPlugin.ts +10 -1
  170. package/src/tests/bunSetup.ts +2 -0
  171. package/src/tests/createTestData.ts +5 -1
  172. package/src/tests/models.ts +6 -0
  173. package/src/tests/testHelper.ts +1 -3
  174. package/src/transformers.test.ts +12 -0
  175. package/src/transformers.ts +7 -1
@@ -244,6 +244,14 @@ exports[`openApi gets the openapi.json 1`] = `
244
244
  "description": "The user's display name",
245
245
  "type": "string",
246
246
  },
247
+ "organizationIds": {
248
+ "default": [],
249
+ "description": "Organization memberships",
250
+ "items": {
251
+ "type": "string",
252
+ },
253
+ "type": "array",
254
+ },
247
255
  "salt": {
248
256
  "type": "string",
249
257
  },
@@ -350,6 +358,14 @@ exports[`openApi gets the openapi.json 1`] = `
350
358
  "description": "The user's display name",
351
359
  "type": "string",
352
360
  },
361
+ "organizationIds": {
362
+ "default": [],
363
+ "description": "Organization memberships",
364
+ "items": {
365
+ "type": "string",
366
+ },
367
+ "type": "array",
368
+ },
353
369
  "salt": {
354
370
  "type": "string",
355
371
  },
@@ -563,6 +579,14 @@ exports[`openApi gets the openapi.json 1`] = `
563
579
  "description": "The user's display name",
564
580
  "type": "string",
565
581
  },
582
+ "organizationIds": {
583
+ "default": [],
584
+ "description": "Organization memberships",
585
+ "items": {
586
+ "type": "string",
587
+ },
588
+ "type": "array",
589
+ },
566
590
  "salt": {
567
591
  "type": "string",
568
592
  },
@@ -669,6 +693,14 @@ exports[`openApi gets the openapi.json 1`] = `
669
693
  "description": "The user's display name",
670
694
  "type": "string",
671
695
  },
696
+ "organizationIds": {
697
+ "default": [],
698
+ "description": "Organization memberships",
699
+ "items": {
700
+ "type": "string",
701
+ },
702
+ "type": "array",
703
+ },
672
704
  "salt": {
673
705
  "type": "string",
674
706
  },
@@ -811,6 +843,14 @@ exports[`openApi gets the openapi.json 1`] = `
811
843
  "description": "The user's display name",
812
844
  "type": "string",
813
845
  },
846
+ "organizationIds": {
847
+ "default": [],
848
+ "description": "Organization memberships",
849
+ "items": {
850
+ "type": "string",
851
+ },
852
+ "type": "array",
853
+ },
814
854
  "salt": {
815
855
  "type": "string",
816
856
  },
@@ -917,6 +957,14 @@ exports[`openApi gets the openapi.json 1`] = `
917
957
  "description": "The user's display name",
918
958
  "type": "string",
919
959
  },
960
+ "organizationIds": {
961
+ "default": [],
962
+ "description": "Organization memberships",
963
+ "items": {
964
+ "type": "string",
965
+ },
966
+ "type": "array",
967
+ },
920
968
  "salt": {
921
969
  "type": "string",
922
970
  },
@@ -1189,6 +1237,14 @@ exports[`openApi gets the openapi.json 1`] = `
1189
1237
  "description": "The user's display name",
1190
1238
  "type": "string",
1191
1239
  },
1240
+ "organizationIds": {
1241
+ "default": [],
1242
+ "description": "Organization memberships",
1243
+ "items": {
1244
+ "type": "string",
1245
+ },
1246
+ "type": "array",
1247
+ },
1192
1248
  "salt": {
1193
1249
  "type": "string",
1194
1250
  },
@@ -1295,6 +1351,14 @@ exports[`openApi gets the openapi.json 1`] = `
1295
1351
  "description": "The user's display name",
1296
1352
  "type": "string",
1297
1353
  },
1354
+ "organizationIds": {
1355
+ "default": [],
1356
+ "description": "Organization memberships",
1357
+ "items": {
1358
+ "type": "string",
1359
+ },
1360
+ "type": "array",
1361
+ },
1298
1362
  "salt": {
1299
1363
  "type": "string",
1300
1364
  },
@@ -1501,6 +1565,14 @@ exports[`openApi gets the openapi.json 1`] = `
1501
1565
  "description": "The user's display name",
1502
1566
  "type": "string",
1503
1567
  },
1568
+ "organizationIds": {
1569
+ "default": [],
1570
+ "description": "Organization memberships",
1571
+ "items": {
1572
+ "type": "string",
1573
+ },
1574
+ "type": "array",
1575
+ },
1504
1576
  "salt": {
1505
1577
  "type": "string",
1506
1578
  },
@@ -1607,6 +1679,14 @@ exports[`openApi gets the openapi.json 1`] = `
1607
1679
  "description": "The user's display name",
1608
1680
  "type": "string",
1609
1681
  },
1682
+ "organizationIds": {
1683
+ "default": [],
1684
+ "description": "Organization memberships",
1685
+ "items": {
1686
+ "type": "string",
1687
+ },
1688
+ "type": "array",
1689
+ },
1610
1690
  "salt": {
1611
1691
  "type": "string",
1612
1692
  },
@@ -1749,6 +1829,14 @@ exports[`openApi gets the openapi.json 1`] = `
1749
1829
  "description": "The user's display name",
1750
1830
  "type": "string",
1751
1831
  },
1832
+ "organizationIds": {
1833
+ "default": [],
1834
+ "description": "Organization memberships",
1835
+ "items": {
1836
+ "type": "string",
1837
+ },
1838
+ "type": "array",
1839
+ },
1752
1840
  "salt": {
1753
1841
  "type": "string",
1754
1842
  },
@@ -1855,6 +1943,14 @@ exports[`openApi gets the openapi.json 1`] = `
1855
1943
  "description": "The user's display name",
1856
1944
  "type": "string",
1857
1945
  },
1946
+ "organizationIds": {
1947
+ "default": [],
1948
+ "description": "Organization memberships",
1949
+ "items": {
1950
+ "type": "string",
1951
+ },
1952
+ "type": "array",
1953
+ },
1858
1954
  "salt": {
1859
1955
  "type": "string",
1860
1956
  },
@@ -2204,6 +2300,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
2204
2300
  "description": "The user's display name",
2205
2301
  "type": "string",
2206
2302
  },
2303
+ "organizationIds": {
2304
+ "default": [],
2305
+ "description": "Organization memberships",
2306
+ "items": {
2307
+ "type": "string",
2308
+ },
2309
+ "type": "array",
2310
+ },
2207
2311
  "salt": {
2208
2312
  "type": "string",
2209
2313
  },
@@ -2310,6 +2414,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
2310
2414
  "description": "The user's display name",
2311
2415
  "type": "string",
2312
2416
  },
2417
+ "organizationIds": {
2418
+ "default": [],
2419
+ "description": "Organization memberships",
2420
+ "items": {
2421
+ "type": "string",
2422
+ },
2423
+ "type": "array",
2424
+ },
2313
2425
  "salt": {
2314
2426
  "type": "string",
2315
2427
  },
@@ -2523,6 +2635,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
2523
2635
  "description": "The user's display name",
2524
2636
  "type": "string",
2525
2637
  },
2638
+ "organizationIds": {
2639
+ "default": [],
2640
+ "description": "Organization memberships",
2641
+ "items": {
2642
+ "type": "string",
2643
+ },
2644
+ "type": "array",
2645
+ },
2526
2646
  "salt": {
2527
2647
  "type": "string",
2528
2648
  },
@@ -2629,6 +2749,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
2629
2749
  "description": "The user's display name",
2630
2750
  "type": "string",
2631
2751
  },
2752
+ "organizationIds": {
2753
+ "default": [],
2754
+ "description": "Organization memberships",
2755
+ "items": {
2756
+ "type": "string",
2757
+ },
2758
+ "type": "array",
2759
+ },
2632
2760
  "salt": {
2633
2761
  "type": "string",
2634
2762
  },
@@ -2771,6 +2899,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
2771
2899
  "description": "The user's display name",
2772
2900
  "type": "string",
2773
2901
  },
2902
+ "organizationIds": {
2903
+ "default": [],
2904
+ "description": "Organization memberships",
2905
+ "items": {
2906
+ "type": "string",
2907
+ },
2908
+ "type": "array",
2909
+ },
2774
2910
  "salt": {
2775
2911
  "type": "string",
2776
2912
  },
@@ -2877,6 +3013,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
2877
3013
  "description": "The user's display name",
2878
3014
  "type": "string",
2879
3015
  },
3016
+ "organizationIds": {
3017
+ "default": [],
3018
+ "description": "Organization memberships",
3019
+ "items": {
3020
+ "type": "string",
3021
+ },
3022
+ "type": "array",
3023
+ },
2880
3024
  "salt": {
2881
3025
  "type": "string",
2882
3026
  },
@@ -3149,6 +3293,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
3149
3293
  "description": "The user's display name",
3150
3294
  "type": "string",
3151
3295
  },
3296
+ "organizationIds": {
3297
+ "default": [],
3298
+ "description": "Organization memberships",
3299
+ "items": {
3300
+ "type": "string",
3301
+ },
3302
+ "type": "array",
3303
+ },
3152
3304
  "salt": {
3153
3305
  "type": "string",
3154
3306
  },
@@ -3255,6 +3407,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
3255
3407
  "description": "The user's display name",
3256
3408
  "type": "string",
3257
3409
  },
3410
+ "organizationIds": {
3411
+ "default": [],
3412
+ "description": "Organization memberships",
3413
+ "items": {
3414
+ "type": "string",
3415
+ },
3416
+ "type": "array",
3417
+ },
3258
3418
  "salt": {
3259
3419
  "type": "string",
3260
3420
  },
@@ -3461,6 +3621,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
3461
3621
  "description": "The user's display name",
3462
3622
  "type": "string",
3463
3623
  },
3624
+ "organizationIds": {
3625
+ "default": [],
3626
+ "description": "Organization memberships",
3627
+ "items": {
3628
+ "type": "string",
3629
+ },
3630
+ "type": "array",
3631
+ },
3464
3632
  "salt": {
3465
3633
  "type": "string",
3466
3634
  },
@@ -3567,6 +3735,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
3567
3735
  "description": "The user's display name",
3568
3736
  "type": "string",
3569
3737
  },
3738
+ "organizationIds": {
3739
+ "default": [],
3740
+ "description": "Organization memberships",
3741
+ "items": {
3742
+ "type": "string",
3743
+ },
3744
+ "type": "array",
3745
+ },
3570
3746
  "salt": {
3571
3747
  "type": "string",
3572
3748
  },
@@ -3709,6 +3885,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
3709
3885
  "description": "The user's display name",
3710
3886
  "type": "string",
3711
3887
  },
3888
+ "organizationIds": {
3889
+ "default": [],
3890
+ "description": "Organization memberships",
3891
+ "items": {
3892
+ "type": "string",
3893
+ },
3894
+ "type": "array",
3895
+ },
3712
3896
  "salt": {
3713
3897
  "type": "string",
3714
3898
  },
@@ -3815,6 +3999,14 @@ exports[`openApi gets the openapi.json with custom endpoint 1`] = `
3815
3999
  "description": "The user's display name",
3816
4000
  "type": "string",
3817
4001
  },
4002
+ "organizationIds": {
4003
+ "default": [],
4004
+ "description": "Organization memberships",
4005
+ "items": {
4006
+ "type": "string",
4007
+ },
4008
+ "type": "array",
4009
+ },
3818
4010
  "salt": {
3819
4011
  "type": "string",
3820
4012
  },
@@ -1,6 +1,7 @@
1
1
  import {beforeEach, describe, expect, it} from "bun:test";
2
2
  import type express from "express";
3
- import {type HydratedDocument, type Model, model, Schema} from "mongoose";
3
+ import type {Request} from "express";
4
+ import mongoose, {type HydratedDocument, type Model, model, Schema} from "mongoose";
4
5
  import supertest from "supertest";
5
6
  import type TestAgent from "supertest/lib/agent";
6
7
  import {
@@ -9,12 +10,15 @@ import {
9
10
  createActionOpenApiMiddleware,
10
11
  defineCollectionAction,
11
12
  defineInstanceAction,
13
+ runActionPermissions,
12
14
  } from "./actions";
13
15
  import {modelRouter, type OpenApiMiddleware} from "./api";
14
16
  import {addAuthRoutes, setupAuth, type UserModel as UserMongooseModel} from "./auth";
15
17
  import {apiUnauthorizedMiddleware} from "./errors";
16
18
  import {Permissions} from "./permissions";
17
19
  import {type IsDeleted, isDeletedPlugin} from "./plugins";
20
+ import {createAccess} from "./rbac/access";
21
+ import {terrenoStatements} from "./rbac/statements";
18
22
  import {
19
23
  authAsUser,
20
24
  type Food,
@@ -187,6 +191,41 @@ describe("modelRouter actions", () => {
187
191
  });
188
192
 
189
193
  describe("routing and permissions", () => {
194
+ it("inherits router access.resource on instance POST when action.access is omitted", async () => {
195
+ const access = createAccess({
196
+ connection: mongoose.connection,
197
+ defaultRoles: [
198
+ {
199
+ displayName: "Reader",
200
+ name: "reader",
201
+ permissions: {food: ["read", "list"]},
202
+ },
203
+ ],
204
+ statements: {
205
+ ...terrenoStatements,
206
+ food: ["create", "read", "update", "delete", "list"],
207
+ },
208
+ });
209
+ await access.roles.seedDefaults();
210
+ await UserModel.updateOne({_id: notAdmin._id}, {$set: {roles: ["reader"]}});
211
+
212
+ mountFoodRouter({
213
+ access: {resource: "food"},
214
+ accessControl: access,
215
+ instanceActions: {
216
+ mark: {
217
+ handler: async () => ({ok: true}),
218
+ method: "POST",
219
+ permissions: [Permissions.IsAny],
220
+ },
221
+ },
222
+ permissions: allPermissions,
223
+ });
224
+ const agent = await authAsUser(app, "notAdmin");
225
+ const res = await agent.post(`/food/${spinach._id}/mark`).send({}).expect(405);
226
+ expect(res.body.title).toBe("Access denied");
227
+ });
228
+
190
229
  it("allows empty permissions array and returns 405 at runtime", async () => {
191
230
  mountFoodRouter({
192
231
  collectionActions: {
@@ -794,4 +833,158 @@ describe("modelRouter actions", () => {
794
833
  expect(mock.captured?.requestBody).toBeDefined();
795
834
  });
796
835
  });
836
+
837
+ describe("runActionPermissions", () => {
838
+ it("combines legacy action.permissions with RBAC access checks", async () => {
839
+ await setupDb();
840
+ const access = createAccess({
841
+ connection: mongoose.connection,
842
+ defaultRoles: [
843
+ {
844
+ displayName: "Reader",
845
+ name: "reader",
846
+ permissions: {todo: ["read"]},
847
+ },
848
+ ],
849
+ statements: {
850
+ ...terrenoStatements,
851
+ todo: ["create", "read", "update", "delete", "list"],
852
+ },
853
+ });
854
+ await access.roles.seedDefaults();
855
+
856
+ const id = new mongoose.Types.ObjectId();
857
+ const reader = {
858
+ _id: id as unknown as User["_id"],
859
+ admin: false,
860
+ id: id.toString(),
861
+ roles: ["reader"],
862
+ };
863
+ const req = {params: {}, user: reader} as unknown as Request;
864
+
865
+ await expect(
866
+ runActionPermissions(
867
+ {
868
+ access: {action: "create", resource: "todo"},
869
+ method: "POST",
870
+ permissions: [Permissions.IsAny],
871
+ },
872
+ "collection",
873
+ FoodModel,
874
+ req,
875
+ undefined,
876
+ access
877
+ )
878
+ ).rejects.toMatchObject({status: 405, title: "Access denied"});
879
+ });
880
+
881
+ it("inherits router access.resource when the action omits access", async () => {
882
+ await setupDb();
883
+ const access = createAccess({
884
+ connection: mongoose.connection,
885
+ defaultRoles: [
886
+ {
887
+ displayName: "Reader",
888
+ name: "reader",
889
+ permissions: {todo: ["read", "list"]},
890
+ },
891
+ ],
892
+ statements: {
893
+ ...terrenoStatements,
894
+ todo: ["create", "read", "update", "delete", "list"],
895
+ },
896
+ });
897
+ await access.roles.seedDefaults();
898
+
899
+ const id = new mongoose.Types.ObjectId();
900
+ const reader = {
901
+ _id: id as unknown as User["_id"],
902
+ admin: false,
903
+ id: id.toString(),
904
+ roles: ["reader"],
905
+ };
906
+ const req = {params: {}, user: reader} as unknown as Request;
907
+
908
+ await expect(
909
+ runActionPermissions(
910
+ {
911
+ method: "POST",
912
+ permissions: [Permissions.IsAny],
913
+ },
914
+ "instance",
915
+ FoodModel,
916
+ req,
917
+ undefined,
918
+ access,
919
+ {resource: "todo"}
920
+ )
921
+ ).rejects.toMatchObject({status: 405, title: "Access denied"});
922
+ });
923
+
924
+ it("keeps empty action permissions disabled when inheriting router access", async () => {
925
+ await setupDb();
926
+ const access = createAccess({
927
+ connection: mongoose.connection,
928
+ statements: {
929
+ ...terrenoStatements,
930
+ todo: ["create", "read", "update", "delete", "list"],
931
+ },
932
+ });
933
+ await access.roles.seedDefaults();
934
+
935
+ const id = new mongoose.Types.ObjectId();
936
+ const superadmin = {
937
+ _id: id as unknown as User["_id"],
938
+ admin: false,
939
+ id: id.toString(),
940
+ roles: ["superadmin"],
941
+ };
942
+ const req = {params: {}, user: superadmin} as unknown as Request;
943
+
944
+ await expect(
945
+ runActionPermissions(
946
+ {method: "POST", permissions: []},
947
+ "instance",
948
+ FoodModel,
949
+ req,
950
+ undefined,
951
+ access,
952
+ {resource: "todo"}
953
+ )
954
+ ).rejects.toMatchObject({status: 405, title: "Access denied"});
955
+ });
956
+
957
+ it("keeps actions disabled when the inherited CRUD action maps to null", async () => {
958
+ await setupDb();
959
+ const access = createAccess({
960
+ connection: mongoose.connection,
961
+ statements: {
962
+ ...terrenoStatements,
963
+ todo: ["create", "read", "update", "delete", "list"],
964
+ },
965
+ });
966
+ await access.roles.seedDefaults();
967
+
968
+ const id = new mongoose.Types.ObjectId();
969
+ const superadmin = {
970
+ _id: id as unknown as User["_id"],
971
+ admin: false,
972
+ id: id.toString(),
973
+ roles: ["superadmin"],
974
+ };
975
+ const req = {params: {}, user: superadmin} as unknown as Request;
976
+
977
+ await expect(
978
+ runActionPermissions(
979
+ {method: "POST", permissions: [Permissions.IsAny]},
980
+ "instance",
981
+ FoodModel,
982
+ req,
983
+ undefined,
984
+ access,
985
+ {actions: {update: null}, resource: "todo"}
986
+ )
987
+ ).rejects.toMatchObject({status: 405, title: "Access denied"});
988
+ });
989
+ });
797
990
  });