<?php
namespace Laravel\Sanctum\Tests;
use Laravel\Sanctum\Http\Middleware\CheckAbilities;
use Mockery;
use PHPUnit\Framework\TestCase;
class CheckAbilitiesTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
Mockery::close();
}
public function test_request_is_passed_along_if_abilities_are_present_on_token()
{
$middleware = new CheckAbilities;
$request = Mockery::mock();
$request->shouldReceive('user')->andReturn($user = Mockery::mock());
$user->shouldReceive('currentAccessToken')->andReturn($token = Mockery::mock());
$user->shouldReceive('tokenCan')->with('foo')->andReturn(true);
$user->shouldReceive('tokenCan')->with('bar')->andReturn(true);
$response = $middleware->handle($request, function () {
return 'response';
}, 'foo', 'bar');
$this->assertSame('response', $response);
}
public function test_exception_is_thrown_if_token_doesnt_have_ability()
{
$this->expectException('Laravel\Sanctum\Exceptions\MissingAbilityException');
$middleware = new CheckAbilities;
$request = Mockery::mock();
$request->shouldReceive('user')->andReturn($user = Mockery::mock());
$user->shouldReceive('currentAccessToken')->andReturn($token = Mockery::mock());
$user->shouldReceive('tokenCan')->with('foo')->andReturn(false);
$middleware->handle($request, function () {
return 'response';
}, 'foo', 'bar');
}
public function test_exception_is_thrown_if_no_authenticated_user()
{
$this->expectException('Illuminate\Auth\AuthenticationException');
$middleware = new CheckAbilities;
$request = Mockery::mock();
$request->shouldReceive('user')->once()->andReturn(null);
$middleware->handle($request, function () {
return 'response';
}, 'foo', 'bar');
}
public function test_exception_is_thrown_if_no_token()
{
$this->expectException('Illuminate\Auth\AuthenticationException');
$middleware = new CheckAbilities;
$request = Mockery::mock();
$request->shouldReceive('user')->andReturn($user = Mockery::mock());
$user->shouldReceive('currentAccessToken')->andReturn(null);
$middleware->handle($request, function () {
return 'response';
}, 'foo', 'bar');
}
}