AuthenticateMiddlewareTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Tests\Feature\Http\Middlewares;
  3. use Illuminate\Support\Facades\Config;
  4. use PHPUnit\Framework\Attributes\Test;
  5. use Tests\FeatureTestCase;
  6. class AuthenticateMiddlewareTest extends FeatureTestCase
  7. {
  8. private const USER_NAME = 'John';
  9. private const USER_EMAIL = 'john@example.com';
  10. #[Test]
  11. public function test_it_always_authenticates_with_reverse_proxy_guard()
  12. {
  13. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  14. $this->app['auth']->shouldUse('reverse-proxy-guard');
  15. $this->json('GET', '/api/v1/groups', [], ['HTTP_REMOTE_USER' => self::USER_NAME]);
  16. $this->assertAuthenticated('reverse-proxy-guard');
  17. }
  18. #[Test]
  19. public function test_it_does_not_authenticate_with_empty_header()
  20. {
  21. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  22. Config::set('auth.auth_proxy_headers.email', 'HTTP_REMOTE_EMAIL');
  23. $this->app['auth']->shouldUse('reverse-proxy-guard');
  24. $this->json('GET', '/api/v1/groups', [], [
  25. 'HTTP_REMOTE_USER' => '',
  26. 'HTTP_REMOTE_EMAIL' => '',
  27. ])->assertStatus(407);
  28. }
  29. #[Test]
  30. public function test_it_does_not_authenticate_with_missing_header()
  31. {
  32. $this->app['auth']->shouldUse('reverse-proxy-guard');
  33. $this->json('GET', '/api/v1/groups', [], [])
  34. ->assertStatus(407);
  35. }
  36. }