19ed332c7SGreg Roach<?php 29ed332c7SGreg Roach 39ed332c7SGreg Roach/** 49ed332c7SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 69ed332c7SGreg Roach * This program is free software: you can redistribute it and/or modify 79ed332c7SGreg Roach * it under the terms of the GNU General Public License as published by 89ed332c7SGreg Roach * the Free Software Foundation, either version 3 of the License, or 99ed332c7SGreg Roach * (at your option) any later version. 109ed332c7SGreg Roach * This program is distributed in the hope that it will be useful, 119ed332c7SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 129ed332c7SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 139ed332c7SGreg Roach * GNU General Public License for more details. 149ed332c7SGreg Roach * You should have received a copy of the GNU General Public License 159ed332c7SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 169ed332c7SGreg Roach */ 179ed332c7SGreg Roach 189ed332c7SGreg Roachdeclare(strict_types=1); 199ed332c7SGreg Roach 209ed332c7SGreg Roachnamespace Fisharebest\Webtrees\Services; 219ed332c7SGreg Roach 229ed332c7SGreg Roachuse Fisharebest\Webtrees\GuestUser; 239ed332c7SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpTooManyRequestsException; 249ed332c7SGreg Roachuse Fisharebest\Webtrees\TestCase; 259ed332c7SGreg Roachuse LogicException; 269ed332c7SGreg Roach 279ed332c7SGreg Roachuse function explode; 289ed332c7SGreg Roachuse function implode; 299ed332c7SGreg Roachuse function range; 309ed332c7SGreg Roachuse function time; 319ed332c7SGreg Roach 329ed332c7SGreg Roach/** 339ed332c7SGreg Roach * Test harness for the class RateLimitService 349ed332c7SGreg Roach */ 359ed332c7SGreg Roachclass RateLimitServiceTest extends TestCase 369ed332c7SGreg Roach{ 379ed332c7SGreg Roach /** 389ed332c7SGreg Roach * @covers \Fisharebest\Webtrees\Services\RateLimitService 399ed332c7SGreg Roach * 409ed332c7SGreg Roach * @return void 419ed332c7SGreg Roach */ 429ed332c7SGreg Roach public function testTooMuchHistory(): void 439ed332c7SGreg Roach { 449ed332c7SGreg Roach $rate_limit_service = new RateLimitService(); 459ed332c7SGreg Roach 469ed332c7SGreg Roach $user = new GuestUser(); 479ed332c7SGreg Roach 489ed332c7SGreg Roach $this->expectException(LogicException::class); 499ed332c7SGreg Roach 509ed332c7SGreg Roach $rate_limit_service->limitRateForUser($user, 1000, 30, 'rate-limit'); 519ed332c7SGreg Roach } 529ed332c7SGreg Roach 539ed332c7SGreg Roach /** 549ed332c7SGreg Roach * @covers \Fisharebest\Webtrees\Services\RateLimitService 559ed332c7SGreg Roach * 569ed332c7SGreg Roach * @return void 579ed332c7SGreg Roach */ 589ed332c7SGreg Roach public function testLimitNotReached(): void 599ed332c7SGreg Roach { 609ed332c7SGreg Roach $rate_limit_service = new RateLimitService(); 619ed332c7SGreg Roach 629ed332c7SGreg Roach $user = new GuestUser(); 639ed332c7SGreg Roach 649ed332c7SGreg Roach $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit'); 659ed332c7SGreg Roach $history = $user->getPreference('rate-limit'); 66f01ab4acSGreg Roach static::assertCount(1, explode(',', $history)); 679ed332c7SGreg Roach 689ed332c7SGreg Roach $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit'); 699ed332c7SGreg Roach $history = $user->getPreference('rate-limit'); 70f01ab4acSGreg Roach static::assertCount(2, explode(',', $history)); 719ed332c7SGreg Roach 729ed332c7SGreg Roach $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit'); 739ed332c7SGreg Roach $history = $user->getPreference('rate-limit'); 74f01ab4acSGreg Roach static::assertCount(3, explode(',', $history)); 759ed332c7SGreg Roach } 769ed332c7SGreg Roach 779ed332c7SGreg Roach /** 789ed332c7SGreg Roach * @covers \Fisharebest\Webtrees\Services\RateLimitService 799ed332c7SGreg Roach * 809ed332c7SGreg Roach * @return void 819ed332c7SGreg Roach */ 829ed332c7SGreg Roach public function testOldEventsIgnored(): void 839ed332c7SGreg Roach { 849ed332c7SGreg Roach $rate_limit_service = new RateLimitService(); 859ed332c7SGreg Roach 869ed332c7SGreg Roach $user = new GuestUser(); 879ed332c7SGreg Roach 889ed332c7SGreg Roach $history = implode(',', range(time() - 35, time() - 31)); 899ed332c7SGreg Roach $user->setPreference('rate-limit', $history); 909ed332c7SGreg Roach 919ed332c7SGreg Roach $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit'); 929ed332c7SGreg Roach $history = $user->getPreference('rate-limit'); 93f01ab4acSGreg Roach static::assertCount(6, explode(',', $history)); 949ed332c7SGreg Roach } 959ed332c7SGreg Roach 969ed332c7SGreg Roach /** 979ed332c7SGreg Roach * @covers \Fisharebest\Webtrees\Services\RateLimitService 989ed332c7SGreg Roach * 999ed332c7SGreg Roach * @return void 1009ed332c7SGreg Roach */ 1019ed332c7SGreg Roach public function testLimitReached(): void 1029ed332c7SGreg Roach { 1039ed332c7SGreg Roach $rate_limit_service = new RateLimitService(); 1049ed332c7SGreg Roach 1059ed332c7SGreg Roach $user = new GuestUser(); 1069ed332c7SGreg Roach 1079ed332c7SGreg Roach $history = implode(',', range(time() - 5, time() - 1)); 1089ed332c7SGreg Roach $user->setPreference('rate-limit', $history); 1099ed332c7SGreg Roach 1109ed332c7SGreg Roach $this->expectException(HttpTooManyRequestsException::class); 1119ed332c7SGreg Roach $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit'); 1129ed332c7SGreg Roach } 1139ed332c7SGreg Roach} 114