Restrict interaction with orphan visits when API key has that role

This commit is contained in:
Alejandro Celaya
2023-05-31 09:11:20 +02:00
parent 12da04ef37
commit eaba5edf7f
11 changed files with 78 additions and 20 deletions

View File

@@ -50,13 +50,14 @@ class VisitsStatsHelperTest extends TestCase
}
#[Test, DataProvider('provideCounts')]
public function returnsExpectedVisitsStats(int $expectedCount): void
public function returnsExpectedVisitsStats(int $expectedCount, ?ApiKey $apiKey): void
{
$repo = $this->createMock(VisitRepository::class);
$callCount = 0;
$repo->expects($this->exactly(2))->method('countNonOrphanVisits')->willReturnCallback(
function (VisitsCountFiltering $options) use ($expectedCount, &$callCount) {
function (VisitsCountFiltering $options) use ($expectedCount, $apiKey, &$callCount) {
Assert::assertEquals($callCount !== 0, $options->excludeBots);
Assert::assertEquals($apiKey, $options->apiKey);
$callCount++;
return $expectedCount * 3;
@@ -67,14 +68,17 @@ class VisitsStatsHelperTest extends TestCase
)->willReturn($expectedCount);
$this->em->expects($this->once())->method('getRepository')->with(Visit::class)->willReturn($repo);
$stats = $this->helper->getVisitsStats();
$stats = $this->helper->getVisitsStats($apiKey);
self::assertEquals(new VisitsStats($expectedCount * 3, $expectedCount), $stats);
}
public static function provideCounts(): iterable
{
return map(range(0, 50, 5), fn (int $value) => [$value]);
return [
...map(range(0, 50, 5), fn (int $value) => [$value, null]),
...map(range(0, 18, 3), fn (int $value) => [$value, ApiKey::create()]),
];
}
#[Test, DataProvider('provideAdminApiKeys')]