mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
apply new role on accompanying period
This commit is contained in:
@@ -30,22 +30,22 @@ use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class AuthorizationHelperTest extends KernelTestCase
|
||||
{
|
||||
|
||||
|
||||
use PrepareUserTrait, PrepareCenterTrait, PrepareScopeTrait, ProphecyTrait;
|
||||
|
||||
public function setUp()
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
static::bootKernel();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return \Chill\MainBundle\Security\Authorization\AuthorizationHelper
|
||||
*/
|
||||
private function getAuthorizationHelper()
|
||||
@@ -54,13 +54,13 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
->get('chill.main.security.authorization.helper')
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test function userCanReach of helper.
|
||||
*
|
||||
*
|
||||
* A user can reach center => the function should return true.
|
||||
*/
|
||||
public function testUserCanReachCenter_UserShouldReach()
|
||||
public function testUserCanReachCenter_UserShouldReach()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
$scope = $this->prepareScope(1, 'default');
|
||||
@@ -72,16 +72,16 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
)
|
||||
));
|
||||
$helper = $this->getAuthorizationHelper();
|
||||
|
||||
|
||||
$this->assertTrue($helper->userCanReachCenter($user, $center));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test function userCanReach of helper
|
||||
*
|
||||
*
|
||||
* A user can not reachcenter =>W the function should return false
|
||||
*/
|
||||
public function testUserCanReachCenter_UserShouldNotReach()
|
||||
public function testUserCanReachCenter_UserShouldNotReach()
|
||||
{
|
||||
$centerA = $this->prepareCenter(1, 'center');
|
||||
$centerB = $this->prepareCenter(2, 'centerB');
|
||||
@@ -94,11 +94,11 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
)
|
||||
));
|
||||
$helper = $this->getAuthorizationHelper();
|
||||
|
||||
|
||||
$this->assertFalse($helper->userCanReachCenter($user, $centerB));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testUserHasAccess_shouldHaveAccess_EntityWithoutScope()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
@@ -114,11 +114,11 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity = $this->getProphet()->prophesize();
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasCenterInterface');
|
||||
$entity->getCenter()->willReturn($center);
|
||||
|
||||
$this->assertTrue($helper->userHasAccess($user, $entity->reveal(),
|
||||
|
||||
$this->assertTrue($helper->userHasAccess($user, $entity->reveal(),
|
||||
'CHILL_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
public function testUserHasAccess_ShouldHaveAccessWithInheritance_EntityWithoutScope()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
@@ -130,17 +130,17 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
)
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
$helper = $this->getAuthorizationHelper();
|
||||
$entity = $this->getProphet()->prophesize();
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasCenterInterface');
|
||||
$entity->getCenter()->willReturn($center);
|
||||
|
||||
$this->assertTrue($helper->userHasAccess($user, $entity->reveal(),
|
||||
|
||||
$this->assertTrue($helper->userHasAccess($user, $entity->reveal(),
|
||||
'CHILL_INHERITED_ROLE_1'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function testuserHasAccess_UserHasNoRole_EntityWithoutScope()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
@@ -156,10 +156,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity = $this->getProphet()->prophesize();
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasCenterInterface');
|
||||
$entity->getCenter()->willReturn($center);
|
||||
|
||||
|
||||
$this->assertFalse($helper->userHasAccess($user, $entity->reveal(), 'CHILL_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* test that a user has no access on a entity, but is granted on the same role
|
||||
* on another center
|
||||
@@ -186,10 +186,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity = $this->getProphet()->prophesize();
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasCenterInterface');
|
||||
$entity->getCenter()->willReturn($centerA);
|
||||
|
||||
|
||||
$this->assertFalse($helper->userHasAccess($user, $entity->reveal(), 'CHILL_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
public function testtestUserHasAccess_UserShouldHaveAccess_EntityWithScope()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
@@ -207,10 +207,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasScopeInterface');
|
||||
$entity->getCenter()->willReturn($center);
|
||||
$entity->getScope()->willReturn($scope);
|
||||
|
||||
|
||||
$this->assertTrue($helper->userHasAccess($user, $entity->reveal(), 'CHILL_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
public function testUserHasAccess_UserHasNoRole_EntityWithScope()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
@@ -228,10 +228,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasScopeInterface');
|
||||
$entity->getCenter()->willReturn($center);
|
||||
$entity->getScope()->willReturn($scope);
|
||||
|
||||
|
||||
$this->assertFalse($helper->userHasAccess($user, $entity->reveal(), 'ANOTHER_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
public function testUserHasAccess_UserHasNoCenter_EntityWithScope()
|
||||
{
|
||||
$centerA = $this->prepareCenter(1, 'center'); //the user will have this center
|
||||
@@ -250,10 +250,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasScopeInterface');
|
||||
$entity->getCenter()->willReturn($centerB);
|
||||
$entity->getScope()->willReturn($scope);
|
||||
|
||||
|
||||
$this->assertFalse($helper->userHasAccess($user, $entity->reveal(), 'CHILL_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
public function testUserHasAccess_UserHasNoScope_EntityWithScope()
|
||||
{
|
||||
$center = $this->prepareCenter(1, 'center');
|
||||
@@ -272,12 +272,12 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$entity->willImplement('\Chill\MainBundle\Entity\HasScopeInterface');
|
||||
$entity->getCenter()->willReturn($center);
|
||||
$entity->getScope()->willReturn($scopeA);
|
||||
|
||||
|
||||
$this->assertFalse($helper->userHasAccess($user, $entity->reveal(), 'CHILL_ROLE'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @dataProvider dataProvider_getReachableCenters
|
||||
* @param Center $shouldHaveCenter
|
||||
* @param User $user
|
||||
@@ -288,7 +288,7 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
{
|
||||
$this->assertEquals($test, $result, $msg);
|
||||
}
|
||||
|
||||
|
||||
public function dataProvider_getReachableCenters()
|
||||
{
|
||||
$this->setUp();
|
||||
@@ -297,10 +297,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$scopeA = $this->prepareScope(1, 'scope default');
|
||||
$scopeB = $this->prepareScope(2, 'scope B');
|
||||
$scopeC = $this->prepareScope(3, 'scope C');
|
||||
|
||||
|
||||
$userA = $this->prepareUser(array(
|
||||
array(
|
||||
'center' => $centerA,
|
||||
'center' => $centerA,
|
||||
'permissionsGroup' => array(
|
||||
['scope' => $scopeB, 'role' => 'CHILL_ROLE_1'],
|
||||
['scope' => $scopeA, 'role' => 'CHILL_ROLE_2']
|
||||
@@ -313,62 +313,62 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
['scope' => $scopeC, 'role' => 'CHILL_ROLE_2']
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
));
|
||||
|
||||
|
||||
$ah = $this->getAuthorizationHelper();
|
||||
|
||||
|
||||
return array(
|
||||
// without scopes
|
||||
array(
|
||||
true,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
true,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_1'), null)),
|
||||
'center A should be available for userA, with role 1 '
|
||||
),
|
||||
array(
|
||||
true,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
true,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_2'), null)),
|
||||
'center A should be available for userA, with role 2 '
|
||||
),
|
||||
array(
|
||||
true,
|
||||
in_array($centerB, $ah->getReachableCenters($userA,
|
||||
true,
|
||||
in_array($centerB, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_2'), null)),
|
||||
'center A should be available for userA, with role 2 '
|
||||
),
|
||||
array(
|
||||
false,
|
||||
false,
|
||||
in_array($centerB, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_1'), null)),
|
||||
'center B should NOT be available for userA, with role 1 '
|
||||
),
|
||||
// with scope
|
||||
array(
|
||||
true,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
true,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_1'), $scopeB)),
|
||||
'center A should be available for userA, with role 1, scopeC '
|
||||
),
|
||||
array(
|
||||
false,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
false,
|
||||
in_array($centerA, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_2'), $scopeC)),
|
||||
'center A should NOT be available for userA, with role 2, scopeA '
|
||||
),
|
||||
array(
|
||||
true,
|
||||
in_array($centerB, $ah->getReachableCenters($userA,
|
||||
true,
|
||||
in_array($centerB, $ah->getReachableCenters($userA,
|
||||
new Role('CHILL_ROLE_2'), $scopeA)),
|
||||
'center B should be available for userA, with role 2, scopeA '
|
||||
),
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @dataProvider dataProvider_getReachableScopes
|
||||
* @param boolean $expectedResult
|
||||
* @param Scope $testedScope
|
||||
@@ -382,11 +382,11 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
{
|
||||
$reachableScopes = $this->getAuthorizationHelper()
|
||||
->getReachableScopes($user, $role, $center);
|
||||
|
||||
|
||||
$this->assertEquals($expectedResult, in_array($testedScope, $reachableScopes),
|
||||
$message);
|
||||
}
|
||||
|
||||
|
||||
public function dataProvider_getReachableScopes()
|
||||
{
|
||||
$centerA = $this->prepareCenter(1, 'center A');
|
||||
@@ -394,10 +394,10 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
$scopeA = $this->prepareScope(1, 'scope default');
|
||||
$scopeB = $this->prepareScope(2, 'scope B');
|
||||
$scopeC = $this->prepareScope(3, 'scope C');
|
||||
|
||||
|
||||
$userA = $this->prepareUser(array(
|
||||
array(
|
||||
'center' => $centerA,
|
||||
'center' => $centerA,
|
||||
'permissionsGroup' => array(
|
||||
['scope' => $scopeB, 'role' => 'CHILL_ROLE_1'],
|
||||
['scope' => $scopeA, 'role' => 'CHILL_ROLE_2']
|
||||
@@ -411,9 +411,9 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
['scope' => $scopeB, 'role' => 'CHILL_ROLE_2']
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
));
|
||||
|
||||
|
||||
return array(
|
||||
array(
|
||||
true,
|
||||
@@ -442,37 +442,30 @@ class AuthorizationHelperTest extends KernelTestCase
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testGetParentRoles()
|
||||
{
|
||||
$parentRoles = $this->getAuthorizationHelper()
|
||||
->getParentRoles(new Role('CHILL_INHERITED_ROLE_1'));
|
||||
|
||||
$this->assertContains(
|
||||
'CHILL_MASTER_ROLE',
|
||||
\array_map(
|
||||
function(Role $role) {
|
||||
return $role->getRole();
|
||||
},
|
||||
$parentRoles
|
||||
),
|
||||
->getParentRoles('CHILL_INHERITED_ROLE_1');
|
||||
|
||||
$this->assertContains('CHILL_MASTER_ROLE', $parentRoles,
|
||||
"Assert that `CHILL_MASTER_ROLE` is a parent of `CHILL_INHERITED_ROLE_1`");
|
||||
}
|
||||
|
||||
|
||||
public function testFindUsersReaching()
|
||||
{
|
||||
$centerA = static::$kernel->getContainer()
|
||||
->get('doctrine.orm.entity_manager')
|
||||
->getRepository(Center::class)
|
||||
->findOneByName('Center A');
|
||||
|
||||
|
||||
$users = $this->getAuthorizationHelper()
|
||||
->findUsersReaching(new Role('CHILL_PERSON_SEE'),
|
||||
->findUsersReaching(new Role('CHILL_PERSON_SEE'),
|
||||
$centerA);
|
||||
|
||||
|
||||
$usernames = \array_map(function(User $u) { return $u->getUsername(); }, $users);
|
||||
|
||||
|
||||
$this->assertContains('center a_social', $usernames);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user