JosephSilber / bouncer

Laravel Eloquent roles and abilities.
MIT License
3.43k stars 330 forks source link

Feature test: After calling an endpoint with json(MEHTOD, URL) that sets an ability, user->can doesn't work #614

Closed robertoperez-digital closed 1 year ago

robertoperez-digital commented 1 year ago

Hi! I'm facing a weird issue

In my feature test I call this

$this->actingAs($user)
    ->json('POST', "URL that creates an object", $data);

In the controller function I do

$object = Object::create([data]);
Bouncer::allow($user)->to('ability', $object);

After the call I check in my test if $user->can('ability', $object); It doesn't return true, I printed the result and is not true, nor false, is like empty??

Additional info: I have a custom Ability class registered in AppServiceProvider

Bouncer::useAbilityModel(Ability::class);

Funny thing, If I call that endpoint from an external tool like postman, everything is working as expected, it is just acting weird in the tests.

Can you give some light on this?

Any info you can give will be very appreciated.

Thank you.

robertoperez-digital commented 1 year ago

Sorry to bother, I found my problem. It was the Bouncer cache trolling me, after the call I tested using Bouncer::refresh(); And $user->can('ability', $object); Returned true as expected I disabled cache for the whole test