xref: /webtrees/tests/app/Services/RateLimitServiceTest.php (revision b50dba3a86d260384d07c30bedf82d8e78ab49f7)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\GuestUser;
23use Fisharebest\Webtrees\Http\Exceptions\HttpTooManyRequestsException;
24use Fisharebest\Webtrees\TestCase;
25use LogicException;
26use PHPUnit\Framework\Attributes\CoversClass;
27
28use function explode;
29use function implode;
30use function range;
31use function time;
32
33#[CoversClass(RateLimitService::class)]
34class RateLimitServiceTest extends TestCase
35{
36    public function testTooMuchHistory(): void
37    {
38        $rate_limit_service = new RateLimitService();
39
40        $user = new GuestUser();
41
42        $this->expectException(LogicException::class);
43
44        $rate_limit_service->limitRateForUser($user, 1000, 30, 'rate-limit');
45    }
46
47    public function testLimitNotReached(): void
48    {
49        $rate_limit_service = new RateLimitService();
50
51        $user = new GuestUser();
52
53        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
54        $history = $user->getPreference('rate-limit');
55        static::assertCount(1, explode(',', $history));
56
57        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
58        $history = $user->getPreference('rate-limit');
59        static::assertCount(2, explode(',', $history));
60
61        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
62        $history = $user->getPreference('rate-limit');
63        static::assertCount(3, explode(',', $history));
64    }
65
66    public function testOldEventsIgnored(): void
67    {
68        $rate_limit_service = new RateLimitService();
69
70        $user = new GuestUser();
71
72        $history = implode(',', range(time() - 35, time() - 31));
73        $user->setPreference('rate-limit', $history);
74
75        $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit');
76        $history = $user->getPreference('rate-limit');
77        static::assertCount(6, explode(',', $history));
78    }
79
80    public function testLimitReached(): void
81    {
82        $rate_limit_service = new RateLimitService();
83
84        $user = new GuestUser();
85
86        $history = implode(',', range(time() - 5, time() - 1));
87        $user->setPreference('rate-limit', $history);
88
89        $this->expectException(HttpTooManyRequestsException::class);
90        $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit');
91    }
92}
93