xref: /webtrees/tests/app/Services/RateLimitServiceTest.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
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;
26
27use function explode;
28use function implode;
29use function range;
30use function time;
31
32/**
33 * Test harness for the class RateLimitService
34 */
35class RateLimitServiceTest extends TestCase
36{
37    /**
38     * @covers \Fisharebest\Webtrees\Services\RateLimitService
39     */
40    public function testTooMuchHistory(): void
41    {
42        $rate_limit_service = new RateLimitService();
43
44        $user = new GuestUser();
45
46        $this->expectException(LogicException::class);
47
48        $rate_limit_service->limitRateForUser($user, 1000, 30, 'rate-limit');
49    }
50
51    /**
52     * @covers \Fisharebest\Webtrees\Services\RateLimitService
53     */
54    public function testLimitNotReached(): void
55    {
56        $rate_limit_service = new RateLimitService();
57
58        $user = new GuestUser();
59
60        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
61        $history = $user->getPreference('rate-limit');
62        static::assertCount(1, explode(',', $history));
63
64        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
65        $history = $user->getPreference('rate-limit');
66        static::assertCount(2, explode(',', $history));
67
68        $rate_limit_service->limitRateForUser($user, 3, 30, 'rate-limit');
69        $history = $user->getPreference('rate-limit');
70        static::assertCount(3, explode(',', $history));
71    }
72
73    /**
74     * @covers \Fisharebest\Webtrees\Services\RateLimitService
75     */
76    public function testOldEventsIgnored(): void
77    {
78        $rate_limit_service = new RateLimitService();
79
80        $user = new GuestUser();
81
82        $history = implode(',', range(time() - 35, time() - 31));
83        $user->setPreference('rate-limit', $history);
84
85        $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit');
86        $history = $user->getPreference('rate-limit');
87        static::assertCount(6, explode(',', $history));
88    }
89
90    /**
91     * @covers \Fisharebest\Webtrees\Services\RateLimitService
92     */
93    public function testLimitReached(): void
94    {
95        $rate_limit_service = new RateLimitService();
96
97        $user = new GuestUser();
98
99        $history = implode(',', range(time() - 5, time() - 1));
100        $user->setPreference('rate-limit', $history);
101
102        $this->expectException(HttpTooManyRequestsException::class);
103        $rate_limit_service->limitRateForUser($user, 5, 30, 'rate-limit');
104    }
105}
106