Added API tests covering the excludion of bot visits

This commit is contained in:
Alejandro Celaya 2021-05-22 21:05:54 +02:00
parent 69d72e754f
commit a12c9f54c4
5 changed files with 68 additions and 17 deletions

View file

@ -22,7 +22,10 @@ class OrphanVisitsPaginatorAdapter extends AbstractCacheableCountPaginatorAdapte
protected function doCount(): int
{
return $this->repo->countOrphanVisits(new VisitsCountFiltering($this->params->getDateRange()));
return $this->repo->countOrphanVisits(new VisitsCountFiltering(
$this->params->getDateRange(),
$this->params->excludeBots(),
));
}
public function getSlice($offset, $length): iterable // phpcs:ignore

View file

@ -41,21 +41,32 @@ class OrphanVisitsTest extends ApiTestCase
* @test
* @dataProvider provideQueries
*/
public function properVisitsAreReturnedBasedInQuery(array $query, int $expectedAmount, array $expectedVisits): void
{
public function properVisitsAreReturnedBasedInQuery(
array $query,
int $totalItems,
int $expectedAmount,
array $expectedVisits
): void {
$resp = $this->callApiWithKey(self::METHOD_GET, '/visits/orphan', [RequestOptions::QUERY => $query]);
$payload = $this->getJsonResponsePayload($resp);
$visits = $payload['visits']['data'] ?? [];
self::assertEquals(3, $payload['visits']['pagination']['totalItems'] ?? -1);
self::assertEquals($totalItems, $payload['visits']['pagination']['totalItems'] ?? -1);
self::assertCount($expectedAmount, $visits);
self::assertEquals($expectedVisits, $visits);
}
public function provideQueries(): iterable
{
yield 'all data' => [[], 3, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND, self::BASE_URL]];
yield 'limit items' => [['itemsPerPage' => 2], 2, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND]];
yield 'limit items and page' => [['itemsPerPage' => 2, 'page' => 2], 1, [self::BASE_URL]];
yield 'all data' => [[], 3, 3, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND, self::BASE_URL]];
yield 'limit items' => [['itemsPerPage' => 2], 3, 2, [self::INVALID_SHORT_URL, self::REGULAR_NOT_FOUND]];
yield 'limit items and page' => [['itemsPerPage' => 2, 'page' => 2], 3, 1, [self::BASE_URL]];
yield 'exclude bots' => [['excludeBots' => true], 2, 2, [self::REGULAR_NOT_FOUND, self::BASE_URL]];
yield 'exclude bots and limit items' => [
['excludeBots' => true, 'itemsPerPage' => 1],
2,
1,
[self::REGULAR_NOT_FOUND],
];
}
}

View file

@ -67,4 +67,30 @@ class ShortUrlVisitsTest extends ApiTestCase
yield 'domain' => ['example.com', 0];
yield 'no domain' => [null, 2];
}
/**
* @test
* @dataProvider provideVisitsForBots
*/
public function properVisitsAreReturnedWhenExcludingBots(bool $excludeBots, int $expectedAmountOfVisits): void
{
$shortCode = 'def456';
$url = new Uri(sprintf('/short-urls/%s/visits', $shortCode));
if ($excludeBots) {
$url = $url->withQuery(Query::build(['excludeBots' => true]));
}
$resp = $this->callApiWithKey(self::METHOD_GET, (string) $url);
$payload = $this->getJsonResponsePayload($resp);
self::assertEquals($expectedAmountOfVisits, $payload['visits']['pagination']['totalItems'] ?? -1);
self::assertCount($expectedAmountOfVisits, $payload['visits']['data'] ?? []);
}
public function provideVisitsForBots(): iterable
{
yield 'bots excluded' => [true, 1];
yield 'bots not excluded' => [false, 2];
}
}

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Action;
use GuzzleHttp\RequestOptions;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
use function sprintf;
@ -14,9 +15,15 @@ class TagVisitsTest extends ApiTestCase
* @test
* @dataProvider provideTags
*/
public function expectedVisitsAreReturned(string $apiKey, string $tag, int $expectedVisitsAmount): void
{
$resp = $this->callApiWithKey(self::METHOD_GET, sprintf('/tags/%s/visits', $tag), [], $apiKey);
public function expectedVisitsAreReturned(
string $apiKey,
string $tag,
bool $excludeBots,
int $expectedVisitsAmount
): void {
$resp = $this->callApiWithKey(self::METHOD_GET, sprintf('/tags/%s/visits', $tag), [
RequestOptions::QUERY => $excludeBots ? ['excludeBots' => true] : [],
], $apiKey);
$payload = $this->getJsonResponsePayload($resp);
self::assertEquals(self::STATUS_OK, $resp->getStatusCode());
@ -27,12 +34,16 @@ class TagVisitsTest extends ApiTestCase
public function provideTags(): iterable
{
yield 'foo with admin API key' => ['valid_api_key', 'foo', 5];
yield 'bar with admin API key' => ['valid_api_key', 'bar', 2];
yield 'baz with admin API key' => ['valid_api_key', 'baz', 0];
yield 'foo with author API key' => ['author_api_key', 'foo', 5];
yield 'bar with author API key' => ['author_api_key', 'bar', 2];
yield 'foo with domain API key' => ['domain_api_key', 'foo', 0];
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];
}
/**

View file

@ -36,7 +36,7 @@ class VisitsFixture extends AbstractFixture implements DependentFixtureInterface
/** @var ShortUrl $defShortUrl */
$defShortUrl = $this->getReference('def456_short_url');
$manager->persist(
Visit::forValidShortUrl($defShortUrl, new Visitor('shlink-tests-agent', '', '127.0.0.1', '')),
Visit::forValidShortUrl($defShortUrl, new Visitor('cf-facebook', '', '127.0.0.1', '')),
);
$manager->persist(
Visit::forValidShortUrl($defShortUrl, new Visitor('shlink-tests-agent', 'https://app.shlink.io', '', '')),