|
| 1 | +<?php |
| 2 | +namespace Pdsinterop\PhpSolid\Tests; |
| 3 | + |
| 4 | +require_once(__DIR__ . "/test-config.php"); |
| 5 | + |
| 6 | +use Pdsinterop\PhpSolid\IpAttempts; |
| 7 | +use Pdsinterop\PhpSolid\Db; |
| 8 | + |
| 9 | +class IpAttemptsTest extends \PHPUnit\Framework\TestCase |
| 10 | +{ |
| 11 | + protected function setUp(): void |
| 12 | + { |
| 13 | + $statements = [ |
| 14 | + 'DROP TABLE IF EXISTS ipAttempts', |
| 15 | + 'CREATE TABLE IF NOT EXISTS ipAttempts ( |
| 16 | + ip VARCHAR(255) NOT NULL, |
| 17 | + type VARCHAR(255) NOT NULL, |
| 18 | + expires NOT NULL |
| 19 | + )' |
| 20 | + ]; |
| 21 | + |
| 22 | + Db::connect(); |
| 23 | + try { |
| 24 | + // create tables |
| 25 | + foreach($statements as $statement){ |
| 26 | + Db::$pdo->exec($statement); |
| 27 | + } |
| 28 | + } catch(\PDOException $e) { |
| 29 | + echo $e->getMessage(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public function testGetAttemptsCountZero() { |
| 34 | + $ip = "10.0.0.1"; |
| 35 | + $count = IpAttempts::getAttemptsCount($ip, "test"); |
| 36 | + $this->assertEquals(0, $count); |
| 37 | + } |
| 38 | + |
| 39 | + public function testGetAttemptsCountOne() { |
| 40 | + $ip = "10.0.0.1"; |
| 41 | + |
| 42 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 43 | + $count = IpAttempts::getAttemptsCount($ip, "test"); |
| 44 | + $this->assertEquals(1, $count); |
| 45 | + } |
| 46 | + |
| 47 | + public function testGetAttemptsCountTwo() { |
| 48 | + $ip = "10.0.0.1"; |
| 49 | + |
| 50 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 51 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 52 | + |
| 53 | + $count = IpAttempts::getAttemptsCount($ip, "test"); |
| 54 | + $this->assertEquals(2, $count); |
| 55 | + } |
| 56 | + |
| 57 | + public function testGetAttemptsCountExpired() { |
| 58 | + $ip = "10.0.0.1"; |
| 59 | + |
| 60 | + IpAttempts::logFailedAttempt($ip, "test", time() - 1); |
| 61 | + IpAttempts::logFailedAttempt($ip, "test", time() - 1); |
| 62 | + |
| 63 | + $count = IpAttempts::getAttemptsCount($ip, "test"); |
| 64 | + $this->assertEquals(0, $count); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGetAttemptsCountOneExpired() { |
| 68 | + $ip = "10.0.0.1"; |
| 69 | + |
| 70 | + IpAttempts::logFailedAttempt($ip, "test", time() + 10); |
| 71 | + IpAttempts::logFailedAttempt($ip, "test", time() - 1); |
| 72 | + |
| 73 | + $count = IpAttempts::getAttemptsCount($ip, "test"); |
| 74 | + $this->assertEquals(1, $count); |
| 75 | + } |
| 76 | + |
| 77 | + public function testCleanup() { |
| 78 | + $ip = "10.0.0.1"; |
| 79 | + |
| 80 | + IpAttempts::logFailedAttempt($ip, "test", time() - 1); |
| 81 | + IpAttempts::logFailedAttempt($ip, "test", time() - 1); |
| 82 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 83 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 84 | + |
| 85 | + $query = Db::$pdo->prepare('SELECT count(*) AS count FROM ipAttempts'); |
| 86 | + $query->execute(); |
| 87 | + $result = $query->fetchAll(); |
| 88 | + $beforeCleanup = $result[0]['count']; |
| 89 | + |
| 90 | + $this->assertEquals(4, $beforeCleanup); |
| 91 | + |
| 92 | + IpAttempts::cleanupAttempts(); |
| 93 | + $query = Db::$pdo->prepare('SELECT count(*) AS count FROM ipAttempts'); |
| 94 | + $query->execute(); |
| 95 | + $result = $query->fetchAll(); |
| 96 | + $afterCleanup = $result[0]['count']; |
| 97 | + |
| 98 | + $this->assertEquals(2, $afterCleanup); |
| 99 | + } |
| 100 | + |
| 101 | + public function testTrustedIpGetAttempts() { |
| 102 | + $ip = "127.0.0.100"; // trusted IP |
| 103 | + |
| 104 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 105 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 106 | + |
| 107 | + $count = IpAttempts::getAttemptsCount($ip, "test"); |
| 108 | + $this->assertEquals(0, $count); |
| 109 | + } |
| 110 | + |
| 111 | + public function testTrustedIpGetAttemptsSkipsDb() { |
| 112 | + $ip = "127.0.0.100"; // trusted IP |
| 113 | + |
| 114 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 115 | + IpAttempts::logFailedAttempt($ip, "test", time() + 3600); |
| 116 | + |
| 117 | + $query = Db::$pdo->prepare('SELECT count(*) AS count FROM ipAttempts'); |
| 118 | + $query->execute(); |
| 119 | + $result = $query->fetchAll(); |
| 120 | + $count = $result[0]['count']; |
| 121 | + $this->assertEquals(0, $count); |
| 122 | + } |
| 123 | +} |
0 commit comments