-
Notifications
You must be signed in to change notification settings - Fork 20
feat(world-model): cross-org references — schema search path + write guard #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/owletto-backend/src/utils/__tests__/entity-management-schema-search.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * Schema search path: createEntity falls back to public-catalog orgs when a | ||
| * type slug isn't registered in the entity's own org. Tenant-local types | ||
| * still win when both exist (so user-defined types beat catalog types of the | ||
| * same slug). | ||
| */ | ||
|
|
||
| import type { EntityLinkRule } from '@lobu/owletto-sdk'; | ||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { cleanupTestDatabase, getTestDb } from '../../__tests__/setup/test-db'; | ||
| import { | ||
| addUserToOrganization, | ||
| createTestConnectorDefinition, | ||
| createTestOrganization, | ||
| createTestUser, | ||
| } from '../../__tests__/setup/test-fixtures'; | ||
| import { applyEntityLinks, clearEntityLinkRulesCache } from '../entity-link-upsert'; | ||
| import { createEntity } from '../entity-management'; | ||
|
|
||
| async function seedEntityType(orgId: string, slug: string) { | ||
| const sql = getTestDb(); | ||
| const rows = await sql<{ id: number }[]>` | ||
| INSERT INTO entity_types (organization_id, slug, name, created_at, updated_at) | ||
| VALUES (${orgId}, ${slug}, ${slug}, current_timestamp, current_timestamp) | ||
| RETURNING id | ||
| `; | ||
| return rows[0].id; | ||
| } | ||
|
|
||
| describe('entity-management schema search path', () => { | ||
| beforeEach(async () => { | ||
| await cleanupTestDatabase(); | ||
| }); | ||
|
|
||
| it('resolves an entity_type from a public-catalog org when missing locally', async () => { | ||
| const tenant = await createTestOrganization({ name: 'Tenant Schema Search' }); | ||
| const publicCatalog = await createTestOrganization({ | ||
| name: 'Public Catalog A', | ||
| visibility: 'public', | ||
| }); | ||
| const user = await createTestUser(); | ||
| await addUserToOrganization(user.id, tenant.id, 'owner'); | ||
|
|
||
| const publicTypeId = await seedEntityType(publicCatalog.id, 'tax_filing'); | ||
|
|
||
| const created = await createEntity({ | ||
| entity_type: 'tax_filing', | ||
| name: 'Self Assessment 2024-25', | ||
| organization_id: tenant.id, | ||
| created_by: user.id, | ||
| } as Parameters<typeof createEntity>[0]); | ||
|
|
||
| expect(created.entity_type).toBe('tax_filing'); | ||
|
|
||
| const sql = getTestDb(); | ||
| const rows = await sql<{ entity_type_id: number; organization_id: string }[]>` | ||
| SELECT entity_type_id, organization_id FROM entities WHERE id = ${created.id} | ||
| `; | ||
| // Materialized: the entity row lives in tenant, but its type points at the | ||
| // public-catalog row. | ||
| expect(String(rows[0].organization_id)).toBe(tenant.id); | ||
| expect(Number(rows[0].entity_type_id)).toBe(publicTypeId); | ||
| }); | ||
|
|
||
| it('prefers a tenant-local entity_type over a public-catalog one with the same slug', async () => { | ||
| const tenant = await createTestOrganization({ name: 'Tenant Local-Wins' }); | ||
| const publicCatalog = await createTestOrganization({ | ||
| name: 'Public Catalog B', | ||
| visibility: 'public', | ||
| }); | ||
| const user = await createTestUser(); | ||
| await addUserToOrganization(user.id, tenant.id, 'owner'); | ||
|
|
||
| await seedEntityType(publicCatalog.id, 'tax_filing'); | ||
| const tenantTypeId = await seedEntityType(tenant.id, 'tax_filing'); | ||
|
|
||
| const created = await createEntity({ | ||
| entity_type: 'tax_filing', | ||
| name: 'Local Override', | ||
| organization_id: tenant.id, | ||
| created_by: user.id, | ||
| } as Parameters<typeof createEntity>[0]); | ||
|
|
||
| const sql = getTestDb(); | ||
| const rows = await sql<{ entity_type_id: number }[]>` | ||
| SELECT entity_type_id FROM entities WHERE id = ${created.id} | ||
| `; | ||
| expect(Number(rows[0].entity_type_id)).toBe(tenantTypeId); | ||
| }); | ||
|
|
||
| it('rejects an unknown entity_type that isn\'t in tenant or any public catalog', async () => { | ||
| const tenant = await createTestOrganization({ name: 'Tenant Unknown-Type' }); | ||
| const user = await createTestUser(); | ||
| await addUserToOrganization(user.id, tenant.id, 'owner'); | ||
|
|
||
| await expect( | ||
| createEntity({ | ||
| entity_type: 'never_registered_anywhere', | ||
| name: 'Should Fail', | ||
| organization_id: tenant.id, | ||
| created_by: user.id, | ||
| } as Parameters<typeof createEntity>[0]) | ||
| ).rejects.toThrow(/Unknown entity type/i); | ||
| }); | ||
|
|
||
| it('does not search private orgs that the caller is not a member of', async () => { | ||
| const tenant = await createTestOrganization({ name: 'Tenant No-Snoop' }); | ||
| const otherPrivate = await createTestOrganization({ | ||
| name: 'Some Other Private Org', | ||
| visibility: 'private', | ||
| }); | ||
| const user = await createTestUser(); | ||
| await addUserToOrganization(user.id, tenant.id, 'owner'); | ||
|
|
||
| await seedEntityType(otherPrivate.id, 'secret_type'); | ||
|
|
||
| await expect( | ||
| createEntity({ | ||
| entity_type: 'secret_type', | ||
| name: 'Should Fail', | ||
| organization_id: tenant.id, | ||
| created_by: user.id, | ||
| } as Parameters<typeof createEntity>[0]) | ||
| ).rejects.toThrow(/Unknown entity type/i); | ||
| }); | ||
|
|
||
| // The same resolver lives in entity-link-upsert.ts (auto-link path). Drift | ||
| // here would be caught by this test. | ||
| it('entity-link-upsert resolves a public-catalog type when no tenant type matches', async () => { | ||
| const tenant = await createTestOrganization({ name: 'Tenant Auto-Link' }); | ||
| const publicCatalog = await createTestOrganization({ | ||
| name: 'Public Catalog C', | ||
| visibility: 'public', | ||
| }); | ||
| const user = await createTestUser(); | ||
| await addUserToOrganization(user.id, tenant.id, 'owner'); | ||
|
|
||
| const publicTypeId = await seedEntityType(publicCatalog.id, 'public_actor'); | ||
|
|
||
| const connectorKey = 'auto-link-cross-org'; | ||
| const feedKey = 'msgs'; | ||
| const originType = 'msg'; | ||
| const rule: EntityLinkRule = { | ||
| entityType: 'public_actor', | ||
| autoCreate: true, | ||
| titlePath: 'metadata.name', | ||
| identities: [{ namespace: 'phone', eventPath: 'metadata.phone' }], | ||
| }; | ||
| await createTestConnectorDefinition({ | ||
| key: connectorKey, | ||
| name: connectorKey, | ||
| organization_id: tenant.id, | ||
| feeds_schema: { | ||
| [feedKey]: { eventKinds: { [originType]: { entityLinks: [rule] } } }, | ||
| }, | ||
| }); | ||
| clearEntityLinkRulesCache(); | ||
|
|
||
| await applyEntityLinks({ | ||
| connectorKey, | ||
| feedKey, | ||
| orgId: tenant.id, | ||
| items: [ | ||
| { origin_type: originType, metadata: { phone: '14155551234', name: 'Alex' } }, | ||
| ], | ||
| }); | ||
|
|
||
| const sql = getTestDb(); | ||
| const rows = await sql<{ id: number; entity_type_id: number; organization_id: string }[]>` | ||
| SELECT id, entity_type_id, organization_id | ||
| FROM entities | ||
| WHERE organization_id = ${tenant.id} AND name = 'Alex' AND deleted_at IS NULL | ||
| `; | ||
| expect(rows.length).toBe(1); | ||
| expect(Number(rows[0].entity_type_id)).toBe(publicTypeId); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change allows
createEntityto resolveentity_typefrom any public org, but metadata validation is still scoped toctx.organizationIdinvalidateEntityMetadata(utils/schema-validation.ts), which returns "valid" when it cannot find a schema. In practice, creating or updating a tenant entity that uses a public catalog type can now bypass that type's JSON schema entirely, so required fields and constraints from the catalog are silently skipped and invalid metadata is persisted.Useful? React with 👍 / 👎.