refactor(security): add typed brute-force detection event
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXT\Unit\Event;
|
||||
|
||||
use KTXC\Security\Event\BruteForceDetectedEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class BruteForceDetectedEventTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
#[TestDox('Brute-force detection state is typed and complete at construction')]
|
||||
public function constructsTypedState(): void
|
||||
{
|
||||
$event = new BruteForceDetectedEvent(
|
||||
'203.0.113.10',
|
||||
5,
|
||||
300,
|
||||
'tenant-a',
|
||||
);
|
||||
|
||||
self::assertSame(BruteForceDetectedEvent::class, $event->getName());
|
||||
self::assertSame('203.0.113.10', $event->getIpAddress());
|
||||
self::assertSame(5, $event->getFailureCount());
|
||||
self::assertSame(300, $event->getWindowSeconds());
|
||||
self::assertSame('tenant-a', $event->getTenantId());
|
||||
self::assertSame('5 failed attempts in 300 seconds', $event->getReason());
|
||||
self::assertSame(
|
||||
['failureCount' => 5, 'windowSeconds' => 300],
|
||||
$event->getData(),
|
||||
);
|
||||
self::assertSame(SecurityEvent::SEVERITY_CRITICAL, $event->getSeverity());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
#[TestDox('Brute-force detection rejects invalid measurements')]
|
||||
public function rejectsInvalidMeasurements(): void
|
||||
{
|
||||
foreach ([
|
||||
['', 5, 300],
|
||||
['203.0.113.10', 0, 300],
|
||||
['203.0.113.10', 5, 0],
|
||||
] as $arguments) {
|
||||
try {
|
||||
new BruteForceDetectedEvent(...$arguments);
|
||||
self::fail('Invalid brute-force measurements were accepted.');
|
||||
} catch (\InvalidArgumentException) {
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user