菜谱项目

PayloadFactoryTest.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Illuminate\Http\Request;
  14. use Tymon\JWTAuth\Claims\JwtId;
  15. use Tymon\JWTAuth\Claims\Custom;
  16. use Tymon\JWTAuth\Claims\Issuer;
  17. use Tymon\JWTAuth\Claims\Subject;
  18. use Tymon\JWTAuth\PayloadFactory;
  19. use Tymon\JWTAuth\Claims\IssuedAt;
  20. use Tymon\JWTAuth\Claims\NotBefore;
  21. use Tymon\JWTAuth\Claims\Expiration;
  22. class PayloadFactoryTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function setUp()
  25. {
  26. Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
  27. $this->claimFactory = Mockery::mock('Tymon\JWTAuth\Claims\Factory');
  28. $this->validator = Mockery::mock('Tymon\JWTAuth\Validators\PayloadValidator');
  29. $this->factory = new PayloadFactory($this->claimFactory, Request::create('/foo', 'GET'), $this->validator);
  30. }
  31. public function tearDown()
  32. {
  33. Mockery::close();
  34. }
  35. /** @test */
  36. public function it_should_return_a_payload_when_passing_an_array_of_claims_to_make_method()
  37. {
  38. $this->validator->shouldReceive('setRefreshFlow->check');
  39. $expTime = 123 + 3600;
  40. $this->claimFactory->shouldReceive('get')->once()->with('sub', 1)->andReturn(new Subject(1));
  41. $this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
  42. $this->claimFactory->shouldReceive('get')->once()->with('iat', 123)->andReturn(new IssuedAt(123));
  43. $this->claimFactory->shouldReceive('get')->once()->with('jti', 'foo')->andReturn(new JwtId('foo'));
  44. $this->claimFactory->shouldReceive('get')->once()->with('nbf', 123)->andReturn(new NotBefore(123));
  45. $this->claimFactory->shouldReceive('get')->once()->with('exp', $expTime)->andReturn(new Expiration($expTime));
  46. $payload = $this->factory->make(['sub' => 1, 'jti' => 'foo', 'iat' => 123]);
  47. $this->assertEquals($payload->get('sub'), 1);
  48. $this->assertEquals($payload->get('iat'), 123);
  49. $this->assertEquals($payload['exp'], $expTime);
  50. $this->assertInstanceOf('Tymon\JWTAuth\Payload', $payload);
  51. }
  52. /** @test **/
  53. public function it_should_check_custom_claim_keys_accurately_and_accept_numeric_claims()
  54. {
  55. $this->validator->shouldReceive('setRefreshFlow->check');
  56. $this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
  57. $this->claimFactory->shouldReceive('get')->once()->with('exp', 123 + 3600)->andReturn(new Expiration(123 + 3600));
  58. $this->claimFactory->shouldReceive('get')->once()->with('iat', 123)->andReturn(new IssuedAt(123));
  59. $this->claimFactory->shouldReceive('get')->once()->with('jti', Mockery::any())->andReturn(new JwtId('foo'));
  60. $this->claimFactory->shouldReceive('get')->once()->with('nbf', 123)->andReturn(new NotBefore(123));
  61. $this->claimFactory->shouldReceive('get')->once()->with(1, 'claim one')->andReturn(new Custom(1, 'claim one'));
  62. $payload = $this->factory->make([1 => 'claim one']);
  63. // if the checker doesn't compare defaults properly, numeric-keyed claims might be ignored
  64. $this->assertEquals('claim one', $payload->get(1));
  65. // iat is $defaultClaims[1], so verify it wasn't skipped due to a bad k-v comparison
  66. $this->assertEquals(123, $payload->get('iat'));
  67. }
  68. /** @test */
  69. public function it_should_return_a_payload_when_chaining_claim_methods()
  70. {
  71. $this->validator->shouldReceive('setRefreshFlow->check');
  72. $this->claimFactory->shouldReceive('get')->once()->with('sub', 1)->andReturn(new Subject(1));
  73. $this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
  74. $this->claimFactory->shouldReceive('get')->once()->with('exp', 123 + 3600)->andReturn(new Expiration(123 + 3600));
  75. $this->claimFactory->shouldReceive('get')->once()->with('iat', 123)->andReturn(new IssuedAt(123));
  76. $this->claimFactory->shouldReceive('get')->once()->with('jti', Mockery::any())->andReturn(new JwtId('foo'));
  77. $this->claimFactory->shouldReceive('get')->once()->with('nbf', 123)->andReturn(new NotBefore(123));
  78. $this->claimFactory->shouldReceive('get')->once()->with('foo', 'baz')->andReturn(new Custom('foo', 'baz'));
  79. $payload = $this->factory->sub(1)->foo('baz')->make();
  80. $this->assertEquals($payload['sub'], 1);
  81. $this->assertEquals($payload->get('jti'), 'foo');
  82. $this->assertEquals($payload->get('foo'), 'baz');
  83. $this->assertInstanceOf('Tymon\JWTAuth\Payload', $payload);
  84. }
  85. /** @test */
  86. public function it_should_return_a_payload_when_passing_miltidimensional_claims()
  87. {
  88. $this->validator->shouldReceive('setRefreshFlow->check');
  89. $userObject = ['name' => 'example'];
  90. $this->claimFactory->shouldReceive('get')->once()->with('sub', $userObject)->andReturn(new Subject($userObject));
  91. $this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
  92. $this->claimFactory->shouldReceive('get')->once()->with('exp', Mockery::any())->andReturn(new Expiration(123 + 3600));
  93. $this->claimFactory->shouldReceive('get')->once()->with('iat', Mockery::any())->andReturn(new IssuedAt(123));
  94. $this->claimFactory->shouldReceive('get')->once()->with('jti', Mockery::any())->andReturn(new JwtId('foo'));
  95. $this->claimFactory->shouldReceive('get')->once()->with('nbf', Mockery::any())->andReturn(new NotBefore(123));
  96. $this->claimFactory->shouldReceive('get')->once()->with('foo', ['bar' => [0, 0, 0]])->andReturn(new Custom('foo', ['bar' => [0, 0, 0]]));
  97. $payload = $this->factory->sub($userObject)->foo(['bar' => [0, 0, 0]])->make();
  98. $this->assertEquals($payload->get('sub'), $userObject);
  99. $this->assertEquals($payload->get('foo'), ['bar' => [0, 0, 0]]);
  100. $this->assertInstanceOf('Tymon\JWTAuth\Payload', $payload);
  101. }
  102. /** @test */
  103. public function it_should_set_the_ttl()
  104. {
  105. $this->factory->setTTL(12345);
  106. $this->assertEquals($this->factory->getTTL(), 12345);
  107. }
  108. }