@rangka/core 0.1.0 → 0.1.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.
Files changed (197) hide show
  1. package/package.json +6 -2
  2. package/.claude/skills/extend-core/SKILL.md +0 -133
  3. package/.turbo/turbo-build.log +0 -4
  4. package/CHANGELOG.md +0 -18
  5. package/CLAUDE.md +0 -180
  6. package/src/__tests__/coerce.test.ts +0 -154
  7. package/src/__tests__/context.test.ts +0 -111
  8. package/src/__tests__/helpers.ts +0 -21
  9. package/src/__tests__/index.test.ts +0 -7
  10. package/src/__tests__/widgets.test.ts +0 -197
  11. package/src/api/__tests__/handlers.test.ts +0 -389
  12. package/src/api/__tests__/include-resolver.test.ts +0 -393
  13. package/src/api/__tests__/middleware.test.ts +0 -100
  14. package/src/api/__tests__/openapi-schema.test.ts +0 -210
  15. package/src/api/__tests__/query-parser.test.ts +0 -291
  16. package/src/api/__tests__/route-generator.test.ts +0 -137
  17. package/src/api/__tests__/server.test.ts +0 -73
  18. package/src/api/__tests__/swagger.test.ts +0 -166
  19. package/src/api/handlers.ts +0 -274
  20. package/src/api/include-resolver.ts +0 -27
  21. package/src/api/index.ts +0 -4
  22. package/src/api/meta-handler.ts +0 -254
  23. package/src/api/openapi-schema.ts +0 -99
  24. package/src/api/query-parser.ts +0 -315
  25. package/src/api/route-generator.ts +0 -448
  26. package/src/api/server.ts +0 -147
  27. package/src/api/types.ts +0 -16
  28. package/src/audit/__tests__/audit.test.ts +0 -144
  29. package/src/audit/index.ts +0 -3
  30. package/src/audit/record.ts +0 -69
  31. package/src/audit/tables.ts +0 -48
  32. package/src/audit/types.ts +0 -26
  33. package/src/auth/__tests__/core-module.test.ts +0 -54
  34. package/src/auth/__tests__/debug.test.ts +0 -47
  35. package/src/auth/__tests__/field-permissions.test.ts +0 -245
  36. package/src/auth/__tests__/integration.test.ts +0 -208
  37. package/src/auth/__tests__/meta-boot.test.ts +0 -538
  38. package/src/auth/__tests__/model-permissions.test.ts +0 -205
  39. package/src/auth/__tests__/password.test.ts +0 -29
  40. package/src/auth/__tests__/permission-registry.test.ts +0 -313
  41. package/src/auth/__tests__/scope-hook.test.ts +0 -509
  42. package/src/auth/__tests__/scope-registry.test.ts +0 -297
  43. package/src/auth/__tests__/scopes.test.ts +0 -66
  44. package/src/auth/__tests__/session.test.ts +0 -214
  45. package/src/auth/core-models.ts +0 -52
  46. package/src/auth/core-module.ts +0 -59
  47. package/src/auth/debug.ts +0 -157
  48. package/src/auth/field-permissions.ts +0 -116
  49. package/src/auth/index.ts +0 -37
  50. package/src/auth/model-permissions.ts +0 -59
  51. package/src/auth/password.ts +0 -22
  52. package/src/auth/permission-registry.ts +0 -171
  53. package/src/auth/scope-filters.ts +0 -11
  54. package/src/auth/scope-registry.ts +0 -121
  55. package/src/auth/scopes.ts +0 -146
  56. package/src/auth/seed.ts +0 -44
  57. package/src/auth/session.ts +0 -178
  58. package/src/auth/types.ts +0 -50
  59. package/src/boot/__tests__/page-scanning.test.ts +0 -170
  60. package/src/boot/__tests__/page-utils.test.ts +0 -225
  61. package/src/boot/__tests__/project-scanner.test.ts +0 -88
  62. package/src/boot/dependency-sort.ts +0 -82
  63. package/src/boot/discovery.ts +0 -85
  64. package/src/boot/index.ts +0 -457
  65. package/src/boot/page-utils.ts +0 -110
  66. package/src/boot/project-scanner.ts +0 -397
  67. package/src/boot/schema-loader.ts +0 -26
  68. package/src/boot/schema-merger.ts +0 -125
  69. package/src/boot/traits.ts +0 -25
  70. package/src/boot/types.ts +0 -73
  71. package/src/context.ts +0 -105
  72. package/src/db/__tests__/cascade-delete.test.ts +0 -182
  73. package/src/db/__tests__/desired-state.test.ts +0 -136
  74. package/src/db/__tests__/diff-engine.test.ts +0 -635
  75. package/src/db/__tests__/field-mapper.test.ts +0 -355
  76. package/src/db/__tests__/introspect.test.ts +0 -70
  77. package/src/db/__tests__/search-filter.test.ts +0 -45
  78. package/src/db/__tests__/sequence.test.ts +0 -221
  79. package/src/db/auto-sync.ts +0 -133
  80. package/src/db/client.ts +0 -147
  81. package/src/db/desired-state.ts +0 -98
  82. package/src/db/diff-engine.ts +0 -305
  83. package/src/db/field-mapper.ts +0 -504
  84. package/src/db/filter-applier.ts +0 -89
  85. package/src/db/include-resolver.ts +0 -40
  86. package/src/db/index.ts +0 -23
  87. package/src/db/introspect.ts +0 -265
  88. package/src/db/model-include-resolver.ts +0 -327
  89. package/src/db/model-ops.ts +0 -281
  90. package/src/db/scope-enforcer.ts +0 -37
  91. package/src/db/types.ts +0 -98
  92. package/src/errors.ts +0 -41
  93. package/src/events/__tests__/bus.test.ts +0 -105
  94. package/src/events/bus.ts +0 -89
  95. package/src/events/index.ts +0 -2
  96. package/src/events/types.ts +0 -9
  97. package/src/external-model/__tests__/computed-fields.test.ts +0 -106
  98. package/src/external-model/__tests__/field-mapper.test.ts +0 -160
  99. package/src/external-model/__tests__/in-memory-ops.test.ts +0 -247
  100. package/src/external-model/__tests__/mutation-executor.test.ts +0 -160
  101. package/src/external-model/__tests__/query-executor.test.ts +0 -284
  102. package/src/external-model/__tests__/schema-converter.test.ts +0 -174
  103. package/src/external-model/computed-fields.ts +0 -15
  104. package/src/external-model/define.ts +0 -5
  105. package/src/external-model/external-model-ops.ts +0 -108
  106. package/src/external-model/field-mapper.ts +0 -66
  107. package/src/external-model/in-memory-ops.ts +0 -107
  108. package/src/external-model/index.ts +0 -7
  109. package/src/external-model/mutation-executor.ts +0 -71
  110. package/src/external-model/query-executor.ts +0 -100
  111. package/src/external-model/schema-converter.ts +0 -53
  112. package/src/external-model/types.ts +0 -32
  113. package/src/fixtures/__tests__/fixtures.test.ts +0 -203
  114. package/src/fixtures/index.ts +0 -10
  115. package/src/fixtures/loader.ts +0 -196
  116. package/src/fixtures/registry.ts +0 -125
  117. package/src/fixtures/types.ts +0 -33
  118. package/src/helpers/assert-ownership.ts +0 -19
  119. package/src/helpers/coerce.ts +0 -28
  120. package/src/helpers/stamping.ts +0 -28
  121. package/src/helpers/validation.ts +0 -14
  122. package/src/hooks/__tests__/context.test.ts +0 -73
  123. package/src/hooks/__tests__/executor.test.ts +0 -433
  124. package/src/hooks/__tests__/middleware.test.ts +0 -224
  125. package/src/hooks/__tests__/registry.test.ts +0 -50
  126. package/src/hooks/context.ts +0 -89
  127. package/src/hooks/errors.ts +0 -11
  128. package/src/hooks/executor.ts +0 -115
  129. package/src/hooks/index.ts +0 -10
  130. package/src/hooks/middleware.ts +0 -220
  131. package/src/hooks/registry.ts +0 -20
  132. package/src/hooks/types.ts +0 -32
  133. package/src/index.ts +0 -172
  134. package/src/jobs/__tests__/enqueue.test.ts +0 -77
  135. package/src/jobs/__tests__/integration.test.ts +0 -71
  136. package/src/jobs/__tests__/registry.test.ts +0 -103
  137. package/src/jobs/__tests__/scheduler.test.ts +0 -92
  138. package/src/jobs/__tests__/worker-execution.test.ts +0 -202
  139. package/src/jobs/__tests__/worker.test.ts +0 -119
  140. package/src/jobs/enqueue.ts +0 -93
  141. package/src/jobs/index.ts +0 -14
  142. package/src/jobs/registry.ts +0 -92
  143. package/src/jobs/scheduler.ts +0 -205
  144. package/src/jobs/tables.ts +0 -132
  145. package/src/jobs/types.ts +0 -62
  146. package/src/jobs/worker.ts +0 -272
  147. package/src/model-api/__tests__/cross-boundary-includes.test.ts +0 -366
  148. package/src/model-api/__tests__/extended-api.test.ts +0 -244
  149. package/src/model-api/__tests__/filter-applier.test.ts +0 -177
  150. package/src/model-api/__tests__/filter-translator.test.ts +0 -186
  151. package/src/model-api/__tests__/include-resolver.test.ts +0 -226
  152. package/src/model-api/__tests__/model-access.test.ts +0 -284
  153. package/src/model-api/__tests__/query-builder.test.ts +0 -224
  154. package/src/model-api/__tests__/scope-enforcer.test.ts +0 -268
  155. package/src/model-api/field-access.ts +0 -28
  156. package/src/model-api/filter-applier.ts +0 -1
  157. package/src/model-api/filter-translator.ts +0 -67
  158. package/src/model-api/include-resolver.ts +0 -2
  159. package/src/model-api/index.ts +0 -86
  160. package/src/model-api/query-builder.ts +0 -155
  161. package/src/model-api/scope-enforcer.ts +0 -3
  162. package/src/model-api/types.ts +0 -139
  163. package/src/plugins/__tests__/adapter-registry.test.ts +0 -92
  164. package/src/plugins/__tests__/lifecycle.test.ts +0 -96
  165. package/src/plugins/__tests__/loader.test.ts +0 -273
  166. package/src/plugins/__tests__/validator.test.ts +0 -275
  167. package/src/plugins/adapter-registry.ts +0 -42
  168. package/src/plugins/define.ts +0 -5
  169. package/src/plugins/index.ts +0 -28
  170. package/src/plugins/lifecycle.ts +0 -27
  171. package/src/plugins/loader.ts +0 -126
  172. package/src/plugins/types.ts +0 -76
  173. package/src/plugins/validator.ts +0 -141
  174. package/src/schema/__tests__/registry-models-by-module.test.ts +0 -58
  175. package/src/schema/registry.ts +0 -93
  176. package/src/schema/relationships.ts +0 -93
  177. package/src/schema/types.ts +0 -43
  178. package/src/services/__tests__/integration.test.ts +0 -63
  179. package/src/services/__tests__/registry.test.ts +0 -175
  180. package/src/services/index.ts +0 -13
  181. package/src/services/registry.ts +0 -156
  182. package/src/services/types.ts +0 -27
  183. package/src/validation/__tests__/field-validator.test.ts +0 -195
  184. package/src/validation/field-validator.ts +0 -113
  185. package/src/validation/index.ts +0 -1
  186. package/src/widgets/index.ts +0 -3
  187. package/src/widgets/slot-validator.ts +0 -87
  188. package/src/widgets/widget-registry.ts +0 -32
  189. package/tests/boot.test.ts +0 -323
  190. package/tests/dependency-sort.test.ts +0 -99
  191. package/tests/discovery.test.ts +0 -126
  192. package/tests/registry.test.ts +0 -216
  193. package/tests/schema-loader.test.ts +0 -52
  194. package/tests/schema-merger.test.ts +0 -180
  195. package/tsconfig.json +0 -9
  196. package/tsconfig.tsbuildinfo +0 -1
  197. package/vitest.config.ts +0 -14
@@ -1,509 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import { createScopeHook, createScopeWriteGuard } from '../scopes.js';
3
- import type { FilterProvider } from '../scopes.js';
4
- import { ScopeRegistry } from '../scope-registry.js';
5
- import { SchemaRegistry } from '../../schema/registry.js';
6
- import type { ResolvedModel } from '../../schema/types.js';
7
- import type { ModuleConfig } from '@rangka/shared';
8
- import type { FastifyRequest, FastifyReply } from 'fastify';
9
- import { AppError } from '../../errors.js';
10
-
11
- function makeModel(overrides: Partial<ResolvedModel> & { qualifiedName: string }): ResolvedModel {
12
- return {
13
- app: overrides.module ?? 'test',
14
- module: overrides.module ?? 'test',
15
- name: overrides.qualifiedName.split('.')[1],
16
- auditLog: false,
17
- traits: [],
18
- fields: [],
19
- indexes: [],
20
- ...overrides,
21
- };
22
- }
23
-
24
- function linkField(name: string, model: string) {
25
- return {
26
- name,
27
- config: { type: 'link' as const, model },
28
- provenance: { source: 'base' as const },
29
- };
30
- }
31
-
32
- function createMockDb(existingIds: string[] = []) {
33
- return {
34
- selectFrom: vi.fn().mockReturnValue({
35
- where: vi.fn().mockReturnValue({
36
- selectAll: vi.fn().mockReturnValue({
37
- executeTakeFirst: vi.fn().mockImplementation(async () => {
38
- return existingIds.length > 0 ? { id: existingIds[0] } : undefined;
39
- }),
40
- }),
41
- }),
42
- }),
43
- } as any;
44
- }
45
-
46
- function createMockRequest(options: {
47
- headers?: Record<string, string>;
48
- method?: string;
49
- body?: Record<string, unknown>;
50
- authContext?: Record<string, unknown>;
51
- }): FastifyRequest {
52
- const req = {
53
- headers: options.headers ?? {},
54
- method: options.method ?? 'GET',
55
- body: options.body,
56
- authContext: options.authContext ?? {
57
- user: {
58
- id: 'user-1',
59
- email: 'test@test.com',
60
- full_name: 'Test',
61
- enabled: true,
62
- password_hash: '',
63
- },
64
- permissions: { models: {}, pages: [], version: 1 },
65
- roles: ['admin'],
66
- },
67
- } as any;
68
- return req;
69
- }
70
-
71
- function createMockReply() {
72
- const reply: any = {
73
- statusCode: 200,
74
- body: null,
75
- status: vi.fn().mockImplementation((code: number) => {
76
- reply.statusCode = code;
77
- return reply;
78
- }),
79
- send: vi.fn().mockImplementation((body: unknown) => {
80
- reply.body = body;
81
- return reply;
82
- }),
83
- };
84
- return reply as FastifyReply & { statusCode: number; body: any };
85
- }
86
-
87
- function buildScopeContext() {
88
- const modules: ModuleConfig[] = [
89
- {
90
- name: 'core',
91
- label: 'Core',
92
- scopes: {
93
- company: { model: 'core.company', default: 'user.default_company', switchable: true },
94
- },
95
- },
96
- ];
97
- const companyModel = makeModel({ qualifiedName: 'core.company', module: 'core' });
98
- const invoiceModel = makeModel({
99
- qualifiedName: 'sales.invoice',
100
- module: 'sales',
101
- scope: 'company',
102
- fields: [linkField('company', 'core.company')],
103
- });
104
- const unscopedModel = makeModel({
105
- qualifiedName: 'sales.customer',
106
- module: 'sales',
107
- fields: [],
108
- });
109
- const schemaRegistry = new SchemaRegistry([companyModel, invoiceModel, unscopedModel]);
110
- const scopeRegistry = new ScopeRegistry(modules, schemaRegistry);
111
- return { scopeRegistry, invoiceModel, unscopedModel, schemaRegistry };
112
- }
113
-
114
- describe('createScopeHook', () => {
115
- it('attaches scope filter from X-Active-Scope header', async () => {
116
- const { scopeRegistry, invoiceModel } = buildScopeContext();
117
- const db = createMockDb(['company-1']);
118
-
119
- const hook = createScopeHook({ model: invoiceModel, scopeRegistry, db });
120
- const request = createMockRequest({
121
- headers: { 'x-active-scope': JSON.stringify({ company: 'company-1' }) },
122
- });
123
- const reply = createMockReply();
124
-
125
- await hook(request, reply);
126
-
127
- const authCtx = (request as any).authContext;
128
- expect(authCtx.scopeFilters).toEqual([
129
- { field: 'company', operator: 'eq', value: 'company-1' },
130
- ]);
131
- });
132
-
133
- it('falls back to user default field when header is missing', async () => {
134
- const { scopeRegistry, invoiceModel } = buildScopeContext();
135
- const db = createMockDb(['company-default']);
136
-
137
- const hook = createScopeHook({ model: invoiceModel, scopeRegistry, db });
138
- const request = createMockRequest({
139
- authContext: {
140
- user: {
141
- id: 'user-1',
142
- email: 'test@test.com',
143
- full_name: 'Test',
144
- enabled: true,
145
- password_hash: '',
146
- default_company: 'company-default',
147
- },
148
- permissions: { models: {}, pages: [], version: 1 },
149
- roles: ['admin'],
150
- },
151
- });
152
- const reply = createMockReply();
153
-
154
- await hook(request, reply);
155
-
156
- const authCtx = (request as any).authContext;
157
- expect(authCtx.scopeFilters).toEqual([
158
- { field: 'company', operator: 'eq', value: 'company-default' },
159
- ]);
160
- });
161
-
162
- it('returns 400 when no scope value available', async () => {
163
- const { scopeRegistry, invoiceModel } = buildScopeContext();
164
- const db = createMockDb([]);
165
-
166
- const hook = createScopeHook({ model: invoiceModel, scopeRegistry, db });
167
- const request = createMockRequest({ headers: {} });
168
- const reply = createMockReply();
169
-
170
- const error = await hook(request, reply).catch((e) => e);
171
-
172
- expect(error).toBeInstanceOf(AppError);
173
- expect(error.statusCode).toBe(400);
174
- expect(error.code).toBe('MISSING_SCOPE');
175
- });
176
-
177
- it('returns 400 when scope value does not exist in DB', async () => {
178
- const { scopeRegistry, invoiceModel } = buildScopeContext();
179
- const db = createMockDb([]);
180
-
181
- const hook = createScopeHook({ model: invoiceModel, scopeRegistry, db });
182
- const request = createMockRequest({
183
- headers: { 'x-active-scope': JSON.stringify({ company: 'nonexistent' }) },
184
- });
185
- const reply = createMockReply();
186
-
187
- const error = await hook(request, reply).catch((e) => e);
188
-
189
- expect(error).toBeInstanceOf(AppError);
190
- expect(error.statusCode).toBe(400);
191
- expect(error.code).toBe('INVALID_SCOPE');
192
- });
193
-
194
- it('skips scope filtering for unscoped models', async () => {
195
- const { scopeRegistry, unscopedModel } = buildScopeContext();
196
- const db = createMockDb([]);
197
-
198
- const hook = createScopeHook({ model: unscopedModel, scopeRegistry, db });
199
- const request = createMockRequest({});
200
- const reply = createMockReply();
201
-
202
- await hook(request, reply);
203
-
204
- const authCtx = (request as any).authContext;
205
- expect(authCtx.scopeFilters).toEqual([]);
206
- });
207
-
208
- it('skips when no auth context', async () => {
209
- const { scopeRegistry, invoiceModel } = buildScopeContext();
210
- const db = createMockDb([]);
211
-
212
- const hook = createScopeHook({ model: invoiceModel, scopeRegistry, db });
213
- const request = createMockRequest({ authContext: {} });
214
- const reply = createMockReply();
215
-
216
- await hook(request, reply);
217
-
218
- expect(reply.status).not.toHaveBeenCalled();
219
- });
220
-
221
- it('header takes precedence over user default', async () => {
222
- const { scopeRegistry, invoiceModel } = buildScopeContext();
223
- const db = createMockDb(['from-header']);
224
-
225
- const hook = createScopeHook({ model: invoiceModel, scopeRegistry, db });
226
- const request = createMockRequest({
227
- headers: { 'x-active-scope': JSON.stringify({ company: 'from-header' }) },
228
- authContext: {
229
- user: {
230
- id: 'user-1',
231
- email: 'test@test.com',
232
- full_name: 'Test',
233
- enabled: true,
234
- password_hash: '',
235
- default_company: 'from-user',
236
- },
237
- permissions: { models: {}, pages: [], version: 1 },
238
- roles: ['admin'],
239
- },
240
- });
241
- const reply = createMockReply();
242
-
243
- await hook(request, reply);
244
-
245
- const authCtx = (request as any).authContext;
246
- expect(authCtx.scopeFilters).toEqual([
247
- { field: 'company', operator: 'eq', value: 'from-header' },
248
- ]);
249
- });
250
-
251
- it('appends filters from filter providers', async () => {
252
- const { scopeRegistry, invoiceModel } = buildScopeContext();
253
- const db = createMockDb(['company-1']);
254
-
255
- const territoryProvider: FilterProvider = (_model, authCtx, _request) => {
256
- const territory = (authCtx.user as any)?.territory;
257
- if (territory) {
258
- return [{ field: 'territory', operator: 'eq', value: territory }];
259
- }
260
- return [];
261
- };
262
-
263
- const hook = createScopeHook({
264
- model: invoiceModel,
265
- scopeRegistry,
266
- db,
267
- filterProviders: [territoryProvider],
268
- });
269
- const request = createMockRequest({
270
- headers: { 'x-active-scope': JSON.stringify({ company: 'company-1' }) },
271
- authContext: {
272
- user: {
273
- id: 'user-1',
274
- email: 'test@test.com',
275
- full_name: 'Test',
276
- enabled: true,
277
- password_hash: '',
278
- territory: 'North',
279
- },
280
- permissions: { models: {}, pages: [], version: 1 },
281
- roles: ['admin'],
282
- },
283
- });
284
- const reply = createMockReply();
285
-
286
- await hook(request, reply);
287
-
288
- const authCtx = (request as any).authContext;
289
- expect(authCtx.scopeFilters).toEqual([
290
- { field: 'company', operator: 'eq', value: 'company-1' },
291
- { field: 'territory', operator: 'eq', value: 'North' },
292
- ]);
293
- });
294
-
295
- it('filter providers run even on unscoped models', async () => {
296
- const { scopeRegistry, unscopedModel } = buildScopeContext();
297
- const db = createMockDb([]);
298
-
299
- const staticProvider: FilterProvider = () => [{ field: 'active', operator: 'eq', value: true }];
300
-
301
- const hook = createScopeHook({
302
- model: unscopedModel,
303
- scopeRegistry,
304
- db,
305
- filterProviders: [staticProvider],
306
- });
307
- const request = createMockRequest({});
308
- const reply = createMockReply();
309
-
310
- await hook(request, reply);
311
-
312
- const authCtx = (request as any).authContext;
313
- expect(authCtx.scopeFilters).toEqual([{ field: 'active', operator: 'eq', value: true }]);
314
- });
315
- });
316
-
317
- describe('createScopeWriteGuard', () => {
318
- it('auto-stamps scope value on POST when body is missing scope field', async () => {
319
- const { scopeRegistry, invoiceModel } = buildScopeContext();
320
- const db = createMockDb([]);
321
- const body: Record<string, unknown> = { total: 100 };
322
-
323
- const guard = createScopeWriteGuard({ model: invoiceModel, scopeRegistry, db });
324
- const request = createMockRequest({
325
- method: 'POST',
326
- body,
327
- authContext: {
328
- user: {
329
- id: 'user-1',
330
- email: 'test@test.com',
331
- full_name: 'Test',
332
- enabled: true,
333
- password_hash: '',
334
- },
335
- permissions: { models: {}, pages: [], version: 1 },
336
- roles: ['admin'],
337
- scopeFilters: [{ field: 'company', operator: 'eq', value: 'company-1' }],
338
- },
339
- });
340
- const reply = createMockReply();
341
-
342
- await guard(request, reply);
343
-
344
- expect(body.company).toBe('company-1');
345
- expect(reply.status).not.toHaveBeenCalled();
346
- });
347
-
348
- it('allows POST when body includes correct scope value', async () => {
349
- const { scopeRegistry, invoiceModel } = buildScopeContext();
350
- const db = createMockDb([]);
351
- const body: Record<string, unknown> = { total: 100, company: 'company-1' };
352
-
353
- const guard = createScopeWriteGuard({ model: invoiceModel, scopeRegistry, db });
354
- const request = createMockRequest({
355
- method: 'POST',
356
- body,
357
- authContext: {
358
- user: {
359
- id: 'user-1',
360
- email: 'test@test.com',
361
- full_name: 'Test',
362
- enabled: true,
363
- password_hash: '',
364
- },
365
- permissions: { models: {}, pages: [], version: 1 },
366
- roles: ['admin'],
367
- scopeFilters: [{ field: 'company', operator: 'eq', value: 'company-1' }],
368
- },
369
- });
370
- const reply = createMockReply();
371
-
372
- await guard(request, reply);
373
-
374
- expect(reply.status).not.toHaveBeenCalled();
375
- });
376
-
377
- it('rejects POST when body has different scope value', async () => {
378
- const { scopeRegistry, invoiceModel } = buildScopeContext();
379
- const db = createMockDb([]);
380
- const body: Record<string, unknown> = { total: 100, company: 'other-company' };
381
-
382
- const guard = createScopeWriteGuard({ model: invoiceModel, scopeRegistry, db });
383
- const request = createMockRequest({
384
- method: 'POST',
385
- body,
386
- authContext: {
387
- user: {
388
- id: 'user-1',
389
- email: 'test@test.com',
390
- full_name: 'Test',
391
- enabled: true,
392
- password_hash: '',
393
- },
394
- permissions: { models: {}, pages: [], version: 1 },
395
- roles: ['admin'],
396
- scopeFilters: [{ field: 'company', operator: 'eq', value: 'company-1' }],
397
- },
398
- });
399
- const reply = createMockReply();
400
-
401
- const error = await guard(request, reply).catch((e) => e);
402
-
403
- expect(error).toBeInstanceOf(AppError);
404
- expect(error.statusCode).toBe(403);
405
- expect(error.code).toBe('SCOPE_VIOLATION');
406
- });
407
-
408
- it('rejects PUT when trying to change scope field', async () => {
409
- const { scopeRegistry, invoiceModel } = buildScopeContext();
410
- const db = createMockDb([]);
411
- const body: Record<string, unknown> = { company: 'other-company' };
412
-
413
- const guard = createScopeWriteGuard({ model: invoiceModel, scopeRegistry, db });
414
- const request = createMockRequest({
415
- method: 'PUT',
416
- body,
417
- authContext: {
418
- user: {
419
- id: 'user-1',
420
- email: 'test@test.com',
421
- full_name: 'Test',
422
- enabled: true,
423
- password_hash: '',
424
- },
425
- permissions: { models: {}, pages: [], version: 1 },
426
- roles: ['admin'],
427
- scopeFilters: [{ field: 'company', operator: 'eq', value: 'company-1' }],
428
- },
429
- });
430
- const reply = createMockReply();
431
-
432
- const error = await guard(request, reply).catch((e) => e);
433
-
434
- expect(error).toBeInstanceOf(AppError);
435
- expect(error.statusCode).toBe(403);
436
- expect(error.code).toBe('SCOPE_VIOLATION');
437
- });
438
-
439
- it('allows PUT when scope field is not in body', async () => {
440
- const { scopeRegistry, invoiceModel } = buildScopeContext();
441
- const db = createMockDb([]);
442
- const body: Record<string, unknown> = { total: 200 };
443
-
444
- const guard = createScopeWriteGuard({ model: invoiceModel, scopeRegistry, db });
445
- const request = createMockRequest({
446
- method: 'PUT',
447
- body,
448
- authContext: {
449
- user: {
450
- id: 'user-1',
451
- email: 'test@test.com',
452
- full_name: 'Test',
453
- enabled: true,
454
- password_hash: '',
455
- },
456
- permissions: { models: {}, pages: [], version: 1 },
457
- roles: ['admin'],
458
- scopeFilters: [{ field: 'company', operator: 'eq', value: 'company-1' }],
459
- },
460
- });
461
- const reply = createMockReply();
462
-
463
- await guard(request, reply);
464
-
465
- expect(reply.status).not.toHaveBeenCalled();
466
- });
467
-
468
- it('skips for GET requests', async () => {
469
- const { scopeRegistry, invoiceModel } = buildScopeContext();
470
- const db = createMockDb([]);
471
-
472
- const guard = createScopeWriteGuard({ model: invoiceModel, scopeRegistry, db });
473
- const request = createMockRequest({ method: 'GET' });
474
- const reply = createMockReply();
475
-
476
- await guard(request, reply);
477
-
478
- expect(reply.status).not.toHaveBeenCalled();
479
- });
480
-
481
- it('skips for unscoped models', async () => {
482
- const { scopeRegistry, unscopedModel } = buildScopeContext();
483
- const db = createMockDb([]);
484
- const body: Record<string, unknown> = { name: 'test' };
485
-
486
- const guard = createScopeWriteGuard({ model: unscopedModel, scopeRegistry, db });
487
- const request = createMockRequest({
488
- method: 'POST',
489
- body,
490
- authContext: {
491
- user: {
492
- id: 'user-1',
493
- email: 'test@test.com',
494
- full_name: 'Test',
495
- enabled: true,
496
- password_hash: '',
497
- },
498
- permissions: { models: {}, pages: [], version: 1 },
499
- roles: ['admin'],
500
- scopeFilters: [],
501
- },
502
- });
503
- const reply = createMockReply();
504
-
505
- await guard(request, reply);
506
-
507
- expect(reply.status).not.toHaveBeenCalled();
508
- });
509
- });