198 lines
7.4 KiB
PHP
198 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace Functional;
|
|
|
|
use ApiPlatform\Symfony\Bundle\Test\Client as ApiClient;
|
|
use App\Entity\Client;
|
|
use App\Entity\OrganizationalUnit;
|
|
use App\Entity\Trace;
|
|
use App\Entity\User;
|
|
use App\Factory\UserFactory;
|
|
use App\Model\OrganizationalUnitTypes;
|
|
use App\Model\TraceStatus;
|
|
use App\Model\UserGroupPermissions;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
/**
|
|
* Una traza pertenece a un cliente y el cliente a una unidad organizativa, así que un usuario
|
|
* que no sea super-admin solo debe ver las trazas de los equipos de las OUs que tiene asignadas
|
|
* (y las de sus OUs hijas).
|
|
*
|
|
* @see \App\Doctrine\UserAllowedOrganizationalUnitExtension
|
|
*/
|
|
class TraceVisibilityTest extends AbstractTest
|
|
{
|
|
private const SA_USER = 'trace-super-admin';
|
|
private const OP_USER = 'trace-ou-operator';
|
|
|
|
/** Crea un usuario con el rol indicado y devuelve su JWT. */
|
|
private function tokenFor(string $username, string $role): string
|
|
{
|
|
UserFactory::createOne(['username' => $username, 'roles' => [$role]]);
|
|
|
|
return $this->getToken([
|
|
'username' => $username,
|
|
'password' => UserFactory::PLAIN_PASSWORD,
|
|
]);
|
|
}
|
|
|
|
private function as(string $token): ApiClient
|
|
{
|
|
return $this->createClientWithCredentials($token);
|
|
}
|
|
|
|
private function getEntityManager(): EntityManagerInterface
|
|
{
|
|
return static::getContainer()->get('doctrine')->getManager();
|
|
}
|
|
|
|
private function createOrganizationalUnit(string $name, ?OrganizationalUnit $parent = null): OrganizationalUnit
|
|
{
|
|
$organizationalUnit = new OrganizationalUnit();
|
|
$organizationalUnit->setName($name);
|
|
$organizationalUnit->setType(OrganizationalUnitTypes::CLASSROOM);
|
|
$organizationalUnit->setParent($parent);
|
|
|
|
$this->getEntityManager()->persist($organizationalUnit);
|
|
|
|
return $organizationalUnit;
|
|
}
|
|
|
|
/** Crea una traza asociada a un cliente de la OU indicada y devuelve su uuid. */
|
|
private function createTrace(?OrganizationalUnit $organizationalUnit, string $command): string
|
|
{
|
|
$entityManager = $this->getEntityManager();
|
|
|
|
$trace = new Trace();
|
|
$trace->setCommand($command);
|
|
$trace->setStatus(TraceStatus::SUCCESS);
|
|
$trace->setExecutedAt(new \DateTime());
|
|
|
|
if ($organizationalUnit !== null) {
|
|
$client = new Client();
|
|
$client->setName('client-'.$command);
|
|
$client->setMaintenance(false);
|
|
$client->setOrganizationalUnit($organizationalUnit);
|
|
$entityManager->persist($client);
|
|
|
|
$trace->setClient($client);
|
|
}
|
|
|
|
$entityManager->persist($trace);
|
|
|
|
return (string) $trace->getUuid();
|
|
}
|
|
|
|
/**
|
|
* Escenario común: una OU permitida con una OU hija, y otra OU ajena al usuario.
|
|
*
|
|
* @return array{allowed: string, child: string, foreign: string, orphan: string}
|
|
*/
|
|
private function createScenario(string $username): array
|
|
{
|
|
$entityManager = $this->getEntityManager();
|
|
|
|
$allowedOrganizationalUnit = $this->createOrganizationalUnit('trace-allowed-ou');
|
|
$childOrganizationalUnit = $this->createOrganizationalUnit('trace-child-ou', $allowedOrganizationalUnit);
|
|
$foreignOrganizationalUnit = $this->createOrganizationalUnit('trace-foreign-ou');
|
|
|
|
$traces = [
|
|
'allowed' => $this->createTrace($allowedOrganizationalUnit, 'allowed'),
|
|
'child' => $this->createTrace($childOrganizationalUnit, 'child'),
|
|
'foreign' => $this->createTrace($foreignOrganizationalUnit, 'foreign'),
|
|
'orphan' => $this->createTrace(null, 'orphan'),
|
|
];
|
|
|
|
/** @var User $user */
|
|
$user = $entityManager->getRepository(User::class)->findOneBy(['username' => $username]);
|
|
$user->addAllowedOrganizationalUnit($allowedOrganizationalUnit);
|
|
$entityManager->persist($user);
|
|
|
|
$entityManager->flush();
|
|
|
|
return $traces;
|
|
}
|
|
|
|
/** @return string[] uuids devueltos por GET /traces */
|
|
private function getTraceUuids(string $token): array
|
|
{
|
|
$response = $this->as($token)->request('GET', '/traces');
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
return array_map(
|
|
static fn (array $trace): string => $trace['uuid'],
|
|
$response->toArray()['hydra:member']
|
|
);
|
|
}
|
|
|
|
/** El super-admin sigue viendo todas las trazas, incluidas las que no tienen cliente. */
|
|
public function testSuperAdminSeesEveryTrace(): void
|
|
{
|
|
$token = $this->tokenFor(self::SA_USER, UserGroupPermissions::ROLE_SUPER_ADMIN);
|
|
$traces = $this->createScenario(self::SA_USER);
|
|
|
|
$uuids = $this->getTraceUuids($token);
|
|
|
|
$this->assertContains($traces['allowed'], $uuids);
|
|
$this->assertContains($traces['child'], $uuids);
|
|
$this->assertContains($traces['foreign'], $uuids);
|
|
$this->assertContains($traces['orphan'], $uuids);
|
|
}
|
|
|
|
/** El resto de roles solo ven las trazas de sus OUs permitidas y de las hijas de éstas. */
|
|
public function testOperatorOnlySeesTracesOfAllowedOrganizationalUnits(): void
|
|
{
|
|
$token = $this->tokenFor(self::OP_USER, UserGroupPermissions::ROLE_ORGANIZATIONAL_UNIT_OPERATOR);
|
|
$traces = $this->createScenario(self::OP_USER);
|
|
|
|
$uuids = $this->getTraceUuids($token);
|
|
|
|
$this->assertContains($traces['allowed'], $uuids, 'Debe ver las trazas de su OU');
|
|
$this->assertContains($traces['child'], $uuids, 'Debe ver las trazas de las OUs hijas');
|
|
$this->assertNotContains($traces['foreign'], $uuids, 'No debe ver las trazas de otras OUs');
|
|
$this->assertNotContains($traces['orphan'], $uuids, 'No debe ver trazas sin cliente');
|
|
}
|
|
|
|
/** El filtro también se aplica al item: una traza ajena no es accesible por uuid. */
|
|
public function testOperatorCannotFetchForeignTraceItem(): void
|
|
{
|
|
$token = $this->tokenFor(self::OP_USER, UserGroupPermissions::ROLE_ORGANIZATIONAL_UNIT_OPERATOR);
|
|
$traces = $this->createScenario(self::OP_USER);
|
|
|
|
$this->as($token)->request('GET', '/traces/'.$traces['allowed']);
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
$this->as($token)->request('GET', '/traces/'.$traces['foreign']);
|
|
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
/** Filtrar por un cliente ajeno no permite saltarse la restricción. */
|
|
public function testOperatorCannotBypassFilterWithClientFilter(): void
|
|
{
|
|
$token = $this->tokenFor(self::OP_USER, UserGroupPermissions::ROLE_ORGANIZATIONAL_UNIT_OPERATOR);
|
|
$traces = $this->createScenario(self::OP_USER);
|
|
|
|
$foreignTrace = $this->getEntityManager()->getRepository(Trace::class)
|
|
->findOneBy(['uuid' => $traces['foreign']]);
|
|
|
|
$response = $this->as($token)->request('GET', '/traces', [
|
|
'query' => ['client.id' => $foreignTrace->getClient()->getId()],
|
|
]);
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
$this->assertSame([], $response->toArray()['hydra:member']);
|
|
}
|
|
|
|
/** Un usuario sin ninguna OU asignada no ve ninguna traza. */
|
|
public function testUserWithoutOrganizationalUnitsSeesNoTraces(): void
|
|
{
|
|
$token = $this->tokenFor(self::OP_USER, UserGroupPermissions::ROLE_ORGANIZATIONAL_UNIT_OPERATOR);
|
|
|
|
$this->createTrace($this->createOrganizationalUnit('trace-unassigned-ou'), 'unassigned');
|
|
$this->getEntityManager()->flush();
|
|
|
|
$this->assertSame([], $this->getTraceUuids($token));
|
|
}
|
|
}
|