|
81 | 81 | * @covers \CrowdSecBouncer\AbstractBouncer::shouldBounceCurrentIp |
82 | 82 | * @covers \CrowdSecBouncer\AbstractBouncer::handleBounceExclusion |
83 | 83 | * @covers \CrowdSecBouncer\AbstractBouncer::pushUsageMetrics |
| 84 | + * @covers \CrowdSecBouncer\AbstractBouncer::resetUsageMetrics |
84 | 85 | */ |
85 | 86 | final class AbstractBouncerTest extends TestCase |
86 | 87 | { |
@@ -1053,6 +1054,89 @@ public function testRun() |
1053 | 1054 | ); |
1054 | 1055 | } |
1055 | 1056 |
|
| 1057 | + public function testResetMetrics() |
| 1058 | + { |
| 1059 | + $client = new BouncerClient($this->configs, null, $this->logger); |
| 1060 | + $cache = new PhpFiles($this->configs, $this->logger); |
| 1061 | + $originCountItem = $cache->getItem(AbstractCache::ORIGINS_COUNT)->get(); |
| 1062 | + $this->assertEquals( |
| 1063 | + null, |
| 1064 | + $originCountItem, |
| 1065 | + 'The origin count for clean should be empty' |
| 1066 | + ); |
| 1067 | + |
| 1068 | + // bouncing URI |
| 1069 | + $client = new BouncerClient($this->configs, null, $this->logger); |
| 1070 | + $cache = new PhpFiles($this->configs, $this->logger); |
| 1071 | + $lapiRemediation = new LapiRemediation($this->configs, $client, $cache, $this->logger); |
| 1072 | + // Mock sendResponse and redirectResponse to avoid PHP UNIT header already sent or exit error |
| 1073 | + $bouncer = $this->getMockForAbstractClass(AbstractBouncer::class, [$this->configs, $lapiRemediation, $this->logger], |
| 1074 | + '', true, |
| 1075 | + true, true, [ |
| 1076 | + 'sendResponse', |
| 1077 | + 'redirectResponse', |
| 1078 | + 'getHttpMethod', |
| 1079 | + 'getPostedVariable', |
| 1080 | + 'getHttpRequestHeader', |
| 1081 | + 'getRemoteIp', |
| 1082 | + 'getRequestUri', |
| 1083 | + ]); |
| 1084 | + |
| 1085 | + $bouncer->method('getRequestUri')->willReturnOnConsecutiveCalls('/home'); |
| 1086 | + $bouncer->method('getRemoteIp')->willReturnOnConsecutiveCalls('127.0.0.3'); |
| 1087 | + $this->assertEquals(true, $bouncer->run(), 'Should bounce uri'); |
| 1088 | + $originCountItem = $cache->getItem(AbstractCache::ORIGINS_COUNT)->get(); |
| 1089 | + $this->assertEquals( |
| 1090 | + ['clean' => ['bypass' => 1]], |
| 1091 | + $originCountItem, |
| 1092 | + 'The origin count for clean should be 1' |
| 1093 | + ); |
| 1094 | + |
| 1095 | + // Test no-forward |
| 1096 | + $bouncerConfigs = array_merge( |
| 1097 | + $this->configs, |
| 1098 | + [ |
| 1099 | + 'forced_test_forwarded_ip' => Constants::X_FORWARDED_DISABLED, |
| 1100 | + ] |
| 1101 | + ); |
| 1102 | + $client = new BouncerClient($bouncerConfigs, null, $this->logger); |
| 1103 | + $cache = new PhpFiles($bouncerConfigs, $this->logger); |
| 1104 | + $lapiRemediation = new LapiRemediation($bouncerConfigs, $client, $cache, $this->logger); |
| 1105 | + // Mock sendResponse and redirectResponse to avoid PHP UNIT header already sent or exit error |
| 1106 | + $bouncer = $this->getMockForAbstractClass(AbstractBouncer::class, [$bouncerConfigs, $lapiRemediation, $this->logger], |
| 1107 | + '', true, |
| 1108 | + true, true, [ |
| 1109 | + 'sendResponse', |
| 1110 | + 'redirectResponse', |
| 1111 | + 'getHttpMethod', |
| 1112 | + 'getPostedVariable', |
| 1113 | + 'getHttpRequestHeader', |
| 1114 | + 'getRemoteIp', |
| 1115 | + 'getRequestUri', |
| 1116 | + ]); |
| 1117 | + |
| 1118 | + $bouncer->method('getRequestUri')->willReturnOnConsecutiveCalls('/home'); |
| 1119 | + $bouncer->method('getRemoteIp')->willReturnOnConsecutiveCalls('127.0.0.7'); |
| 1120 | + |
| 1121 | + $bouncer->run(); |
| 1122 | + |
| 1123 | + $originCountItem = $cache->getItem(AbstractCache::ORIGINS_COUNT)->get(); |
| 1124 | + $this->assertEquals( |
| 1125 | + ['clean' => ['bypass' => 2]], |
| 1126 | + $originCountItem, |
| 1127 | + 'The origin count for clean should be 2' |
| 1128 | + ); |
| 1129 | + |
| 1130 | + // Test: reset metrics |
| 1131 | + $result = $bouncer->resetUsageMetrics(); |
| 1132 | + $originCountItem = $cache->getItem(AbstractCache::ORIGINS_COUNT)->get(); |
| 1133 | + $this->assertEquals( |
| 1134 | + [], |
| 1135 | + $originCountItem, |
| 1136 | + 'The origin count item should be reset' |
| 1137 | + ); |
| 1138 | + } |
| 1139 | + |
1056 | 1140 | public function testPrivateAndProtectedMethods() |
1057 | 1141 | { |
1058 | 1142 | // handleCache |
|
0 commit comments