|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Routing; |
| 4 | + |
| 5 | +use Illuminate\Routing\Middleware\ThrottleRequests; |
| 6 | +use Orchestra\Testbench\Attributes\DefineEnvironment; |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Statamic\Facades\Blueprint; |
| 9 | +use Statamic\Facades\Form; |
| 10 | +use Tests\PreventSavingStacheItemsToDisk; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class RouteMiddlewareTest extends TestCase |
| 14 | +{ |
| 15 | + use PreventSavingStacheItemsToDisk; |
| 16 | + |
| 17 | + protected function withAuthThrottleMiddleware($app) |
| 18 | + { |
| 19 | + $app['config']->set('statamic.routes.auth_middleware', [ThrottleRequests::class.':2,1']); |
| 20 | + } |
| 21 | + |
| 22 | + protected function withFormsThrottleMiddleware($app) |
| 23 | + { |
| 24 | + $app['config']->set('statamic.routes.forms_middleware', [ThrottleRequests::class.':2,1']); |
| 25 | + } |
| 26 | + |
| 27 | + #[Test] |
| 28 | + public function no_extra_middleware_is_applied_to_auth_routes_by_default() |
| 29 | + { |
| 30 | + for ($i = 0; $i < 5; $i++) { |
| 31 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong']) |
| 32 | + ->assertStatus(302); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + #[DefineEnvironment('withAuthThrottleMiddleware')] |
| 38 | + public function custom_middleware_is_applied_to_auth_login_route() |
| 39 | + { |
| 40 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 41 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 42 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(429); |
| 43 | + } |
| 44 | + |
| 45 | + #[Test] |
| 46 | + #[DefineEnvironment('withAuthThrottleMiddleware')] |
| 47 | + public function custom_auth_middleware_is_applied_to_all_auth_routes() |
| 48 | + { |
| 49 | + $this->post('/!/auth/password/email', ['email' => 'test@example.com'])->assertStatus(302); |
| 50 | + $this->post('/!/auth/password/email', ['email' => 'test@example.com'])->assertStatus(302); |
| 51 | + $this->post('/!/auth/password/email', ['email' => 'test@example.com'])->assertStatus(429); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + #[DefineEnvironment('withAuthThrottleMiddleware')] |
| 56 | + public function custom_auth_middleware_does_not_affect_forms_route() |
| 57 | + { |
| 58 | + $this->createContactForm(); |
| 59 | + |
| 60 | + // Auth routes reach the throttle limit |
| 61 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 62 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 63 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(429); |
| 64 | + |
| 65 | + // Forms route is unaffected |
| 66 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 67 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 68 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 69 | + } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function no_extra_middleware_is_applied_to_forms_route_by_default() |
| 73 | + { |
| 74 | + $this->createContactForm(); |
| 75 | + |
| 76 | + for ($i = 0; $i < 5; $i++) { |
| 77 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #[Test] |
| 82 | + #[DefineEnvironment('withFormsThrottleMiddleware')] |
| 83 | + public function custom_middleware_is_applied_to_forms_route() |
| 84 | + { |
| 85 | + $this->createContactForm(); |
| 86 | + |
| 87 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 88 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 89 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(429); |
| 90 | + } |
| 91 | + |
| 92 | + #[Test] |
| 93 | + #[DefineEnvironment('withFormsThrottleMiddleware')] |
| 94 | + public function custom_forms_middleware_does_not_affect_auth_routes() |
| 95 | + { |
| 96 | + $this->createContactForm(); |
| 97 | + |
| 98 | + // Forms route reaches the throttle limit |
| 99 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 100 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(302); |
| 101 | + $this->post('/!/forms/contact', ['email' => 'test@example.com'])->assertStatus(429); |
| 102 | + |
| 103 | + // Auth routes are unaffected |
| 104 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 105 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 106 | + $this->post('/!/auth/login', ['email' => 'test@example.com', 'password' => 'wrong'])->assertStatus(302); |
| 107 | + } |
| 108 | + |
| 109 | + private function createContactForm(): void |
| 110 | + { |
| 111 | + $blueprint = Blueprint::make()->setContents([ |
| 112 | + 'fields' => [ |
| 113 | + ['handle' => 'email', 'field' => ['type' => 'text', 'validate' => 'required|email']], |
| 114 | + ], |
| 115 | + ]); |
| 116 | + |
| 117 | + Blueprint::shouldReceive('find')->with('forms.contact')->andReturn($blueprint); |
| 118 | + Blueprint::makePartial(); |
| 119 | + |
| 120 | + $form = Form::make()->handle('contact'); |
| 121 | + Form::shouldReceive('find')->with('contact')->andReturn($form); |
| 122 | + Form::makePartial(); |
| 123 | + } |
| 124 | +} |
0 commit comments