Ensured visits with empty remote address are not tried to be located

This commit is contained in:
Alejandro Celaya
2018-10-18 19:22:24 +02:00
parent 36ab475578
commit 5be7f839f3
3 changed files with 30 additions and 11 deletions
@@ -59,6 +59,14 @@ class ProcessVisitsCommand extends Command
$count = 0; $count = 0;
foreach ($visits as $visit) { foreach ($visits as $visit) {
if (! $visit->hasRemoteAddr()) {
$io->writeln(
sprintf('<comment>%s</comment>', $this->translator->translate('Ignored visit with no IP address')),
OutputInterface::VERBOSITY_VERBOSE
);
continue;
}
$ipAddr = $visit->getRemoteAddr(); $ipAddr = $visit->getRemoteAddr();
$io->write(sprintf('%s <info>%s</info>', $this->translator->translate('Processing IP'), $ipAddr)); $io->write(sprintf('%s <info>%s</info>', $this->translator->translate('Processing IP'), $ipAddr));
if ($ipAddr === IpAddress::LOCALHOST) { if ($ipAddr === IpAddress::LOCALHOST) {
@@ -11,8 +11,11 @@ use Shlinkio\Shlink\Common\Service\IpApiLocationResolver;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Service\VisitService; use Shlinkio\Shlink\Core\Service\VisitService;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
use function count;
use function round;
class ProcessVisitsCommandTest extends TestCase class ProcessVisitsCommandTest extends TestCase
{ {
@@ -67,15 +70,15 @@ class ProcessVisitsCommandTest extends TestCase
'command' => 'visit:process', 'command' => 'visit:process',
]); ]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
$this->assertEquals(0, \strpos($output, 'Processing IP 1.2.3.0')); $this->assertContains('Processing IP 1.2.3.0', $output);
$this->assertGreaterThan(0, \strpos($output, 'Processing IP 4.3.2.0')); $this->assertContains('Processing IP 4.3.2.0', $output);
$this->assertGreaterThan(0, \strpos($output, 'Processing IP 12.34.56.0')); $this->assertContains('Processing IP 12.34.56.0', $output);
} }
/** /**
* @test * @test
*/ */
public function localhostAddressIsIgnored() public function localhostAndEmptyAddressIsIgnored()
{ {
$visits = [ $visits = [
(new Visit())->setRemoteAddr('1.2.3.4'), (new Visit())->setRemoteAddr('1.2.3.4'),
@@ -83,19 +86,22 @@ class ProcessVisitsCommandTest extends TestCase
(new Visit())->setRemoteAddr('12.34.56.78'), (new Visit())->setRemoteAddr('12.34.56.78'),
(new Visit())->setRemoteAddr('127.0.0.1'), (new Visit())->setRemoteAddr('127.0.0.1'),
(new Visit())->setRemoteAddr('127.0.0.1'), (new Visit())->setRemoteAddr('127.0.0.1'),
(new Visit())->setRemoteAddr(''),
(new Visit())->setRemoteAddr(null),
]; ];
$this->visitService->getUnlocatedVisits()->willReturn($visits) $this->visitService->getUnlocatedVisits()->willReturn($visits)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(\count($visits) - 2); $this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 4);
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]) $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
->shouldBeCalledTimes(\count($visits) - 2); ->shouldBeCalledTimes(count($visits) - 4);
$this->commandTester->execute([ $this->commandTester->execute([
'command' => 'visit:process', 'command' => 'visit:process',
]); ], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
$this->assertGreaterThan(0, \strpos($output, 'Ignored localhost address')); $this->assertContains('Ignored localhost address', $output);
$this->assertContains('Ignored visit with no IP address', $output);
} }
/** /**
@@ -130,8 +136,8 @@ class ProcessVisitsCommandTest extends TestCase
'command' => 'visit:process', 'command' => 'visit:process',
]); ]);
$getApiLimit->shouldHaveBeenCalledTimes(\count($visits)); $getApiLimit->shouldHaveBeenCalledTimes(count($visits));
$getApiInterval->shouldHaveBeenCalledTimes(\round(\count($visits) / $apiLimit)); $getApiInterval->shouldHaveBeenCalledTimes(round(count($visits) / $apiLimit));
$resolveIpLocation->shouldHaveBeenCalledTimes(\count($visits)); $resolveIpLocation->shouldHaveBeenCalledTimes(count($visits));
} }
} }
+5
View File
@@ -102,6 +102,11 @@ class Visit extends AbstractEntity implements \JsonSerializable
return $this; return $this;
} }
public function hasRemoteAddr(): bool
{
return ! empty($this->remoteAddr);
}
private function obfuscateAddress(?string $address): ?string private function obfuscateAddress(?string $address): ?string
{ {
// Localhost addresses do not need to be obfuscated // Localhost addresses do not need to be obfuscated