You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BruteForceMiddlewareTest.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\AppFramework\Middleware\Security;
  22. use OC\AppFramework\Middleware\Security\BruteForceMiddleware;
  23. use OC\AppFramework\Utility\ControllerMethodReflector;
  24. use OC\Security\Bruteforce\Throttler;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Http\Response;
  27. use OCP\IRequest;
  28. use Test\TestCase;
  29. class BruteForceMiddlewareTest extends TestCase {
  30. /** @var ControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  31. private $reflector;
  32. /** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
  33. private $throttler;
  34. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  35. private $request;
  36. /** @var BruteForceMiddleware */
  37. private $bruteForceMiddleware;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->reflector = $this->createMock(ControllerMethodReflector::class);
  41. $this->throttler = $this->createMock(Throttler::class);
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->bruteForceMiddleware = new BruteForceMiddleware(
  44. $this->reflector,
  45. $this->throttler,
  46. $this->request
  47. );
  48. }
  49. public function testBeforeControllerWithAnnotation() {
  50. $this->reflector
  51. ->expects($this->once())
  52. ->method('hasAnnotation')
  53. ->with('BruteForceProtection')
  54. ->willReturn(true);
  55. $this->reflector
  56. ->expects($this->once())
  57. ->method('getAnnotationParameter')
  58. ->with('BruteForceProtection', 'action')
  59. ->willReturn('login');
  60. $this->request
  61. ->expects($this->once())
  62. ->method('getRemoteAddress')
  63. ->willReturn('127.0.0.1');
  64. $this->throttler
  65. ->expects($this->once())
  66. ->method('sleepDelayOrThrowOnMax')
  67. ->with('127.0.0.1', 'login');
  68. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  69. $controller = $this->createMock(Controller::class);
  70. $this->bruteForceMiddleware->beforeController($controller, 'testMethod');
  71. }
  72. public function testBeforeControllerWithoutAnnotation() {
  73. $this->reflector
  74. ->expects($this->once())
  75. ->method('hasAnnotation')
  76. ->with('BruteForceProtection')
  77. ->willReturn(false);
  78. $this->reflector
  79. ->expects($this->never())
  80. ->method('getAnnotationParameter');
  81. $this->request
  82. ->expects($this->never())
  83. ->method('getRemoteAddress');
  84. $this->throttler
  85. ->expects($this->never())
  86. ->method('sleepDelayOrThrowOnMax');
  87. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  88. $controller = $this->createMock(Controller::class);
  89. $this->bruteForceMiddleware->beforeController($controller, 'testMethod');
  90. }
  91. public function testAfterControllerWithAnnotationAndThrottledRequest() {
  92. /** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
  93. $response = $this->createMock(Response::class);
  94. $this->reflector
  95. ->expects($this->once())
  96. ->method('hasAnnotation')
  97. ->with('BruteForceProtection')
  98. ->willReturn(true);
  99. $response
  100. ->expects($this->once())
  101. ->method('isThrottled')
  102. ->willReturn(true);
  103. $response
  104. ->expects($this->once())
  105. ->method('getThrottleMetadata')
  106. ->willReturn([]);
  107. $this->reflector
  108. ->expects($this->once())
  109. ->method('getAnnotationParameter')
  110. ->with('BruteForceProtection', 'action')
  111. ->willReturn('login');
  112. $this->request
  113. ->expects($this->once())
  114. ->method('getRemoteAddress')
  115. ->willReturn('127.0.0.1');
  116. $this->throttler
  117. ->expects($this->once())
  118. ->method('sleepDelay')
  119. ->with('127.0.0.1', 'login');
  120. $this->throttler
  121. ->expects($this->once())
  122. ->method('registerAttempt')
  123. ->with('login', '127.0.0.1');
  124. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  125. $controller = $this->createMock(Controller::class);
  126. $this->bruteForceMiddleware->afterController($controller, 'testMethod', $response);
  127. }
  128. public function testAfterControllerWithAnnotationAndNotThrottledRequest() {
  129. /** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
  130. $response = $this->createMock(Response::class);
  131. $this->reflector
  132. ->expects($this->once())
  133. ->method('hasAnnotation')
  134. ->with('BruteForceProtection')
  135. ->willReturn(true);
  136. $response
  137. ->expects($this->once())
  138. ->method('isThrottled')
  139. ->willReturn(false);
  140. $this->reflector
  141. ->expects($this->never())
  142. ->method('getAnnotationParameter');
  143. $this->request
  144. ->expects($this->never())
  145. ->method('getRemoteAddress');
  146. $this->throttler
  147. ->expects($this->never())
  148. ->method('sleepDelay');
  149. $this->throttler
  150. ->expects($this->never())
  151. ->method('registerAttempt');
  152. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  153. $controller = $this->createMock(Controller::class);
  154. $this->bruteForceMiddleware->afterController($controller, 'testMethod', $response);
  155. }
  156. public function testAfterControllerWithoutAnnotation() {
  157. $this->reflector
  158. ->expects($this->once())
  159. ->method('hasAnnotation')
  160. ->with('BruteForceProtection')
  161. ->willReturn(false);
  162. $this->reflector
  163. ->expects($this->never())
  164. ->method('getAnnotationParameter');
  165. $this->request
  166. ->expects($this->never())
  167. ->method('getRemoteAddress');
  168. $this->throttler
  169. ->expects($this->never())
  170. ->method('sleepDelay');
  171. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  172. $controller = $this->createMock(Controller::class);
  173. /** @var Response|\PHPUnit\Framework\MockObject\MockObject $response */
  174. $response = $this->createMock(Response::class);
  175. $this->bruteForceMiddleware->afterController($controller, 'testMethod', $response);
  176. }
  177. }