AuthenticateMiddlewareTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Illuminate\Tests\Auth;
  3. use Illuminate\Auth\AuthenticationException;
  4. use Illuminate\Auth\AuthManager;
  5. use Illuminate\Auth\EloquentUserProvider;
  6. use Illuminate\Auth\Middleware\Authenticate;
  7. use Illuminate\Auth\RequestGuard;
  8. use Illuminate\Config\Repository as Config;
  9. use Illuminate\Container\Container;
  10. use Illuminate\Http\Request;
  11. use Mockery as m;
  12. use PHPUnit\Framework\TestCase;
  13. use stdClass;
  14. class AuthenticateMiddlewareTest extends TestCase
  15. {
  16. protected $auth;
  17. protected function setUp(): void
  18. {
  19. $container = Container::setInstance(new Container);
  20. $this->auth = new AuthManager($container);
  21. $container->singleton('config', function () {
  22. return $this->createConfig();
  23. });
  24. }
  25. protected function tearDown(): void
  26. {
  27. m::close();
  28. Container::setInstance(null);
  29. }
  30. public function testDefaultUnauthenticatedThrows()
  31. {
  32. $this->expectException(AuthenticationException::class);
  33. $this->expectExceptionMessage('Unauthenticated.');
  34. $this->registerAuthDriver('default', false);
  35. $this->authenticate();
  36. }
  37. public function testDefaultUnauthenticatedThrowsWithGuards()
  38. {
  39. try {
  40. $this->registerAuthDriver('default', false);
  41. $this->authenticate('default');
  42. } catch (AuthenticationException $e) {
  43. $this->assertContains('default', $e->guards());
  44. return;
  45. }
  46. $this->fail();
  47. }
  48. public function testDefaultAuthenticatedKeepsDefaultDriver()
  49. {
  50. $driver = $this->registerAuthDriver('default', true);
  51. $this->authenticate();
  52. $this->assertSame($driver, $this->auth->guard());
  53. }
  54. public function testSecondaryAuthenticatedUpdatesDefaultDriver()
  55. {
  56. $this->registerAuthDriver('default', false);
  57. $secondary = $this->registerAuthDriver('secondary', true);
  58. $this->authenticate('secondary');
  59. $this->assertSame($secondary, $this->auth->guard());
  60. }
  61. public function testMultipleDriversUnauthenticatedThrows()
  62. {
  63. $this->expectException(AuthenticationException::class);
  64. $this->expectExceptionMessage('Unauthenticated.');
  65. $this->registerAuthDriver('default', false);
  66. $this->registerAuthDriver('secondary', false);
  67. $this->authenticate('default', 'secondary');
  68. }
  69. public function testMultipleDriversUnauthenticatedThrowsWithGuards()
  70. {
  71. $expectedGuards = ['default', 'secondary'];
  72. try {
  73. $this->registerAuthDriver('default', false);
  74. $this->registerAuthDriver('secondary', false);
  75. $this->authenticate(...$expectedGuards);
  76. } catch (AuthenticationException $e) {
  77. $this->assertEquals($expectedGuards, $e->guards());
  78. return;
  79. }
  80. $this->fail();
  81. }
  82. public function testMultipleDriversAuthenticatedUpdatesDefault()
  83. {
  84. $this->registerAuthDriver('default', false);
  85. $secondary = $this->registerAuthDriver('secondary', true);
  86. $this->authenticate('default', 'secondary');
  87. $this->assertSame($secondary, $this->auth->guard());
  88. }
  89. /**
  90. * Create a new config repository instance.
  91. *
  92. * @return \Illuminate\Config\Repository
  93. */
  94. protected function createConfig()
  95. {
  96. return new Config([
  97. 'auth' => [
  98. 'defaults' => ['guard' => 'default'],
  99. 'guards' => [
  100. 'default' => ['driver' => 'default'],
  101. 'secondary' => ['driver' => 'secondary'],
  102. ],
  103. ],
  104. ]);
  105. }
  106. /**
  107. * Create and register a new auth driver with the auth manager.
  108. *
  109. * @param string $name
  110. * @param bool $authenticated
  111. * @return \Illuminate\Auth\RequestGuard
  112. */
  113. protected function registerAuthDriver($name, $authenticated)
  114. {
  115. $driver = $this->createAuthDriver($authenticated);
  116. $this->auth->extend($name, function () use ($driver) {
  117. return $driver;
  118. });
  119. return $driver;
  120. }
  121. /**
  122. * Create a new auth driver.
  123. *
  124. * @param bool $authenticated
  125. * @return \Illuminate\Auth\RequestGuard
  126. */
  127. protected function createAuthDriver($authenticated)
  128. {
  129. return new RequestGuard(function () use ($authenticated) {
  130. return $authenticated ? new stdClass : null;
  131. }, m::mock(Request::class), m::mock(EloquentUserProvider::class));
  132. }
  133. /**
  134. * Call the authenticate middleware with the given guards.
  135. *
  136. * @param string ...$guards
  137. * @return void
  138. *
  139. * @throws \Illuminate\Auth\AuthenticationException
  140. */
  141. protected function authenticate(...$guards)
  142. {
  143. $request = m::mock(Request::class);
  144. $nextParam = null;
  145. $next = function ($param) use (&$nextParam) {
  146. $nextParam = $param;
  147. };
  148. (new Authenticate($this->auth))->handle($request, $next, ...$guards);
  149. $this->assertSame($request, $nextParam);
  150. }
  151. }