xref: /webtrees/tests/app/Services/RateLimitServiceTest.php (revision b50dba3a86d260384d07c30bedf82d8e78ab49f7)
19ed332c7SGreg Roach<?php
29ed332c7SGreg Roach
39ed332c7SGreg Roach/**
49ed332c7SGreg Roach * webtrees: online genealogy
5d11be702SGreg 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;
26*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
27*202c018bSGreg Roach
289ed332c7SGreg Roachuse function explode;
299ed332c7SGreg Roachuse function implode;
309ed332c7SGreg Roachuse function range;
319ed332c7SGreg Roachuse function time;
329ed332c7SGreg Roach
33*202c018bSGreg Roach#[CoversClass(RateLimitService::class)]
349ed332c7SGreg Roachclass RateLimitServiceTest extends TestCase
359ed332c7SGreg Roach{
369ed332c7SGreg Roach    public function testTooMuchHistory(): void
379ed332c7SGreg Roach    {
389ed332c7SGreg Roach        $rate_limit_service = new RateLimitService();
399ed332c7SGreg Roach
409ed332c7SGreg Roach        $user = new GuestUser();
419ed332c7SGreg Roach
429ed332c7SGreg Roach        $this->expectException(LogicException::class);
439ed332c7SGreg Roach
449ed332c7SGreg Roach        $rate_limit_service->limitRateForUser($user, 1000, 30, 'rate-limit');
459ed332c7SGreg Roach    }
469ed332c7SGreg Roach
479ed332c7SGreg Roach    public function testLimitNotReached(): void
489ed332c7SGreg Roach    {
499ed332c7SGreg Roach        $rate_limit_service = new RateLimitService();
509ed332c7SGreg Roach
519ed332c7SGreg Roach        $user = new GuestUser();
529ed332c7SGreg Roach
539ed332c7SGreg Roach        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
549ed332c7SGreg Roach        $history = $user->getPreference('rate-limit');
55f01ab4acSGreg Roach        static::assertCount(1, explode(',', $history));
569ed332c7SGreg Roach
579ed332c7SGreg Roach        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
589ed332c7SGreg Roach        $history = $user->getPreference('rate-limit');
59f01ab4acSGreg Roach        static::assertCount(2, explode(',', $history));
609ed332c7SGreg Roach
619ed332c7SGreg Roach        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
629ed332c7SGreg Roach        $history = $user->getPreference('rate-limit');
63f01ab4acSGreg Roach        static::assertCount(3, explode(',', $history));
649ed332c7SGreg Roach    }
659ed332c7SGreg Roach
669ed332c7SGreg Roach    public function testOldEventsIgnored(): void
679ed332c7SGreg Roach    {
689ed332c7SGreg Roach        $rate_limit_service = new RateLimitService();
699ed332c7SGreg Roach
709ed332c7SGreg Roach        $user = new GuestUser();
719ed332c7SGreg Roach
729ed332c7SGreg Roach        $history = implode(',', range(time() - 35, time() - 31));
739ed332c7SGreg Roach        $user->setPreference('rate-limit', $history);
749ed332c7SGreg Roach
759ed332c7SGreg Roach        $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit');
769ed332c7SGreg Roach        $history = $user->getPreference('rate-limit');
77f01ab4acSGreg Roach        static::assertCount(6, explode(',', $history));
789ed332c7SGreg Roach    }
799ed332c7SGreg Roach
809ed332c7SGreg Roach    public function testLimitReached(): void
819ed332c7SGreg Roach    {
829ed332c7SGreg Roach        $rate_limit_service = new RateLimitService();
839ed332c7SGreg Roach
849ed332c7SGreg Roach        $user = new GuestUser();
859ed332c7SGreg Roach
869ed332c7SGreg Roach        $history = implode(',', range(time() - 5, time() - 1));
879ed332c7SGreg Roach        $user->setPreference('rate-limit', $history);
889ed332c7SGreg Roach
899ed332c7SGreg Roach        $this->expectException(HttpTooManyRequestsException::class);
909ed332c7SGreg Roach        $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit');
919ed332c7SGreg Roach    }
929ed332c7SGreg Roach}
93