@ -9,34 +9,13 @@ namespace Test\AppFramework\Middleware\Security;
use OC\AppFramework\Middleware\Security\BruteForceMiddleware;
use OC\AppFramework\Utility\ControllerMethodReflector;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use Psr\Log\LoggerInterface;
use Test\AppFramework\Middleware\Security\Mock\BruteForceMiddlewareController;
use Test\TestCase;
class TestController extends Controller {
/**
* @BruteForceProtection(action=login)
*/
public function testMethodWithAnnotation() {
}
public function testMethodWithoutAnnotation() {
}
#[BruteForceProtection(action: 'single')]
public function singleAttribute(): void {
}
#[BruteForceProtection(action: 'first')]
#[BruteForceProtection(action: 'second')]
public function multipleAttributes(): void {
}
}
class BruteForceMiddlewareTest extends TestCase {
/** @var ControllerMethodReflector */
private $reflector;
@ -74,7 +53,7 @@ class BruteForceMiddlewareTest extends TestCase {
->method('sleepDelayOrThrowOnMax')
->with('127.0.0.1', 'login');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'testMethodWithAnnotation');
$this->bruteForceMiddleware->beforeController($controller, 'testMethodWithAnnotation');
}
@ -89,7 +68,7 @@ class BruteForceMiddlewareTest extends TestCase {
->method('sleepDelayOrThrowOnMax')
->with('::1', 'single');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'singleAttribute');
$this->bruteForceMiddleware->beforeController($controller, 'singleAttribute');
}
@ -113,7 +92,7 @@ class BruteForceMiddlewareTest extends TestCase {
return 0;
});
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'multipleAttributes');
$this->bruteForceMiddleware->beforeController($controller, 'multipleAttributes');
}
@ -126,7 +105,7 @@ class BruteForceMiddlewareTest extends TestCase {
->expects($this->never())
->method('sleepDelayOrThrowOnMax');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
$this->bruteForceMiddleware->beforeController($controller, 'testMethodWithoutAnnotation');
}
@ -155,7 +134,7 @@ class BruteForceMiddlewareTest extends TestCase {
->method('registerAttempt')
->with('login', '127.0.0.1');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'testMethodWithAnnotation');
$this->bruteForceMiddleware->afterController($controller, 'testMethodWithAnnotation', $response);
}
@ -177,7 +156,7 @@ class BruteForceMiddlewareTest extends TestCase {
->expects($this->never())
->method('registerAttempt');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'testMethodWithAnnotation');
$this->bruteForceMiddleware->afterController($controller, 'testMethodWithAnnotation', $response);
}
@ -207,7 +186,7 @@ class BruteForceMiddlewareTest extends TestCase {
->method('registerAttempt')
->with('single', '::1');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'singleAttribute');
$this->bruteForceMiddleware->afterController($controller, 'singleAttribute', $response);
}
@ -254,7 +233,7 @@ class BruteForceMiddlewareTest extends TestCase {
$this->assertEquals($expected, func_get_args());
});
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'multipleAttributes');
$this->bruteForceMiddleware->afterController($controller, 'multipleAttributes', $response);
}
@ -284,7 +263,7 @@ class BruteForceMiddlewareTest extends TestCase {
->method('registerAttempt')
->with('second', '::1');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'multipleAttributes');
$this->bruteForceMiddleware->afterController($controller, 'multipleAttributes', $response);
}
@ -297,7 +276,7 @@ class BruteForceMiddlewareTest extends TestCase {
->expects($this->never())
->method('sleepDelayOrThrowOnMax');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
/** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
$response = $this->createMock(Response::class);
@ -312,7 +291,7 @@ class BruteForceMiddlewareTest extends TestCase {
->expects($this->never())
->method('sleepDelayOrThrowOnMax');
$controller = new Test Controller('test', $this->request);
$controller = new BruteForceMiddleware Controller('test', $this->request);
$this->reflector->reflect($controller, 'testMethodWithoutAnnotation');
/** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
$response = $this->createMock(Response::class);
@ -321,7 +300,7 @@ class BruteForceMiddlewareTest extends TestCase {
$this->logger->expects($this->once())
->method('debug')
->with('Response for Test\AppFramework\Middleware\Security\Test Controller::testMethodWithoutAnnotation got bruteforce throttled but has no annotation nor attribute defined.');
->with('Response for Test\AppFramework\Middleware\Security\Mock\BruteForceMiddleware Controller::testMethodWithoutAnnotation got bruteforce throttled but has no annotation nor attribute defined.');
$this->bruteForceMiddleware->afterController($controller, 'testMethodWithoutAnnotation', $response);
}