Skip to content

Commit 2663547

Browse files
committed
fix: tests
1 parent 76fc3da commit 2663547

8 files changed

+42
-12
lines changed

docker-compose.yml

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ services:
1010
ports:
1111
- "9307:3306"
1212

13+
adminer:
14+
image: adminer
15+
container_name: abuse-adminer
16+
restart: always
17+
ports:
18+
- 9506:8080
19+
networks:
20+
- abuse
21+
1322
redis:
1423
image: redis:6.0-alpine
1524
container_name: redis

tests/Abuse/Base.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function testIsValid(): void
5353

5454
public function testCleanup(): void
5555
{
56-
// Check that there is only one log
56+
// Check that there are 3 logs
5757
$logs = $this->abuse->getLogs(0, 10);
5858
$this->assertEquals(3, \count($logs));
5959

60-
sleep(5);
60+
sleep(2);
6161
// Delete the log
6262

6363
$status = $this->abuse->cleanup($this->getCleanupDateTime());

tests/Abuse/Bench/Database/TimeLimitBench.php renamed to tests/Abuse/Bench/Database.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Abuse\Bench\Database;
3+
namespace Abuse\Bench;
44

55
use PDO;
66
use Utopia\Abuse\Abuse;
@@ -13,7 +13,7 @@
1313
use Utopia\Exception;
1414
use Utopia\Tests\Bench\Base;
1515

16-
class TimeLimitBench extends Base
16+
class DatabaseBench extends Base
1717
{
1818
protected Database $db;
1919

tests/Abuse/Bench/Redis/TimeLimitBench.php renamed to tests/Abuse/Bench/Redis.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
namespace Abuse\Bench\Redis;
3+
namespace Abuse\Bench;
44

55
use Redis as Client;
66
use Utopia\Abuse\Abuse;
77
use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimit;
88
use Utopia\Tests\Bench\Base;
99

10-
final class TimeLimitBench extends Base
10+
final class RedisBench extends Base
1111
{
1212
protected Client $redis;
1313

tests/Abuse/Bench/RedisCluster.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Abuse\Bench;
4+
5+
use RedisCluster as Client;
6+
use Utopia\Abuse\Abuse;
7+
use Utopia\Abuse\Adapters\TimeLimit\RedisCluster as TimeLimit;
8+
use Utopia\Abuse\Adapters\TimeLimit\RedisCluster;
9+
use Utopia\Tests\Bench\Base;
10+
11+
final class RedisClusterBench extends Base
12+
{
13+
protected Client $redis;
14+
15+
/**
16+
* @throws \Exception
17+
*/
18+
public function setUp(): void
19+
{
20+
$this->redis = new Client(null, ['redis-cluster-0:6379', 'redis-cluster-1:6379', 'redis-cluster-2:6379', 'redis-cluster-3:6379']);
21+
$this->adapter = new RedisCluster('login-attempt-from-{{ip}}', 3, 60 * 5, $this->redis);
22+
$this->abuse = new Abuse($this->adapter);
23+
}
24+
}

tests/Abuse/DatabaseTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function setUp(): void
4545

4646
$adapter->setParam('{{ip}}', '127.0.0.1');
4747
$this->abuse = new Abuse($adapter);
48-
$this->abuse->cleanup($this->getCleanupDateTime());
4948
}
5049

5150
public function getAdapter(string $key, int $limit, int $seconds): Adapter
@@ -55,6 +54,6 @@ public function getAdapter(string $key, int $limit, int $seconds): Adapter
5554

5655
public function getCleanupDateTime(): string
5756
{
58-
return DateTime::addSeconds(new \DateTime(), -1);
57+
return DateTime::format(new \DateTime());
5958
}
6059
}

tests/Abuse/RedisClusterTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function setUp(): void
2323
$adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 1, $this->redis);
2424
$adapter->setParam('{{ip}}', '127.0.0.1');
2525
$this->abuse = new Abuse($adapter);
26-
$this->abuse->cleanup($this->getCleanupDateTime());
2726
}
2827

2928
public function getAdapter(string $key, int $limit, int $seconds): Adapter
@@ -33,7 +32,7 @@ public function getAdapter(string $key, int $limit, int $seconds): Adapter
3332

3433
public function getCleanupDateTime(): string
3534
{
36-
$interval = DateInterval::createFromDateString(1 . ' seconds');
35+
$interval = DateInterval::createFromDateString('now');
3736
return strval((new \DateTime())->sub($interval)->getTimestamp());
3837
}
3938
}

tests/Abuse/RedisTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public function setUp(): void
2424
$adapter = new TimeLimit('login-attempt-from-{{ip}}', 3, 1, $this->redis);
2525
$adapter->setParam('{{ip}}', '127.0.0.1');
2626
$this->abuse = new Abuse($adapter);
27-
$this->abuse->cleanup($this->getCleanupDateTime());
2827
}
2928

3029
public function getAdapter(string $key, int $limit, int $seconds): Adapter
@@ -34,7 +33,7 @@ public function getAdapter(string $key, int $limit, int $seconds): Adapter
3433

3534
public function getCleanupDateTime(): string
3635
{
37-
$interval = DateInterval::createFromDateString(1 . ' seconds');
36+
$interval = DateInterval::createFromDateString('now');
3837
return strval((new \DateTime())->sub($interval)->getTimestamp());
3938
}
4039
}

0 commit comments

Comments
 (0)