AuthenticateMiddlewareTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Tests\Feature\Http\Middlewares;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. use Illuminate\Support\Facades\Config;
  6. class AuthenticateMiddlewareTest extends FeatureTestCase
  7. {
  8. private const USER_NAME = 'John';
  9. private const USER_EMAIL = 'john@example.com';
  10. /**
  11. * @test
  12. */
  13. public function test_it_always_authenticates_with_reverse_proxy_guard()
  14. {
  15. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  16. $this->app['auth']->shouldUse('reverse-proxy-guard');
  17. $this->json('GET', '/api/v1/groups', [], ['HTTP_REMOTE_USER' => self::USER_NAME]);
  18. $this->assertAuthenticated('reverse-proxy-guard');
  19. }
  20. /**
  21. * @test
  22. */
  23. public function test_user_is_set_from_reverse_proxy_info()
  24. {
  25. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  26. Config::set('auth.auth_proxy_headers.email', 'HTTP_REMOTE_EMAIL');
  27. $this->app['auth']->shouldUse('reverse-proxy-guard');
  28. $this->json('GET', '/api/v1/groups', [], [
  29. 'HTTP_REMOTE_USER' => self::USER_NAME,
  30. 'HTTP_REMOTE_EMAIL' => self::USER_EMAIL
  31. ]);
  32. $this->assertAuthenticated('reverse-proxy-guard');
  33. $user = $this->app->make('auth')->guard('reverse-proxy-guard')->user();
  34. $this->assertEquals(self::USER_NAME, $user->name);
  35. $this->assertEquals(self::USER_EMAIL, $user->email);
  36. }
  37. /**
  38. * @test
  39. */
  40. public function test_it_does_not_authenticate_with_empty_header()
  41. {
  42. Config::set('auth.auth_proxy_headers.user', 'HTTP_REMOTE_USER');
  43. Config::set('auth.auth_proxy_headers.email', 'HTTP_REMOTE_EMAIL');
  44. $this->app['auth']->shouldUse('reverse-proxy-guard');
  45. $this->json('GET', '/api/v1/groups', [], [
  46. 'HTTP_REMOTE_USER' => '',
  47. 'HTTP_REMOTE_EMAIL' => ''
  48. ])->assertUnauthorized();
  49. }
  50. /**
  51. * @test
  52. */
  53. public function test_it_does_not_authenticate_with_missing_header()
  54. {
  55. $this->app['auth']->shouldUse('reverse-proxy-guard');
  56. $this->json('GET', '/api/v1/groups', [], [])
  57. ->assertUnauthorized();
  58. }
  59. }