ApiAuthenticationWithEloquentTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Illuminate\Tests\Integration\Auth;
  3. use Illuminate\Database\QueryException;
  4. use Illuminate\Foundation\Auth\User as FoundationUser;
  5. use Illuminate\Support\Facades\Route;
  6. use Illuminate\Support\Str;
  7. use Orchestra\Testbench\TestCase;
  8. /**
  9. * @requires extension pdo_mysql
  10. */
  11. class ApiAuthenticationWithEloquentTest extends TestCase
  12. {
  13. protected function getEnvironmentSetUp($app)
  14. {
  15. // Auth configuration
  16. $app['config']->set('auth.defaults.guard', 'api');
  17. $app['config']->set('auth.providers.users.model', User::class);
  18. $app['config']->set('auth.guards.api', [
  19. 'driver' => 'token',
  20. 'provider' => 'users',
  21. 'hash' => false,
  22. ]);
  23. // Database configuration
  24. $app['config']->set('database.default', 'testbench');
  25. $app['config']->set('database.connections.testbench', [
  26. 'driver' => 'mysql',
  27. 'host' => env('DB_HOST', '127.0.0.1'),
  28. 'username' => 'root',
  29. 'password' => 'invalid-credentials',
  30. 'database' => 'forge',
  31. 'prefix' => '',
  32. ]);
  33. }
  34. public function testAuthenticationViaApiWithEloquentUsingWrongDatabaseCredentialsShouldNotCauseInfiniteLoop()
  35. {
  36. Route::get('/auth', function () {
  37. return 'success';
  38. })->middleware('auth:api');
  39. $this->expectException(QueryException::class);
  40. $this->expectExceptionMessage("Access denied for user 'root'@");
  41. try {
  42. $this->withoutExceptionHandling()->get('/auth', ['Authorization' => 'Bearer whatever']);
  43. } catch (QueryException $e) {
  44. if (Str::startsWith($e->getMessage(), 'SQLSTATE[HY000] [2002]')) {
  45. $this->markTestSkipped('MySQL instance required.');
  46. }
  47. throw $e;
  48. }
  49. }
  50. }
  51. class User extends FoundationUser
  52. {
  53. //
  54. }