1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\MailgunWebhooksBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Azine\MailgunWebhooksBundle\Command\CheckIpAddressIsBlacklistedCommand; |
6
|
|
|
use Azine\MailgunWebhooksBundle\Entity\HetrixToolsBlacklistResponseNotification; |
7
|
|
|
use Azine\MailgunWebhooksBundle\Entity\Repositories\HetrixToolsBlacklistResponseNotificationRepository; |
8
|
|
|
use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\HetrixtoolsServiceResponse; |
9
|
|
|
use Doctrine\ORM\EntityRepository; |
10
|
|
|
use Symfony\Component\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
|
13
|
|
|
class CheckIpAddressIsBlacklistedCommandTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
private $registry; |
16
|
|
|
|
17
|
|
|
private $entityManager; |
18
|
|
|
|
19
|
|
|
private $entityRepository; |
20
|
|
|
|
21
|
|
|
private $hetrixtoolsService; |
22
|
|
|
|
23
|
|
|
private $azineMailgunService; |
24
|
|
|
|
25
|
|
|
private $hetrixtoolsRespose; |
26
|
|
|
|
27
|
|
|
private $hetrixtoolsResposeData = array( |
28
|
|
|
'status' => HetrixtoolsServiceResponse::RESPONSE_STATUS_SUCCESS, |
29
|
|
|
'api_calls_left' => 5, |
30
|
|
|
'blacklist_check_credits_left' => 5, |
31
|
|
|
'blacklisted_count' => 5, |
32
|
|
|
'blacklisted_on' => array( |
33
|
|
|
array( |
34
|
|
|
'rbl' => 'dnsbl.cobion.com', |
35
|
|
|
'delist' => 'https://example.test.com/ip/198.51.100.42', |
36
|
|
|
), |
37
|
|
|
array( |
38
|
|
|
'rbl' => 'pbl.spamhaus.org', |
39
|
|
|
'delist' => 'https://www.example.org/query/ip/198.51.100.42', |
40
|
|
|
), |
41
|
|
|
), |
42
|
|
|
'links' => array( |
43
|
|
|
'report_link' => 'https://example.com/report/blacklist/token/', |
44
|
|
|
'whitelabel_report_link' => '', |
45
|
|
|
'api_report_link' => 'https://api.example.com/v1/token/blacklist/report/198.51.100.42/', |
46
|
|
|
'api_blacklist_check_link' => 'https://api.example.com/v2/token/blacklist-check/ipv4/198.51.100.42/', |
47
|
|
|
), |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
public function setUp() |
51
|
|
|
{ |
52
|
|
|
$this->hetrixtoolsRespose = new HetrixtoolsServiceResponse(); |
53
|
|
|
$this->hetrixtoolsRespose->status = HetrixtoolsServiceResponse::RESPONSE_STATUS_SUCCESS; |
54
|
|
|
$this->hetrixtoolsRespose->api_calls_left = 5; |
55
|
|
|
$this->hetrixtoolsRespose->blacklist_check_credits_left = 5; |
56
|
|
|
$this->hetrixtoolsRespose->blacklisted_count = 5; |
57
|
|
|
$this->hetrixtoolsRespose->blacklisted_on = array( |
58
|
|
|
array( |
59
|
|
|
'rbl' => 'dnsbl.cobion.com', |
60
|
|
|
'delist' => 'https://example.test.com/ip/198.51.100.42', |
61
|
|
|
), |
62
|
|
|
array( |
63
|
|
|
'rbl' => 'pbl.spamhaus.org', |
64
|
|
|
'delist' => 'https://www.example.org/query/ip/198.51.100.42', |
65
|
|
|
), |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->hetrixtoolsRespose->links = array( |
69
|
|
|
'report_link' => 'https://example.com/report/blacklist/token/', |
70
|
|
|
'whitelabel_report_link' => '', |
71
|
|
|
'api_report_link' => 'https://api.example.com/v1/token/blacklist/report/198.51.100.42/', |
72
|
|
|
'api_blacklist_check_link' => 'https://api.example.com/v2/token/blacklist-check/ipv4/198.51.100.42/', |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$this->entityRepository = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->setMethods(array('getLastKnownSenderIpData', 'findBy'))->getMock(); |
76
|
|
|
$this->entityRepository->expects($this->any())->method('getLastKnownSenderIpData')->will($this->returnValue(array('ip' => '198.51.100.42', 'timestamp' => '1552971782'))); |
77
|
|
|
|
78
|
|
|
$this->entityManager = $this->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->setMethods(array('getRepository'))->getMock(); |
79
|
|
|
$this->entityManager->expects($this->any())->method('getRepository')->will($this->returnValue($this->entityRepository)); |
80
|
|
|
|
81
|
|
|
$this->registry = $this->getMockBuilder("Doctrine\Common\Persistence\ManagerRegistry")->disableOriginalConstructor()->getMock(); |
82
|
|
|
$this->registry->expects($this->any())->method('getManager')->will($this->returnValue($this->entityManager)); |
83
|
|
|
|
84
|
|
|
$this->hetrixtoolsService = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\AzineMailgunHetrixtoolsService")->disableOriginalConstructor()->setMethods(array('checkIpAddressInBlacklist'))->getMock(); |
85
|
|
|
|
86
|
|
|
$this->azineMailgunService = $this->getMockBuilder("Azine\MailgunWebhooksBundle\Services\AzineMailgunMailerService")->disableOriginalConstructor()->setMethods(array('sendBlacklistNotification'))->getMock(); |
87
|
|
|
$this->azineMailgunService->expects($this->any())->method('sendBlacklistNotification')->will($this->returnvalue(1)); |
88
|
|
|
|
89
|
|
|
$this->blackListNotificationRepository = $this->getMockBuilder(HetrixToolsBlacklistResponseNotificationRepository::class)->disableOriginalConstructor()->getMock(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testSendingBlackListReportFirstTimeSent() |
93
|
|
|
{ |
94
|
|
|
$tester = $this->getTester(28); |
95
|
|
|
|
96
|
|
|
//test if response status is 'SUCCESS' and ip is blacklisted |
97
|
|
|
$this->hetrixtoolsService->expects($this->any())->method('checkIpAddressInBlacklist')->will($this->returnValue($this->hetrixtoolsRespose)); |
98
|
|
|
|
99
|
|
|
$this->entityRepository->expects($this->any())->method('findBy')->will($this->returnValue(array())); |
100
|
|
|
|
101
|
|
|
$tester->execute(array('')); |
102
|
|
|
|
103
|
|
|
$display = $tester->getDisplay(); |
104
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_WAS_SENT, $display); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testSendingBlackListReportNotMutedSent() |
108
|
|
|
{ |
109
|
|
|
$tester = $this->getTester(0); |
110
|
|
|
|
111
|
|
|
//test if response status is 'SUCCESS' and ip is blacklisted |
112
|
|
|
$this->hetrixtoolsService->expects($this->any())->method('checkIpAddressInBlacklist')->will($this->returnValue($this->hetrixtoolsRespose)); |
113
|
|
|
|
114
|
|
|
$tester->execute(array('')); |
115
|
|
|
|
116
|
|
|
$display = $tester->getDisplay(); |
117
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_WAS_SENT, $display); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testSendingBlackListReportLastNotificationIsLongSinceAndListsAreTheSameSent() |
121
|
|
|
{ |
122
|
|
|
$tester = $this->getTester(10); |
123
|
|
|
|
124
|
|
|
//test if response status is 'SUCCESS' and ip is blacklisted |
125
|
|
|
$this->hetrixtoolsService->expects($this->any())->method('checkIpAddressInBlacklist')->will($this->returnValue($this->hetrixtoolsRespose)); |
126
|
|
|
|
127
|
|
|
$lastNotification = new HetrixToolsBlacklistResponseNotification(); |
128
|
|
|
$lastNotification->setIgnoreUntil(new \DateTime('1 hour ago')); |
129
|
|
|
$lastNotification->setData($this->hetrixtoolsResposeData); |
130
|
|
|
$this->entityRepository->expects($this->any())->method('findBy')->will($this->returnValue(array($lastNotification))); |
131
|
|
|
|
132
|
|
|
$tester->execute(array('')); |
133
|
|
|
|
134
|
|
|
$display = $tester->getDisplay(); |
135
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_WAS_SENT, $display); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testSendingBlackListReportLastNotificationIsRecentButListsAreNotTheSameSent() |
139
|
|
|
{ |
140
|
|
|
$tester = $this->getTester(10); |
141
|
|
|
|
142
|
|
|
//test if response status is 'SUCCESS' and ip is blacklisted |
143
|
|
|
$this->hetrixtoolsService->expects($this->any())->method('checkIpAddressInBlacklist')->will($this->returnValue($this->hetrixtoolsRespose)); |
144
|
|
|
|
145
|
|
|
$lastNotification = new HetrixToolsBlacklistResponseNotification(); |
146
|
|
|
$lastNotification->setIgnoreUntil(new \DateTime('1 hour')); |
147
|
|
|
$hetrixtoolsResposeData = array( |
148
|
|
|
'status' => HetrixtoolsServiceResponse::RESPONSE_STATUS_SUCCESS, |
149
|
|
|
'api_calls_left' => 5, |
150
|
|
|
'blacklist_check_credits_left' => 5, |
151
|
|
|
'blacklisted_count' => 5, |
152
|
|
|
'blacklisted_on' => array( |
153
|
|
|
array( |
154
|
|
|
'rbl' => 'pbl.spamhaus.org', |
155
|
|
|
'delist' => 'https://www.example.org/query/ip/198.51.100.42', |
156
|
|
|
), |
157
|
|
|
), |
158
|
|
|
'links' => array( |
159
|
|
|
'report_link' => 'https://example.com/report/blacklist/token/', |
160
|
|
|
'whitelabel_report_link' => '', |
161
|
|
|
'api_report_link' => 'https://api.example.com/v1/token/blacklist/report/198.51.100.42/', |
162
|
|
|
'api_blacklist_check_link' => 'https://api.example.com/v2/token/blacklist-check/ipv4/198.51.100.42/', |
163
|
|
|
), |
164
|
|
|
); |
165
|
|
|
$lastNotification->setData($hetrixtoolsResposeData); |
166
|
|
|
$this->entityRepository->expects($this->any())->method('findBy')->will($this->returnValue(array($lastNotification))); |
167
|
|
|
|
168
|
|
|
$tester->execute(array('')); |
169
|
|
|
|
170
|
|
|
$display = $tester->getDisplay(); |
171
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_WAS_SENT, $display); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testSendingBlackListReportLastNotificationIsRecentButListsNotChangedMuted() |
175
|
|
|
{ |
176
|
|
|
$tester = $this->getTester(10); |
177
|
|
|
|
178
|
|
|
//test if response status is 'SUCCESS' and ip is blacklisted |
179
|
|
|
$this->hetrixtoolsService->expects($this->any())->method('checkIpAddressInBlacklist')->will($this->returnValue($this->hetrixtoolsRespose)); |
180
|
|
|
|
181
|
|
|
$lastNotification = new HetrixToolsBlacklistResponseNotification(); |
182
|
|
|
$lastNotification->setIgnoreUntil(new \DateTime('1 hour')); |
183
|
|
|
$lastNotification->setData($this->hetrixtoolsResposeData); |
184
|
|
|
$this->entityRepository->expects($this->any())->method('findBy')->will($this->returnValue(array($lastNotification))); |
185
|
|
|
|
186
|
|
|
$tester->execute(array('')); |
187
|
|
|
|
188
|
|
|
$display = $tester->getDisplay(); |
189
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::BLACKLIST_REPORT_IS_SAME_AS_PREVIOUS, $display); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testSendingBlackListReportNotListedNotSent() |
193
|
|
|
{ |
194
|
|
|
$tester = $this->getTester(); |
195
|
|
|
|
196
|
|
|
//test if response status is 'SUCCESS' and ip is blacklisted |
197
|
|
|
$this->hetrixtoolsService->expects($this->any())->method('checkIpAddressInBlacklist')->will($this->returnValue($this->hetrixtoolsRespose)); |
198
|
|
|
|
199
|
|
|
//test if response status is 'SUCCESS' but ip is not blacklisted |
200
|
|
|
$this->hetrixtoolsRespose->blacklisted_count = 0; |
201
|
|
|
|
202
|
|
|
$tester->execute(array('')); |
203
|
|
|
|
204
|
|
|
$display = $tester->getDisplay(); |
205
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::IP_IS_NOT_BLACKLISTED, $display); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testSendingBlackListReportNoResponseShowError() |
209
|
|
|
{ |
210
|
|
|
$tester = $this->getTester(); |
211
|
|
|
$this->hetrixtoolsService->expects($this->once())->method('checkIpAddressInBlacklist')->will($this->throwException(new \InvalidArgumentException('no parseable response received.'))); |
212
|
|
|
|
213
|
|
|
$tester->execute(array('')); |
214
|
|
|
|
215
|
|
|
$display = $tester->getDisplay(); |
216
|
|
|
$this->assertContains(CheckIpAddressIsBlacklistedCommand::NO_VALID_RESPONSE_FROM_HETRIX, $display); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return CommandTester |
221
|
|
|
*/ |
222
|
|
|
private function getTester($muteDays = 0) |
223
|
|
|
{ |
224
|
|
|
$application = new Application(); |
225
|
|
|
$application->add(new CheckIpAddressIsBlacklistedCommand($this->registry, $this->hetrixtoolsService, $this->azineMailgunService, 'test', $muteDays)); |
226
|
|
|
$command = $this->getCheckIpAddressIsBlacklistedCommand($application); |
227
|
|
|
$tester = new CommandTester($command); |
228
|
|
|
|
229
|
|
|
return $tester; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param Application $application |
234
|
|
|
* |
235
|
|
|
* @return CheckIpAddressIsBlacklistedCommand |
236
|
|
|
*/ |
237
|
|
|
private function getCheckIpAddressIsBlacklistedCommand($application) |
238
|
|
|
{ |
239
|
|
|
return $application->find('mailgun:check-ip-in-blacklist'); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|