Skip to content

Commit

Permalink
Test that LoginListener is triggered by UsersCheckPasswordCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
doobry-systemli committed Nov 6, 2023
1 parent 5ec5978 commit 618e974
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions tests/Command/UsersCheckPasswordCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Command\UsersCheckPasswordCommand;
use App\Entity\User;
use App\Enum\Roles;
use App\Event\LoginEvent;
use App\EventListener\LoginListener;
use App\Handler\MailCryptKeyHandler;
use App\Handler\UserAuthenticationHandler;
use App\Helper\FileDescriptorReader;
Expand All @@ -13,6 +15,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;

class UsersCheckPasswordCommandTest extends TestCase
{
Expand All @@ -21,16 +27,21 @@ class UsersCheckPasswordCommandTest extends TestCase
protected $quotaUser;
protected $mailCryptUser;
protected $spamUser;
protected $loginListener;

public function setUp(): void
{
$this->plainUser = new User();
$this->plainUser->setPassword('passwordhash');
$this->quotaUser = new User();
$this->quotaUser->setPassword('passwordhash');
$this->quotaUser->setQuota(1024);
$this->mailCryptUser = new User();
$this->mailCryptUser->setPassword('passwordhash');
$this->mailCryptUser->setMailCrypt(true);
$this->mailCryptUser->setMailCryptPublicKey('somePublicKey');
$this->spamUser = new User();
$this->spamUser->setPassword('passwordhash');
$this->spamUser->setRoles([Roles::SPAM]);
}

Expand Down Expand Up @@ -92,6 +103,35 @@ public function testExecuteFd3($inputStream, $returnCode): void
$this->assertEquals($returnCode, $commandTester->getStatusCode());
}

public function testExecuteCallsLoginListener(): void
{
$inputStream = "[email protected]\x00password";
$returnCode = 0;

$manager = $this->getManager();
$reader = $this->getReaderFd3($inputStream);
$handler = $this->getHandler();
$mailCryptKeyHandler = $this->getMailCryptKeyHandler();
$mailCrypt = 2;
$mailUID = 5000;
$mailGID = 5000;
$mailLocation = 'var/vmail';

$command = new UsersCheckPasswordCommand($manager,
$reader,
$handler,
$mailCryptKeyHandler,
$mailCrypt,
$mailUID,
$mailGID,
$mailLocation);
$commandTester = new CommandTester($command);

$this->loginListener->expects(self::once())->method('onLogin');
$commandTester->execute([]);
self::assertEquals($returnCode, $commandTester->getStatusCode());
}

/**
* @dataProvider validContentProvider
*/
Expand Down Expand Up @@ -239,19 +279,21 @@ public function getManager(): EntityManagerInterface

public function getHandler(): UserAuthenticationHandler
{
$handler = $this->getMockBuilder(UserAuthenticationHandler::class)
->disableOriginalConstructor()
->getMock();
$handler->method('authenticate')->willReturnMap(
$passwordHasher = $this->createMock(PasswordHasherInterface::class);
$passwordHasher->method('verify')->willReturnMap(
[
[$this->plainUser, 'password', $this->plainUser],
[$this->quotaUser, 'password', $this->quotaUser],
[$this->mailCryptUser, 'password', $this->mailCryptUser],
[$this->spamUser, 'password', $this->spamUser],
['passwordhash', 'password', true],
['passwordhash', 'wrongpassword', false],
['passwordhash', '', false],
]
);
$passwordHasherFactory = $this->createMock(PasswordHasherFactoryInterface::class);
$passwordHasherFactory->method('getPasswordHasher')->willReturn($passwordHasher);

return $handler;
$this->loginListener = $this->createMock(LoginListener::class);
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener(LoginEvent::NAME, [$this->loginListener, 'onLogin']);
return new UserAuthenticationHandler($passwordHasherFactory, $eventDispatcher);
}

public function getMailCryptKeyHandler(): MailCryptKeyHandler
Expand Down

0 comments on commit 618e974

Please sign in to comment.