From 397a49d2f783e14c3922b186f11e1e79ef68284f Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 20 Dec 2024 19:14:35 +0530 Subject: [PATCH 1/3] chore: update tests and change timestamp type --- docker-compose.yml | 6 +- phpunit.xml | 2 +- src/Abuse/Adapter.php | 4 +- src/Abuse/Adapters/TimeLimit.php | 24 +-- src/Abuse/Adapters/TimeLimit/Database.php | 41 +++-- src/Abuse/Adapters/TimeLimit/Redis.php | 26 ++-- src/Abuse/Adapters/TimeLimit/RedisCluster.php | 22 +-- tests/Abuse/Base.php | 143 ++++++++++++------ tests/Abuse/DatabaseTest.php | 43 +++--- tests/Abuse/RedisClusterTest.php | 46 ++++-- tests/Abuse/RedisTest.php | 42 ++--- 11 files changed, 241 insertions(+), 158 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a139da027b..f614d13765 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,6 +48,8 @@ services: - REDIS_HOSTS=redis-cluster-0:6379 ports: - "8081:5540" + volumes: + - redisinsight:/data redis-cluster-0: image: docker.io/bitnami/redis-cluster:7.4 @@ -88,4 +90,6 @@ services: - abuse networks: - abuse: \ No newline at end of file + abuse: +volumes: + redisinsight: diff --git a/phpunit.xml b/phpunit.xml index 10d408caa0..ec39a7edea 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,7 +6,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true" + stopOnFailure="false" > diff --git a/src/Abuse/Adapter.php b/src/Abuse/Adapter.php index 4af27dc40a..e96fa6f7de 100644 --- a/src/Abuse/Adapter.php +++ b/src/Abuse/Adapter.php @@ -79,8 +79,8 @@ abstract class Adapter /** * Delete all logs older than $datetime * - * @param string $datetime + * @param int $timestamp * @return bool */ - abstract public function cleanup(string $datetime): bool; + abstract public function cleanup(int $timestamp): bool; } diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index dcafee0835..d0bd655a7a 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -18,9 +18,9 @@ abstract class TimeLimit extends Adapter protected ?int $count = null; /** - * @var string + * @var int */ - protected string $time; + protected int $timestamp; /** * Check @@ -28,14 +28,14 @@ abstract class TimeLimit extends Adapter * Checks if number of counts is bigger or smaller than current limit * * @param string $key - * @param string $datetime + * @param int $timestamp * @return int * * @throws \Exception */ - abstract protected function count(string $key, string $datetime): int; + abstract protected function count(string $key, int $timestamp): int; - abstract protected function hit(string $key, string $datetime): void; + abstract protected function hit(string $key, int $timestamp): void; /** * Check @@ -54,8 +54,8 @@ abstract class TimeLimit extends Adapter $key = $this->parseKey(); - if ($this->limit > $this->count($key, $this->time)) { - $this->hit($key, $this->time); + if ($this->limit > $this->count($key, $this->timestamp)) { + $this->hit($key, $this->timestamp); return false; } @@ -74,7 +74,7 @@ abstract class TimeLimit extends Adapter */ public function remaining(): int { - $left = $this->limit - ($this->count($this->parseKey(), $this->time) + 1); // Add one because we need to say how many left not how many done + $left = $this->limit - ($this->count($this->parseKey(), $this->timestamp) + 1); // Add one because we need to say how many left not how many done return (0 > $left) ? 0 : $left; } @@ -94,12 +94,12 @@ abstract class TimeLimit extends Adapter /** * Time * - * Return the Datetime + * Return the timestamp * - * @return string + * @return int */ - public function time(): string + public function time(): int { - return $this->time; + return $this->timestamp; } } diff --git a/src/Abuse/Adapters/TimeLimit/Database.php b/src/Abuse/Adapters/TimeLimit/Database.php index a7ba5662c2..ae36f01cd0 100644 --- a/src/Abuse/Adapters/TimeLimit/Database.php +++ b/src/Abuse/Adapters/TimeLimit/Database.php @@ -80,8 +80,8 @@ class Database extends TimeLimit public function __construct(string $key, int $limit, int $seconds, UtopiaDB $db) { $this->key = $key; - $time = (int) \date('U', (int) (\floor(\time() / $seconds)) * $seconds); // todo: any good Idea without time()? - $this->time = DateTime::format((new \DateTime())->setTimestamp($time)); + $now = \time(); + $this->timestamp = (int)($now - ($now % $seconds)); $this->limit = $limit; $this->db = $db; } @@ -121,12 +121,12 @@ class Database extends TimeLimit * Checks if number of counts is bigger or smaller than current limit * * @param string $key - * @param string $datetime + * @param int $timestamp * @return int * * @throws \Exception */ - protected function count(string $key, string $datetime): int + protected function count(string $key, int $timestamp): int { if (0 == $this->limit) { // No limit no point for counting return 0; @@ -136,11 +136,13 @@ class Database extends TimeLimit return $this->count; } + $timestamp = $this->toDateTime($timestamp); + /** @var array $result */ - $result = Authorization::skip(function () use ($key, $datetime) { + $result = Authorization::skip(function () use ($key, $timestamp) { return $this->db->find(self::COLLECTION, [ Query::equal('key', [$key]), - Query::equal('time', [$datetime]), + Query::equal('time', [$timestamp]), ]); }); @@ -158,28 +160,29 @@ class Database extends TimeLimit /** * @param string $key - * @param string $datetime + * @param int $timestamp * @return void * * @throws AuthorizationException|Structure|\Exception|\Throwable */ - protected function hit(string $key, string $datetime): void + protected function hit(string $key, int $timestamp): void { if (0 == $this->limit) { // No limit no point for counting return; } - Authorization::skip(function () use ($datetime, $key) { + $timestamp = $this->toDateTime($timestamp); + Authorization::skip(function () use ($timestamp, $key) { $data = $this->db->findOne(self::COLLECTION, [ Query::equal('key', [$key]), - Query::equal('time', [$datetime]), + Query::equal('time', [$timestamp]), ]); if ($data->isEmpty()) { $data = [ '$permissions' => [], 'key' => $key, - 'time' => $datetime, + 'time' => $timestamp, 'count' => 1, '$collection' => self::COLLECTION, ]; @@ -190,7 +193,7 @@ class Database extends TimeLimit // Duplicate in case of race condition $data = $this->db->findOne(self::COLLECTION, [ Query::equal('key', [$key]), - Query::equal('time', [$datetime]), + Query::equal('time', [$timestamp]), ]); if (!$data->isEmpty()) { @@ -246,17 +249,18 @@ class Database extends TimeLimit /** * Delete logs older than $timestamp seconds * - * @param string $datetime + * @param int $timestamp * @return bool * * @throws AuthorizationException|\Exception */ - public function cleanup(string $datetime): bool + public function cleanup(int $timestamp): bool { - Authorization::skip(function () use ($datetime) { + $timestamp = $this->toDateTime($timestamp); + Authorization::skip(function () use ($timestamp) { do { $documents = $this->db->find(self::COLLECTION, [ - Query::lessThan('time', $datetime), + Query::lessThan('time', $timestamp), ]); foreach ($documents as $document) { @@ -267,4 +271,9 @@ class Database extends TimeLimit return true; } + + protected function toDateTime(int $timestamp): string + { + return DateTime::format((new \DateTime())->setTimestamp($timestamp)); + } } diff --git a/src/Abuse/Adapters/TimeLimit/Redis.php b/src/Abuse/Adapters/TimeLimit/Redis.php index 3657a570d5..80a55726f2 100644 --- a/src/Abuse/Adapters/TimeLimit/Redis.php +++ b/src/Abuse/Adapters/TimeLimit/Redis.php @@ -17,8 +17,8 @@ class Redis extends TimeLimit { $this->redis = $redis; $this->key = $key; - $time = (int) \date('U', (int) (\floor(\time() / $seconds)) * $seconds); - $this->time = strval($time); + $now = \time(); + $this->timestamp = (int)($now - ($now % $seconds)); $this->limit = $limit; } @@ -26,10 +26,10 @@ class Redis extends TimeLimit * Undocumented function * * @param string $key - * @param string $datetime + * @param int $timestamp * @return integer */ - protected function count(string $key, string $datetime): int + protected function count(string $key, int $timestamp): int { if (0 == $this->limit) { // No limit no point for counting return 0; @@ -40,7 +40,7 @@ class Redis extends TimeLimit } /** @var string $count */ - $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime); + $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp); if (!$count) { $this->count = 0; } else { @@ -52,25 +52,25 @@ class Redis extends TimeLimit /** * @param string $key - * @param string $datetime + * @param int $timestamp * @return void * */ - protected function hit(string $key, string $datetime): void + protected function hit(string $key, int $timestamp): void { if (0 == $this->limit) { // No limit no point for counting return; } /** @var string $count */ - $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime); + $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp); if (!$count) { $this->count = 0; } else { $this->count = intval($count); } - $this->redis->incr(self::NAMESPACE . '__'. $key .'__'. $datetime); + $this->redis->incr(self::NAMESPACE . '__'. $key .'__'. $timestamp); $this->count++; } @@ -100,17 +100,17 @@ class Redis extends TimeLimit } /** - * Delete all logs older than $datetime + * Delete all logs older than $timestamp * - * @param string $datetime + * @param int $timestamp * @return bool */ - public function cleanup(string $datetime): bool + public function cleanup(int $timestamp): bool { $iterator = null; while ($iterator !== 0) { $keys = $this->redis->scan($iterator, self::NAMESPACE . '__*__*', 1000); - $keys = $this->filterKeys($keys ? $keys : [], (int) $datetime); + $keys = $this->filterKeys($keys ? $keys : [], $timestamp); $this->redis->del($keys); } return true; diff --git a/src/Abuse/Adapters/TimeLimit/RedisCluster.php b/src/Abuse/Adapters/TimeLimit/RedisCluster.php index 24145d2b4b..4416ac8534 100644 --- a/src/Abuse/Adapters/TimeLimit/RedisCluster.php +++ b/src/Abuse/Adapters/TimeLimit/RedisCluster.php @@ -17,8 +17,8 @@ class RedisCluster extends TimeLimit { $this->redis = $redis; $this->key = $key; - $time = (int) \date('U', (int) (\floor(\time() / $seconds)) * $seconds); - $this->time = strval($time); + $now = \time(); + $this->timestamp = (int)($now - ($now % $seconds)); $this->limit = $limit; } @@ -29,7 +29,7 @@ class RedisCluster extends TimeLimit * @param string $datetime * @return integer */ - protected function count(string $key, string $datetime): int + protected function count(string $key, int $timestamp): int { if (0 == $this->limit) { // No limit no point for counting return 0; @@ -40,7 +40,7 @@ class RedisCluster extends TimeLimit } /** @var string|false $count */ - $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime); + $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp); if ($count === false) { $this->count = 0; } else { @@ -57,21 +57,21 @@ class RedisCluster extends TimeLimit * @param string $datetime * @return void */ - protected function hit(string $key, string $datetime): void + protected function hit(string $key, int $timestamp): void { if (0 == $this->limit) { // No limit no point for counting return; } /** @var string|false $count */ - $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $datetime); + $count = $this->redis->get(self::NAMESPACE . '__'. $key .'__'. $timestamp); if ($count === false) { $this->count = 0; } else { $this->count = intval($count); } - $this->redis->incr(self::NAMESPACE . '__'. $key .'__'. $datetime); + $this->redis->incr(self::NAMESPACE . '__'. $key .'__'. $timestamp); $this->count++; } @@ -100,15 +100,15 @@ class RedisCluster extends TimeLimit } /** - * Delete all logs older than $datetime + * Delete all logs older than $timestamp * - * @param string $datetime + * @param int $timestamp * @return bool */ - public function cleanup(string $datetime): bool + public function cleanup(int $timestamp): bool { $keys = $this->scan(self::NAMESPACE . '__*__*'); - $keys = $this->filterKeys($keys ? $keys : [], (int) $datetime); + $keys = $this->filterKeys($keys ? $keys : [], $timestamp); /** @phpstan-ignore-next-line */ $this->redis->del($keys); return true; diff --git a/tests/Abuse/Base.php b/tests/Abuse/Base.php index 2b7e291bed..a59b32d5f4 100644 --- a/tests/Abuse/Base.php +++ b/tests/Abuse/Base.php @@ -4,67 +4,116 @@ namespace Utopia\Tests; use PHPUnit\Framework\TestCase; use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapter; +use Utopia\Abuse\Adapters\TimeLimit; abstract class Base extends TestCase { - protected Abuse $abuse; + abstract public function getAdapter(string $key, int $limit, int $seconds): TimeLimit; - protected Abuse $abuseIp; - - abstract public function getAdapter(string $key, int $limit, int $seconds): Adapter; - - abstract public function getCleanupDateTime(): string; - - public function tearDown(): void + /** + * Test a static key with a limit of 2 requests per second + */ + public function testStaticKey(): void { - unset($this->abuse); + $adapter = $this->getAdapter('static-key', 2, 1); + $abuse = new Abuse($adapter); + $this->assertEquals($abuse->check(), false); + $this->assertEquals($abuse->check(), false); + $this->assertEquals($abuse->check(), true); } - public function testImitate2Requests(): void + /** + * Test a dynamic key with a limit of 2 requests per second + */ + public function testDynamicKey(): void { - $key = '{{ip}}'; - $value = '0.0.0.10'; + $adapter = $this->getAdapter('dyamic-key-{{ip}}', 2, 1); + $adapter->setParam('{{ip}}', '0.0.0.10'); + $abuse = new Abuse($adapter); + $this->assertEquals($abuse->check(), false); + $this->assertEquals($abuse->check(), false); + $this->assertEquals($abuse->check(), true); + } - $adapter = $this->getAdapter($key, 1, 1); - $adapter->setParam($key, $value); - $this->abuseIp = new Abuse($adapter); - $this->assertEquals($this->abuseIp->check(), false); - $this->assertEquals($this->abuseIp->check(), true); + /** + * Test a dynamic key with 2 params + */ + public function testDynamicKeyWith2Params(): void + { + $adapter = $this->getAdapter('two-params-{{ip}}-{{email}}', 2, 1); + $adapter->setParam('{{ip}}', '0.0.0.10'); + $adapter->setParam('{{email}}', 'test@test.com'); + $abuse = new Abuse($adapter); + $this->assertEquals($abuse->check(), false); + $this->assertEquals($abuse->check(), false); + $this->assertEquals($abuse->check(), true); + } + + /** + * Test a dynamic key with higher request rate like 100 requests per second + */ + public function testDynamicKeyFastRequests(): void + { + $adapter = $this->getAdapter('fast-requests-{{ip}}', 100, 1); + $adapter->setParam('{{ip}}', '0.0.0.10'); + $abuse = new Abuse($adapter); + for ($i = 0; $i < 100; $i++) { + $this->assertEquals($abuse->check(), false); + } + $this->assertEquals($abuse->check(), true); + } + + /** + * Test that the limit is reset after the time limit + */ + public function testLimitReset(): void + { + $adapter = $this->getAdapter('limit-reset-{{ip}}', 100, 2); + $adapter->setParam('{{ip}}', '127.0.0.1'); + $abuse = new Abuse($adapter); + for ($i = 0; $i < 100; $i++) { + $this->assertEquals($abuse->check(), false); + } + $this->assertEquals($abuse->check(), true); + + // Wait for the limit to reset + sleep(2); + + /** Seems to be a bug in the code where if use the same adapter, it caches the result of the previous check */ + $adapter = $this->getAdapter('limit-reset-{{ip}}', 100, 1); + $adapter->setParam('{{ip}}', '127.0.0.1'); + $abuse = new Abuse($adapter); + $this->assertEquals($abuse->check(), false); + } + + /** + * Test logs are deleted after cleanup + */ + public function testCleanup(): void + { + $adapter = $this->getAdapter('', 1, 1); + $abuse = new Abuse($adapter); + + $logs = $abuse->getLogs(0, 100); + $this->assertEquals(6, \count($logs)); // 6 keys are created in the test sleep(1); - $adapter = $this->getAdapter($key, 1, 1); - $adapter->setParam($key, $value); - $this->abuseIp = new Abuse($adapter); - - $this->assertEquals($this->abuseIp->check(), false); - $this->assertEquals($this->abuseIp->check(), true); - } - - public function testIsValid(): void - { - // Use vars to resolve adapter key - $this->assertEquals($this->abuse->check(), false); - $this->assertEquals($this->abuse->check(), false); - $this->assertEquals($this->abuse->check(), false); - $this->assertEquals($this->abuse->check(), true); - } - - public function testCleanup(): void - { - // Check that there are 3 logs - $logs = $this->abuse->getLogs(0, 10); - $this->assertEquals(3, \count($logs)); - - sleep(2); - // Delete the log - - $status = $this->abuse->cleanup($this->getCleanupDateTime()); + $status = $abuse->cleanup(time()); $this->assertEquals($status, true); - // Check that there are no logs in the DB - $logs = $this->abuse->getLogs(0, 10); + $logs = $abuse->getLogs(0, 100); $this->assertEquals(0, \count($logs)); } + + /** + * Verify that the time format is correct + */ + public function testTimeFormat(): void + { + $now = time(); + $adapter = $this->getAdapter('', 1, 1); + $this->assertEquals($adapter->time(), $now); + $this->assertEquals(true, \is_int($adapter->time())); + } } diff --git a/tests/Abuse/DatabaseTest.php b/tests/Abuse/DatabaseTest.php index 8ca37426bf..dd2be3e21c 100755 --- a/tests/Abuse/DatabaseTest.php +++ b/tests/Abuse/DatabaseTest.php @@ -3,57 +3,56 @@ namespace Utopia\Tests; use PDO; -use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapter; -use Utopia\Abuse\Adapters\TimeLimit\Database as TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit\Database as AdapterDatabase; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Cache; use Utopia\Database\Adapter\MariaDB; use Utopia\Database\Adapter\MySQL; use Utopia\Database\Database; -use Utopia\Database\DateTime; -use Utopia\Exception; class DatabaseTest extends Base { - protected Database $db; + protected static ?Database $db = null; - /** - * @throws Exception - * @throws \Exception - */ - public function setUp(): void + public static function setUpBeforeClass(): void + { + if (self::$db === null) { + self::$db = self::initialiseDatabase(); + } + } + + private static function initialiseDatabase(): Database { - // Limit login attempts to 3 time in 5 minutes time frame $dbHost = 'mysql'; $dbUser = 'root'; $dbPort = '3306'; $dbPass = 'password'; $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPdoAttributes()); - $db = new Database(new MySQL($pdo), new Cache(new NoCache())); $db->setDatabase('utopiaTests'); $db->setNamespace('namespace'); - $this->db = $db; - $adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 60 * 5, $db); - if (! $db->exists('utopiaTests')) { + $adapter = new AdapterDatabase('', 1, 1, $db); + if (!$db->exists('utopiaTests')) { $db->create(); $adapter->setup(); } - $adapter->setParam('{{ip}}', '127.0.0.1'); - $this->abuse = new Abuse($adapter); + return $db; } - public function getAdapter(string $key, int $limit, int $seconds): Adapter + public function getAdapter(string $key, int $limit, int $seconds): TimeLimit { - return new TimeLimit($key, $limit, $seconds, $this->db); + return new AdapterDatabase($key, $limit, $seconds, self::$db); } - public function getCleanupDateTime(): string + public static function tearDownAfterClass(): void { - return DateTime::format(new \DateTime()); + if (self::$db !== null) { + self::$db->delete(); + self::$db = null; + } } } diff --git a/tests/Abuse/RedisClusterTest.php b/tests/Abuse/RedisClusterTest.php index ba1a60da96..31fbed968a 100644 --- a/tests/Abuse/RedisClusterTest.php +++ b/tests/Abuse/RedisClusterTest.php @@ -2,37 +2,51 @@ namespace Utopia\Tests; -use DateInterval; -use RedisCluster; -use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapter; -use Utopia\Abuse\Adapters\TimeLimit\RedisCluster as TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit\RedisCluster as AdapterRedisCluster; use Utopia\Exception; class RedisClusterTest extends Base { - protected RedisCluster $redis; + /** + * @var \RedisCluster|null + */ + protected static ?\RedisCluster $redis = null; /** * @throws Exception * @throws \Exception */ - public function setUp(): void + public static function setUpBeforeClass(): void { - $this->redis = new RedisCluster(null, ['redis-cluster-0:6379', 'redis-cluster-1:6379', 'redis-cluster-2:6379', 'redis-cluster-3:6379']); - $adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 1, $this->redis); - $adapter->setParam('{{ip}}', '127.0.0.1'); - $this->abuse = new Abuse($adapter); + if (self::$redis === null) { + self::$redis = self::initialiseRedis(); + } } - public function getAdapter(string $key, int $limit, int $seconds): Adapter + private static function initialiseRedis(): \RedisCluster { - return new TimeLimit($key, $limit, $seconds, $this->redis); + return new \RedisCluster(null, [ + 'redis-cluster-0:6379', + 'redis-cluster-1:6379', + 'redis-cluster-2:6379', + 'redis-cluster-3:6379' + ]); } - public function getCleanupDateTime(): string + public function getAdapter(string $key, int $limit, int $seconds): TimeLimit { - $interval = DateInterval::createFromDateString('now'); - return strval((new \DateTime())->sub($interval)->getTimestamp()); + return new AdapterRedisCluster($key, $limit, $seconds, self::$redis); + } + + /** + * Clean up Redis connection after all tests + */ + public static function tearDownAfterClass(): void + { + if (self::$redis !== null) { + self::$redis->close(); + self::$redis = null; + } } } diff --git a/tests/Abuse/RedisTest.php b/tests/Abuse/RedisTest.php index e0bf69858a..643fdc50cf 100644 --- a/tests/Abuse/RedisTest.php +++ b/tests/Abuse/RedisTest.php @@ -2,38 +2,46 @@ namespace Utopia\Tests; -use DateInterval; -use Redis as Client; -use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapter; -use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimit; +use Redis; +use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\TimeLimit\Redis as AdapterRedis; use Utopia\Exception; class RedisTest extends Base { - protected Client $redis; + protected static ?\Redis $redis = null; /** * @throws Exception * @throws \Exception */ - public function setUp(): void + public static function setUpBeforeClass(): void { - $this->redis = new Client(); - $this->redis->connect('redis', 6379); - $adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 1, $this->redis); - $adapter->setParam('{{ip}}', '127.0.0.1'); - $this->abuse = new Abuse($adapter); + if (self::$redis === null) { + self::$redis = self::initialiseRedis(); + } } - public function getAdapter(string $key, int $limit, int $seconds): Adapter + private static function initialiseRedis(): \Redis { - return new TimeLimit($key, $limit, $seconds, $this->redis); + $redis = new \Redis(); + $redis->connect('redis', 6379); + return $redis; } - public function getCleanupDateTime(): string + public function getAdapter(string $key, int $limit, int $seconds): TimeLimit { - $interval = DateInterval::createFromDateString('now'); - return strval((new \DateTime())->sub($interval)->getTimestamp()); + return new AdapterRedis($key, $limit, $seconds, self::$redis); + } + + /** + * Clean up Redis connection after all tests + */ + public static function tearDownAfterClass(): void + { + if (self::$redis !== null) { + self::$redis->close(); + self::$redis = null; + } } } From d3119238ea05edd59f93ec99fb11545070e86eff Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 20 Dec 2024 19:41:20 +0530 Subject: [PATCH 2/3] chore: codeql and linter --- src/Abuse/Abuse.php | 8 ++++---- src/Abuse/Adapters/ReCaptcha.php | 6 +++--- src/Abuse/Adapters/TimeLimit/RedisCluster.php | 8 ++++---- tests/Abuse/DatabaseTest.php | 11 ++++++----- tests/Abuse/RedisClusterTest.php | 14 ++++++-------- tests/Abuse/RedisTest.php | 11 ++++++----- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/Abuse/Abuse.php b/src/Abuse/Abuse.php index 7386c8a4fa..948939913d 100644 --- a/src/Abuse/Abuse.php +++ b/src/Abuse/Abuse.php @@ -44,13 +44,13 @@ class Abuse } /** - * Delete all logs older than $datetime + * Delete all logs older than $timestamp * - * @param string $datetime + * @param int $timestamp * @return bool */ - public function cleanup(string $datetime): bool + public function cleanup(int $timestamp): bool { - return $this->adapter->cleanup($datetime); + return $this->adapter->cleanup($timestamp); } } diff --git a/src/Abuse/Adapters/ReCaptcha.php b/src/Abuse/Adapters/ReCaptcha.php index ae117ac5b9..15ecbdda84 100644 --- a/src/Abuse/Adapters/ReCaptcha.php +++ b/src/Abuse/Adapters/ReCaptcha.php @@ -90,14 +90,14 @@ class ReCaptcha extends Adapter } /** - * Delete logs older than $datetime + * Delete logs older than $timestamp * - * @param string $datetime + * @param int $timestamp * @return bool * * @throws Exception */ - public function cleanup(string $datetime): bool + public function cleanup(int $timestamp): bool { throw new Exception('Method not supported'); } diff --git a/src/Abuse/Adapters/TimeLimit/RedisCluster.php b/src/Abuse/Adapters/TimeLimit/RedisCluster.php index 4416ac8534..80bd0e519a 100644 --- a/src/Abuse/Adapters/TimeLimit/RedisCluster.php +++ b/src/Abuse/Adapters/TimeLimit/RedisCluster.php @@ -23,10 +23,10 @@ class RedisCluster extends TimeLimit } /** - * Get count for a key at specific datetime + * Get count for a key at specific timestamp * * @param string $key - * @param string $datetime + * @param int $timestamp * @return integer */ protected function count(string $key, int $timestamp): int @@ -51,10 +51,10 @@ class RedisCluster extends TimeLimit } /** - * Record a hit for a key at specific datetime + * Record a hit for a key at specific timestamp * * @param string $key - * @param string $datetime + * @param int $timestamp * @return void */ protected function hit(string $key, int $timestamp): void diff --git a/tests/Abuse/DatabaseTest.php b/tests/Abuse/DatabaseTest.php index dd2be3e21c..58356b2fe1 100755 --- a/tests/Abuse/DatabaseTest.php +++ b/tests/Abuse/DatabaseTest.php @@ -13,13 +13,15 @@ use Utopia\Database\Database; class DatabaseTest extends Base { - protected static ?Database $db = null; + protected static Database $db; public static function setUpBeforeClass(): void { - if (self::$db === null) { - self::$db = self::initialiseDatabase(); + if (isset(self::$db)) { + return; } + + self::$db = self::initialiseDatabase(); } private static function initialiseDatabase(): Database @@ -50,9 +52,8 @@ class DatabaseTest extends Base public static function tearDownAfterClass(): void { - if (self::$db !== null) { + if (isset(self::$db)) { self::$db->delete(); - self::$db = null; } } } diff --git a/tests/Abuse/RedisClusterTest.php b/tests/Abuse/RedisClusterTest.php index 31fbed968a..e1029f946a 100644 --- a/tests/Abuse/RedisClusterTest.php +++ b/tests/Abuse/RedisClusterTest.php @@ -8,10 +8,7 @@ use Utopia\Exception; class RedisClusterTest extends Base { - /** - * @var \RedisCluster|null - */ - protected static ?\RedisCluster $redis = null; + protected static \RedisCluster $redis; /** * @throws Exception @@ -19,9 +16,11 @@ class RedisClusterTest extends Base */ public static function setUpBeforeClass(): void { - if (self::$redis === null) { - self::$redis = self::initialiseRedis(); + if (isset(self::$redis)) { + return; } + + self::$redis = self::initialiseRedis(); } private static function initialiseRedis(): \RedisCluster @@ -44,9 +43,8 @@ class RedisClusterTest extends Base */ public static function tearDownAfterClass(): void { - if (self::$redis !== null) { + if (isset(self::$redis)) { self::$redis->close(); - self::$redis = null; } } } diff --git a/tests/Abuse/RedisTest.php b/tests/Abuse/RedisTest.php index 643fdc50cf..085da1e4fa 100644 --- a/tests/Abuse/RedisTest.php +++ b/tests/Abuse/RedisTest.php @@ -9,7 +9,7 @@ use Utopia\Exception; class RedisTest extends Base { - protected static ?\Redis $redis = null; + protected static \Redis $redis; /** * @throws Exception @@ -17,9 +17,11 @@ class RedisTest extends Base */ public static function setUpBeforeClass(): void { - if (self::$redis === null) { - self::$redis = self::initialiseRedis(); + if (isset(self::$redis)) { + return; } + + self::$redis = self::initialiseRedis(); } private static function initialiseRedis(): \Redis @@ -39,9 +41,8 @@ class RedisTest extends Base */ public static function tearDownAfterClass(): void { - if (self::$redis !== null) { + if (isset(self::$redis)) { self::$redis->close(); - self::$redis = null; } } } From 82f77e2875a58eca55ff5edbf4ae3083ceaf5f9b Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Fri, 20 Dec 2024 21:47:50 +0530 Subject: [PATCH 3/3] chore: review comment --- tests/Abuse/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Abuse/Base.php b/tests/Abuse/Base.php index a59b32d5f4..a1d008fffe 100644 --- a/tests/Abuse/Base.php +++ b/tests/Abuse/Base.php @@ -27,7 +27,7 @@ abstract class Base extends TestCase */ public function testDynamicKey(): void { - $adapter = $this->getAdapter('dyamic-key-{{ip}}', 2, 1); + $adapter = $this->getAdapter('dynamic-key-{{ip}}', 2, 1); $adapter->setParam('{{ip}}', '0.0.0.10'); $abuse = new Abuse($adapter); $this->assertEquals($abuse->check(), false);