Skip to content

Commit

Permalink
fix: Broken tests in SubCrudRecordsControllerTests
Browse files Browse the repository at this point in the history
  • Loading branch information
KallynGowdy committed Nov 14, 2024
1 parent 59d9aef commit 83ba6ea
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 176 deletions.
1 change: 1 addition & 0 deletions src/aux-common/common/PolicyPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ export const RESOURCE_KIND_VALIDATION = z.enum([
WEBHOOK_RESOURCE_KIND,
NOTIFICATION_RESOURCE_KIND,
PACKAGE_RESOURCE_KIND,
PACKAGE_VERSION_RESOURCE_KIND,
]);

export const ACTION_KINDS_VALIDATION = z.enum([
Expand Down
51 changes: 42 additions & 9 deletions src/aux-records/crud/sub/SubCrudRecordsController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,74 +30,107 @@ import {
PUBLIC_READ_MARKER,
} from '@casual-simulation/aux-common';
import { testCrudRecordsController } from './SubCrudRecordsControllerTests';
import { SubCrudRecord, SubCrudRecordsStore } from './SubCrudRecordsStore';
import {
SubCrudRecordsConfiguration,
SubCrudRecordsController,
} from './SubCrudRecordsController';

console.log = jest.fn();

describe('SubCrudRecordsController', () => {
describe('allows record key access', () => {
testCrudRecordsController<
TestItem,
TestSubItemKey,
TestSubItem,
SubCrudRecordsStore<TestSubItemKey, TestSubItem>,
CrudRecordsStore<TestItem>,
TestController
>(
true,
'data',
(services) => new MemorySubCrudRecordsStore(services.store),
(services) => new MemoryCrudRecordsStore(services.store),
(services, recordItemStore) =>
new MemorySubCrudRecordsStore(services.store, recordItemStore),
(config, services) =>
new TestController({
...config,
resourceKind: 'data',
name: 'testItem',
}),
(item) => ({
key1: item,
key2: `key${item}`,
}),
(item) => item,
(item) => item
);
});

describe('denies record key access', () => {
testCrudRecordsController<
TestItem,
TestSubItemKey,
TestSubItem,
SubCrudRecordsStore<TestSubItemKey, TestSubItem>,
CrudRecordsStore<TestItem>,
TestController
>(
false,
'marker',
(services) => new MemoryCrudRecordsStore(services.store),
(services, recordItemStore) =>
new MemorySubCrudRecordsStore(services.store, recordItemStore),
(config, services) =>
new TestController({
...config,
resourceKind: 'marker',
name: 'testItem',
}),
(item) => ({
key1: item,
key2: `key${item}`,
}),
(item) => item,
(item) => item
);
});
});

export interface TestItem extends CrudRecord {}

export class TestController extends CrudRecordsController<TestItem> {
export interface TestSubItemKey {
key1: number;
key2: string;
}

export interface TestSubItem extends SubCrudRecord<TestSubItemKey> {}

export class TestController extends SubCrudRecordsController<
TestSubItemKey,
TestSubItem
> {
private __checkSubscriptionMetrics: (
action: ActionKinds,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
item?: TestSubItem
) => Promise<CheckSubscriptionMetricsResult>;

set checkSubscriptionMetrics(
value: (
action: ActionKinds,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
item?: TestSubItem
) => Promise<CheckSubscriptionMetricsResult>
) {
this.__checkSubscriptionMetrics = value;
}

constructor(
config: CrudRecordsConfiguration<TestItem>,
config: SubCrudRecordsConfiguration<TestSubItemKey, TestSubItem>,
checkSubscriptionMetrics?: (
action: ActionKinds,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
item?: TestSubItem
) => Promise<CheckSubscriptionMetricsResult>
) {
super(config);
Expand All @@ -108,7 +141,7 @@ export class TestController extends CrudRecordsController<TestItem> {
action: ActionKinds,
context: AuthorizationContext,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
item?: TestSubItem
): Promise<CheckSubscriptionMetricsResult> {
if (this.__checkSubscriptionMetrics) {
return await this.__checkSubscriptionMetrics(
Expand Down
2 changes: 1 addition & 1 deletion src/aux-records/crud/sub/SubCrudRecordsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export abstract class SubCrudRecordsController<
request.key
);

if (!result) {
if (!result.item) {
return {
success: false,
errorCode: 'data_not_found',
Expand Down
Loading

0 comments on commit 83ba6ea

Please sign in to comment.