mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
chore: update tests and change timestamp type
This commit is contained in:
1 parent
627d1ba564
commit
397a49d2f7
11 files changed
+241
-158
No files matched your search
+5
-1
@@ -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:
|
||||
abuse:
|
||||
volumes:
|
||||
redisinsight:
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="true"
|
||||
stopOnFailure="false"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Application Test Suite">
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<Document> $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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
+96
-47
@@ -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()));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
-17
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user