‪TYPO3CMS  11.5
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 
23 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24 
25 class ‪RateLimiterFactoryTest extends FunctionalTestCase
26 {
30  protected ‪$initializeDatabase = false;
31 
32  public 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 
66  public function ‪loginRateLimiterReturnsExpectedResults(string $loginType, int $loginRateLimit, int $tokens, bool $expected): void
67  {
68  ‪$GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimit'] = $loginRateLimit;
69  $userAuth = new class ($loginType) extends ‪AbstractUserAuthentication {
70  public function __construct($loginType)
71  {
72  $this->loginType = $loginType;
73  }
74  };
75 
76  $request = (new ServerRequest('https://example.com', 'POST'));
77  $subject = new RateLimiterFactory();
78  $rateLimiter = $subject->createLoginRateLimiter($userAuth, $request);
79  self::assertEquals($expected, $rateLimiter->consume($tokens)->isAccepted());
80  }
81 
85  public function ‪loginRateLimiterRespectsIpExcludeList(): void
86  {
87  $loginType = 'BE';
88  ‪$GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimit'] = 5;
89  ‪$GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimitIpExcludeList'] = '127.0.0.1';
90 
91  $request = (new ServerRequest('https://example.com', 'POST', 'php://input', [], ['REMOTE_ADDR' => '127.0.0.1']));
92  $userAuth = new class ($loginType) extends AbstractUserAuthentication {
93  public function __construct($loginType)
94  {
95  $this->loginType = $loginType;
96  }
97  };
98  $subject = new RateLimiterFactory();
99  $rateLimiter = $subject->createLoginRateLimiter($userAuth, $request);
100  self::assertTrue($rateLimiter->consume(6)->isAccepted());
101  }
102 }
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\loginRateLimiterReturnsExpectedResults
‪loginRateLimiterReturnsExpectedResults(string $loginType, int $loginRateLimit, int $tokens, bool $expected)
Definition: RateLimiterFactoryTest.php:65
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter
Definition: RateLimiterFactoryTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\loginRateLimiterLimitsRequestsDataProvider
‪loginRateLimiterLimitsRequestsDataProvider()
Definition: RateLimiterFactoryTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\$initializeDatabase
‪bool $initializeDatabase
Definition: RateLimiterFactoryTest.php:29
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest
Definition: RateLimiterFactoryTest.php:26
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Core\Tests\Functional\RateLimiter\RateLimiterFactoryTest\loginRateLimiterRespectsIpExcludeList
‪loginRateLimiterRespectsIpExcludeList()
Definition: RateLimiterFactoryTest.php:84
‪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:56