Add API tests for visits domain filtering

This commit is contained in:
Alejandro Celaya
2025-11-01 09:56:15 +01:00
parent 37088b1a4b
commit e21cea1971
4 changed files with 46 additions and 22 deletions

View File

@@ -31,5 +31,7 @@ class NonOrphanVisitsTest extends ApiTestCase
yield 'bots excluded' => [['excludeBots' => 'true'], 6, 6];
yield 'bots excluded and pagination' => [['excludeBots' => 'true', 'page' => 1, 'itemsPerPage' => 4], 6, 4];
yield 'date filter' => [['startDate' => Chronos::now()->addDays(1)->toAtomString()], 0, 0];
yield 'domain filter' => [['domain' => 'example.com'], 0, 0];
yield 'default domain filter' => [['domain' => 'DEFAULT'], 7, 7];
}
}

View File

@@ -19,7 +19,7 @@ class OrphanVisitsTest extends ApiTestCase
'userAgent' => 'cf-facebook',
'visitLocation' => null,
'potentialBot' => true,
'visitedUrl' => 'foo.com',
'visitedUrl' => 'https://example.com/short',
'type' => 'invalid_short_url',
'redirectUrl' => null,
];
@@ -29,7 +29,7 @@ class OrphanVisitsTest extends ApiTestCase
'userAgent' => 'shlink-tests-agent',
'visitLocation' => null,
'potentialBot' => false,
'visitedUrl' => '',
'visitedUrl' => 'https://s.test/bar',
'type' => 'regular_404',
'redirectUrl' => null,
];
@@ -39,7 +39,7 @@ class OrphanVisitsTest extends ApiTestCase
'userAgent' => 'shlink-tests-agent',
'visitLocation' => null,
'potentialBot' => false,
'visitedUrl' => '',
'visitedUrl' => 'https://s.test/foo',
'type' => 'base_url',
'redirectUrl' => null,
];
@@ -80,6 +80,14 @@ class OrphanVisitsTest extends ApiTestCase
1,
[self::INVALID_SHORT_URL],
];
yield 'example domain only' => [['domain' => 'example.com'], 1, 1, [self::INVALID_SHORT_URL]];
yield 'default domain only' => [['domain' => 's.test'], 2, 2, [self::REGULAR_NOT_FOUND, self::BASE_URL]];
yield 'default domain only with DEFAULT keyword' => [
['domain' => 'DEFAULT'],
2,
2,
[self::REGULAR_NOT_FOUND, self::BASE_URL],
];
}
#[Test]

View File

@@ -17,11 +17,11 @@ class TagVisitsTest extends ApiTestCase
public function expectedVisitsAreReturned(
string $apiKey,
string $tag,
bool $excludeBots,
array $query,
int $expectedVisitsAmount,
): void {
$resp = $this->callApiWithKey(self::METHOD_GET, sprintf('/tags/%s/visits', $tag), [
RequestOptions::QUERY => $excludeBots ? ['excludeBots' => true] : [],
RequestOptions::QUERY => $query,
], $apiKey);
$payload = $this->getJsonResponsePayload($resp);
@@ -33,16 +33,18 @@ class TagVisitsTest extends ApiTestCase
public static function provideTags(): iterable
{
yield 'foo with admin API key' => ['valid_api_key', 'foo', false, 5];
yield 'foo with admin API key and no bots' => ['valid_api_key', 'foo', true, 4];
yield 'bar with admin API key' => ['valid_api_key', 'bar', false, 2];
yield 'bar with admin API key and no bots' => ['valid_api_key', 'bar', true, 1];
yield 'baz with admin API key' => ['valid_api_key', 'baz', false, 0];
yield 'foo with author API key' => ['author_api_key', 'foo', false, 5];
yield 'foo with author API key and no bots' => ['author_api_key', 'foo', true, 4];
yield 'bar with author API key' => ['author_api_key', 'bar', false, 2];
yield 'bar with author API key and no bots' => ['author_api_key', 'bar', true, 1];
yield 'foo with domain API key' => ['domain_api_key', 'foo', false, 0];
yield 'foo with admin API key' => ['valid_api_key', 'foo', [], 5];
yield 'foo with admin API key and no bots' => ['valid_api_key', 'foo', ['excludeBots' => true], 4];
yield 'bar with admin API key' => ['valid_api_key', 'bar', [], 2];
yield 'bar with admin API key and no bots' => ['valid_api_key', 'bar', ['excludeBots' => true], 1];
yield 'baz with admin API key' => ['valid_api_key', 'baz', [], 0];
yield 'foo with author API key' => ['author_api_key', 'foo', [], 5];
yield 'foo with author API key and no bots' => ['author_api_key', 'foo', ['excludeBots' => true], 4];
yield 'bar with author API key' => ['author_api_key', 'bar', [], 2];
yield 'bar with author API key and no bots' => ['author_api_key', 'bar', ['excludeBots' => true], 1];
yield 'foo with domain API key' => ['domain_api_key', 'foo', [], 0];
yield 'foo with specific domain' => ['valid_api_key', 'foo', ['domain' => 'example.com'], 0];
yield 'foo with default domain' => ['valid_api_key', 'foo', ['domain' => 'DEFAULT'], 5];
}
#[Test, DataProvider('provideApiKeysAndTags')]

View File

@@ -58,20 +58,32 @@ class VisitsFixture extends AbstractFixture implements DependentFixtureInterface
Visitor::fromParams('shlink-tests-agent', 'https://app.shlink.io', ''),
));
// Orphan visits (s.test is the default domain in tests env)
$manager->persist($this->setVisitDate(
fn () => Visit::forBasePath(Visitor::fromParams('shlink-tests-agent', 'https://s.test', '1.2.3.4')),
fn () => Visit::forBasePath(Visitor::fromParams(
'shlink-tests-agent',
'https://s.test',
'1.2.3.4',
visitedUrl: 'https://s.test/foo',
)),
'2020-01-01',
));
$manager->persist($this->setVisitDate(
fn () => Visit::forRegularNotFound(
Visitor::fromParams('shlink-tests-agent', 'https://s.test/foo/bar', '1.2.3.4'),
),
fn () => Visit::forRegularNotFound(Visitor::fromParams(
'shlink-tests-agent',
'https://s.test/foo/bar',
'1.2.3.4',
visitedUrl: 'https://s.test/bar',
)),
'2020-02-01',
));
$manager->persist($this->setVisitDate(
fn () => Visit::forInvalidShortUrl(
Visitor::fromParams('cf-facebook', 'https://s.test/foo', '1.2.3.4', 'foo.com'),
),
fn () => Visit::forInvalidShortUrl(Visitor::fromParams(
'cf-facebook',
'https://s.test/foo',
'1.2.3.4',
visitedUrl: 'https://example.com/short',
)),
'2020-03-01',
));