|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\RemoteClaimsBundle\Tests\Validator\Constraints; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaim; |
|
14
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface; |
|
15
|
|
|
use LoginCidadao\RemoteClaimsBundle\Model\TagUri; |
|
16
|
|
|
use LoginCidadao\RemoteClaimsBundle\Validator\Constraints\HostBelongsToClaimProvider; |
|
17
|
|
|
use LoginCidadao\RemoteClaimsBundle\Validator\Constraints\HostBelongsToClaimProviderValidator; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
|
20
|
|
|
|
|
21
|
|
|
class HostBelongsToClaimProviderValidatorTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testValidateOk() |
|
24
|
|
|
{ |
|
25
|
|
|
$claimName = TagUri::createFromString('tag:example.com,2017:my_claim'); |
|
26
|
|
|
$value = $this->getRemoteClaim($claimName, ['https://my.example.com/', 'https://example.com/valid']); |
|
27
|
|
|
|
|
28
|
|
|
$validator = new HostBelongsToClaimProviderValidator(); |
|
29
|
|
|
$validator->validate($value, new HostBelongsToClaimProvider()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testValidateFail() |
|
33
|
|
|
{ |
|
34
|
|
|
$host = 'example.com'; |
|
35
|
|
|
$claimName = TagUri::createFromString("tag:{$host},2017:my_claim"); |
|
36
|
|
|
|
|
37
|
|
|
$violationBuilder = $this->createMock('Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface'); |
|
38
|
|
|
$violationBuilder->expects($this->once())->method('addViolation'); |
|
39
|
|
|
$violationBuilder->expects($this->once())->method('setParameter') |
|
40
|
|
|
->with('{{ host }}', $host)->willReturn($violationBuilder); |
|
41
|
|
|
|
|
42
|
|
|
/** @var ExecutionContextInterface|\PHPUnit_Framework_MockObject_MockObject $context */ |
|
43
|
|
|
$context = $this->createMock('Symfony\Component\Validator\Context\ExecutionContextInterface'); |
|
44
|
|
|
$context->expects($this->once())->method('buildViolation')->willReturn($violationBuilder); |
|
45
|
|
|
|
|
46
|
|
|
$value = $this->getRemoteClaim($claimName, ['https://my.example.com/']); |
|
47
|
|
|
|
|
48
|
|
|
$validator = new HostBelongsToClaimProviderValidator(); |
|
49
|
|
|
$validator->initialize($context); |
|
50
|
|
|
$validator->validate($value, new HostBelongsToClaimProvider()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getRemoteClaim($claimName, $redirectUris) |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var ClaimProviderInterface|\PHPUnit_Framework_MockObject_MockObject $provider */ |
|
56
|
|
|
$provider = $this->createMock('LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface'); |
|
57
|
|
|
$provider->expects($this->once())->method('getRedirectUris') |
|
58
|
|
|
->willReturn($redirectUris); |
|
59
|
|
|
|
|
60
|
|
|
$remoteClaim = (new RemoteClaim()) |
|
61
|
|
|
->setName($claimName) |
|
62
|
|
|
->setProvider($provider); |
|
63
|
|
|
|
|
64
|
|
|
return $remoteClaim; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|