1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the haveibeenpwned-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\HaveIBeenPwnedBundle\Tests\EventListener; |
13
|
|
|
|
14
|
|
|
use Throwable; |
15
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachedAccountEvent; |
16
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachesEvent; |
17
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachEvent; |
18
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\DataClassesEvent; |
19
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\PasteAccountEvent; |
20
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\RangeEvent; |
21
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\EventListener\HaveIBeenPwnedEventListener; |
22
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Tests\AbstractTestCase; |
23
|
|
|
use WBW\Library\HaveIBeenPwned\Api\RequestInterface; |
24
|
|
|
use WBW\Library\HaveIBeenPwned\Request\BreachedAccountRequest; |
25
|
|
|
use WBW\Library\HaveIBeenPwned\Request\BreachesRequest; |
26
|
|
|
use WBW\Library\HaveIBeenPwned\Request\BreachRequest; |
27
|
|
|
use WBW\Library\HaveIBeenPwned\Request\DataClassesRequest; |
28
|
|
|
use WBW\Library\HaveIBeenPwned\Request\PasteAccountRequest; |
29
|
|
|
use WBW\Library\HaveIBeenPwned\Request\RangeRequest; |
30
|
|
|
use WBW\Library\HaveIBeenPwned\Response\BreachesResponse; |
31
|
|
|
use WBW\Library\HaveIBeenPwned\Response\DataClassesResponse; |
32
|
|
|
use WBW\Library\HaveIBeenPwned\Response\PastesResponse; |
33
|
|
|
use WBW\Library\HaveIBeenPwned\Response\RangesResponse; |
34
|
|
|
use WBW\Library\Provider\Exception\ApiException; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* HaveIBeenPwned event listener test. |
38
|
|
|
* |
39
|
|
|
* @author webeweb <https://github.com/webeweb> |
40
|
|
|
* @package WBW\Bundle\HaveIBeenPwnedBundle\Tests\EventListener |
41
|
|
|
*/ |
42
|
|
|
class HaveIBeenPwnedEventListenerTest extends AbstractTestCase { |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function setUp(): void { |
48
|
|
|
parent::setUp(); |
49
|
|
|
|
50
|
|
|
$this->wait(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tests onBreach() |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
58
|
|
|
*/ |
59
|
|
|
public function testOnBreach(): void { |
60
|
|
|
|
61
|
|
|
// Set a Breach event mock. |
62
|
|
|
$breachEvent = new BreachEvent($this->breach); |
63
|
|
|
|
64
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
65
|
|
|
|
66
|
|
|
$res = $obj->onBreach($breachEvent); |
67
|
|
|
$this->assertSame($breachEvent, $res); |
68
|
|
|
|
69
|
|
|
$this->assertInstanceOf(BreachRequest::class, $res->getRequest()); |
70
|
|
|
$this->assertInstanceOf(BreachesResponse::class, $res->getResponse()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Tests onBreachedAccount() |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
78
|
|
|
*/ |
79
|
|
|
public function testOnBreachedAccount(): void { |
80
|
|
|
|
81
|
|
|
// Set a Breached account event mock. |
82
|
|
|
$breachEvent = new BreachedAccountEvent($this->breachedAccount); |
83
|
|
|
|
84
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
|
88
|
|
|
// This unit test failed on Travis-CI. |
89
|
|
|
$res = $obj->onBreachedAccount($breachEvent); |
90
|
|
|
$this->assertSame($breachEvent, $res); |
91
|
|
|
|
92
|
|
|
$this->assertInstanceOf(BreachedAccountRequest::class, $res->getRequest()); |
93
|
|
|
$this->assertInstanceOf(BreachesResponse::class, $res->getResponse()); |
94
|
|
|
} catch (Throwable $ex) { |
95
|
|
|
|
96
|
|
|
$this->assertInstanceOf(ApiException::class, $ex); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Tests onBreaches() |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
105
|
|
|
*/ |
106
|
|
|
public function testOnBreaches(): void { |
107
|
|
|
|
108
|
|
|
// Set a Breaches event mock. |
109
|
|
|
$breachesEvent = new BreachesEvent($this->breaches); |
110
|
|
|
|
111
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
112
|
|
|
|
113
|
|
|
$res = $obj->onBreaches($breachesEvent); |
114
|
|
|
$this->assertSame($breachesEvent, $res); |
115
|
|
|
|
116
|
|
|
$this->assertInstanceOf(BreachesRequest::class, $res->getRequest()); |
117
|
|
|
$this->assertInstanceOf(BreachesResponse::class, $res->getResponse()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Tests onDataClasses() |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
125
|
|
|
*/ |
126
|
|
|
public function testOnDataClasses(): void { |
127
|
|
|
|
128
|
|
|
// Set a Data classes event mock. |
129
|
|
|
$dataClassesEvent = new DataClassesEvent(); |
130
|
|
|
|
131
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
132
|
|
|
|
133
|
|
|
$res = $obj->onDataClasses($dataClassesEvent); |
134
|
|
|
$this->assertSame($dataClassesEvent, $res); |
135
|
|
|
|
136
|
|
|
$this->assertInstanceOf(DataClassesRequest::class, $res->getRequest()); |
137
|
|
|
$this->assertInstanceOf(DataClassesResponse::class, $res->getResponse()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Tests onPasteAccount() |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
145
|
|
|
*/ |
146
|
|
|
public function testOnPasteAccount(): void { |
147
|
|
|
|
148
|
|
|
// Set a Paste account event mock. |
149
|
|
|
$breachEvent = new PasteAccountEvent($this->pasteAccount); |
150
|
|
|
|
151
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
152
|
|
|
|
153
|
|
|
try { |
154
|
|
|
|
155
|
|
|
$res = $obj->onPasteAccount($breachEvent); |
156
|
|
|
$this->assertSame($breachEvent, $res); |
157
|
|
|
$this->assertInstanceOf(PasteAccountRequest::class, $res->getRequest()); |
158
|
|
|
$this->assertInstanceOf(PastesResponse::class, $res->getResponse()); |
159
|
|
|
} catch (Throwable $ex) { |
160
|
|
|
|
161
|
|
|
$this->assertInstanceOf(ApiException::class, $ex); |
162
|
|
|
$this->assertEquals(401, $ex->getPrevious()->getCode()); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Tests onRange() |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
* @throws Throwable Throws an exception if an error occurs. |
171
|
|
|
*/ |
172
|
|
|
public function testOnRange(): void { |
173
|
|
|
|
174
|
|
|
// Set a Range event mock. |
175
|
|
|
$rangeEvent = new RangeEvent($this->range); |
176
|
|
|
|
177
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
178
|
|
|
|
179
|
|
|
$res = $obj->onRange($rangeEvent); |
180
|
|
|
$this->assertSame($rangeEvent, $res); |
181
|
|
|
|
182
|
|
|
$this->assertInstanceOf(RangeRequest::class, $res->getRequest()); |
183
|
|
|
$this->assertInstanceOf(RangesResponse::class, $res->getResponse()); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Tests __construct() |
188
|
|
|
* |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
|
|
public function test__construct(): void { |
192
|
|
|
|
193
|
|
|
$this->assertEquals("wbw.haveibeenpwned.event_listener", HaveIBeenPwnedEventListener::SERVICE_NAME); |
194
|
|
|
|
195
|
|
|
$obj = new HaveIBeenPwnedEventListener($this->logger); |
196
|
|
|
|
197
|
|
|
$this->assertNotNull($obj->getApiProvider()); |
198
|
|
|
$this->assertSame($this->logger, $obj->getLogger()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Wait. |
203
|
|
|
* |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
protected function wait() { |
207
|
|
|
sleep(intval(RequestInterface::RATE_LIMITING * 4 / 1000)); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|