summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorLukas Reschke <lukas@statuscode.ch>2021-09-13 13:07:37 +0200
committerGitHub <noreply@github.com>2021-09-13 13:07:37 +0200
commit0dcc5c0e9f346912ed8c263950ad91f35a41bff8 (patch)
treec360c1c57b53a007ffec586428901ad1669b2dad /tests
parentd31456093d3bb2ff55fe0fde07050f07301459d6 (diff)
parent474a5b55d39e621c12947b50ea6c8febfceed7d7 (diff)
downloadnextcloud-server-0dcc5c0e9f346912ed8c263950ad91f35a41bff8.tar.gz
nextcloud-server-0dcc5c0e9f346912ed8c263950ad91f35a41bff8.zip
Merge pull request #28728 from nextcloud/add-database-backend-limiter
Add database ratelimiting backend
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php (renamed from tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php)26
-rw-r--r--tests/lib/Security/RateLimiting/LimiterTest.php29
2 files changed, 19 insertions, 36 deletions
diff --git a/tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php b/tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php
index ff58bd5c09e..e7cb4a93a29 100644
--- a/tests/lib/Security/RateLimiting/Backend/MemoryCacheTest.php
+++ b/tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php
@@ -24,20 +24,20 @@ declare(strict_types=1);
namespace Test\Security\RateLimiting\Backend;
-use OC\Security\RateLimiting\Backend\MemoryCache;
+use OC\Security\RateLimiting\Backend\MemoryCacheBackend;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ICache;
use OCP\ICacheFactory;
use Test\TestCase;
-class MemoryCacheTest extends TestCase {
+class MemoryCacheBackendTest extends TestCase {
/** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
private $cacheFactory;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $timeFactory;
/** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
private $cache;
- /** @var MemoryCache */
+ /** @var MemoryCacheBackend */
private $memoryCache;
protected function setUp(): void {
@@ -50,10 +50,10 @@ class MemoryCacheTest extends TestCase {
$this->cacheFactory
->expects($this->once())
->method('createDistributed')
- ->with('OC\Security\RateLimiting\Backend\MemoryCache')
+ ->with('OC\Security\RateLimiting\Backend\MemoryCacheBackend')
->willReturn($this->cache);
- $this->memoryCache = new MemoryCache(
+ $this->memoryCache = new MemoryCacheBackend(
$this->cacheFactory,
$this->timeFactory
);
@@ -66,7 +66,7 @@ class MemoryCacheTest extends TestCase {
->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
->willReturn(null);
- $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User', 123));
+ $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User'));
}
public function testGetAttempts() {
@@ -82,12 +82,12 @@ class MemoryCacheTest extends TestCase {
'1',
'2',
'87',
- '123',
- '123',
- '124',
+ '223',
+ '223',
+ '224',
]));
- $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User', 123));
+ $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User'));
}
public function testRegisterAttemptWithNoAttemptsBefore() {
@@ -106,7 +106,7 @@ class MemoryCacheTest extends TestCase {
->method('set')
->with(
'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
- json_encode(['123'])
+ json_encode(['223'])
);
$this->memoryCache->registerAttempt('Method', 'User', 100);
@@ -116,7 +116,7 @@ class MemoryCacheTest extends TestCase {
$this->timeFactory
->expects($this->once())
->method('getTime')
- ->willReturn(129);
+ ->willReturn(86);
$this->cache
->expects($this->once())
@@ -140,7 +140,7 @@ class MemoryCacheTest extends TestCase {
'123',
'123',
'124',
- '129',
+ '186',
])
);
diff --git a/tests/lib/Security/RateLimiting/LimiterTest.php b/tests/lib/Security/RateLimiting/LimiterTest.php
index 8b3509a4790..a5b187fb546 100644
--- a/tests/lib/Security/RateLimiting/LimiterTest.php
+++ b/tests/lib/Security/RateLimiting/LimiterTest.php
@@ -26,13 +26,10 @@ namespace Test\Security\RateLimiting;
use OC\Security\RateLimiting\Backend\IBackend;
use OC\Security\RateLimiting\Limiter;
-use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUser;
use Test\TestCase;
class LimiterTest extends TestCase {
- /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
- private $timeFactory;
/** @var IBackend|\PHPUnit\Framework\MockObject\MockObject */
private $backend;
/** @var Limiter */
@@ -41,11 +38,9 @@ class LimiterTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->timeFactory = $this->createMock(ITimeFactory::class);
$this->backend = $this->createMock(IBackend::class);
$this->limiter = new Limiter(
- $this->timeFactory,
$this->backend
);
}
@@ -60,8 +55,7 @@ class LimiterTest extends TestCase {
->method('getAttempts')
->with(
'MyIdentifier',
- '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
- 100
+ '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
)
->willReturn(101);
@@ -69,17 +63,12 @@ class LimiterTest extends TestCase {
}
public function testRegisterAnonRequestSuccess() {
- $this->timeFactory
- ->expects($this->once())
- ->method('getTime')
- ->willReturn(2000);
$this->backend
->expects($this->once())
->method('getAttempts')
->with(
'MyIdentifier',
- '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
- 100
+ '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
)
->willReturn(99);
$this->backend
@@ -88,7 +77,7 @@ class LimiterTest extends TestCase {
->with(
'MyIdentifier',
'4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
- 2000
+ 100
);
$this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
@@ -110,8 +99,7 @@ class LimiterTest extends TestCase {
->method('getAttempts')
->with(
'MyIdentifier',
- 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
- 100
+ 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
)
->willReturn(101);
@@ -126,17 +114,12 @@ class LimiterTest extends TestCase {
->method('getUID')
->willReturn('MyUid');
- $this->timeFactory
- ->expects($this->once())
- ->method('getTime')
- ->willReturn(2000);
$this->backend
->expects($this->once())
->method('getAttempts')
->with(
'MyIdentifier',
- 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
- 100
+ 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
)
->willReturn(99);
$this->backend
@@ -145,7 +128,7 @@ class LimiterTest extends TestCase {
->with(
'MyIdentifier',
'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
- 2000
+ 100
);
$this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);