菜谱项目

JWTManagerTest.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Providers\JWT;
  11. use Mockery;
  12. use Tymon\JWTAuth\Token;
  13. use Tymon\JWTAuth\Payload;
  14. use Tymon\JWTAuth\JWTManager;
  15. use Tymon\JWTAuth\Claims\JwtId;
  16. use Tymon\JWTAuth\Claims\Issuer;
  17. use Tymon\JWTAuth\Claims\Subject;
  18. use Tymon\JWTAuth\Claims\IssuedAt;
  19. use Tymon\JWTAuth\Claims\NotBefore;
  20. use Tymon\JWTAuth\Claims\Expiration;
  21. class JWTManagerTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function setUp()
  24. {
  25. $this->jwt = Mockery::mock('Tymon\JWTAuth\Providers\JWT\JWTInterface');
  26. $this->blacklist = Mockery::mock('Tymon\JWTAuth\Blacklist');
  27. $this->factory = Mockery::mock('Tymon\JWTAuth\PayloadFactory');
  28. $this->manager = new JWTManager($this->jwt, $this->blacklist, $this->factory);
  29. $this->validator = Mockery::mock('Tymon\JWTAuth\Validators\PayloadValidator');
  30. $this->validator->shouldReceive('setRefreshFlow->check');
  31. }
  32. public function tearDown()
  33. {
  34. Mockery::close();
  35. }
  36. /** @test */
  37. public function it_should_encode_a_payload()
  38. {
  39. $claims = [
  40. new Subject(1),
  41. new Issuer('http://example.com'),
  42. new Expiration(123 + 3600),
  43. new NotBefore(123),
  44. new IssuedAt(123),
  45. new JwtId('foo'),
  46. ];
  47. $payload = new Payload($claims, $this->validator);
  48. $this->jwt->shouldReceive('encode')->with($payload->toArray())->andReturn('foo.bar.baz');
  49. $token = $this->manager->encode($payload);
  50. $this->assertEquals($token, 'foo.bar.baz');
  51. }
  52. /** @test */
  53. public function it_should_decode_a_token()
  54. {
  55. $claims = [
  56. new Subject(1),
  57. new Issuer('http://example.com'),
  58. new Expiration(123 + 3600),
  59. new NotBefore(123),
  60. new IssuedAt(123),
  61. new JwtId('foo'),
  62. ];
  63. $payload = new Payload($claims, $this->validator);
  64. $token = new Token('foo.bar.baz');
  65. $this->jwt->shouldReceive('decode')->once()->with('foo.bar.baz')->andReturn($payload->toArray());
  66. $this->factory->shouldReceive('setRefreshFlow->make')->with($payload->toArray())->andReturn($payload);
  67. $this->blacklist->shouldReceive('has')->with($payload)->andReturn(false);
  68. $payload = $this->manager->decode($token);
  69. $this->assertInstanceOf('Tymon\JWTAuth\Payload', $payload);
  70. }
  71. /** @test */
  72. public function it_should_throw_exception_when_token_is_blacklisted()
  73. {
  74. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenBlacklistedException');
  75. $claims = [
  76. new Subject(1),
  77. new Issuer('http://example.com'),
  78. new Expiration(123 + 3600),
  79. new NotBefore(123),
  80. new IssuedAt(123),
  81. new JwtId('foo'),
  82. ];
  83. $payload = new Payload($claims, $this->validator);
  84. $token = new Token('foo.bar.baz');
  85. $this->jwt->shouldReceive('decode')->once()->with('foo.bar.baz')->andReturn($payload->toArray());
  86. $this->factory->shouldReceive('setRefreshFlow->make')->with($payload->toArray())->andReturn($payload);
  87. $this->blacklist->shouldReceive('has')->with($payload)->andReturn(true);
  88. $this->manager->decode($token);
  89. }
  90. /** @test */
  91. public function it_should_refresh_a_token()
  92. {
  93. $claims = [
  94. new Subject(1),
  95. new Issuer('http://example.com'),
  96. new Expiration(123 - 3600),
  97. new NotBefore(123),
  98. new IssuedAt(123),
  99. new JwtId('foo'),
  100. ];
  101. $payload = new Payload($claims, $this->validator, true);
  102. $token = new Token('foo.bar.baz');
  103. $this->jwt->shouldReceive('decode')->once()->with('foo.bar.baz')->andReturn($payload->toArray());
  104. $this->jwt->shouldReceive('encode')->with($payload->toArray())->andReturn('baz.bar.foo');
  105. $this->factory->shouldReceive('setRefreshFlow')->andReturn($this->factory);
  106. $this->factory->shouldReceive('make')->andReturn($payload);
  107. $this->blacklist->shouldReceive('has')->with($payload)->andReturn(false);
  108. $this->blacklist->shouldReceive('add')->once()->with($payload);
  109. $token = $this->manager->refresh($token);
  110. $this->assertInstanceOf('Tymon\JWTAuth\Token', $token);
  111. $this->assertEquals('baz.bar.foo', $token);
  112. }
  113. /** @test */
  114. public function it_should_invalidate_a_token()
  115. {
  116. $claims = [
  117. new Subject(1),
  118. new Issuer('http://example.com'),
  119. new Expiration(123 + 3600),
  120. new NotBefore(123),
  121. new IssuedAt(123),
  122. new JwtId('foo'),
  123. ];
  124. $payload = new Payload($claims, $this->validator);
  125. $token = new Token('foo.bar.baz');
  126. $this->jwt->shouldReceive('decode')->once()->with('foo.bar.baz')->andReturn($payload->toArray());
  127. $this->factory->shouldReceive('setRefreshFlow->make')->with($payload->toArray())->andReturn($payload);
  128. $this->blacklist->shouldReceive('has')->with($payload)->andReturn(false);
  129. $this->blacklist->shouldReceive('add')->with($payload)->andReturn(true);
  130. $this->manager->invalidate($token);
  131. }
  132. /** @test */
  133. public function it_should_throw_an_exception_when_enable_blacklist_is_set_to_false()
  134. {
  135. $this->setExpectedException('Tymon\JWTAuth\Exceptions\JWTException');
  136. $token = new Token('foo.bar.baz');
  137. $this->manager->setBlacklistEnabled(false)->invalidate($token);
  138. }
  139. /** @test */
  140. public function it_should_get_the_payload_factory()
  141. {
  142. $this->assertInstanceOf('Tymon\JWTAuth\PayloadFactory', $this->manager->getPayloadFactory());
  143. }
  144. /** @test */
  145. public function it_should_get_the_jwt_provider()
  146. {
  147. $this->assertInstanceOf('Tymon\JWTAuth\Providers\JWT\JWTInterface', $this->manager->getJWTProvider());
  148. }
  149. /** @test */
  150. public function it_should_get_the_blacklist()
  151. {
  152. $this->assertInstanceOf('Tymon\JWTAuth\Blacklist', $this->manager->getBlacklist());
  153. }
  154. }