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