菜谱项目

JWTAuthTest.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\Token;
  13. use Tymon\JWTAuth\JWTAuth;
  14. use Illuminate\Http\Request;
  15. class JWTAuthTest extends \PHPUnit_Framework_TestCase
  16. {
  17. public function setUp()
  18. {
  19. $this->user = Mockery::mock('Tymon\JWTAuth\Providers\User\UserInterface');
  20. $this->manager = Mockery::mock('Tymon\JWTAuth\JWTManager');
  21. $this->auth = Mockery::mock('Tymon\JWTAuth\Providers\Auth\AuthInterface');
  22. $this->jwtAuth = new JWTAuth($this->manager, $this->user, $this->auth, Request::create('/foo', 'GET'));
  23. }
  24. public function tearDown()
  25. {
  26. Mockery::close();
  27. }
  28. /** @test */
  29. public function it_should_return_a_user_when_passing_a_token_containing_a_valid_subject_claim()
  30. {
  31. $payload = Mockery::mock('Tymon\JWTAuth\Payload');
  32. $payload->shouldReceive('offsetGet')->once()->andReturn(1);
  33. $this->manager->shouldReceive('decode')->once()->andReturn($payload);
  34. $this->user->shouldReceive('getBy')->once()->andReturn((object) ['id' => 1]);
  35. $user = $this->jwtAuth->toUser('foo.bar.baz');
  36. $this->assertEquals(1, $user->id);
  37. }
  38. /** @test */
  39. public function it_should_return_false_when_passing_a_token_containing_an_invalid_subject_claim()
  40. {
  41. $payload = Mockery::mock('Tymon\JWTAuth\Payload');
  42. $payload->shouldReceive('offsetGet')->once()->andReturn(1);
  43. $this->manager->shouldReceive('decode')->once()->andReturn($payload);
  44. $this->user->shouldReceive('getBy')->once()->andReturn(false);
  45. $user = $this->jwtAuth->toUser('foo.bar.baz');
  46. $this->assertFalse($user);
  47. }
  48. /** @test */
  49. public function it_should_return_a_token_when_passing_a_user()
  50. {
  51. $this->manager->shouldReceive('getPayloadFactory->make')->once()->andReturn(Mockery::mock('Tymon\JWTAuth\Payload'));
  52. $this->manager->shouldReceive('encode->get')->once()->andReturn('foo.bar.baz');
  53. $token = $this->jwtAuth->fromUser((object) ['id' => 1]);
  54. $this->assertEquals($token, 'foo.bar.baz');
  55. }
  56. /** @test */
  57. public function it_should_return_a_token_when_passing_valid_credentials_to_attempt_method()
  58. {
  59. $this->manager->shouldReceive('getPayloadFactory->make')->once()->andReturn(Mockery::mock('Tymon\JWTAuth\Payload'));
  60. $this->manager->shouldReceive('encode->get')->once()->andReturn('foo.bar.baz');
  61. $this->auth->shouldReceive('byCredentials')->once()->andReturn(true);
  62. $this->auth->shouldReceive('user')->once()->andReturn((object) ['id' => 1]);
  63. $token = $this->jwtAuth->attempt();
  64. $this->assertEquals($token, 'foo.bar.baz');
  65. }
  66. /** @test */
  67. public function it_should_return_false_when_passing_invalid_credentials_to_attempt_method()
  68. {
  69. $this->manager->shouldReceive('encode->get')->never();
  70. $this->auth->shouldReceive('byCredentials')->once()->andReturn(false);
  71. $this->auth->shouldReceive('user')->never();
  72. $token = $this->jwtAuth->attempt();
  73. $this->assertFalse($token);
  74. }
  75. /** @test */
  76. public function it_should_throw_an_exception_when_not_providing_a_token()
  77. {
  78. $this->setExpectedException('Tymon\JWTAuth\Exceptions\JWTException');
  79. $this->jwtAuth->toUser();
  80. }
  81. /** @test */
  82. public function it_should_return_the_owning_user_from_a_token_containing_an_existing_user()
  83. {
  84. $payload = Mockery::mock('Tymon\JWTAuth\Payload');
  85. $payload->shouldReceive('get')->once()->with('sub')->andReturn(1);
  86. $this->manager->shouldReceive('decode')->once()->andReturn($payload);
  87. $this->auth->shouldReceive('byId')->once()->with(1)->andReturn(true);
  88. $this->auth->shouldReceive('user')->once()->andReturn((object) ['id' => 1]);
  89. $user = $this->jwtAuth->authenticate('foo.bar.baz');
  90. $this->assertEquals($user->id, 1);
  91. }
  92. /** @test */
  93. public function it_should_return_false_when_passing_a_token_not_containing_an_existing_user()
  94. {
  95. $payload = Mockery::mock('Tymon\JWTAuth\Payload');
  96. $payload->shouldReceive('get')->once()->with('sub')->andReturn(1);
  97. $this->manager->shouldReceive('decode')->once()->andReturn($payload);
  98. $this->auth->shouldReceive('byId')->once()->with(1)->andReturn(false);
  99. $this->auth->shouldReceive('user')->never();
  100. $user = $this->jwtAuth->authenticate('foo.bar.baz');
  101. $this->assertFalse($user);
  102. }
  103. /** @test */
  104. public function it_should_refresh_a_token()
  105. {
  106. $newToken = Mockery::mock('Tymon\JWTAuth\Token');
  107. $newToken->shouldReceive('get')->once()->andReturn('baz.bar.foo');
  108. $this->manager->shouldReceive('refresh')->once()->andReturn($newToken);
  109. $result = $this->jwtAuth->setToken('foo.bar.baz')->refresh();
  110. $this->assertEquals($result, 'baz.bar.foo');
  111. }
  112. /** @test */
  113. public function it_should_invalidate_a_token()
  114. {
  115. $this->manager->shouldReceive('invalidate')->once()->andReturn(true);
  116. $result = $this->jwtAuth->invalidate('foo.bar.baz');
  117. $this->assertTrue($result);
  118. }
  119. /** @test */
  120. public function it_should_retrieve_the_token_from_the_auth_header()
  121. {
  122. $request = Request::create('/foo', 'GET');
  123. $request->headers->set('authorization', 'Bearer foo.bar.baz');
  124. $jwtAuth = new JWTAuth($this->manager, $this->user, $this->auth, $request);
  125. $this->assertInstanceOf('Tymon\JWTAuth\Token', $jwtAuth->parseToken()->getToken());
  126. $this->assertEquals($jwtAuth->getToken(), 'foo.bar.baz');
  127. }
  128. /** @test */
  129. public function it_should_retrieve_the_token_from_the_query_string()
  130. {
  131. $request = Request::create('/foo', 'GET', ['token' => 'foo.bar.baz']);
  132. $jwtAuth = new JWTAuth($this->manager, $this->user, $this->auth, $request);
  133. $this->assertInstanceOf('Tymon\JWTAuth\Token', $jwtAuth->parseToken()->getToken());
  134. $this->assertEquals($jwtAuth->getToken(), 'foo.bar.baz');
  135. }
  136. /** @test */
  137. public function it_should_throw_an_exception_when_token_not_present_in_request()
  138. {
  139. $this->setExpectedException('Tymon\JWTAuth\Exceptions\JWTException');
  140. $request = Request::create('/foo', 'GET');
  141. $jwtAuth = new JWTAuth($this->manager, $this->user, $this->auth, $request);
  142. $jwtAuth->parseToken();
  143. }
  144. /** @test */
  145. public function it_should_return_false_when_no_token_is_set()
  146. {
  147. $this->assertFalse($this->jwtAuth->getToken());
  148. }
  149. /** @test */
  150. public function it_should_set_the_identifier()
  151. {
  152. $this->jwtAuth->setIdentifier('foo');
  153. $this->assertEquals($this->jwtAuth->getIdentifier(), 'foo');
  154. }
  155. /** @test */
  156. public function it_should_magically_call_the_manager()
  157. {
  158. $this->manager->shouldReceive('getBlacklist')->andReturn(new \StdClass);
  159. $blacklist = $this->jwtAuth->getBlacklist();
  160. $this->assertInstanceOf('StdClass', $blacklist);
  161. }
  162. /** @test */
  163. public function it_should_set_the_request()
  164. {
  165. $request = Request::create('/foo', 'GET', ['token' => 'some.random.token']);
  166. $token = $this->jwtAuth->setRequest($request)->getToken();
  167. $this->assertEquals('some.random.token', $token);
  168. }
  169. /** @test */
  170. public function it_should_get_the_manager_instance()
  171. {
  172. $manager = $this->jwtAuth->manager();
  173. $this->assertInstanceOf('Tymon\JWTAuth\JWTManager', $manager);
  174. }
  175. }