How to test policies correctly?
Hello, I am confused a tiny bit about policy testing.
As an example, right now what I do is the following:
Previously, I was doing this:
Both pass, but I am unsure as to what I should be doing... Maybe matching on the type of error is better (not found, forbidden, etc), instead of matching on any error?
6 Replies
Solution
You can use
Ash.can
or for code interfaces their can_
variantAnd assert on the result of that
You can provide data to assert that a particular record wouldn't or would be seen by that request
refute Accounts.can_get_user_by_email?(actor, users.admin.email, actor: actor, data: users.admin)
You're right, forgot about that
yea, I understand. Thanks!
I am just gonna drop one more comment here if anyone in the future reads this.
The
can_
variant doesn't work exactly as I would expect due to the default acceses_type
(filter
). This means that it returns a not found error when I call my read
action, which in turn means that can_
actually returns true even when I would expect it to return false.
All in all, I am just gonna match on the error probably 😛That's the purpose of the data option
It means "if you run this action does it return specifically this thing"
Causing it to return false
Hmmm, I'll read the docs about it later again. Thanks for your help!