|
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\OpenIDBundle\Tests\Service; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService; |
|
14
|
|
|
|
|
15
|
|
|
class SubjectIdentifierServiceTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
private function getRepo() |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$repo = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifierRepository') |
|
20
|
|
|
->disableOriginalConstructor() |
|
21
|
|
|
->getMock(); |
|
22
|
|
|
|
|
23
|
|
|
return $repo; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function getPerson($id = null) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface'); |
|
29
|
|
|
if ($id) { |
|
30
|
|
|
$person->expects($this->once())->method('getId')->willReturn($id); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $person; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function getClientMetadata($type = null) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$metadata = $this->getMock('LoginCidadao\OpenIDBundle\Entity\ClientMetadata'); |
|
39
|
|
|
if (!$type) { |
|
40
|
|
|
return $metadata; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$metadata->expects($this->once())->method('getSubjectType')->willReturn($type); |
|
44
|
|
|
if ($type === 'pairwise') { |
|
45
|
|
|
$metadata->expects($this->once())->method('getSectorIdentifier') |
|
46
|
|
|
->willReturn('https://example.com/sector.identifier'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $metadata; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testPairwiseSubjectIdentifier() |
|
53
|
|
|
{ |
|
54
|
|
|
$id = 123456; |
|
55
|
|
|
$secret = 'my.secret'; |
|
56
|
|
|
|
|
57
|
|
|
$repo = $this->getRepo(); |
|
58
|
|
|
$person = $this->getPerson($id); |
|
59
|
|
|
$metadata = $this->getClientMetadata('pairwise'); |
|
60
|
|
|
|
|
61
|
|
|
$service = new SubjectIdentifierService($repo, $secret); |
|
62
|
|
|
$sub = $service->getSubjectIdentifier($person, $metadata); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertNotNull($sub); |
|
65
|
|
|
$this->assertNotEquals($id, $sub); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testPublicSubjectIdentifier() |
|
69
|
|
|
{ |
|
70
|
|
|
$id = 654321; |
|
71
|
|
|
$secret = 'my.secret'; |
|
72
|
|
|
|
|
73
|
|
|
$repo = $this->getRepo(); |
|
74
|
|
|
$person = $this->getPerson($id); |
|
75
|
|
|
$metadata = $this->getClientMetadata('public'); |
|
76
|
|
|
|
|
77
|
|
|
$service = new SubjectIdentifierService($repo, $secret); |
|
78
|
|
|
$sub = $service->getSubjectIdentifier($person, $metadata); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertNotNull($sub); |
|
81
|
|
|
$this->assertEquals($id, $sub); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testPublicSubjectIdentifierNullMetadata() |
|
85
|
|
|
{ |
|
86
|
|
|
$id = 654321; |
|
87
|
|
|
$secret = 'my.secret'; |
|
88
|
|
|
|
|
89
|
|
|
$repo = $this->getRepo(); |
|
90
|
|
|
$person = $this->getPerson($id); |
|
91
|
|
|
|
|
92
|
|
|
$service = new SubjectIdentifierService($repo, $secret); |
|
93
|
|
|
$sub = $service->getSubjectIdentifier($person, null); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertNotNull($sub); |
|
96
|
|
|
$this->assertEquals($id, $sub); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testFetchSubjectIdentifier() |
|
100
|
|
|
{ |
|
101
|
|
|
$id = 123456; |
|
102
|
|
|
$secret = 'my.secret'; |
|
103
|
|
|
$expectedSub = 'my_sub_id'; |
|
104
|
|
|
|
|
105
|
|
|
$subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'); |
|
106
|
|
|
$subId->expects($this->once())->method('getSubjectIdentifier')->willReturn($expectedSub); |
|
107
|
|
|
|
|
108
|
|
|
$repo = $this->getRepo(); |
|
109
|
|
|
$repo->expects($this->once())->method('findOneBy')->willReturn($subId); |
|
110
|
|
|
|
|
111
|
|
|
$person = $this->getPerson(); |
|
112
|
|
|
$metadata = $this->getClientMetadata(); |
|
113
|
|
|
|
|
114
|
|
|
$service = new SubjectIdentifierService($repo, $secret); |
|
115
|
|
|
$sub = $service->getSubjectIdentifier($person, $metadata); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertNotNull($sub); |
|
118
|
|
|
$this->assertNotEquals($id, $sub); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testIsSubjectIdentifierPersisted() |
|
122
|
|
|
{ |
|
123
|
|
|
$secret = 'my.secret'; |
|
124
|
|
|
|
|
125
|
|
|
$subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'); |
|
126
|
|
|
$client = $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface'); |
|
127
|
|
|
|
|
128
|
|
|
$repo = $this->getRepo(); |
|
129
|
|
|
$repo->expects($this->once())->method('findOneBy')->willReturn($subId); |
|
130
|
|
|
|
|
131
|
|
|
$service = new SubjectIdentifierService($repo, $secret); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertTrue($service->isSubjectIdentifierPersisted($this->getPerson(), $client)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.