feat: Permettre au super admin de se connecter et accéder à son dashboard
Le super admin (table super_admins, master DB) ne pouvait pas se connecter via /api/login car ce firewall n'utilisait que le provider tenant. De même, le JWT n'était pas enrichi pour les super admins, l'endpoint /api/me/roles les rejetait, et le frontend redirigeait systématiquement vers /dashboard. Un chain provider (super_admin + tenant) résout l'authentification, le JwtPayloadEnricher et MyRolesProvider gèrent désormais les deux types d'utilisateurs, et le frontend redirige selon le rôle après login.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\SuperAdmin\Infrastructure\Api\Processor;
|
||||
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\SuperAdmin\Application\Command\CreateEstablishment\CreateEstablishmentHandler;
|
||||
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdminId;
|
||||
use App\SuperAdmin\Infrastructure\Api\Processor\CreateEstablishmentProcessor;
|
||||
use App\SuperAdmin\Infrastructure\Api\Resource\EstablishmentResource;
|
||||
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
|
||||
use App\SuperAdmin\Infrastructure\Security\SecuritySuperAdmin;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
final class CreateEstablishmentProcessorTest extends TestCase
|
||||
{
|
||||
private const string SUPER_ADMIN_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
|
||||
#[Test]
|
||||
public function processCreatesEstablishmentAndReturnsResource(): void
|
||||
{
|
||||
$repository = new InMemoryEstablishmentRepository();
|
||||
$clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-16 10:00:00');
|
||||
}
|
||||
};
|
||||
$handler = new CreateEstablishmentHandler($repository, $clock);
|
||||
|
||||
$securityUser = new SecuritySuperAdmin(
|
||||
SuperAdminId::fromString(self::SUPER_ADMIN_ID),
|
||||
'superadmin@classeo.fr',
|
||||
'hashed',
|
||||
);
|
||||
|
||||
$security = $this->createMock(Security::class);
|
||||
$security->method('getUser')->willReturn($securityUser);
|
||||
|
||||
$processor = new CreateEstablishmentProcessor($handler, $security);
|
||||
|
||||
$input = new EstablishmentResource();
|
||||
$input->name = 'École Gamma';
|
||||
$input->subdomain = 'ecole-gamma';
|
||||
$input->adminEmail = 'admin@ecole-gamma.fr';
|
||||
|
||||
$result = $processor->process($input, new Post());
|
||||
|
||||
self::assertNotNull($result->id);
|
||||
self::assertNotNull($result->tenantId);
|
||||
self::assertSame('École Gamma', $result->name);
|
||||
self::assertSame('ecole-gamma', $result->subdomain);
|
||||
self::assertSame('active', $result->status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\SuperAdmin\Infrastructure\Api\Provider;
|
||||
|
||||
use ApiPlatform\Metadata\GetCollection;
|
||||
use App\SuperAdmin\Application\Query\GetEstablishments\GetEstablishmentsHandler;
|
||||
use App\SuperAdmin\Domain\Model\Establishment\Establishment;
|
||||
use App\SuperAdmin\Domain\Model\SuperAdmin\SuperAdminId;
|
||||
use App\SuperAdmin\Infrastructure\Api\Provider\EstablishmentCollectionProvider;
|
||||
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemoryEstablishmentRepository;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class EstablishmentCollectionProviderTest extends TestCase
|
||||
{
|
||||
private const string SUPER_ADMIN_ID = '550e8400-e29b-41d4-a716-446655440001';
|
||||
|
||||
#[Test]
|
||||
public function provideReturnsEmptyArrayWhenNoEstablishments(): void
|
||||
{
|
||||
$repository = new InMemoryEstablishmentRepository();
|
||||
$handler = new GetEstablishmentsHandler($repository);
|
||||
$provider = new EstablishmentCollectionProvider($handler);
|
||||
|
||||
$result = $provider->provide(new GetCollection());
|
||||
|
||||
self::assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function provideReturnsMappedResources(): void
|
||||
{
|
||||
$repository = new InMemoryEstablishmentRepository();
|
||||
$repository->save(Establishment::creer(
|
||||
name: 'École Alpha',
|
||||
subdomain: 'ecole-alpha',
|
||||
createdBy: SuperAdminId::fromString(self::SUPER_ADMIN_ID),
|
||||
createdAt: new DateTimeImmutable('2026-02-16 10:00:00'),
|
||||
));
|
||||
|
||||
$handler = new GetEstablishmentsHandler($repository);
|
||||
$provider = new EstablishmentCollectionProvider($handler);
|
||||
|
||||
$result = $provider->provide(new GetCollection());
|
||||
|
||||
self::assertCount(1, $result);
|
||||
self::assertSame('École Alpha', $result[0]->name);
|
||||
self::assertSame('ecole-alpha', $result[0]->subdomain);
|
||||
self::assertSame('active', $result[0]->status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\SuperAdmin\Infrastructure\Console;
|
||||
|
||||
use App\Administration\Application\Port\PasswordHasher;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\SuperAdmin\Infrastructure\Console\CreateTestSuperAdminCommand;
|
||||
use App\SuperAdmin\Infrastructure\Persistence\InMemory\InMemorySuperAdminRepository;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
final class CreateTestSuperAdminCommandTest extends TestCase
|
||||
{
|
||||
private InMemorySuperAdminRepository $repository;
|
||||
private CommandTester $commandTester;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemorySuperAdminRepository();
|
||||
|
||||
$clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-17 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$passwordHasher = new class implements PasswordHasher {
|
||||
public function hash(string $plainPassword): string
|
||||
{
|
||||
return 'hashed_' . $plainPassword;
|
||||
}
|
||||
|
||||
public function verify(string $hashedPassword, string $plainPassword): bool
|
||||
{
|
||||
return $hashedPassword === 'hashed_' . $plainPassword;
|
||||
}
|
||||
};
|
||||
|
||||
$command = new CreateTestSuperAdminCommand(
|
||||
$this->repository,
|
||||
$passwordHasher,
|
||||
$clock,
|
||||
);
|
||||
|
||||
$this->commandTester = new CommandTester($command);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function createsNewSuperAdmin(): void
|
||||
{
|
||||
$exitCode = $this->commandTester->execute([
|
||||
'--email' => 'sadmin@test.com',
|
||||
'--password' => 'SuperAdmin123',
|
||||
'--first-name' => 'Super',
|
||||
'--last-name' => 'Admin',
|
||||
]);
|
||||
|
||||
self::assertSame(Command::SUCCESS, $exitCode);
|
||||
self::assertStringContainsString('Test super admin created successfully', $this->commandTester->getDisplay());
|
||||
|
||||
$superAdmin = $this->repository->findByEmail('sadmin@test.com');
|
||||
self::assertNotNull($superAdmin);
|
||||
self::assertSame('sadmin@test.com', $superAdmin->email);
|
||||
self::assertSame('Super', $superAdmin->firstName);
|
||||
self::assertSame('Admin', $superAdmin->lastName);
|
||||
self::assertSame('hashed_SuperAdmin123', $superAdmin->hashedPassword);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function returnsSuccessForExistingEmail(): void
|
||||
{
|
||||
// Create first
|
||||
$this->commandTester->execute([
|
||||
'--email' => 'sadmin@test.com',
|
||||
'--password' => 'SuperAdmin123',
|
||||
]);
|
||||
|
||||
// Create again with same email
|
||||
$exitCode = $this->commandTester->execute([
|
||||
'--email' => 'sadmin@test.com',
|
||||
'--password' => 'SuperAdmin123',
|
||||
]);
|
||||
|
||||
self::assertSame(Command::SUCCESS, $exitCode);
|
||||
self::assertStringContainsString('already exists', $this->commandTester->getDisplay());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function usesDefaultValues(): void
|
||||
{
|
||||
$exitCode = $this->commandTester->execute([]);
|
||||
|
||||
self::assertSame(Command::SUCCESS, $exitCode);
|
||||
|
||||
$superAdmin = $this->repository->findByEmail('sadmin@test.com');
|
||||
self::assertNotNull($superAdmin);
|
||||
self::assertSame('Super', $superAdmin->firstName);
|
||||
self::assertSame('Admin', $superAdmin->lastName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user