|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Tests\Chain\Toolbox\Security; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Chain\Toolbox\Event\ToolCallArgumentsResolved; |
| 8 | +use PhpLlm\LlmChain\Chain\Toolbox\Security\EventListener\IsGrantedAttributeListener; |
| 9 | +use PhpLlm\LlmChain\Platform\Tool\ExecutionReference; |
| 10 | +use PhpLlm\LlmChain\Platform\Tool\Tool; |
| 11 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolWithIsGrantedOnClass; |
| 12 | +use PhpLlm\LlmChain\Tests\Fixture\Tool\ToolWithIsGrantedOnMethod; |
| 13 | +use PHPUnit\Framework\Attributes\Before; |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use PHPUnit\Framework\Attributes\TestWith; |
| 17 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 21 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 22 | +use Symfony\Component\ExpressionLanguage\Expression; |
| 23 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
| 24 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 25 | + |
| 26 | +#[CoversClass(IsGrantedAttributeListener::class)] |
| 27 | +#[UsesClass(EventDispatcher::class)] |
| 28 | +#[UsesClass(ToolCallArgumentsResolved::class)] |
| 29 | +#[UsesClass(Expression::class)] |
| 30 | +#[UsesClass(AccessDeniedException::class)] |
| 31 | +#[UsesClass(Tool::class)] |
| 32 | +#[UsesClass(ExecutionReference::class)] |
| 33 | +class IsGrantedAttributeListenerTest extends TestCase |
| 34 | +{ |
| 35 | + private EventDispatcherInterface $dispatcher; |
| 36 | + private AuthorizationCheckerInterface&MockObject $authChecker; |
| 37 | + |
| 38 | + #[Before] |
| 39 | + protected function setupTool(): void |
| 40 | + { |
| 41 | + $this->dispatcher = new EventDispatcher(); |
| 42 | + $this->authChecker = $this->createMock(AuthorizationCheckerInterface::class); |
| 43 | + $this->dispatcher->addListener(ToolCallArgumentsResolved::class, new IsGrantedAttributeListener($this->authChecker)); |
| 44 | + } |
| 45 | + |
| 46 | + #[Test] |
| 47 | + #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'simple'), 'simple', '')])] |
| 48 | + #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'argumentAsSubject'), 'argumentAsSubject', '')])] |
| 49 | + #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'expressionAsSubject'), 'expressionAsSubject', '')])] |
| 50 | + #[TestWith([new ToolWithIsGrantedOnClass(), new Tool(new ExecutionReference(ToolWithIsGrantedOnClass::class, '__invoke'), 'ToolWithIsGrantedOnClass', '')])] |
| 51 | + public function itWillThrowWhenNotGranted(object $tool, Tool $metadata): void |
| 52 | + { |
| 53 | + $this->authChecker->expects(self::once())->method('isGranted')->willReturn(false); |
| 54 | + |
| 55 | + self::expectException(AccessDeniedException::class); |
| 56 | + self::expectExceptionMessage(\sprintf('No access to %s tool.', $metadata->name)); |
| 57 | + $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, [])); |
| 58 | + } |
| 59 | + |
| 60 | + #[Test] |
| 61 | + #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'simple'), '', '')], 'method')] |
| 62 | + public function itWillNotThrowWhenGranted(object $tool, Tool $metadata): void |
| 63 | + { |
| 64 | + $this->authChecker->expects(self::once())->method('isGranted')->with('ROLE_USER')->willReturn(true); |
| 65 | + $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, [])); |
| 66 | + } |
| 67 | + |
| 68 | + #[Test] |
| 69 | + #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'argumentAsSubject'), '', '')], 'method')] |
| 70 | + #[TestWith([new ToolWithIsGrantedOnClass(), new Tool(new ExecutionReference(ToolWithIsGrantedOnClass::class, '__invoke'), '', '')], 'class')] |
| 71 | + public function itWillProvideArgumentAsSubject(object $tool, Tool $metadata): void |
| 72 | + { |
| 73 | + $this->authChecker->expects(self::once())->method('isGranted')->with('test:permission', 44)->willReturn(true); |
| 74 | + $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, ['itemId' => 44])); |
| 75 | + } |
| 76 | + |
| 77 | + #[Test] |
| 78 | + #[TestWith([new ToolWithIsGrantedOnMethod(), new Tool(new ExecutionReference(ToolWithIsGrantedOnMethod::class, 'expressionAsSubject'), '', '')], 'method')] |
| 79 | + public function itWillEvaluateSubjectExpression(object $tool, Tool $metadata): void |
| 80 | + { |
| 81 | + $this->authChecker->expects(self::once())->method('isGranted')->with('test:permission', 44)->willReturn(true); |
| 82 | + $this->dispatcher->dispatch(new ToolCallArgumentsResolved($tool, $metadata, ['itemId' => 44])); |
| 83 | + } |
| 84 | +} |
0 commit comments