123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
- *
- * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
- */
- namespace Lcobucci\JWT;
- use Lcobucci\JWT\Claim\Factory as ClaimFactory;
- use Lcobucci\JWT\Parsing\Decoder;
- use RuntimeException;
- /**
- * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
- * @since 0.1.0
- */
- class ParserTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var Decoder|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $decoder;
- /**
- * @var ClaimFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $claimFactory;
- /**
- * @var Claim|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $defaultClaim;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- $this->decoder = $this->getMock(Decoder::class);
- $this->claimFactory = $this->getMock(ClaimFactory::class, [], [], '', false);
- $this->defaultClaim = $this->getMock(Claim::class);
- $this->claimFactory->expects($this->any())
- ->method('create')
- ->willReturn($this->defaultClaim);
- }
- /**
- * @return Parser
- */
- private function createParser()
- {
- return new Parser($this->decoder, $this->claimFactory);
- }
- /**
- * @test
- *
- * @covers Lcobucci\JWT\Parser::__construct
- */
- public function constructMustConfigureTheAttributes()
- {
- $parser = $this->createParser();
- $this->assertAttributeSame($this->decoder, 'decoder', $parser);
- $this->assertAttributeSame($this->claimFactory, 'claimFactory', $parser);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- *
- * @expectedException InvalidArgumentException
- */
- public function parseMustRaiseExceptionWhenJWSIsNotAString()
- {
- $parser = $this->createParser();
- $parser->parse(['asdasd']);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- *
- * @expectedException InvalidArgumentException
- */
- public function parseMustRaiseExceptionWhenJWSDontHaveThreeParts()
- {
- $parser = $this->createParser();
- $parser->parse('');
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- * @covers Lcobucci\JWT\Parser::parseHeader
- *
- * @expectedException RuntimeException
- */
- public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded()
- {
- $this->decoder->expects($this->any())
- ->method('jsonDecode')
- ->willThrowException(new RuntimeException());
- $parser = $this->createParser();
- $parser->parse('asdfad.asdfasdf.');
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- * @covers Lcobucci\JWT\Parser::parseHeader
- *
- * @expectedException InvalidArgumentException
- */
- public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken()
- {
- $this->decoder->expects($this->any())
- ->method('jsonDecode')
- ->willReturn(['enc' => 'AAA']);
- $parser = $this->createParser();
- $parser->parse('a.a.');
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- * @uses Lcobucci\JWT\Token::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- * @covers Lcobucci\JWT\Parser::parseHeader
- * @covers Lcobucci\JWT\Parser::parseClaims
- * @covers Lcobucci\JWT\Parser::parseSignature
- *
- */
- public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed()
- {
- $this->decoder->expects($this->at(1))
- ->method('jsonDecode')
- ->willReturn(['typ' => 'JWT', 'alg' => 'none']);
- $this->decoder->expects($this->at(3))
- ->method('jsonDecode')
- ->willReturn(['aud' => 'test']);
- $parser = $this->createParser();
- $token = $parser->parse('a.a.');
- $this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'none'], 'headers', $token);
- $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
- $this->assertAttributeEquals(null, 'signature', $token);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- * @uses Lcobucci\JWT\Token::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- * @covers Lcobucci\JWT\Parser::parseHeader
- * @covers Lcobucci\JWT\Parser::parseClaims
- * @covers Lcobucci\JWT\Parser::parseSignature
- */
- public function parseShouldReplicateClaimValueOnHeaderWhenNeeded()
- {
- $this->decoder->expects($this->at(1))
- ->method('jsonDecode')
- ->willReturn(['typ' => 'JWT', 'alg' => 'none', 'aud' => 'test']);
- $this->decoder->expects($this->at(3))
- ->method('jsonDecode')
- ->willReturn(['aud' => 'test']);
- $parser = $this->createParser();
- $token = $parser->parse('a.a.');
- $this->assertAttributeEquals(
- ['typ' => 'JWT', 'alg' => 'none', 'aud' => $this->defaultClaim],
- 'headers',
- $token
- );
- $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
- $this->assertAttributeEquals(null, 'signature', $token);
- }
- /**
- * @test
- *
- * @uses Lcobucci\JWT\Parser::__construct
- * @uses Lcobucci\JWT\Token::__construct
- * @uses Lcobucci\JWT\Signature::__construct
- *
- * @covers Lcobucci\JWT\Parser::parse
- * @covers Lcobucci\JWT\Parser::splitJwt
- * @covers Lcobucci\JWT\Parser::parseHeader
- * @covers Lcobucci\JWT\Parser::parseClaims
- * @covers Lcobucci\JWT\Parser::parseSignature
- */
- public function parseMustReturnASignedTokenWhenSignatureIsInformed()
- {
- $this->decoder->expects($this->at(1))
- ->method('jsonDecode')
- ->willReturn(['typ' => 'JWT', 'alg' => 'HS256']);
- $this->decoder->expects($this->at(3))
- ->method('jsonDecode')
- ->willReturn(['aud' => 'test']);
- $this->decoder->expects($this->at(4))
- ->method('base64UrlDecode')
- ->willReturn('aaa');
- $parser = $this->createParser();
- $token = $parser->parse('a.a.a');
- $this->assertAttributeEquals(['typ' => 'JWT', 'alg' => 'HS256'], 'headers', $token);
- $this->assertAttributeEquals(['aud' => $this->defaultClaim], 'claims', $token);
- $this->assertAttributeEquals(new Signature('aaa'), 'signature', $token);
- }
- }
|