|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Jobs\BanIpOnCloudflare; |
| 4 | +use Illuminate\Support\Facades\Cache; |
| 5 | +use Illuminate\Support\Facades\Queue; |
| 6 | + |
| 7 | +beforeEach(function (): void { |
| 8 | + Queue::fake(); |
| 9 | + Cache::flush(); |
| 10 | +}); |
| 11 | + |
| 12 | +it('dispatches ban job for wp-admin requests in production', function (): void { |
| 13 | + app()->detectEnvironment(fn () => 'production'); |
| 14 | + |
| 15 | + $this->get('/wp-admin') |
| 16 | + ->assertForbidden(); |
| 17 | + |
| 18 | + Queue::assertPushed(BanIpOnCloudflare::class, fn ($job): bool => $job->reason === 'WordPress probe detected'); |
| 19 | +}); |
| 20 | + |
| 21 | +it('dispatches ban job for wp-login requests in production', function (): void { |
| 22 | + app()->detectEnvironment(fn () => 'production'); |
| 23 | + |
| 24 | + $this->get('/wp-login.php') |
| 25 | + ->assertForbidden(); |
| 26 | + |
| 27 | + Queue::assertPushed(BanIpOnCloudflare::class); |
| 28 | +}); |
| 29 | + |
| 30 | +it('dispatches ban job for wp-includes requests in production', function (): void { |
| 31 | + app()->detectEnvironment(fn () => 'production'); |
| 32 | + |
| 33 | + $this->get('/wp-includes/some-file.php') |
| 34 | + ->assertForbidden(); |
| 35 | + |
| 36 | + Queue::assertPushed(BanIpOnCloudflare::class); |
| 37 | +}); |
| 38 | + |
| 39 | +it('dispatches ban job for xmlrpc requests in production', function (): void { |
| 40 | + app()->detectEnvironment(fn () => 'production'); |
| 41 | + |
| 42 | + $this->get('/xmlrpc.php') |
| 43 | + ->assertForbidden(); |
| 44 | + |
| 45 | + Queue::assertPushed(BanIpOnCloudflare::class); |
| 46 | +}); |
| 47 | + |
| 48 | +it('tracks 404 errors and bans after threshold in production', function (): void { |
| 49 | + app()->detectEnvironment(fn () => 'production'); |
| 50 | + |
| 51 | + for ($i = 1; $i <= 4; $i++) { |
| 52 | + $this->get('/nonexistent-page-'.$i) |
| 53 | + ->assertNotFound(); |
| 54 | + |
| 55 | + Queue::assertNotPushed(BanIpOnCloudflare::class); |
| 56 | + } |
| 57 | + |
| 58 | + $this->get('/nonexistent-page-5') |
| 59 | + ->assertNotFound(); |
| 60 | + |
| 61 | + Queue::assertPushed(BanIpOnCloudflare::class, fn ($job): bool => str_contains((string) $job->reason, 'Exceeded 404 threshold')); |
| 62 | +}); |
| 63 | + |
| 64 | +it('does not track 404s in non-production environments', function (): void { |
| 65 | + app()->detectEnvironment(fn () => 'local'); |
| 66 | + |
| 67 | + for ($i = 1; $i <= 10; $i++) { |
| 68 | + $this->get('/nonexistent-page-'.$i) |
| 69 | + ->assertNotFound(); |
| 70 | + } |
| 71 | + |
| 72 | + Queue::assertNotPushed(BanIpOnCloudflare::class); |
| 73 | +}); |
| 74 | + |
| 75 | +it('does not ban wp-admin in non-production environments', function (): void { |
| 76 | + app()->detectEnvironment(fn () => 'local'); |
| 77 | + |
| 78 | + $this->get('/wp-admin') |
| 79 | + ->assertNotFound(); |
| 80 | + |
| 81 | + Queue::assertNotPushed(BanIpOnCloudflare::class); |
| 82 | +}); |
| 83 | + |
| 84 | +it('clears 404 count after banning', function (): void { |
| 85 | + app()->detectEnvironment(fn () => 'production'); |
| 86 | + |
| 87 | + for ($i = 1; $i <= 5; $i++) { |
| 88 | + $this->get('/nonexistent-page-'.$i); |
| 89 | + } |
| 90 | + |
| 91 | + expect(Cache::get('ip_404_count:127.0.0.1'))->toBeNull(); |
| 92 | +}); |
0 commit comments