From b51d34f8da72bbe08470b345ec4b8fbf8fa2da49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 5 Jan 2026 22:20:20 +0100 Subject: [PATCH] Improve PR quality --- .../Adapters/TimeLimit/Appwrite/TablesDB.php | 4 -- src/Abuse/Adapters/TimeLimit/Database.php | 3 -- src/Abuse/Adapters/TimeLimit/Redis.php | 3 -- src/Abuse/Adapters/TimeLimit/RedisCluster.php | 3 -- tests/Abuse/Base.php | 38 ++++++++++++------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php b/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php index 89db961696..be0fa12e6c 100644 --- a/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php +++ b/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php @@ -260,10 +260,6 @@ class TablesDB extends TimeLimit */ protected function set(string $key, int $timestamp, int $value): void { - if (0 == $this->limit) { // No limit no point for counting - return; - } - $timestamp = $this->toDateTime($timestamp); $response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [ diff --git a/src/Abuse/Adapters/TimeLimit/Database.php b/src/Abuse/Adapters/TimeLimit/Database.php index 2e7648830a..a2eb69726b 100644 --- a/src/Abuse/Adapters/TimeLimit/Database.php +++ b/src/Abuse/Adapters/TimeLimit/Database.php @@ -225,9 +225,6 @@ class Database extends TimeLimit */ protected function set(string $key, int $timestamp, int $value): void { - if (0 == $this->limit) { // No limit no point for counting - return; - } $timestamp = $this->toDateTime($timestamp); Authorization::skip(function () use ($timestamp, $key, $value) { diff --git a/src/Abuse/Adapters/TimeLimit/Redis.php b/src/Abuse/Adapters/TimeLimit/Redis.php index 8db3e29950..e626573568 100644 --- a/src/Abuse/Adapters/TimeLimit/Redis.php +++ b/src/Abuse/Adapters/TimeLimit/Redis.php @@ -87,9 +87,6 @@ class Redis extends TimeLimit */ protected function set(string $key, int $timestamp, int $value): void { - if (0 == $this->limit) { // No limit no point for counting - return; - } $key = self::NAMESPACE . '__' . $key . '__' . $timestamp; $this->redis->multi() diff --git a/src/Abuse/Adapters/TimeLimit/RedisCluster.php b/src/Abuse/Adapters/TimeLimit/RedisCluster.php index abb6379030..731415e9ae 100644 --- a/src/Abuse/Adapters/TimeLimit/RedisCluster.php +++ b/src/Abuse/Adapters/TimeLimit/RedisCluster.php @@ -89,9 +89,6 @@ class RedisCluster extends TimeLimit */ protected function set(string $key, int $timestamp, int $value): void { - if (0 == $this->limit) { // No limit no point for counting - return; - } $key = self::NAMESPACE . '__' . $key . '__' . $timestamp; diff --git a/tests/Abuse/Base.php b/tests/Abuse/Base.php index 6aefd41f4a..fcdb115c99 100644 --- a/tests/Abuse/Base.php +++ b/tests/Abuse/Base.php @@ -102,27 +102,37 @@ abstract class Base extends TestCase */ public function testReset(): void { - $adapter = $this->getAdapter('reset-test-{{ip}}', 5, 1); - $adapter->setParam('{{ip}}', '192.168.1.1'); - $abuse = new Abuse($adapter); - - // Make 3 checks - $this->assertSame($abuse->check(), false); - $this->assertSame($abuse->check(), false); - $this->assertSame($abuse->check(), false); - - // Reset the count - $abuse->reset(); - - // Should be able to make 5 checks again - $adapter = $this->getAdapter('reset-test-{{ip}}', 5, 1); + $adapter = $this->getAdapter('reset-test-{{ip}}', 5, 600); $adapter->setParam('{{ip}}', '192.168.1.1'); $abuse = new Abuse($adapter); + + // 5 OK, 6th has limit $this->assertSame($abuse->check(), false); $this->assertSame($abuse->check(), false); $this->assertSame($abuse->check(), false); $this->assertSame($abuse->check(), false); $this->assertSame($abuse->check(), false); $this->assertSame($abuse->check(), true); + + // Reset the count + $abuse->reset(); + + // Should be 5 more OK, then 6th limit + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), true); + + // TO be sure, lets do bunch of requests with resets + // All should pass successfully + $adapter = $this->getAdapter('reset-test-{{ip}}', 2, 600); + $adapter->setParam('{{ip}}', '192.168.1.2'); + $abuse = new Abuse($adapter); + for ($i = 0; $i < 15; $i++) { + $this->assertSame($abuse->check(), false); + $abuse->reset(); + } } }