‪TYPO3CMS  ‪main
RateLimiterFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Symfony\Component\RateLimiter\RateLimit;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
28 final class ‪RateLimiterFactoryTest extends FunctionalTestCase
29 {
30  protected bool ‪$initializeDatabase = false;
31 
32  public static function ‪loginRateLimiterLimitsRequestsDataProvider(): array
33  {
34  return [
35  'backend accepted' => [
36  'BE',
37  5,
38  1,
39  true,
40  ],
41  'backend denied' => [
42  'BE',
43  5,
44  6,
45  false,
46  ],
47  'frontend accepted' => [
48  'FE',
49  5,
50  1,
51  true,
52  ],
53  'frontend denied' => [
54  'FE',
55  5,
56  6,
57  false,
58  ],
59  ];
60  }
61 
62  #[DataProvider('loginRateLimiterLimitsRequestsDataProvider')]
63  #[Test]
64  public function ‪loginRateLimiterReturnsExpectedResults(string $loginType, int $loginRateLimit, int $tokens, bool $expected): void
65  {
66  ‪$GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimit'] = $loginRateLimit;
67  $userAuth = new class ($loginType) extends ‪AbstractUserAuthentication {
68  public function __construct($loginType)
69  {
70  $this->loginType = $loginType;
71  }
72  };
73 
74  $request = (new ‪ServerRequest('https://example.com', 'POST'));
75  $subject = new ‪RateLimiterFactory();
76  $rateLimiter = $subject->createLoginRateLimiter($userAuth, $request);
77  $rateLimit = null;
78  for ($i = 0; $i < $tokens; $i++) {
79  $rateLimit = $rateLimiter->consume();
80  }
81  self::assertInstanceOf(RateLimit::class, $rateLimit);
82  self::assertEquals($expected, $rateLimit->isAccepted());
83  }
84 
85  #[Test]
87  {
88  $loginType = 'BE';
89  ‪$GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimit'] = 5;
90  ‪$GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimitIpExcludeList'] = '127.0.0.1';
91 
92  $request = (new ‪ServerRequest('https://example.com', 'POST', 'php://input', [], ['REMOTE_ADDR' => '127.0.0.1']));
93  $userAuth = new class ($loginType) extends ‪AbstractUserAuthentication {
94  public function __construct($loginType)
95  {
96  $this->loginType = $loginType;
97  }
98  };
99  $subject = new ‪RateLimiterFactory();
100  $rateLimiter = $subject->createLoginRateLimiter($userAuth, $request);
101  self::assertTrue($rateLimiter->consume(6)->isAccepted());
102  }
103 }
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\loginRateLimiterReturnsExpectedResults
‪loginRateLimiterReturnsExpectedResults(string $loginType, int $loginRateLimit, int $tokens, bool $expected)
Definition: RateLimiterFactoryTest.php:64
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter
Definition: RateLimiterFactoryTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\$initializeDatabase
‪bool $initializeDatabase
Definition: RateLimiterFactoryTest.php:30
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest
Definition: RateLimiterFactoryTest.php:29
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\loginRateLimiterLimitsRequestsDataProvider
‪static loginRateLimiterLimitsRequestsDataProvider()
Definition: RateLimiterFactoryTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\loginRateLimiterRespectsIpExcludeList
‪loginRateLimiterRespectsIpExcludeList()
Definition: RateLimiterFactoryTest.php:86
‪TYPO3\CMS\Core\RateLimiter\RateLimiterFactory
Definition: RateLimiterFactory.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:65