菜谱项目

BlacklistTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Carbon\Carbon;
  13. use Tymon\JWTAuth\Payload;
  14. use Tymon\JWTAuth\Blacklist;
  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 BlacklistTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function setUp()
  24. {
  25. Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
  26. $this->storage = Mockery::mock('Tymon\JWTAuth\Providers\Storage\StorageInterface');
  27. $this->blacklist = new Blacklist($this->storage);
  28. $this->blacklist->setRefreshTTL(20160);
  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_add_a_valid_token_to_the_blacklist()
  38. {
  39. $claims = [
  40. new Subject(1),
  41. new Issuer('http://example.com'),
  42. new Expiration(100 + 3600),
  43. new NotBefore(100),
  44. new IssuedAt(100),
  45. new JwtId('foo'),
  46. ];
  47. $payload = new Payload($claims, $this->validator);
  48. $this->storage->shouldReceive('add')->once()->with('foo', [], 20160);
  49. $this->assertTrue($this->blacklist->add($payload));
  50. }
  51. /** @test */
  52. public function it_should_return_true_when_adding_a_refreshable_expired_token_to_the_blacklist()
  53. {
  54. $claims = [
  55. new Subject(1),
  56. new Issuer('http://example.com'),
  57. new Expiration(101),
  58. new NotBefore(100),
  59. new IssuedAt(100),
  60. new JwtId('foo'),
  61. ];
  62. $payload = new Payload($claims, $this->validator, true);
  63. $this->storage->shouldReceive('add')->once()->with('foo', [], 20160);
  64. $this->assertTrue($this->blacklist->add($payload));
  65. }
  66. /** @test */
  67. public function it_should_return_false_when_adding_an_unrefreshable_token_to_the_blacklist()
  68. {
  69. $claims = [
  70. new Subject(1),
  71. new Issuer('http://example.com'),
  72. new Expiration(100), // default refresh_ttl
  73. new NotBefore(100),
  74. new IssuedAt(100 - 20160 * 60),
  75. new JwtId('foo'),
  76. ];
  77. $payload = new Payload($claims, $this->validator, true);
  78. $this->storage->shouldReceive('add')->never();
  79. $this->assertFalse($this->blacklist->add($payload));
  80. }
  81. /** @test */
  82. public function it_should_return_false_when_adding_a_unrefreshable_token_after_modifying_refresh_ttl()
  83. {
  84. $claims = [
  85. new Subject(1),
  86. new Issuer('http://example.com'),
  87. new Expiration(101),
  88. new NotBefore(100),
  89. new IssuedAt(100),
  90. new JwtId('foo'),
  91. ];
  92. $payload = new Payload($claims, $this->validator, true);
  93. $this->storage->shouldReceive('add')->never();
  94. $this->blacklist->setRefreshTTL(0);
  95. $this->assertFalse($this->blacklist->add($payload));
  96. }
  97. /** @test */
  98. public function it_should_check_whether_a_token_has_been_blacklisted()
  99. {
  100. $claims = [
  101. new Subject(1),
  102. new Issuer('http://example.com'),
  103. new Expiration(123 + 3600),
  104. new NotBefore(123),
  105. new IssuedAt(123),
  106. new JwtId('foobar'),
  107. ];
  108. $payload = new Payload($claims, $this->validator);
  109. $this->storage->shouldReceive('has')->once()->with('foobar')->andReturn(true);
  110. $this->assertTrue($this->blacklist->has($payload));
  111. }
  112. /** @test */
  113. public function it_should_remove_a_token_from_the_blacklist()
  114. {
  115. $claims = [
  116. new Subject(1),
  117. new Issuer('http://example.com'),
  118. new Expiration(123 + 3600),
  119. new NotBefore(123),
  120. new IssuedAt(123),
  121. new JwtId('foobar'),
  122. ];
  123. $payload = new Payload($claims, $this->validator);
  124. $this->storage->shouldReceive('destroy')->once()->with('foobar')->andReturn(true);
  125. $this->assertTrue($this->blacklist->remove($payload));
  126. }
  127. /** @test */
  128. public function it_should_empty_the_blacklist()
  129. {
  130. $this->storage->shouldReceive('flush')->once();
  131. $this->assertTrue($this->blacklist->clear());
  132. }
  133. }