2019-01-27 02:50:01 +01:00
|
|
|
<?php
|
|
|
|
|
2019-05-30 16:57:55 +02:00
|
|
|
namespace tests\Unit\Domain\Secure;
|
2019-01-27 02:50:01 +01:00
|
|
|
|
2019-02-17 14:33:19 +01:00
|
|
|
use Infinito\DBAL\Types\Meta\Right\CRUDType;
|
2020-04-02 21:13:35 +02:00
|
|
|
use Infinito\DBAL\Types\Meta\Right\LayerType;
|
2019-05-30 16:52:02 +02:00
|
|
|
use Infinito\Domain\Request\Entity\RequestedEntityInterface;
|
2020-04-02 21:13:35 +02:00
|
|
|
use Infinito\Domain\Request\Right\RequestedRight;
|
2019-05-30 16:52:02 +02:00
|
|
|
use Infinito\Domain\Request\Right\RequestedRightInterface;
|
2020-04-02 21:13:35 +02:00
|
|
|
use Infinito\Domain\Right\RightTransformerService;
|
|
|
|
use Infinito\Domain\Secure\SecureRequestedRightCheckerService;
|
|
|
|
use Infinito\Entity\Meta\Right;
|
|
|
|
use Infinito\Entity\Source\AbstractSource;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-01-27 02:50:01 +01:00
|
|
|
|
2019-01-27 09:11:34 +01:00
|
|
|
/**
|
|
|
|
* @author kevinfrantz
|
|
|
|
*/
|
2019-02-16 09:19:49 +01:00
|
|
|
class SecureRequestedRightCheckerServiceTest extends TestCase
|
2019-01-27 02:50:01 +01:00
|
|
|
{
|
|
|
|
public function testGranted(): void
|
|
|
|
{
|
|
|
|
$reciever = new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
$layer = LayerType::SOURCE;
|
|
|
|
$crud = CRUDType::READ;
|
|
|
|
$source = new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
$right = new Right();
|
|
|
|
$right->setSource($source);
|
|
|
|
$right->setLayer($layer);
|
2019-02-21 18:46:57 +01:00
|
|
|
$right->setActionType($crud);
|
2019-01-27 02:50:01 +01:00
|
|
|
$right->setReciever($reciever);
|
|
|
|
$source->getLaw()->getRights()->add($right);
|
|
|
|
$requestedRight = new RequestedRight();
|
2019-02-21 18:46:57 +01:00
|
|
|
$requestedRight->setActionType($crud);
|
2019-01-27 02:50:01 +01:00
|
|
|
$requestedRight->setLayer($layer);
|
|
|
|
$requestedRight->setReciever($reciever);
|
|
|
|
$requestedEntity = $this->createMock(RequestedEntityInterface::class);
|
|
|
|
$requestedEntity->method('hasId')->willReturn(true);
|
|
|
|
$requestedEntity->method('getEntity')->willReturn($source);
|
2019-04-15 01:37:17 +02:00
|
|
|
$requestedEntity->method('hasIdentity')->willReturn(true);
|
2019-01-27 02:50:01 +01:00
|
|
|
$requestedRight->setRequestedEntity($requestedEntity);
|
|
|
|
$rightTransformerService = new RightTransformerService();
|
2019-02-16 09:19:49 +01:00
|
|
|
$secureEntityChecker = new SecureRequestedRightCheckerService($rightTransformerService);
|
2019-01-27 02:50:01 +01:00
|
|
|
$result = $secureEntityChecker->check($requestedRight);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotGranted(): void
|
|
|
|
{
|
|
|
|
$reciever = new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
$layer = LayerType::SOURCE;
|
|
|
|
$crud = CRUDType::READ;
|
|
|
|
$source = new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
$right = new Right();
|
|
|
|
$right->setSource($source);
|
|
|
|
$right->setLayer($layer);
|
2019-02-21 18:46:57 +01:00
|
|
|
$right->setActionType(CRUDType::CREATE);
|
2019-01-27 02:50:01 +01:00
|
|
|
$right->setReciever($reciever);
|
|
|
|
$source->getLaw()->getRights()->add($right);
|
|
|
|
$requestedRight = new RequestedRight();
|
2019-02-21 18:46:57 +01:00
|
|
|
$requestedRight->setActionType($crud);
|
2019-01-27 02:50:01 +01:00
|
|
|
$requestedRight->setLayer($layer);
|
|
|
|
$requestedRight->setReciever($reciever);
|
|
|
|
$requestedEntity = $this->createMock(RequestedEntityInterface::class);
|
|
|
|
$requestedEntity->method('hasId')->willReturn(true);
|
|
|
|
$requestedEntity->method('getEntity')->willReturn($source);
|
2019-04-15 01:37:17 +02:00
|
|
|
$requestedEntity->method('hasIdentity')->willReturn(true);
|
2019-01-27 02:50:01 +01:00
|
|
|
$requestedRight->setRequestedEntity($requestedEntity);
|
|
|
|
$rightTransformerService = new RightTransformerService();
|
2019-02-16 09:19:49 +01:00
|
|
|
$secureEntityChecker = new SecureRequestedRightCheckerService($rightTransformerService);
|
2019-01-27 02:50:01 +01:00
|
|
|
$result = $secureEntityChecker->check($requestedRight);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2019-02-16 16:45:05 +01:00
|
|
|
|
|
|
|
public function testRightAppliesToAll(): void
|
|
|
|
{
|
|
|
|
$reciever = new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
$layer = LayerType::SOURCE;
|
|
|
|
$crud = CRUDType::READ;
|
|
|
|
$source = new class() extends AbstractSource {
|
|
|
|
};
|
|
|
|
$requestedRight = $this->createMock(RequestedRightInterface::class);
|
2019-02-21 18:46:57 +01:00
|
|
|
$requestedRight->method('getActionType')->willReturn($crud);
|
2019-02-16 16:45:05 +01:00
|
|
|
$requestedRight->method('getLayer')->willReturn($layer);
|
|
|
|
$requestedRight->method('getReciever')->willReturn($reciever);
|
|
|
|
$requestedRight->method('getSource')->willReturn($source);
|
|
|
|
$rightTransformerService = new RightTransformerService();
|
|
|
|
$secureEntityChecker = new SecureRequestedRightCheckerService($rightTransformerService);
|
|
|
|
$this->assertFalse($secureEntityChecker->check($requestedRight));
|
|
|
|
$right = new Right();
|
2019-02-21 18:46:57 +01:00
|
|
|
$right->setActionType($crud);
|
2019-02-16 16:45:05 +01:00
|
|
|
$right->setLayer($layer);
|
|
|
|
$right->setSource($source);
|
|
|
|
$source->getLaw()->getRights()->add($right);
|
|
|
|
$this->assertTrue($secureEntityChecker->check($requestedRight));
|
|
|
|
$right->setReciever(new class() extends AbstractSource {
|
|
|
|
});
|
|
|
|
$this->assertFalse($secureEntityChecker->check($requestedRight));
|
|
|
|
}
|
2019-01-27 02:50:01 +01:00
|
|
|
}
|