-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Work on supporting sub-items for CRUD records
- This pattern should make it easier to support storing sub-items inside record items. - Key aspects of a record item is that it has an address and one or more markers. - Key aspects of sub-items are that they can have an arbitrary key (underneath an address), and inherit markers.
- Loading branch information
1 parent
50bee8f
commit 59d9aef
Showing
6 changed files
with
2,178 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
src/aux-records/crud/sub/SubCrudRecordsController.spec.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,124 @@ | ||
import { MemoryStore } from '../../MemoryStore'; | ||
import { | ||
AuthorizationContext, | ||
AuthorizeUserAndInstancesForResourcesSuccess, | ||
PolicyController, | ||
} from '../../PolicyController'; | ||
import { RecordsController } from '../../RecordsController'; | ||
import { | ||
createTestControllers, | ||
createTestRecordKey, | ||
createTestUser, | ||
} from '../../TestUtils'; | ||
import { MemoryCrudRecordsStore } from './../MemoryCrudRecordsStore'; | ||
import { | ||
CrudRecord, | ||
CrudRecordsStore, | ||
CrudSubscriptionMetrics, | ||
} from './../CrudRecordsStore'; | ||
import { | ||
CheckSubscriptionMetricsResult, | ||
CheckSubscriptionMetricsSuccess, | ||
CrudRecordItemSuccess, | ||
CrudRecordsConfiguration, | ||
CrudRecordsController, | ||
} from '../CrudRecordsController'; | ||
import { MemorySubCrudRecordsStore } from './SubMemoryCrudRecordsStore'; | ||
import { | ||
ActionKinds, | ||
PRIVATE_MARKER, | ||
PUBLIC_READ_MARKER, | ||
} from '@casual-simulation/aux-common'; | ||
import { testCrudRecordsController } from './SubCrudRecordsControllerTests'; | ||
|
||
console.log = jest.fn(); | ||
|
||
describe('SubCrudRecordsController', () => { | ||
describe('allows record key access', () => { | ||
testCrudRecordsController< | ||
TestItem, | ||
CrudRecordsStore<TestItem>, | ||
TestController | ||
>( | ||
true, | ||
'data', | ||
(services) => new MemorySubCrudRecordsStore(services.store), | ||
(config, services) => | ||
new TestController({ | ||
...config, | ||
resourceKind: 'data', | ||
name: 'testItem', | ||
}), | ||
(item) => item | ||
); | ||
}); | ||
|
||
describe('denies record key access', () => { | ||
testCrudRecordsController< | ||
TestItem, | ||
CrudRecordsStore<TestItem>, | ||
TestController | ||
>( | ||
false, | ||
'marker', | ||
(services) => new MemoryCrudRecordsStore(services.store), | ||
(config, services) => | ||
new TestController({ | ||
...config, | ||
resourceKind: 'marker', | ||
name: 'testItem', | ||
}), | ||
(item) => item | ||
); | ||
}); | ||
}); | ||
|
||
export interface TestItem extends CrudRecord {} | ||
|
||
export class TestController extends CrudRecordsController<TestItem> { | ||
private __checkSubscriptionMetrics: ( | ||
action: ActionKinds, | ||
authorization: AuthorizeUserAndInstancesForResourcesSuccess, | ||
item?: TestItem | ||
) => Promise<CheckSubscriptionMetricsResult>; | ||
|
||
set checkSubscriptionMetrics( | ||
value: ( | ||
action: ActionKinds, | ||
authorization: AuthorizeUserAndInstancesForResourcesSuccess, | ||
item?: TestItem | ||
) => Promise<CheckSubscriptionMetricsResult> | ||
) { | ||
this.__checkSubscriptionMetrics = value; | ||
} | ||
|
||
constructor( | ||
config: CrudRecordsConfiguration<TestItem>, | ||
checkSubscriptionMetrics?: ( | ||
action: ActionKinds, | ||
authorization: AuthorizeUserAndInstancesForResourcesSuccess, | ||
item?: TestItem | ||
) => Promise<CheckSubscriptionMetricsResult> | ||
) { | ||
super(config); | ||
this.__checkSubscriptionMetrics = checkSubscriptionMetrics as any; | ||
} | ||
|
||
protected async _checkSubscriptionMetrics( | ||
action: ActionKinds, | ||
context: AuthorizationContext, | ||
authorization: AuthorizeUserAndInstancesForResourcesSuccess, | ||
item?: TestItem | ||
): Promise<CheckSubscriptionMetricsResult> { | ||
if (this.__checkSubscriptionMetrics) { | ||
return await this.__checkSubscriptionMetrics( | ||
action, | ||
authorization, | ||
item | ||
); | ||
} | ||
return { | ||
success: true, | ||
}; | ||
} | ||
} |
Oops, something went wrong.