菜谱项目

GetUserFromTokenTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of jwt-auth.
  4. *
  5. * (c) Sean Tymon <tymon148@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Tymon\JWTAuth\Test;
  11. use Mockery;
  12. use Tymon\JWTAuth\Middleware\GetUserFromToken;
  13. use Tymon\JWTAuth\Exceptions\TokenExpiredException;
  14. use Tymon\JWTAuth\Exceptions\TokenInvalidException;
  15. class GetUserFromTokenTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function setUp()
  18. {
  19. $this->events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
  20. $this->auth = Mockery::mock('Tymon\JWTAuth\JWTAuth');
  21. $this->request = Mockery::mock('Illuminate\Http\Request');
  22. $this->response = Mockery::mock('Illuminate\Contracts\Routing\ResponseFactory');
  23. $this->middleware = new GetUserFromToken($this->response, $this->events, $this->auth);
  24. $this->auth->shouldReceive('setRequest')->once()->with($this->request)->andReturn($this->auth);
  25. }
  26. public function tearDown()
  27. {
  28. Mockery::close();
  29. }
  30. /** @test */
  31. public function it_should_fire_an_event_when_no_token_is_available()
  32. {
  33. $this->auth->shouldReceive('getToken')->once()->andReturn(false);
  34. $this->events->shouldReceive('fire')->once()->with('tymon.jwt.absent', [], true);
  35. $this->response->shouldReceive('json')->with(['error' => 'token_not_provided'], 400);
  36. $this->middleware->handle($this->request, function () {
  37. });
  38. }
  39. /** @test */
  40. public function it_should_fire_an_event_when_the_token_has_expired()
  41. {
  42. $exception = new TokenExpiredException;
  43. $this->auth->shouldReceive('getToken')->once()->andReturn('foo');
  44. $this->auth->shouldReceive('authenticate')->once()->with('foo')->andThrow($exception);
  45. $this->events->shouldReceive('fire')->once()->with('tymon.jwt.expired', [$exception], true);
  46. $this->response->shouldReceive('json')->with(['error' => 'token_expired'], 401);
  47. $this->middleware->handle($this->request, function () {
  48. });
  49. }
  50. /** @test */
  51. public function it_should_fire_an_event_when_the_token_is_invalid()
  52. {
  53. $exception = new TokenInvalidException;
  54. $this->auth->shouldReceive('getToken')->once()->andReturn('foo');
  55. $this->auth->shouldReceive('authenticate')->once()->with('foo')->andThrow($exception);
  56. $this->events->shouldReceive('fire')->once()->with('tymon.jwt.invalid', [$exception], true);
  57. $this->response->shouldReceive('json')->with(['error' => 'token_invalid'], 400);
  58. $this->middleware->handle($this->request, function () {
  59. });
  60. }
  61. /** @test */
  62. public function it_should_fire_an_event_when_no_user_is_found()
  63. {
  64. $this->auth->shouldReceive('getToken')->once()->andReturn('foo');
  65. $this->auth->shouldReceive('authenticate')->once()->with('foo')->andReturn(false);
  66. $this->events->shouldReceive('fire')->once()->with('tymon.jwt.user_not_found', [], true);
  67. $this->response->shouldReceive('json')->with(['error' => 'user_not_found'], 404);
  68. $this->middleware->handle($this->request, function () {
  69. });
  70. }
  71. /** @test */
  72. public function it_should_fire_an_event_when_the_token_has_been_decoded_and_user_is_found()
  73. {
  74. $user = (object) ['id' => 1];
  75. $this->auth->shouldReceive('getToken')->once()->andReturn('foo');
  76. $this->auth->shouldReceive('authenticate')->once()->with('foo')->andReturn($user);
  77. $this->events->shouldReceive('fire')->once()->with('tymon.jwt.valid', $user);
  78. $this->response->shouldReceive('json')->never();
  79. $this->middleware->handle($this->request, function () {
  80. });
  81. }
  82. }