1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Emarref\Jwt\Verification; |
4
|
|
|
|
5
|
|
|
use Emarref\Jwt\Claim; |
6
|
|
|
|
7
|
|
View Code Duplication |
class IssuerVerifierTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token\Payload |
11
|
|
|
*/ |
12
|
|
|
private $payload; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token |
16
|
|
|
*/ |
17
|
|
|
private $token; |
18
|
|
|
|
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
$this->payload = $this->getMockBuilder('Emarref\Jwt\Token\Payload')->getMock(); |
22
|
|
|
|
23
|
|
|
$this->token = $this->getMockBuilder('Emarref\Jwt\Token')->getMock(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @expectedException \InvalidArgumentException |
28
|
|
|
* @expectedExceptionMessage Cannot verify invalid issuer value. |
29
|
|
|
*/ |
30
|
|
|
public function testInvalidIssuer() |
31
|
|
|
{ |
32
|
|
|
new IssuerVerifier(new \stdClass()); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testNoIssuer() |
36
|
|
|
{ |
37
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
38
|
|
|
->method('getPayload') |
39
|
|
|
->will($this->returnValue($this->payload)); |
40
|
|
|
|
41
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
42
|
|
|
->method('findClaimByName') |
43
|
|
|
->with(Claim\Issuer::NAME) |
44
|
|
|
->will($this->returnValue(null)); |
45
|
|
|
|
46
|
|
|
$verifier = new IssuerVerifier(); |
47
|
|
|
$verifier->verify($this->token); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidIssuerException |
52
|
|
|
* @expectedExceptionMessage Issuer is invalid. |
53
|
|
|
*/ |
54
|
|
|
public function testIssuerInPayloadOnly() |
55
|
|
|
{ |
56
|
|
|
$issuerClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Issuer') |
57
|
|
|
->getMock(); |
58
|
|
|
|
59
|
|
|
$issuerClaim->expects($this->once()) |
60
|
|
|
->method('getValue') |
61
|
|
|
->will($this->returnValue('an_issuer')); |
62
|
|
|
|
63
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
64
|
|
|
->method('getPayload') |
65
|
|
|
->will($this->returnValue($this->payload)); |
66
|
|
|
|
67
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
68
|
|
|
->method('findClaimByName') |
69
|
|
|
->with(Claim\Issuer::NAME) |
70
|
|
|
->will($this->returnValue($issuerClaim)); |
71
|
|
|
|
72
|
|
|
$verifier = new IssuerVerifier(); |
73
|
|
|
$verifier->verify($this->token); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidIssuerException |
78
|
|
|
* @expectedExceptionMessage Issuer is invalid. |
79
|
|
|
*/ |
80
|
|
|
public function testIssuerInContextOnly() |
81
|
|
|
{ |
82
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
83
|
|
|
->method('getPayload') |
84
|
|
|
->will($this->returnValue($this->payload)); |
85
|
|
|
|
86
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
87
|
|
|
->method('findClaimByName') |
88
|
|
|
->with(Claim\Issuer::NAME) |
89
|
|
|
->will($this->returnValue(null)); |
90
|
|
|
|
91
|
|
|
$verifier = new IssuerVerifier('an_issuer'); |
92
|
|
|
$verifier->verify($this->token); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @expectedException \Emarref\Jwt\Exception\InvalidIssuerException |
97
|
|
|
* @expectedExceptionMessage Issuer is invalid. |
98
|
|
|
*/ |
99
|
|
|
public function testIssuerMismatch() |
100
|
|
|
{ |
101
|
|
|
$issuerClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Issuer') |
102
|
|
|
->getMock(); |
103
|
|
|
|
104
|
|
|
$issuerClaim->expects($this->once()) |
105
|
|
|
->method('getValue') |
106
|
|
|
->will($this->returnValue('an_issuer')); |
107
|
|
|
|
108
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
109
|
|
|
->method('getPayload') |
110
|
|
|
->will($this->returnValue($this->payload)); |
111
|
|
|
|
112
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
113
|
|
|
->method('findClaimByName') |
114
|
|
|
->with(Claim\Issuer::NAME) |
115
|
|
|
->will($this->returnValue($issuerClaim)); |
116
|
|
|
|
117
|
|
|
$verifier = new IssuerVerifier('some_other_issuer'); |
118
|
|
|
$verifier->verify($this->token); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testSuccess() |
122
|
|
|
{ |
123
|
|
|
$issuerClaim = $this->getMockBuilder('Emarref\Jwt\Claim\Issuer') |
124
|
|
|
->getMock(); |
125
|
|
|
|
126
|
|
|
$issuerClaim->expects($this->once()) |
127
|
|
|
->method('getValue') |
128
|
|
|
->will($this->returnValue('an_issuer')); |
129
|
|
|
|
130
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
131
|
|
|
->method('getPayload') |
132
|
|
|
->will($this->returnValue($this->payload)); |
133
|
|
|
|
134
|
|
|
$this->payload->expects($this->once()) |
|
|
|
|
135
|
|
|
->method('findClaimByName') |
136
|
|
|
->with(Claim\Issuer::NAME) |
137
|
|
|
->will($this->returnValue($issuerClaim)); |
138
|
|
|
|
139
|
|
|
$verifier = new IssuerVerifier('an_issuer'); |
140
|
|
|
$verifier->verify($this->token); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.