mirror of
https://github.com/nextcloud/server.git
synced 2026-11-13 05:32:30 +01:00
This is clearer, safer and better typed. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
122 lines
3.4 KiB
PHP
122 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace Test\Security\RateLimiting\Backend;
|
|
|
|
use OC\Security\RateLimiting\Backend\MemoryCacheBackend;
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
use OCP\ICache;
|
|
use OCP\IConfig;
|
|
use Test\TestCase;
|
|
|
|
class MemoryCacheBackendTest extends TestCase {
|
|
/** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
|
|
private $cache;
|
|
/** @var MemoryCacheBackend */
|
|
private $memoryCache;
|
|
|
|
#[\Override]
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
$this->memoryCache = $this->createInstanceWithMocks(MemoryCacheBackend::class);
|
|
|
|
$this->getAutoMock(IConfig::class)->method('getSystemValueBool')
|
|
->with('ratelimit.protection.enabled')
|
|
->willReturn(true);
|
|
}
|
|
|
|
public function testGetAttemptsWithNoAttemptsBefore(): void {
|
|
$this->getCacheAutoMock(MemoryCacheBackend::class)
|
|
->expects($this->once())
|
|
->method('get')
|
|
->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
|
|
->willReturn(null);
|
|
|
|
$this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User'));
|
|
}
|
|
|
|
public function testGetAttempts(): void {
|
|
$this->getAutoMock(ITimeFactory::class)
|
|
->expects($this->once())
|
|
->method('getTime')
|
|
->willReturn(210);
|
|
$this->getCacheAutoMock(MemoryCacheBackend::class)
|
|
->expects($this->once())
|
|
->method('get')
|
|
->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
|
|
->willReturn(json_encode([
|
|
'1',
|
|
'2',
|
|
'87',
|
|
'223',
|
|
'223',
|
|
'224',
|
|
]));
|
|
|
|
$this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User'));
|
|
}
|
|
|
|
public function testRegisterAttemptWithNoAttemptsBefore(): void {
|
|
$this->getAutoMock(ITimeFactory::class)
|
|
->expects($this->once())
|
|
->method('getTime')
|
|
->willReturn(123);
|
|
|
|
$this->getCacheAutoMock(MemoryCacheBackend::class)
|
|
->expects($this->once())
|
|
->method('get')
|
|
->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
|
|
->willReturn(null);
|
|
$this->getCacheAutoMock(MemoryCacheBackend::class)
|
|
->expects($this->once())
|
|
->method('set')
|
|
->with(
|
|
'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
|
|
json_encode(['223'])
|
|
);
|
|
|
|
$this->memoryCache->registerAttempt('Method', 'User', 100);
|
|
}
|
|
|
|
public function testRegisterAttempt(): void {
|
|
$this->getAutoMock(ITimeFactory::class)
|
|
->expects($this->once())
|
|
->method('getTime')
|
|
->willReturn(86);
|
|
|
|
$this->getCacheAutoMock(MemoryCacheBackend::class)
|
|
->expects($this->once())
|
|
->method('get')
|
|
->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
|
|
->willReturn(json_encode([
|
|
'1',
|
|
'2',
|
|
'87',
|
|
'123',
|
|
'123',
|
|
'124',
|
|
]));
|
|
$this->getCacheAutoMock(MemoryCacheBackend::class)
|
|
->expects($this->once())
|
|
->method('set')
|
|
->with(
|
|
'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
|
|
json_encode([
|
|
'87',
|
|
'123',
|
|
'123',
|
|
'124',
|
|
'186',
|
|
])
|
|
);
|
|
|
|
$this->memoryCache->registerAttempt('Method', 'User', 100);
|
|
}
|
|
}
|