ogcore/tests/Functional/OrganizationalUnitTest.php

227 lines
9.6 KiB
PHP

<?php
namespace Functional;
use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\OrganizationalUnit;
use App\Entity\UserGroup;
use App\Factory\ClientFactory;
use App\Factory\HardwareProfileFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\UserFactory;
use App\Model\OrganizationalUnitTypes;
use App\Model\UserGroupPermissions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class OrganizationalUnitTest extends AbstractTest
{
CONST string USER_ADMIN = 'ogadmin';
CONST string ORGANIZATIONAL_UNIT_CREATE = 'test-organizational-unit-create';
CONST string ORGANIZATIONAL_UNIT_UPDATE = 'test-organizational-unit-update';
CONST string ORGANIZATIONAL_UNIT_DELETE = 'test-organizational-unit-delete';
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testGetCollectionOrganizationalUnit(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
OrganizationalUnitFactory::createMany(2, ['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
OrganizationalUnitFactory::createMany(2, ['type' => OrganizationalUnitTypes::CLASSROOMS_GROUP]);
OrganizationalUnitFactory::createMany(2, ['type' => OrganizationalUnitTypes::CLASSROOM]);
OrganizationalUnitFactory::createMany(2, ['type' => OrganizationalUnitTypes::CLIENTS_GROUP]);
$this->createClientWithCredentials()->request('GET', '/organizational-units');
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/OrganizationalUnit',
'@id' => '/organizational-units',
'@type' => 'hydra:Collection',
'hydra:totalItems' => 8,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testCreateOrganizationalUnit(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
$this->createClientWithCredentials()->request('POST', '/organizational-units',['json' => [
'name' => self::ORGANIZATIONAL_UNIT_CREATE,
'type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT,
'remotePc' => true,
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
$this->assertJsonContains([
'@context' => '/contexts/OrganizationalUnitOutput',
'@type' => 'OrganizationalUnit',
'name' => self::ORGANIZATIONAL_UNIT_CREATE,
'type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT,
'remotePc' => true,
]);
}
/**
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
*/
public function testUpdateOrganizationalUnit(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
OrganizationalUnitFactory::createOne(['name' => self::ORGANIZATIONAL_UNIT_CREATE, 'type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
$iri = $this->findIriBy(OrganizationalUnit::class, ['name' => self::ORGANIZATIONAL_UNIT_CREATE]);
$this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
'name' => self::ORGANIZATIONAL_UNIT_UPDATE,
'comments' => 'comments',
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'@id' => $iri,
'name' => self::ORGANIZATIONAL_UNIT_UPDATE,
'comments' => 'comments'
]);
}
/**
* Verifica que un ou-admin puede editar (PUT/GET) un aula que él mismo creó.
* Antes del fix, el POST no añadía la OU a allowedOrganizationalUnits y
* el GET/PUT devolvía 403/404.
*/
public function testOuAdminCanEditOrganizationalUnitTheyCreated(): void
{
$username = 'ou-admin-test';
UserFactory::createOne([
'username' => $username,
'roles' => [UserGroupPermissions::ROLE_ORGANIZATIONAL_UNIT_ADMIN],
]);
$token = $this->getToken(['username' => $username, 'password' => UserFactory::PLAIN_PASSWORD]);
$client = $this->createClientWithCredentials($token);
$response = $client->request('POST', '/organizational-units', ['json' => [
'name' => 'aula-creada-por-ou-admin',
'type' => OrganizationalUnitTypes::CLASSROOM,
]]);
$this->assertResponseStatusCodeSame(201);
$iri = $response->toArray()['@id'];
$client->request('GET', $iri);
$this->assertResponseStatusCodeSame(200);
$client->request('PUT', $iri, ['json' => [
'name' => 'aula-creada-por-ou-admin-editada',
]]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'aula-creada-por-ou-admin-editada']);
}
/**
* Verifica que un ou-operator NO puede crear OUs (POST → 403).
* Antes del fix de permisos, los operadores podían crear OUs.
* Con la restricción actual, deben recibir 403.
*/
public function testOuOperatorCannotCreateOrganizationalUnit(): void
{
$username = 'ou-operator-test';
UserFactory::createOne([
'username' => $username,
'roles' => [UserGroupPermissions::ROLE_ORGANIZATIONAL_UNIT_OPERATOR],
]);
$token = $this->getToken(['username' => $username, 'password' => UserFactory::PLAIN_PASSWORD]);
$this->createClientWithCredentials($token)->request('POST', '/organizational-units', ['json' => [
'name' => 'aula-operador-no-puede-crear',
'type' => OrganizationalUnitTypes::CLASSROOM,
]]);
$this->assertResponseStatusCodeSame(403);
}
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testDeleteOrganizationalUnit(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
OrganizationalUnitFactory::createOne(['name' => self::ORGANIZATIONAL_UNIT_DELETE, 'type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
$iri = $this->findIriBy(OrganizationalUnit::class, ['name' => self::ORGANIZATIONAL_UNIT_DELETE]);
$this->createClientWithCredentials()->request('DELETE', $iri);
$this->assertResponseStatusCodeSame(204);
$this->assertNull(
static::getContainer()->get('doctrine')->getRepository(UserGroup::class)->findOneBy(['name' => self::ORGANIZATIONAL_UNIT_DELETE])
);
}
/**
* Verifica que el perfil de hardware asignado en el aula se propaga a todos
* los clientes que tiene por debajo, y que ese perfil queda compartido.
*
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
*/
public function testPropagateHardwareProfileFromOrganizationalUnitToClients(): void
{
UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
$ou = OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
ClientFactory::createOne(['name' => 'test-propagate-client-a', 'organizationalUnit' => $ou]);
ClientFactory::createOne(['name' => 'test-propagate-client-b', 'organizationalUnit' => $ou]);
HardwareProfileFactory::createOne(['description' => 'test-propagate-hw-profile']);
$ouIri = $this->findIriBy(OrganizationalUnit::class, ['id' => $ou->getId()]);
$hardwareProfileIri = $this->findIriBy(HardwareProfile::class, ['description' => 'test-propagate-hw-profile']);
$clientAIri = $this->findIriBy(Client::class, ['name' => 'test-propagate-client-a']);
$clientBIri = $this->findIriBy(Client::class, ['name' => 'test-propagate-client-b']);
$client = $this->createClientWithCredentials();
$client->request('PUT', $ouIri, ['json' => [
'networkSettings' => ['hardwareProfile' => $hardwareProfileIri],
]]);
$this->assertResponseIsSuccessful();
$client->request('POST', $ouIri . '/propagate-network-settings');
$this->assertResponseIsSuccessful();
$client->request('GET', $clientAIri);
$this->assertJsonContains(['hardwareProfile' => ['@id' => $hardwareProfileIri]]);
$client->request('GET', $clientBIri);
$this->assertJsonContains(['hardwareProfile' => ['@id' => $hardwareProfileIri]]);
}
}