-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add EntityEdgeDeletionPermissionInferenceBehavior for canViewer…
…DeleteAsync
- Loading branch information
Showing
3 changed files
with
259 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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
203 changes: 203 additions & 0 deletions
203
.../src/utils/__tests__/canViewerDeleteAsync-edgeDeletionPermissionInferenceBehavior-test.ts
This file contains 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,203 @@ | ||
import Entity from '../../Entity'; | ||
import { EntityCompanionDefinition } from '../../EntityCompanionProvider'; | ||
import EntityConfiguration from '../../EntityConfiguration'; | ||
import { | ||
EntityEdgeDeletionBehavior, | ||
EntityEdgeDeletionPermissionInferenceBehavior, | ||
} from '../../EntityFieldDefinition'; | ||
import { UUIDField } from '../../EntityFields'; | ||
import EntityPrivacyPolicy from '../../EntityPrivacyPolicy'; | ||
import ReadonlyEntity from '../../ReadonlyEntity'; | ||
import ViewerContext from '../../ViewerContext'; | ||
import AlwaysAllowPrivacyPolicyRule from '../../rules/AlwaysAllowPrivacyPolicyRule'; | ||
import AlwaysDenyPrivacyPolicyRule from '../../rules/AlwaysDenyPrivacyPolicyRule'; | ||
import { canViewerDeleteAsync } from '../EntityPrivacyUtils'; | ||
import { createUnitTestEntityCompanionProvider } from '../testing/createUnitTestEntityCompanionProvider'; | ||
|
||
describe(canViewerDeleteAsync, () => { | ||
describe('edgeDeletionPermissionInferenceBehavior', () => { | ||
it('optimizes when EntityEdgeDeletionPermissionInferenceBehavior.ALLOW_SINGLE_ENTITY_INDUCTION', async () => { | ||
const companionProvider = createUnitTestEntityCompanionProvider(); | ||
const viewerContext = new ViewerContext(companionProvider); | ||
|
||
// create root | ||
const testEntity = await TestEntity.creator(viewerContext).enforceCreateAsync(); | ||
|
||
// create a bunch of leaves referencing root with | ||
// edgeDeletionPermissionInferenceBehavior = EntityEdgeDeletionPermissionInferenceBehavior.ALLOW_SINGLE_ENTITY_INDUCTION | ||
for (let i = 0; i < 10; i++) { | ||
await TestLeafEntity.creator(viewerContext) | ||
.setField('test_entity_id', testEntity.getID()) | ||
.enforceCreateAsync(); | ||
} | ||
|
||
const companion = viewerContext.getViewerScopedEntityCompanionForClass(TestLeafEntity); | ||
const authorizeDeleteSpy = jest.spyOn( | ||
companion.entityCompanion.privacyPolicy, | ||
'authorizeDeleteAsync', | ||
); | ||
|
||
const canViewerDelete = await canViewerDeleteAsync(TestEntity, testEntity); | ||
expect(canViewerDelete).toBe(true); | ||
|
||
expect(authorizeDeleteSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('does not optimize when undefined', async () => { | ||
const companionProvider = createUnitTestEntityCompanionProvider(); | ||
const viewerContext = new ViewerContext(companionProvider); | ||
|
||
// create root | ||
const testEntity = await TestEntity.creator(viewerContext).enforceCreateAsync(); | ||
|
||
// create a bunch of leaves with no edgeDeletionPermissionInferenceBehavior | ||
for (let i = 0; i < 10; i++) { | ||
await TestLeafNoInductionEntity.creator(viewerContext) | ||
.setField('test_entity_id', testEntity.getID()) | ||
.enforceCreateAsync(); | ||
} | ||
|
||
const companion = | ||
viewerContext.getViewerScopedEntityCompanionForClass(TestLeafNoInductionEntity); | ||
const authorizeDeleteSpy = jest.spyOn( | ||
companion.entityCompanion.privacyPolicy, | ||
'authorizeDeleteAsync', | ||
); | ||
|
||
const canViewerDelete = await canViewerDeleteAsync(TestEntity, testEntity); | ||
expect(canViewerDelete).toBe(true); | ||
|
||
expect(authorizeDeleteSpy).toHaveBeenCalledTimes(10); | ||
}); | ||
}); | ||
}); | ||
|
||
type TestEntityFields = { | ||
id: string; | ||
}; | ||
|
||
type TestLeafEntityFields = { | ||
id: string; | ||
test_entity_id: string | null; | ||
}; | ||
|
||
class AlwaysAllowEntityPrivacyPolicy< | ||
TFields extends object, | ||
TID extends NonNullable<TFields[TSelectedFields]>, | ||
TViewerContext extends ViewerContext, | ||
TEntity extends ReadonlyEntity<TFields, TID, TViewerContext, TSelectedFields>, | ||
TSelectedFields extends keyof TFields = keyof TFields, | ||
> extends EntityPrivacyPolicy<TFields, TID, TViewerContext, TEntity, TSelectedFields> { | ||
protected override readonly readRules = [ | ||
new AlwaysAllowPrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>(), | ||
]; | ||
protected override readonly createRules = [ | ||
new AlwaysAllowPrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>(), | ||
]; | ||
protected override readonly updateRules = [ | ||
new AlwaysDenyPrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>(), | ||
]; | ||
protected override readonly deleteRules = [ | ||
new AlwaysAllowPrivacyPolicyRule<TFields, TID, TViewerContext, TEntity, TSelectedFields>(), | ||
]; | ||
} | ||
|
||
class TestEntity extends Entity<TestEntityFields, string, ViewerContext> { | ||
static defineCompanionDefinition(): EntityCompanionDefinition< | ||
TestEntityFields, | ||
string, | ||
ViewerContext, | ||
TestEntity, | ||
AlwaysAllowEntityPrivacyPolicy<TestEntityFields, string, ViewerContext, TestEntity> | ||
> { | ||
return { | ||
entityClass: TestEntity, | ||
entityConfiguration: new EntityConfiguration<TestEntityFields>({ | ||
idField: 'id', | ||
tableName: 'blah', | ||
inboundEdges: [TestLeafEntity, TestLeafNoInductionEntity], | ||
schema: { | ||
id: new UUIDField({ | ||
columnName: 'custom_id', | ||
}), | ||
}, | ||
databaseAdapterFlavor: 'postgres', | ||
cacheAdapterFlavor: 'redis', | ||
}), | ||
privacyPolicyClass: AlwaysAllowEntityPrivacyPolicy, | ||
}; | ||
} | ||
} | ||
|
||
class TestLeafEntity extends Entity<TestLeafEntityFields, string, ViewerContext> { | ||
static defineCompanionDefinition(): EntityCompanionDefinition< | ||
TestLeafEntityFields, | ||
string, | ||
ViewerContext, | ||
TestLeafEntity, | ||
AlwaysAllowEntityPrivacyPolicy<TestLeafEntityFields, string, ViewerContext, TestLeafEntity> | ||
> { | ||
return { | ||
entityClass: TestLeafEntity, | ||
entityConfiguration: new EntityConfiguration<TestLeafEntityFields>({ | ||
idField: 'id', | ||
tableName: 'blah_2', | ||
schema: { | ||
id: new UUIDField({ | ||
columnName: 'custom_id', | ||
}), | ||
test_entity_id: new UUIDField({ | ||
columnName: 'test_entity_id', | ||
association: { | ||
associatedEntityClass: TestEntity, | ||
edgeDeletionBehavior: EntityEdgeDeletionBehavior.CASCADE_DELETE, | ||
edgeDeletionPermissionInferenceBehavior: | ||
EntityEdgeDeletionPermissionInferenceBehavior.ALLOW_SINGLE_ENTITY_INDUCTION, | ||
}, | ||
}), | ||
}, | ||
databaseAdapterFlavor: 'postgres', | ||
cacheAdapterFlavor: 'redis', | ||
}), | ||
privacyPolicyClass: AlwaysAllowEntityPrivacyPolicy, | ||
}; | ||
} | ||
} | ||
|
||
class TestLeafNoInductionEntity extends Entity<TestLeafEntityFields, string, ViewerContext> { | ||
static defineCompanionDefinition(): EntityCompanionDefinition< | ||
TestLeafEntityFields, | ||
string, | ||
ViewerContext, | ||
TestLeafNoInductionEntity, | ||
AlwaysAllowEntityPrivacyPolicy< | ||
TestLeafEntityFields, | ||
string, | ||
ViewerContext, | ||
TestLeafNoInductionEntity | ||
> | ||
> { | ||
return { | ||
entityClass: TestLeafNoInductionEntity, | ||
entityConfiguration: new EntityConfiguration<TestLeafEntityFields>({ | ||
idField: 'id', | ||
tableName: 'blah_3', | ||
schema: { | ||
id: new UUIDField({ | ||
columnName: 'custom_id', | ||
}), | ||
test_entity_id: new UUIDField({ | ||
columnName: 'test_entity_id', | ||
association: { | ||
associatedEntityClass: TestEntity, | ||
edgeDeletionBehavior: EntityEdgeDeletionBehavior.CASCADE_DELETE, | ||
}, | ||
}), | ||
}, | ||
databaseAdapterFlavor: 'postgres', | ||
cacheAdapterFlavor: 'redis', | ||
}), | ||
privacyPolicyClass: AlwaysAllowEntityPrivacyPolicy, | ||
}; | ||
} | ||
} |