菜谱项目

PayloadValidatorTest.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 Carbon\Carbon;
  12. use Tymon\JWTAuth\Validators\PayloadValidator;
  13. class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function setUp()
  16. {
  17. Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
  18. $this->validator = new PayloadValidator();
  19. }
  20. /** @test */
  21. public function it_should_return_true_when_providing_a_valid_payload()
  22. {
  23. $payload = [
  24. 'iss' => 'http://example.com',
  25. 'iat' => 100,
  26. 'nbf' => 100,
  27. 'exp' => 100 + 3600,
  28. 'sub' => 1,
  29. 'jti' => 'foo',
  30. ];
  31. $this->assertTrue($this->validator->isValid($payload));
  32. }
  33. /** @test */
  34. public function it_should_throw_an_exception_when_providing_an_expired_payload()
  35. {
  36. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenExpiredException');
  37. $payload = [
  38. 'iss' => 'http://example.com',
  39. 'iat' => 20,
  40. 'nbf' => 20,
  41. 'exp' => 120,
  42. 'sub' => 1,
  43. 'jti' => 'foo',
  44. ];
  45. $this->validator->check($payload);
  46. }
  47. /** @test */
  48. public function it_should_throw_an_exception_when_providing_an_invalid_nbf_claim()
  49. {
  50. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenInvalidException');
  51. $payload = [
  52. 'iss' => 'http://example.com',
  53. 'iat' => 100,
  54. 'nbf' => 150,
  55. 'exp' => 150 + 3600,
  56. 'sub' => 1,
  57. 'jti' => 'foo',
  58. ];
  59. $this->validator->check($payload);
  60. }
  61. /** @test */
  62. public function it_should_throw_an_exception_when_providing_an_invalid_iat_claim()
  63. {
  64. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenInvalidException');
  65. $payload = [
  66. 'iss' => 'http://example.com',
  67. 'iat' => 150,
  68. 'nbf' => 100,
  69. 'exp' => 150 + 3600,
  70. 'sub' => 1,
  71. 'jti' => 'foo',
  72. ];
  73. $this->validator->check($payload);
  74. }
  75. /** @test */
  76. public function it_should_throw_an_exception_when_providing_an_invalid_payload()
  77. {
  78. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenInvalidException');
  79. $payload = [
  80. 'iss' => 'http://example.com',
  81. 'sub' => 1,
  82. ];
  83. $this->validator->check($payload);
  84. }
  85. /** @test */
  86. public function it_should_throw_an_exception_when_providing_an_invalid_expiry()
  87. {
  88. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenInvalidException');
  89. $payload = [
  90. 'iss' => 'http://example.com',
  91. 'iat' => 100,
  92. 'exp' => 'foo',
  93. 'sub' => 1,
  94. 'jti' => 'foo',
  95. ];
  96. $this->validator->check($payload);
  97. }
  98. /** @test **/
  99. public function it_should_throw_an_exception_when_required_claims_are_missing()
  100. {
  101. $this->setExpectedException('Tymon\JWTAuth\Exceptions\TokenInvalidException');
  102. $payload = [
  103. 'iss' => 'http://example.com',
  104. 'foo' => 'bar',
  105. // these are inserted to check for regression to a previous bug
  106. // where the check would only compare keys of autoindexed name arrays
  107. // (There are enough to account for all of the required claims' indices)
  108. 'autoindexed',
  109. 'autoindexed',
  110. 'autoindexed',
  111. 'autoindexed',
  112. 'autoindexed',
  113. 'autoindexed',
  114. 'autoindexed',
  115. ];
  116. $this->validator->check($payload);
  117. }
  118. }