99
1010use OC \AppFramework \Middleware \Security \BruteForceMiddleware ;
1111use OC \AppFramework \Utility \ControllerMethodReflector ;
12- use OCP \AppFramework \Controller ;
13- use OCP \AppFramework \Http \Attribute \BruteForceProtection ;
1412use OCP \AppFramework \Http \Response ;
1513use OCP \IRequest ;
1614use OCP \Security \Bruteforce \IThrottler ;
1715use Psr \Log \LoggerInterface ;
16+ use Test \AppFramework \Middleware \Security \Mock \BruteForceMiddlewareController ;
1817use Test \TestCase ;
1918
20- class TestController extends Controller {
21- /**
22- * @BruteForceProtection(action=login)
23- */
24- public function testMethodWithAnnotation () {
25- }
26-
27- public function testMethodWithoutAnnotation () {
28- }
29-
30- #[BruteForceProtection(action: 'single ' )]
31- public function singleAttribute (): void {
32- }
33-
34- #[BruteForceProtection(action: 'first ' )]
35- #[BruteForceProtection(action: 'second ' )]
36- public function multipleAttributes (): void {
37- }
38- }
39-
4019class BruteForceMiddlewareTest extends TestCase {
4120 /** @var ControllerMethodReflector */
4221 private $ reflector ;
@@ -74,7 +53,7 @@ public function testBeforeControllerWithAnnotation(): void {
7453 ->method ('sleepDelayOrThrowOnMax ' )
7554 ->with ('127.0.0.1 ' , 'login ' );
7655
77- $ controller = new TestController ('test ' , $ this ->request );
56+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
7857 $ this ->reflector ->reflect ($ controller , 'testMethodWithAnnotation ' );
7958 $ this ->bruteForceMiddleware ->beforeController ($ controller , 'testMethodWithAnnotation ' );
8059 }
@@ -89,7 +68,7 @@ public function testBeforeControllerWithSingleAttribute(): void {
8968 ->method ('sleepDelayOrThrowOnMax ' )
9069 ->with ('::1 ' , 'single ' );
9170
92- $ controller = new TestController ('test ' , $ this ->request );
71+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
9372 $ this ->reflector ->reflect ($ controller , 'singleAttribute ' );
9473 $ this ->bruteForceMiddleware ->beforeController ($ controller , 'singleAttribute ' );
9574 }
@@ -113,7 +92,7 @@ public function testBeforeControllerWithMultipleAttributes(): void {
11392 return 0 ;
11493 });
11594
116- $ controller = new TestController ('test ' , $ this ->request );
95+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
11796 $ this ->reflector ->reflect ($ controller , 'multipleAttributes ' );
11897 $ this ->bruteForceMiddleware ->beforeController ($ controller , 'multipleAttributes ' );
11998 }
@@ -126,7 +105,7 @@ public function testBeforeControllerWithoutAnnotation(): void {
126105 ->expects ($ this ->never ())
127106 ->method ('sleepDelayOrThrowOnMax ' );
128107
129- $ controller = new TestController ('test ' , $ this ->request );
108+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
130109 $ this ->reflector ->reflect ($ controller , 'testMethodWithoutAnnotation ' );
131110 $ this ->bruteForceMiddleware ->beforeController ($ controller , 'testMethodWithoutAnnotation ' );
132111 }
@@ -155,7 +134,7 @@ public function testAfterControllerWithAnnotationAndThrottledRequest(): void {
155134 ->method ('registerAttempt ' )
156135 ->with ('login ' , '127.0.0.1 ' );
157136
158- $ controller = new TestController ('test ' , $ this ->request );
137+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
159138 $ this ->reflector ->reflect ($ controller , 'testMethodWithAnnotation ' );
160139 $ this ->bruteForceMiddleware ->afterController ($ controller , 'testMethodWithAnnotation ' , $ response );
161140 }
@@ -177,7 +156,7 @@ public function testAfterControllerWithAnnotationAndNotThrottledRequest(): void
177156 ->expects ($ this ->never ())
178157 ->method ('registerAttempt ' );
179158
180- $ controller = new TestController ('test ' , $ this ->request );
159+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
181160 $ this ->reflector ->reflect ($ controller , 'testMethodWithAnnotation ' );
182161 $ this ->bruteForceMiddleware ->afterController ($ controller , 'testMethodWithAnnotation ' , $ response );
183162 }
@@ -207,7 +186,7 @@ public function testAfterControllerWithSingleAttribute(): void {
207186 ->method ('registerAttempt ' )
208187 ->with ('single ' , '::1 ' );
209188
210- $ controller = new TestController ('test ' , $ this ->request );
189+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
211190 $ this ->reflector ->reflect ($ controller , 'singleAttribute ' );
212191 $ this ->bruteForceMiddleware ->afterController ($ controller , 'singleAttribute ' , $ response );
213192 }
@@ -254,7 +233,7 @@ public function testAfterControllerWithMultipleAttributesGeneralMatch(): void {
254233 $ this ->assertEquals ($ expected , func_get_args ());
255234 });
256235
257- $ controller = new TestController ('test ' , $ this ->request );
236+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
258237 $ this ->reflector ->reflect ($ controller , 'multipleAttributes ' );
259238 $ this ->bruteForceMiddleware ->afterController ($ controller , 'multipleAttributes ' , $ response );
260239 }
@@ -284,7 +263,7 @@ public function testAfterControllerWithMultipleAttributesSpecificMatch(): void {
284263 ->method ('registerAttempt ' )
285264 ->with ('second ' , '::1 ' );
286265
287- $ controller = new TestController ('test ' , $ this ->request );
266+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
288267 $ this ->reflector ->reflect ($ controller , 'multipleAttributes ' );
289268 $ this ->bruteForceMiddleware ->afterController ($ controller , 'multipleAttributes ' , $ response );
290269 }
@@ -297,7 +276,7 @@ public function testAfterControllerWithoutAnnotation(): void {
297276 ->expects ($ this ->never ())
298277 ->method ('sleepDelayOrThrowOnMax ' );
299278
300- $ controller = new TestController ('test ' , $ this ->request );
279+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
301280 $ this ->reflector ->reflect ($ controller , 'testMethodWithoutAnnotation ' );
302281 /** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
303282 $ response = $ this ->createMock (Response::class);
@@ -312,7 +291,7 @@ public function testAfterControllerWithThrottledResponseButUnhandled(): void {
312291 ->expects ($ this ->never ())
313292 ->method ('sleepDelayOrThrowOnMax ' );
314293
315- $ controller = new TestController ('test ' , $ this ->request );
294+ $ controller = new BruteForceMiddlewareController ('test ' , $ this ->request );
316295 $ this ->reflector ->reflect ($ controller , 'testMethodWithoutAnnotation ' );
317296 /** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
318297 $ response = $ this ->createMock (Response::class);
@@ -321,7 +300,7 @@ public function testAfterControllerWithThrottledResponseButUnhandled(): void {
321300
322301 $ this ->logger ->expects ($ this ->once ())
323302 ->method ('debug ' )
324- ->with ('Response for Test\AppFramework\Middleware\Security\TestController ::testMethodWithoutAnnotation got bruteforce throttled but has no annotation nor attribute defined. ' );
303+ ->with ('Response for Test\AppFramework\Middleware\Security\Mock\BruteForceMiddlewareController ::testMethodWithoutAnnotation got bruteforce throttled but has no annotation nor attribute defined. ' );
325304
326305 $ this ->bruteForceMiddleware ->afterController ($ controller , 'testMethodWithoutAnnotation ' , $ response );
327306 }
0 commit comments