-
Hello! What a great project this is, but I'm having some trouble wrapping my head around how to use MongoAbility. I've read through this doc page, https://casl.js.org/v6/en/api/casl-ability, and some issues/discussions but I'm not any wiser. First of all, this is my code: export const defineUserAbility = (user: User, groupIds: string[]) => {
const {
can: allow,
cannot: forbid,
build,
} = new AbilityBuilder(createMongoAbility);
if (!user.onboarded) {
return build();
}
if (user.role === "TEACHER") {
allow("create", "Group");
allow("create", "Task", { groupId: { $in: groupIds } });
allow("create", "Invite", { groupId: { $in: groupIds } });
}
if (user.role === "STUDENT") {
allow("join", "Group");
}
return build();
}; From this I then do So now in my test I have the following: const ability = defineUserAbility(user, ["group1", "group2"]);
expect(ability.can("create", "Group")).toBeTrue(); // works fine
expect(
ability.can("create", "Task", subject("Task", { groupId: "group1" })),
).toBeTrue(); the first one works, but the second one gives me a TypeError:
and if I ignore the error I get the following output when executing:
So it feels like it's using the Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
expect(
ability.can("create", subject("Task", { groupId: "group1" })),
).toBeTrue(); try this ^^^ |
Beta Was this translation helpful? Give feedback.
try this ^^^