|
1 | | -import { defineAbility } from "@casl/ability" |
| 1 | +import { MongoAbility, defineAbility } from "@casl/ability" |
2 | 2 | import { accessibleBy } from "../src" |
3 | | -import { testConversionToMongoQuery } from "./mongo_query.spec" |
4 | 3 |
|
5 | | -declare module '../src' { |
6 | | - interface RecordTypes { |
7 | | - Post: true |
| 4 | +describe('accessibleBy', () => { |
| 5 | + type AppAbility = MongoAbility<[string, Post['kind'] | Post]> |
| 6 | + interface Post { |
| 7 | + kind: 'Post'; |
| 8 | + _id: string; |
| 9 | + state: string; |
| 10 | + private: boolean; |
| 11 | + isPublished: boolean | null; |
| 12 | + authorId: number; |
| 13 | + views: number; |
| 14 | + 'comments.author': string; |
8 | 15 | } |
9 | | -} |
10 | 16 |
|
11 | | -describe('accessibleBy', () => { |
12 | 17 | it('returns `{ $expr: false }` when there are no rules for specific subject/action', () => { |
13 | | - const ability = defineAbility((can) => { |
| 18 | + const ability = defineAbility<AppAbility>((can) => { |
14 | 19 | can('read', 'Post') |
15 | 20 | }) |
16 | 21 |
|
17 | | - const query = accessibleBy(ability, 'update').Post |
| 22 | + const query = accessibleBy(ability, 'update').ofType('Post') |
18 | 23 |
|
19 | 24 | expect(query).toEqual({ $expr: { $eq: [0, 1] } }) |
20 | 25 | }) |
21 | 26 |
|
22 | 27 | it('returns `{ $expr: false }` if there is a rule that forbids previous one', () => { |
23 | | - const ability = defineAbility((can, cannot) => { |
| 28 | + const ability = defineAbility<AppAbility>((can, cannot) => { |
24 | 29 | can('update', 'Post', { authorId: 1 }) |
25 | 30 | cannot('update', 'Post') |
26 | 31 | }) |
27 | 32 |
|
28 | | - const query = accessibleBy(ability, 'update').Post |
| 33 | + const query = accessibleBy(ability, 'update').ofType('Post') |
29 | 34 |
|
30 | 35 | expect(query).toEqual({ $expr: { $eq: [0, 1] } }) |
31 | 36 | }) |
32 | 37 |
|
33 | 38 | describe('it behaves like `toMongoQuery` when converting rules', () => { |
34 | | - testConversionToMongoQuery((ability, subjectType, action) => |
35 | | - accessibleBy(ability, action)[subjectType]) |
| 39 | + it('accepts ability action as third argument', () => { |
| 40 | + const ability = defineAbility<AppAbility>((can) => { |
| 41 | + can('update', 'Post', { _id: 'mega' }) |
| 42 | + }) |
| 43 | + const query = accessibleBy(ability, 'update').ofType('Post') |
| 44 | + |
| 45 | + expect(query).toEqual({ |
| 46 | + $or: [{ _id: 'mega' }] |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('OR-es conditions for regular rules and AND-es for inverted ones', () => { |
| 51 | + const ability = defineAbility<AppAbility>((can, cannot) => { |
| 52 | + can('read', 'Post', { _id: 'mega' }) |
| 53 | + can('read', 'Post', { state: 'draft' }) |
| 54 | + cannot('read', 'Post', { private: true }) |
| 55 | + cannot('read', 'Post', { state: 'archived' }) |
| 56 | + }) |
| 57 | + const query = accessibleBy(ability).ofType('Post') |
| 58 | + |
| 59 | + expect(query).toEqual({ |
| 60 | + $or: [ |
| 61 | + { state: 'draft' }, |
| 62 | + { _id: 'mega' } |
| 63 | + ], |
| 64 | + $and: [ |
| 65 | + { $nor: [{ state: 'archived' }] }, |
| 66 | + { $nor: [{ private: true }] } |
| 67 | + ] |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('can find records where property', () => { |
| 72 | + it('is present', () => { |
| 73 | + const ability = defineAbility<AppAbility>((can) => { |
| 74 | + can('read', 'Post', { |
| 75 | + isPublished: { $exists: true, $ne: null } |
| 76 | + }) |
| 77 | + }) |
| 78 | + const query = accessibleBy(ability).ofType('Post') |
| 79 | + |
| 80 | + expect(query).toEqual({ $or: [{ isPublished: { $exists: true, $ne: null } }] }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('is blank', () => { |
| 84 | + const ability = defineAbility<AppAbility>((can) => { |
| 85 | + can('read', 'Post', { isPublished: { $exists: false } }) |
| 86 | + can('read', 'Post', { isPublished: null }) |
| 87 | + }) |
| 88 | + const query = accessibleBy(ability).ofType('Post') |
| 89 | + |
| 90 | + expect(query).toEqual({ |
| 91 | + $or: [ |
| 92 | + { isPublished: null }, |
| 93 | + { isPublished: { $exists: false } } |
| 94 | + ] |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('is defined by `$in` criteria', () => { |
| 99 | + const ability = defineAbility<AppAbility>((can) => { |
| 100 | + can('read', 'Post', { state: { $in: ['draft', 'archived'] } }) |
| 101 | + }) |
| 102 | + const query = accessibleBy(ability).ofType('Post') |
| 103 | + |
| 104 | + expect(query).toEqual({ $or: [{ state: { $in: ['draft', 'archived'] } }] }) |
| 105 | + }) |
| 106 | + |
| 107 | + it('is defined by `$all` criteria', () => { |
| 108 | + const ability = defineAbility<AppAbility>((can) => { |
| 109 | + can('read', 'Post', { state: { $all: ['draft', 'archived'] } }) |
| 110 | + }) |
| 111 | + const query = accessibleBy(ability).ofType('Post') |
| 112 | + |
| 113 | + expect(query).toEqual({ $or: [{ state: { $all: ['draft', 'archived'] } }] }) |
| 114 | + }) |
| 115 | + it('is defined by `$lt` and `$lte` criteria', () => { |
| 116 | + const ability = defineAbility<AppAbility>((can) => { |
| 117 | + can('read', 'Post', { views: { $lt: 10 } }) |
| 118 | + can('read', 'Post', { views: { $lt: 5 } }) |
| 119 | + }) |
| 120 | + const query = accessibleBy(ability).ofType('Post') |
| 121 | + |
| 122 | + expect(query).toEqual({ $or: [{ views: { $lt: 5 } }, { views: { $lt: 10 } }] }) |
| 123 | + }) |
| 124 | + |
| 125 | + it('is defined by `$gt` and `$gte` criteria', () => { |
| 126 | + const ability = defineAbility<AppAbility>((can) => { |
| 127 | + can('read', 'Post', { views: { $gt: 10 } }) |
| 128 | + can('read', 'Post', { views: { $gte: 100 } }) |
| 129 | + }) |
| 130 | + const query = accessibleBy(ability).ofType('Post') |
| 131 | + |
| 132 | + expect(query).toEqual({ $or: [{ views: { $gte: 100 } }, { views: { $gt: 10 } }] }) |
| 133 | + }) |
| 134 | + |
| 135 | + it('is defined by `$ne` criteria', () => { |
| 136 | + const ability = defineAbility<AppAbility>((can) => { |
| 137 | + can('read', 'Post', { authorId: { $ne: 5 } }) |
| 138 | + }) |
| 139 | + const query = accessibleBy(ability).ofType('Post') |
| 140 | + |
| 141 | + expect(query).toEqual({ $or: [{ authorId: { $ne: 5 } }] }) |
| 142 | + }) |
| 143 | + |
| 144 | + it('is defined by dot notation fields', () => { |
| 145 | + const ability = defineAbility<AppAbility>((can) => { |
| 146 | + can('read', 'Post', { 'comments.author': 'Ted' }) |
| 147 | + }) |
| 148 | + const query = accessibleBy(ability).ofType('Post') |
| 149 | + |
| 150 | + expect(query).toEqual({ $or: [{ 'comments.author': 'Ted' }] }) |
| 151 | + }) |
| 152 | + }) |
36 | 153 | }) |
37 | 154 | }) |
0 commit comments