bootstrap test for isGranted

This commit is contained in:
Julien Fastré 2016-02-01 23:20:40 +01:00
parent a4fbf27519
commit bcdee0be94
3 changed files with 58 additions and 8 deletions

View File

@ -28,7 +28,7 @@ use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Chill\MainBundle\Form\Type\Export\PickCenterType; use Chill\MainBundle\Form\Type\Export\PickCenterType;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
@ -104,7 +104,7 @@ class ExportManager
public function __construct( public function __construct(
LoggerInterface $logger, LoggerInterface $logger,
EntityManagerInterface $em, EntityManagerInterface $em,
AuthorizationChecker $authorizationChecker, AuthorizationCheckerInterface $authorizationChecker,
AuthorizationHelper $authorizationHelper, AuthorizationHelper $authorizationHelper,
TokenStorageInterface $tokenStorage) TokenStorageInterface $tokenStorage)
{ {

View File

@ -45,10 +45,10 @@ trait PrepareUserTrait
* ``` * ```
* array( * array(
* array( 'center' => $centerA, 'permissionsGroup' => array( * array( 'center' => $centerA, 'permissionsGroup' => array(
* [ 'role' => 'CHILL_REPORT_SEE', 'scope' => $scopeA] * 'role' => 'CHILL_REPORT_SEE', 'scope' => $scopeA
* ), * ),
* array( 'center' => $centerB, 'permissionsGroup' => array( * array( 'center' => $centerB, 'permissionsGroup' => array(
* [ 'role' => 'CHILL_ACTIVITY_UPDATE', 'scope' => $scopeB] * 'role' => 'CHILL_ACTIVITY_UPDATE', 'scope' => $scopeB
* ) * )
* ) * )
* ``` * ```

View File

@ -21,6 +21,7 @@ namespace Chill\MainBundle\Tests\Export;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Chill\MainBundle\Export\ExportManager; use Chill\MainBundle\Export\ExportManager;
use Symfony\Component\Security\Core\Role\Role;
/** /**
* Test the export manager * Test the export manager
@ -30,6 +31,11 @@ use Chill\MainBundle\Export\ExportManager;
*/ */
class ExportManagerTest extends KernelTestCase class ExportManagerTest extends KernelTestCase
{ {
use \Chill\MainBundle\Test\PrepareCenterTrait;
use \Chill\MainBundle\Test\PrepareUserTrait;
use \Chill\MainBundle\Test\PrepareScopeTrait;
/** /**
* *
* @var \Symfony\Component\DependencyInjection\ContainerInterface * @var \Symfony\Component\DependencyInjection\ContainerInterface
@ -69,10 +75,10 @@ class ExportManagerTest extends KernelTestCase
* @param \Symfony\Component\Security\Core\User\UserInterface $user * @param \Symfony\Component\Security\Core\User\UserInterface $user
* @return ExportManager * @return ExportManager
*/ */
public function createExportManager( protected function createExportManager(
\Psr\Log\LoggerInterface $logger = null, \Psr\Log\LoggerInterface $logger = null,
\Doctrine\ORM\EntityManagerInterface $em = null, \Doctrine\ORM\EntityManagerInterface $em = null,
\Symfony\Component\Security\Core\Authorization\AuthorizationChecker $authorizationChecker = null, \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface $authorizationChecker = null,
\Chill\MainBundle\Security\Authorization\AuthorizationHelper $authorizationHelper = null, \Chill\MainBundle\Security\Authorization\AuthorizationHelper $authorizationHelper = null,
\Symfony\Component\Security\Core\User\UserInterface $user = null \Symfony\Component\Security\Core\User\UserInterface $user = null
) )
@ -287,9 +293,53 @@ class ExportManagerTest extends KernelTestCase
$this->assertNotContains($formatterBar->reveal(), $obtained); $this->assertNotContains($formatterBar->reveal(), $obtained);
} }
public function testIsGrantedForElementUserIsGranted() public function testIsGrantedForElementWithExportAndUserIsGranted()
{ {
$this->markTestSkipped(); $center = $this->prepareCenter(100, 'center A');
$user = $this->prepareUser(array());
$authorizationChecker = $this->prophet->prophesize();
$authorizationChecker->willImplement('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
$authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center)
->willReturn(True);
$exportManager = $this->createExportManager(null, null,
$authorizationChecker->reveal(), null, $user);
$export = $this->prophet->prophesize();
$export->willImplement('Chill\MainBundle\Export\ExportInterface');
$export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY'));
$result = $exportManager->isGrantedForElement($export->reveal(), null, array($center));
$this->assertTrue($result);
}
public function testIsGrantedForElementWithExportAndUserIsGrantedNotForAllCenters()
{
$center = $this->prepareCenter(100, 'center A');
$centerB = $this->prepareCenter(102, 'center B');
$user = $this->prepareUser(array());
$authorizationChecker = $this->prophet->prophesize();
$authorizationChecker->willImplement('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
$authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center)
->willReturn(true);
$authorizationChecker->isGranted('CHILL_STAT_DUMMY', $centerB)
->willReturn(false);
$exportManager = $this->createExportManager(null, null,
$authorizationChecker->reveal(), null, $user);
$export = $this->prophet->prophesize();
$export->willImplement('Chill\MainBundle\Export\ExportInterface');
$export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY'));
$result = $exportManager->isGrantedForElement($export->reveal(), null, array($center, $centerB));
$this->assertFalse($result);
} }