@jperezmart/nest-casl-testing 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +33 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +26 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Javier Pérez Martín
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _casl_ability = require("@casl/ability");
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* Build a CASL ability directly from a permissions map and a user, without
|
|
6
|
+
* booting a Nest application. Intended for unit-testing permission definitions
|
|
7
|
+
* in projects that consume `@jperezmart/nest-casl`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const ability = buildAbilityForTest(permissions, { id: "1", roles: ["author"] });
|
|
12
|
+
* expect(ability.can("update", subject("Article", { authorId: "1" }))).toBe(true);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
function buildAbilityForTest(permissions, user, options = {}) {
|
|
16
|
+
const builder = new _casl_ability.AbilityBuilder(_casl_ability.createMongoAbility);
|
|
17
|
+
const buildOptions = options.detectSubjectType ? { detectSubjectType: options.detectSubjectType } : void 0;
|
|
18
|
+
const roles = Array.isArray(user.roles) ? user.roles : [];
|
|
19
|
+
if (options.superuserRole !== void 0 && roles.includes(options.superuserRole)) {
|
|
20
|
+
builder.can("manage", "all");
|
|
21
|
+
return builder.build(buildOptions);
|
|
22
|
+
}
|
|
23
|
+
for (const role of roles) {
|
|
24
|
+
const definition = permissions[role];
|
|
25
|
+
if (definition === true) builder.can("manage", "all");
|
|
26
|
+
else if (typeof definition === "function") definition(user, builder);
|
|
27
|
+
}
|
|
28
|
+
return builder.build(buildOptions);
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.buildAbilityForTest = buildAbilityForTest;
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["AbilityBuilder","createMongoAbility"],"sources":["../src/index.ts"],"sourcesContent":["import type { AnyAbility, MongoAbility } from '@casl/ability';\nimport { AbilityBuilder, createMongoAbility } from '@casl/ability';\nimport type {\n AuthorizableUser,\n DefinePermissions,\n Permissions,\n} from '@jperezmart/nest-casl';\n\n/** Extra options accepted by {@link buildAbilityForTest}. */\nexport interface BuildAbilityForTestOptions<Roles extends string = string> {\n /** Role that bypasses all checks (mirrors `CaslModule.forRoot`). */\n superuserRole?: Roles;\n\n /** Custom subject-type detection (mirrors `CaslModule.forRoot`). */\n detectSubjectType?: (subject: object) => string;\n}\n\n/**\n * Build a CASL ability directly from a permissions map and a user, without\n * booting a Nest application. Intended for unit-testing permission definitions\n * in projects that consume `@jperezmart/nest-casl`.\n *\n * @example\n * ```ts\n * const ability = buildAbilityForTest(permissions, { id: \"1\", roles: [\"author\"] });\n * expect(ability.can(\"update\", subject(\"Article\", { authorId: \"1\" }))).toBe(true);\n * ```\n */\nexport function buildAbilityForTest<\n Roles extends string = string,\n TUser extends AuthorizableUser<Roles> = AuthorizableUser<Roles>,\n TAbility extends AnyAbility = AnyAbility,\n>(\n permissions: Permissions<Roles, TUser, TAbility>,\n user: TUser,\n options: BuildAbilityForTestOptions<Roles> = {},\n): TAbility {\n const builder = new AbilityBuilder<MongoAbility>(createMongoAbility);\n const buildOptions = options.detectSubjectType\n ? { detectSubjectType: options.detectSubjectType }\n : undefined;\n\n // Mirror AbilityFactory: a missing / non-array `roles` means \"no roles\".\n const roles: string[] = Array.isArray(user.roles) ? user.roles : [];\n\n if (\n options.superuserRole !== undefined &&\n roles.includes(options.superuserRole)\n ) {\n builder.can('manage', 'all');\n return builder.build(buildOptions) as unknown as TAbility;\n }\n\n for (const role of roles) {\n const definition = (permissions as Record<string, unknown>)[role];\n if (definition === true) {\n builder.can('manage', 'all');\n } else if (typeof definition === 'function') {\n (definition as DefinePermissions<TUser, AnyAbility>)(\n user,\n builder as unknown as AbilityBuilder<AnyAbility>,\n );\n }\n }\n\n return builder.build(buildOptions) as unknown as TAbility;\n}\n"],"mappings":";;;;;;;;;;;;;;AA4BA,SAAgB,oBAKd,aACA,MACA,UAA6C,CAAC,GACpC;CACV,MAAM,UAAU,IAAIA,cAAAA,eAA6BC,cAAAA,kBAAkB;CACnE,MAAM,eAAe,QAAQ,oBACzB,EAAE,mBAAmB,QAAQ,kBAAkB,IAC/C,KAAA;CAGJ,MAAM,QAAkB,MAAM,QAAQ,KAAK,KAAK,IAAI,KAAK,QAAQ,CAAC;CAElE,IACE,QAAQ,kBAAkB,KAAA,KAC1B,MAAM,SAAS,QAAQ,aAAa,GACpC;EACA,QAAQ,IAAI,UAAU,KAAK;EAC3B,OAAO,QAAQ,MAAM,YAAY;CACnC;CAEA,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,aAAc,YAAwC;EAC5D,IAAI,eAAe,MACjB,QAAQ,IAAI,UAAU,KAAK;OACtB,IAAI,OAAO,eAAe,YAC/B,WACE,MACA,OACF;CAEJ;CAEA,OAAO,QAAQ,MAAM,YAAY;AACnC"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AnyAbility } from "@casl/ability";
|
|
2
|
+
import { AuthorizableUser, Permissions } from "@jperezmart/nest-casl";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/** Extra options accepted by {@link buildAbilityForTest}. */
|
|
6
|
+
interface BuildAbilityForTestOptions<Roles extends string = string> {
|
|
7
|
+
/** Role that bypasses all checks (mirrors `CaslModule.forRoot`). */
|
|
8
|
+
superuserRole?: Roles;
|
|
9
|
+
/** Custom subject-type detection (mirrors `CaslModule.forRoot`). */
|
|
10
|
+
detectSubjectType?: (subject: object) => string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Build a CASL ability directly from a permissions map and a user, without
|
|
14
|
+
* booting a Nest application. Intended for unit-testing permission definitions
|
|
15
|
+
* in projects that consume `@jperezmart/nest-casl`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const ability = buildAbilityForTest(permissions, { id: "1", roles: ["author"] });
|
|
20
|
+
* expect(ability.can("update", subject("Article", { authorId: "1" }))).toBe(true);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare function buildAbilityForTest<Roles extends string = string, TUser extends AuthorizableUser<Roles> = AuthorizableUser<Roles>, TAbility extends AnyAbility = AnyAbility>(permissions: Permissions<Roles, TUser, TAbility>, user: TUser, options?: BuildAbilityForTestOptions<Roles>): TAbility;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { BuildAbilityForTestOptions, buildAbilityForTest };
|
|
26
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;UASiB,0BAAA;EAAA;EAEf,aAAA,GAAgB,KAAK;EAFoB;EAKzC,iBAAA,IAAqB,OAAA;AAAA;;;;;;AAAe;AActC;;;;;iBAAgB,mBAAA,8CAEA,gBAAA,CAAiB,KAAA,IAAS,gBAAA,CAAiB,KAAA,oBACxC,UAAA,GAAa,UAAA,EAE9B,WAAA,EAAa,WAAA,CAAY,KAAA,EAAO,KAAA,EAAO,QAAA,GACvC,IAAA,EAAM,KAAA,EACN,OAAA,GAAS,0BAAA,CAA2B,KAAA,IACnC,QAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AnyAbility } from "@casl/ability";
|
|
2
|
+
import { AuthorizableUser, Permissions } from "@jperezmart/nest-casl";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
/** Extra options accepted by {@link buildAbilityForTest}. */
|
|
6
|
+
interface BuildAbilityForTestOptions<Roles extends string = string> {
|
|
7
|
+
/** Role that bypasses all checks (mirrors `CaslModule.forRoot`). */
|
|
8
|
+
superuserRole?: Roles;
|
|
9
|
+
/** Custom subject-type detection (mirrors `CaslModule.forRoot`). */
|
|
10
|
+
detectSubjectType?: (subject: object) => string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Build a CASL ability directly from a permissions map and a user, without
|
|
14
|
+
* booting a Nest application. Intended for unit-testing permission definitions
|
|
15
|
+
* in projects that consume `@jperezmart/nest-casl`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const ability = buildAbilityForTest(permissions, { id: "1", roles: ["author"] });
|
|
20
|
+
* expect(ability.can("update", subject("Article", { authorId: "1" }))).toBe(true);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare function buildAbilityForTest<Roles extends string = string, TUser extends AuthorizableUser<Roles> = AuthorizableUser<Roles>, TAbility extends AnyAbility = AnyAbility>(permissions: Permissions<Roles, TUser, TAbility>, user: TUser, options?: BuildAbilityForTestOptions<Roles>): TAbility;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { BuildAbilityForTestOptions, buildAbilityForTest };
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;UASiB,0BAAA;EAAA;EAEf,aAAA,GAAgB,KAAK;EAFoB;EAKzC,iBAAA,IAAqB,OAAA;AAAA;;;;;;AAAe;AActC;;;;;iBAAgB,mBAAA,8CAEA,gBAAA,CAAiB,KAAA,IAAS,gBAAA,CAAiB,KAAA,oBACxC,UAAA,GAAa,UAAA,EAE9B,WAAA,EAAa,WAAA,CAAY,KAAA,EAAO,KAAA,EAAO,QAAA,GACvC,IAAA,EAAM,KAAA,EACN,OAAA,GAAS,0BAAA,CAA2B,KAAA,IACnC,QAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AbilityBuilder, createMongoAbility } from "@casl/ability";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* Build a CASL ability directly from a permissions map and a user, without
|
|
5
|
+
* booting a Nest application. Intended for unit-testing permission definitions
|
|
6
|
+
* in projects that consume `@jperezmart/nest-casl`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const ability = buildAbilityForTest(permissions, { id: "1", roles: ["author"] });
|
|
11
|
+
* expect(ability.can("update", subject("Article", { authorId: "1" }))).toBe(true);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
function buildAbilityForTest(permissions, user, options = {}) {
|
|
15
|
+
const builder = new AbilityBuilder(createMongoAbility);
|
|
16
|
+
const buildOptions = options.detectSubjectType ? { detectSubjectType: options.detectSubjectType } : void 0;
|
|
17
|
+
const roles = Array.isArray(user.roles) ? user.roles : [];
|
|
18
|
+
if (options.superuserRole !== void 0 && roles.includes(options.superuserRole)) {
|
|
19
|
+
builder.can("manage", "all");
|
|
20
|
+
return builder.build(buildOptions);
|
|
21
|
+
}
|
|
22
|
+
for (const role of roles) {
|
|
23
|
+
const definition = permissions[role];
|
|
24
|
+
if (definition === true) builder.can("manage", "all");
|
|
25
|
+
else if (typeof definition === "function") definition(user, builder);
|
|
26
|
+
}
|
|
27
|
+
return builder.build(buildOptions);
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { buildAbilityForTest };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { AnyAbility, MongoAbility } from '@casl/ability';\nimport { AbilityBuilder, createMongoAbility } from '@casl/ability';\nimport type {\n AuthorizableUser,\n DefinePermissions,\n Permissions,\n} from '@jperezmart/nest-casl';\n\n/** Extra options accepted by {@link buildAbilityForTest}. */\nexport interface BuildAbilityForTestOptions<Roles extends string = string> {\n /** Role that bypasses all checks (mirrors `CaslModule.forRoot`). */\n superuserRole?: Roles;\n\n /** Custom subject-type detection (mirrors `CaslModule.forRoot`). */\n detectSubjectType?: (subject: object) => string;\n}\n\n/**\n * Build a CASL ability directly from a permissions map and a user, without\n * booting a Nest application. Intended for unit-testing permission definitions\n * in projects that consume `@jperezmart/nest-casl`.\n *\n * @example\n * ```ts\n * const ability = buildAbilityForTest(permissions, { id: \"1\", roles: [\"author\"] });\n * expect(ability.can(\"update\", subject(\"Article\", { authorId: \"1\" }))).toBe(true);\n * ```\n */\nexport function buildAbilityForTest<\n Roles extends string = string,\n TUser extends AuthorizableUser<Roles> = AuthorizableUser<Roles>,\n TAbility extends AnyAbility = AnyAbility,\n>(\n permissions: Permissions<Roles, TUser, TAbility>,\n user: TUser,\n options: BuildAbilityForTestOptions<Roles> = {},\n): TAbility {\n const builder = new AbilityBuilder<MongoAbility>(createMongoAbility);\n const buildOptions = options.detectSubjectType\n ? { detectSubjectType: options.detectSubjectType }\n : undefined;\n\n // Mirror AbilityFactory: a missing / non-array `roles` means \"no roles\".\n const roles: string[] = Array.isArray(user.roles) ? user.roles : [];\n\n if (\n options.superuserRole !== undefined &&\n roles.includes(options.superuserRole)\n ) {\n builder.can('manage', 'all');\n return builder.build(buildOptions) as unknown as TAbility;\n }\n\n for (const role of roles) {\n const definition = (permissions as Record<string, unknown>)[role];\n if (definition === true) {\n builder.can('manage', 'all');\n } else if (typeof definition === 'function') {\n (definition as DefinePermissions<TUser, AnyAbility>)(\n user,\n builder as unknown as AbilityBuilder<AnyAbility>,\n );\n }\n }\n\n return builder.build(buildOptions) as unknown as TAbility;\n}\n"],"mappings":";;;;;;;;;;;;;AA4BA,SAAgB,oBAKd,aACA,MACA,UAA6C,CAAC,GACpC;CACV,MAAM,UAAU,IAAI,eAA6B,kBAAkB;CACnE,MAAM,eAAe,QAAQ,oBACzB,EAAE,mBAAmB,QAAQ,kBAAkB,IAC/C,KAAA;CAGJ,MAAM,QAAkB,MAAM,QAAQ,KAAK,KAAK,IAAI,KAAK,QAAQ,CAAC;CAElE,IACE,QAAQ,kBAAkB,KAAA,KAC1B,MAAM,SAAS,QAAQ,aAAa,GACpC;EACA,QAAQ,IAAI,UAAU,KAAK;EAC3B,OAAO,QAAQ,MAAM,YAAY;CACnC;CAEA,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,aAAc,YAAwC;EAC5D,IAAI,eAAe,MACjB,QAAQ,IAAI,UAAU,KAAK;OACtB,IAAI,OAAO,eAAe,YAC/B,WACE,MACA,OACF;CAEJ;CAEA,OAAO,QAAQ,MAAM,YAAY;AACnC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jperezmart/nest-casl-testing",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Testing utilities for @jperezmart/nest-casl — build and assert abilities without booting Nest.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Javier Pérez Martín <javier.perez.martin97@gmail.com>",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"main": "./dist/index.cjs",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@casl/ability": "^7.0.0",
|
|
32
|
+
"eslint": "^10.3.0",
|
|
33
|
+
"tsdown": "^0.22.2",
|
|
34
|
+
"typescript": "^6.0.3",
|
|
35
|
+
"vitest": "^4.1.9",
|
|
36
|
+
"@jperezmart/eslint-config": "0.0.0",
|
|
37
|
+
"@jperezmart/nest-casl": "0.1.0",
|
|
38
|
+
"@jperezmart/typescript-config": "0.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@casl/ability": "^7.0.0",
|
|
42
|
+
"@jperezmart/nest-casl": "^0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsdown",
|
|
49
|
+
"clean": "rm -rf dist .turbo *.tsbuildinfo",
|
|
50
|
+
"dev": "tsdown --watch",
|
|
51
|
+
"lint": "eslint .",
|
|
52
|
+
"test": "vitest run",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
}
|
|
55
|
+
}
|