1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<?php
declare(strict_types=1);
namespace Lcobucci\JWT\Tests\Validation\Constraint;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Signature;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\ConstraintViolation;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\MockObject\MockObject;
#[PHPUnit\CoversClass(ConstraintViolation::class)]
#[PHPUnit\CoversClass(SignedWith::class)]
#[PHPUnit\UsesClass(Signer\Key\InMemory::class)]
#[PHPUnit\UsesClass(Token\DataSet::class)]
#[PHPUnit\UsesClass(Token\Plain::class)]
#[PHPUnit\UsesClass(Token\Signature::class)]
final class SignedWithTest extends ConstraintTestCase
{
private Signer&MockObject $signer;
private Signer\Key $key;
private Signature $signature;
#[PHPUnit\Before]
public function createDependencies(): void
{
$this->signer = $this->createMock(Signer::class);
$this->signer->method('algorithmId')->willReturn('RS256');
$this->key = Signer\Key\InMemory::plainText('123');
$this->signature = new Signature('1234', '5678');
}
#[PHPUnit\Test]
public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void
{
$constraint = new SignedWith($this->signer, $this->key);
$this->expectException(ConstraintViolation::class);
$this->expectExceptionMessage('You should pass a plain token');
$constraint->assert($this->createMock(Token::class));
}
#[PHPUnit\Test]
public function assertShouldRaiseExceptionWhenSignerIsNotTheSame(): void
{
$token = $this->buildToken([], ['alg' => 'test'], $this->signature);
$this->signer->expects($this->never())->method('verify');
$constraint = new SignedWith($this->signer, $this->key);
$this->expectException(ConstraintViolation::class);
$this->expectExceptionMessage('Token signer mismatch');
$constraint->assert($token);
}
#[PHPUnit\Test]
public function assertShouldRaiseExceptionWhenSignatureIsInvalid(): void
{
$token = $this->buildToken([], ['alg' => 'RS256'], $this->signature);
$this->signer->expects($this->once())
->method('verify')
->with($this->signature->hash(), $token->payload(), $this->key)
->willReturn(false);
$constraint = new SignedWith($this->signer, $this->key);
$this->expectException(ConstraintViolation::class);
$this->expectExceptionMessage('Token signature mismatch');
$constraint->assert($token);
}
#[PHPUnit\Test]
public function assertShouldNotRaiseExceptionWhenSignatureIsValid(): void
{
$token = $this->buildToken([], ['alg' => 'RS256'], $this->signature);
$this->signer->expects($this->once())
->method('verify')
->with($this->signature->hash(), $token->payload(), $this->key)
->willReturn(true);
$constraint = new SignedWith($this->signer, $this->key);
$constraint->assert($token);
$this->addToAssertionCount(1);
}
}
|