|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CrowdSecBouncer\Tests\Unit; |
| 6 | + |
| 7 | +/** |
| 8 | + * Test for templating. |
| 9 | + * |
| 10 | + * @author CrowdSec team |
| 11 | + * |
| 12 | + * @see https://crowdsec.net CrowdSec Official Website |
| 13 | + * |
| 14 | + * @copyright Copyright (c) 2022+ CrowdSec |
| 15 | + * @license MIT License |
| 16 | + */ |
| 17 | + |
| 18 | +use CrowdSecBouncer\BouncerException; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use CrowdSecBouncer\AbstractBouncer; |
| 21 | +use CrowdSecBouncer\Constants; |
| 22 | +use CrowdSec\RemediationEngine\LapiRemediation; |
| 23 | + |
| 24 | +/** |
| 25 | + * @covers \CrowdSecBouncer\AbstractBouncer::__construct |
| 26 | + * @covers \CrowdSecBouncer\AbstractBouncer::pruneCache |
| 27 | + * @covers \CrowdSecBouncer\AbstractBouncer::clearCache |
| 28 | + * @covers \CrowdSecBouncer\AbstractBouncer::refreshBlocklistCache |
| 29 | + * @covers \CrowdSecBouncer\AbstractBouncer::testCacheConnection |
| 30 | + * |
| 31 | + * @uses \CrowdSecBouncer\AbstractBouncer::configure |
| 32 | + * @uses \CrowdSecBouncer\AbstractBouncer::getConfigs |
| 33 | + * @uses \CrowdSecBouncer\AbstractBouncer::getLogger |
| 34 | + * @covers \CrowdSecBouncer\AbstractBouncer::getRemediationEngine |
| 35 | + * @uses \CrowdSecBouncer\Configuration::addBouncerNodes |
| 36 | + * @uses \CrowdSecBouncer\Configuration::addCacheNodes |
| 37 | + * @uses \CrowdSecBouncer\Configuration::addConnectionNodes |
| 38 | + * @uses \CrowdSecBouncer\Configuration::addDebugNodes |
| 39 | + * @uses \CrowdSecBouncer\Configuration::addTemplateNodes |
| 40 | + * @uses \CrowdSecBouncer\Configuration::getConfigTreeBuilder |
| 41 | + * |
| 42 | + */ |
| 43 | +final class AbstractBouncerTest extends TestCase |
| 44 | +{ |
| 45 | + |
| 46 | + protected $configs = [ |
| 47 | + #============================================================================# |
| 48 | + # Bouncer configs |
| 49 | + #============================================================================# |
| 50 | + 'use_curl' => false, |
| 51 | + 'debug_mode' => true, |
| 52 | + 'disable_prod_log' => false, |
| 53 | + 'log_directory_path' => __DIR__ . '/.logs', |
| 54 | + 'display_errors' => true, |
| 55 | + 'forced_test_ip' => '', |
| 56 | + 'forced_test_forwarded_ip' => '', |
| 57 | + 'bouncing_level' => Constants::BOUNCING_LEVEL_NORMAL, |
| 58 | + 'trust_ip_forward_array' => [], |
| 59 | + 'excluded_uris' => [], |
| 60 | + 'cache_system' => Constants::CACHE_SYSTEM_PHPFS, |
| 61 | + 'captcha_cache_duration' => Constants::CACHE_EXPIRATION_FOR_CAPTCHA, |
| 62 | + 'custom_css' => '', |
| 63 | + 'hide_mentions' => false, |
| 64 | + 'color' => [ |
| 65 | + 'text' => [ |
| 66 | + 'primary' => 'black', |
| 67 | + 'secondary' => '#AAA', |
| 68 | + 'button' => 'white', |
| 69 | + 'error_message' => '#b90000', |
| 70 | + ], |
| 71 | + 'background' => [ |
| 72 | + 'page' => '#eee', |
| 73 | + 'container' => 'white', |
| 74 | + 'button' => '#626365', |
| 75 | + 'button_hover' => '#333', |
| 76 | + ], |
| 77 | + ], |
| 78 | + 'text' => [ |
| 79 | + 'captcha_wall' => [ |
| 80 | + 'tab_title' => 'Oops..', |
| 81 | + 'title' => 'Hmm, sorry but...', |
| 82 | + 'subtitle' => 'Please complete the security check.', |
| 83 | + 'refresh_image_link' => 'refresh image', |
| 84 | + 'captcha_placeholder' => 'Type here...', |
| 85 | + 'send_button' => 'CONTINUE', |
| 86 | + 'error_message' => 'Please try again.', |
| 87 | + 'footer' => '', |
| 88 | + ], |
| 89 | + 'ban_wall' => [ |
| 90 | + 'tab_title' => 'Oops..', |
| 91 | + 'title' => '🤭 Oh!', |
| 92 | + 'subtitle' => 'This page is protected against cyber attacks and your IP has been banned by our system.', |
| 93 | + 'footer' => '', |
| 94 | + ], |
| 95 | + ], |
| 96 | + #============================================================================# |
| 97 | + # Client configs |
| 98 | + #============================================================================# |
| 99 | + 'auth_type' => Constants::AUTH_KEY, |
| 100 | + 'tls_cert_path' => '', |
| 101 | + 'tls_key_path' => '', |
| 102 | + 'tls_verify_peer' => true, |
| 103 | + 'tls_ca_cert_path' => '', |
| 104 | + 'api_key' => 'unit-test', |
| 105 | + 'api_url' => Constants::DEFAULT_LAPI_URL, |
| 106 | + 'api_timeout' => 1, |
| 107 | + #============================================================================# |
| 108 | + # Remediation engine configs |
| 109 | + #============================================================================# |
| 110 | + 'fallback_remediation' => Constants::REMEDIATION_CAPTCHA, |
| 111 | + 'ordered_remediations' => [Constants::REMEDIATION_BAN, Constants::REMEDIATION_CAPTCHA], |
| 112 | + 'fs_cache_path' => __DIR__ . '/.cache', |
| 113 | + 'redis_dsn' => 'redis://localhost:6379', |
| 114 | + 'memcached_dsn' => 'memcached://localhost:11211', |
| 115 | + 'clean_ip_cache_duration' => 1, |
| 116 | + 'bad_ip_cache_duration' => 1, |
| 117 | + 'stream_mode' => false, |
| 118 | + 'geolocation' => [ |
| 119 | + 'enabled' => false, |
| 120 | + 'type' => Constants::GEOLOCATION_TYPE_MAXMIND, |
| 121 | + 'cache_duration' => Constants::CACHE_EXPIRATION_FOR_GEO, |
| 122 | + 'maxmind' => [ |
| 123 | + 'database_type' => Constants::MAXMIND_COUNTRY, |
| 124 | + 'database_path' => '/some/path/GeoLite2-Country.mmdb', |
| 125 | + ], |
| 126 | + ], |
| 127 | + ]; |
| 128 | + |
| 129 | + public function testCacheMethodsException() |
| 130 | + { |
| 131 | + $configs = $this->configs; |
| 132 | + $mockRemediation = $this->getMockBuilder(LapiRemediation::class) |
| 133 | + ->disableOriginalConstructor() |
| 134 | + ->onlyMethods(['pruneCache', 'clearCache', 'refreshDecisions', 'getCacheStorage']) |
| 135 | + ->getMock(); |
| 136 | + $client = $this->getMockForAbstractClass(AbstractBouncer::class, [$configs, $mockRemediation]); |
| 137 | + |
| 138 | + $this->assertInstanceOf(LapiRemediation::class, $client->getRemediationEngine()); |
| 139 | + |
| 140 | + $mockRemediation->method('pruneCache')->willThrowException(new \Exception('unit test prune cache', 123)); |
| 141 | + |
| 142 | + $errorMessage = ''; |
| 143 | + $errorCode = 0; |
| 144 | + try { |
| 145 | + $client->pruneCache(); |
| 146 | + } catch (BouncerException $e) { |
| 147 | + $errorMessage = $e->getMessage(); |
| 148 | + $errorCode = $e->getCode(); |
| 149 | + } |
| 150 | + |
| 151 | + $this->assertEquals(123, $errorCode); |
| 152 | + $this->assertEquals('Error while pruning cache: unit test prune cache', $errorMessage); |
| 153 | + |
| 154 | + $mockRemediation->method('clearCache')->willThrowException(new \Exception('unit test clear cache', 456)); |
| 155 | + |
| 156 | + $errorMessage = ''; |
| 157 | + $errorCode = 0; |
| 158 | + try { |
| 159 | + $client->clearCache(); |
| 160 | + } catch (BouncerException $e) { |
| 161 | + $errorMessage = $e->getMessage(); |
| 162 | + $errorCode = $e->getCode(); |
| 163 | + } |
| 164 | + |
| 165 | + $this->assertEquals(456, $errorCode); |
| 166 | + $this->assertEquals('Error while clearing cache: unit test clear cache', $errorMessage); |
| 167 | + |
| 168 | + $mockRemediation->method('refreshDecisions')->willThrowException(new \Exception('unit test refresh', 789)); |
| 169 | + |
| 170 | + $errorMessage = ''; |
| 171 | + $errorCode = 0; |
| 172 | + try { |
| 173 | + $client->refreshBlocklistCache(); |
| 174 | + } catch (BouncerException $e) { |
| 175 | + $errorMessage = $e->getMessage(); |
| 176 | + $errorCode = $e->getCode(); |
| 177 | + } |
| 178 | + |
| 179 | + $this->assertEquals(789, $errorCode); |
| 180 | + $this->assertEquals('Error while refreshing decisions: unit test refresh', $errorMessage); |
| 181 | + |
| 182 | + $mockRemediation->method('getCacheStorage')->willThrowException(new \Exception('unit test get cache storage', |
| 183 | + 101112)); |
| 184 | + |
| 185 | + $errorMessage = ''; |
| 186 | + $errorCode = 0; |
| 187 | + try { |
| 188 | + $client->testCacheConnection(); |
| 189 | + } catch (BouncerException $e) { |
| 190 | + $errorMessage = $e->getMessage(); |
| 191 | + $errorCode = $e->getCode(); |
| 192 | + } |
| 193 | + |
| 194 | + $this->assertEquals(101112, $errorCode); |
| 195 | + $this->assertEquals('Error while testing cache connection: unit test get cache storage', $errorMessage); |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments