182 lines
7.5 KiB
PHP
182 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace Functional;
|
|
|
|
use App\Entity\OrganizationalUnit;
|
|
use App\Entity\UserGroup;
|
|
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])
|
|
);
|
|
}
|
|
} |