|
1 | 1 | import { expectTypeOf } from 'expect-type' |
2 | 2 | import { |
3 | 3 | AbilityBuilder, |
4 | | - AbilityClass, |
5 | | - PureAbility, |
6 | | - SubjectType, |
7 | | - MongoQuery, |
8 | | - AbilityTuple, |
9 | | - MongoAbility, |
10 | | - createMongoAbility |
| 4 | + AbilityClass, AbilityTuple, createMongoAbility, MongoAbility, PureAbility |
11 | 5 | } from '../../src' |
12 | 6 |
|
13 | 7 | describe('AbilityBuilder types', () => { |
14 | | - type Method<T extends any[]> = (...args: T) => any |
15 | | - |
16 | 8 | it('infers types from `PureAbility` default generics', () => { |
17 | 9 | const builder = new AbilityBuilder<PureAbility<AbilityTuple>>(PureAbility) |
18 | | - type Can = typeof builder.can |
19 | 10 |
|
20 | | - expectTypeOf<Method<[string, SubjectType]>>().toMatchTypeOf<Can>() |
21 | | - expectTypeOf<Method<[string[], SubjectType[]]>>().toMatchTypeOf<Can>() |
22 | | - expectTypeOf<Method<[string, SubjectType, unknown]>>().toMatchTypeOf<Can>() |
23 | | - expectTypeOf<[string, SubjectType, string | string[]]>().toMatchTypeOf<Parameters<Can>>() |
24 | | - expectTypeOf<[string, SubjectType, string | string[], unknown]>() |
25 | | - .toMatchTypeOf<Parameters<Can>>() |
26 | | - expectTypeOf<[string, SubjectType, {}]>().not.toMatchTypeOf<Parameters<Can>>() |
27 | | - expectTypeOf<[string, SubjectType, string]>().not.toEqualTypeOf<Parameters<Can>>() |
28 | | - expectTypeOf<[string]>().not.toEqualTypeOf<Parameters<Can>>() |
| 11 | + builder.can('read', 'Subject') |
| 12 | + builder.can('read', class {}) |
| 13 | + // @ts-expect-error only `string | class` can be a subject type |
| 14 | + builder.can('read', {}) |
| 15 | + |
| 16 | + builder.can(['read', 'update'], ['Subject1', 'Subject2']) |
| 17 | + builder.can('update', ['Subject1', 'Subject2']) |
| 18 | + builder.can(['read', 'update'], 'Subject') |
| 19 | + // @ts-expect-error only `string | string[]` can be used as action |
| 20 | + builder.can(1, 'Subject') |
| 21 | + |
| 22 | + builder.can('read', 'Subject', {}) |
| 23 | + builder.can('read', 'Subject', { title: 'new' }) |
| 24 | + builder.can('read', 'Subject', { title: 'new2', anyOtherProperty: true }) |
| 25 | + builder.can('read', 'Subject', 1) |
| 26 | + builder.can('read', 'Subject', () => {}) |
| 27 | + builder.can('read', 'Subject', 'field1') |
| 28 | + builder.can('read', 'Subject', 'field1', { condition: true }) |
| 29 | + builder.can('read', 'Subject', ['field1', 'field2']) |
| 30 | + builder.can('read', 'Subject', ['field1', 'field2'], () => {}) |
| 31 | + // @ts-expect-error expects 3rd parameter to fields -> `string | string[]` |
| 32 | + builder.can('read', 'Subject', {}, () => {}) |
29 | 33 | }) |
30 | 34 |
|
31 | 35 | it('infers types from `createMongoAbility` default generics', () => { |
32 | 36 | const builder = new AbilityBuilder(createMongoAbility) |
33 | | - type Can = typeof builder.can |
34 | 37 |
|
35 | | - expectTypeOf<Method<[string, SubjectType]>>().toMatchTypeOf<Can>() |
36 | | - expectTypeOf<Method<[string[], SubjectType[]]>>().toMatchTypeOf<Can>() |
37 | | - expectTypeOf<Method<[string, SubjectType, MongoQuery]>>().toMatchTypeOf<Can>() |
38 | | - expectTypeOf<[string, SubjectType, string | string[]]>().toMatchTypeOf<Parameters<Can>>() |
39 | | - expectTypeOf<[string, SubjectType, string | string[], MongoQuery]>() |
40 | | - .toMatchTypeOf<Parameters<Can>>() |
| 38 | + builder.can('read', 'Subject') |
| 39 | + builder.can('read', class {}) |
| 40 | + // @ts-expect-error only `string | class` can be a subject type |
| 41 | + builder.can('read', {}) |
| 42 | + |
| 43 | + builder.can(['read', 'update'], ['Subject1', 'Subject2']) |
| 44 | + builder.can('update', ['Subject1', 'Subject2']) |
| 45 | + builder.can(['read', 'update'], 'Subject') |
| 46 | + // @ts-expect-error only `string | string[]` can be used as action |
| 47 | + builder.can(1, 'Subject') |
| 48 | + |
| 49 | + builder.can('read', 'Subject', {}) |
| 50 | + builder.can('read', 'Subject', { |
| 51 | + title: { |
| 52 | + unknownOperator$: true, // TODO: change types to error this |
| 53 | + } |
| 54 | + }) |
| 55 | + builder.can('read', 'Subject', { |
| 56 | + published: { |
| 57 | + $eq: true |
| 58 | + } |
| 59 | + }) |
| 60 | + // @ts-expect-error conditions is expected to be a MongoQuery |
| 61 | + builder.can('read', 'Subject', () => {}) |
| 62 | + // @ts-expect-error conditions is expected to be a MongoQuery |
| 63 | + builder.can('read', 'Subject', 1) |
| 64 | + |
| 65 | + builder.can('read', 'Subject', 'field') |
| 66 | + builder.can('read', 'Subject', 'field', { |
| 67 | + published: { |
| 68 | + $eq: true |
| 69 | + } |
| 70 | + }) |
| 71 | + builder.can('read', 'Subject', ['field1', 'field2'], { |
| 72 | + published: { |
| 73 | + $eq: true |
| 74 | + } |
| 75 | + }) |
| 76 | + builder.can('read', 'Subject', ['field1', 'field2']) |
41 | 77 | }) |
42 | 78 |
|
43 | 79 | it('infers single action argument type from ClaimAbility', () => { |
@@ -95,6 +131,32 @@ describe('AbilityBuilder types', () => { |
95 | 131 | }) |
96 | 132 | }) |
97 | 133 |
|
| 134 | + describe('action and subject pairs restrictions', () => { |
| 135 | + type Post = { id: number, title: string, kind: 'Post' } |
| 136 | + type User = { id: number, name: string, kind: 'User' } |
| 137 | + type AppAbility = MongoAbility< |
| 138 | + ['read' | 'update' | 'delete' | 'create', 'Post' | Post] | |
| 139 | + ['read' | 'update', 'User' | User] |
| 140 | + > |
| 141 | + let builder: AbilityBuilder<AppAbility> |
| 142 | + |
| 143 | + beforeEach(() => { |
| 144 | + builder = new AbilityBuilder<AppAbility>(createMongoAbility) |
| 145 | + }) |
| 146 | + |
| 147 | + it('allows to use only specified actions for specified subjects', () => { |
| 148 | + builder.can('read', 'Post', { id: 1 }) |
| 149 | + builder.can('read', 'Post', { id: 1, title: 'test' }) |
| 150 | + builder.can(['update', 'delete', 'create'], 'Post', { id: 1, title: 'test' }) |
| 151 | + // @ts-expect-error "manage" is not in allowed list of actions for this subject |
| 152 | + builder.can('manage', 'Post') |
| 153 | + |
| 154 | + builder.can(['read', 'update'], 'User', { id: 1 }) |
| 155 | + // @ts-expect-error "delete" is not in allowed list of actions for this subject |
| 156 | + builder.can('delete', 'User', { id: 1 }) |
| 157 | + }) |
| 158 | + }) |
| 159 | + |
98 | 160 | describe('class type as a subject', () => { |
99 | 161 | class Post { |
100 | 162 | id!: number |
|
0 commit comments