BruteForceMiddlewareTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\Http\Client\IResponse;
  28. use OCP\IRequest;
  29. use Test\TestCase;
  30. class BruteForceMiddlewareTest extends TestCase {
  31. /** @var ControllerMethodReflector|\PHPUnit_Framework_MockObject_MockObject */
  32. private $reflector;
  33. /** @var Throttler|\PHPUnit_Framework_MockObject_MockObject */
  34. private $throttler;
  35. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  36. private $request;
  37. /** @var BruteForceMiddleware */
  38. private $bruteForceMiddleware;
  39. public function setUp() {
  40. parent::setUp();
  41. $this->reflector = $this->createMock(ControllerMethodReflector::class);
  42. $this->throttler = $this->createMock(Throttler::class);
  43. $this->request = $this->createMock(IRequest::class);
  44. $this->bruteForceMiddleware = new BruteForceMiddleware(
  45. $this->reflector,
  46. $this->throttler,
  47. $this->request
  48. );
  49. }
  50. public function testBeforeControllerWithAnnotation() {
  51. $this->reflector
  52. ->expects($this->once())
  53. ->method('hasAnnotation')
  54. ->with('BruteForceProtection')
  55. ->willReturn(true);
  56. $this->reflector
  57. ->expects($this->once())
  58. ->method('getAnnotationParameter')
  59. ->with('BruteForceProtection', 'action')
  60. ->willReturn('login');
  61. $this->request
  62. ->expects($this->once())
  63. ->method('getRemoteAddress')
  64. ->willReturn('127.0.0.1');
  65. $this->throttler
  66. ->expects($this->once())
  67. ->method('sleepDelay')
  68. ->with('127.0.0.1', 'login');
  69. /** @var Controller|\PHPUnit_Framework_MockObject_MockObject $controller */
  70. $controller = $this->createMock(Controller::class);
  71. $this->bruteForceMiddleware->beforeController($controller, 'testMethod');
  72. }
  73. public function testBeforeControllerWithoutAnnotation() {
  74. $this->reflector
  75. ->expects($this->once())
  76. ->method('hasAnnotation')
  77. ->with('BruteForceProtection')
  78. ->willReturn(false);
  79. $this->reflector
  80. ->expects($this->never())
  81. ->method('getAnnotationParameter');
  82. $this->request
  83. ->expects($this->never())
  84. ->method('getRemoteAddress');
  85. $this->throttler
  86. ->expects($this->never())
  87. ->method('sleepDelay');
  88. /** @var Controller|\PHPUnit_Framework_MockObject_MockObject $controller */
  89. $controller = $this->createMock(Controller::class);
  90. $this->bruteForceMiddleware->beforeController($controller, 'testMethod');
  91. }
  92. public function testAfterControllerWithAnnotationAndThrottledRequest() {
  93. /** @var Response|\PHPUnit_Framework_MockObject_MockObject $response */
  94. $response = $this->createMock(Response::class);
  95. $this->reflector
  96. ->expects($this->once())
  97. ->method('hasAnnotation')
  98. ->with('BruteForceProtection')
  99. ->willReturn(true);
  100. $response
  101. ->expects($this->once())
  102. ->method('isThrottled')
  103. ->willReturn(true);
  104. $response
  105. ->expects($this->once())
  106. ->method('getThrottleMetadata')
  107. ->willReturn([]);
  108. $this->reflector
  109. ->expects($this->once())
  110. ->method('getAnnotationParameter')
  111. ->with('BruteForceProtection', 'action')
  112. ->willReturn('login');
  113. $this->request
  114. ->expects($this->once())
  115. ->method('getRemoteAddress')
  116. ->willReturn('127.0.0.1');
  117. $this->throttler
  118. ->expects($this->once())
  119. ->method('sleepDelay')
  120. ->with('127.0.0.1', 'login');
  121. $this->throttler
  122. ->expects($this->once())
  123. ->method('registerAttempt')
  124. ->with('login', '127.0.0.1');
  125. /** @var Controller|\PHPUnit_Framework_MockObject_MockObject $controller */
  126. $controller = $this->createMock(Controller::class);
  127. $this->bruteForceMiddleware->afterController($controller, 'testMethod' ,$response);
  128. }
  129. public function testAfterControllerWithAnnotationAndNotThrottledRequest() {
  130. /** @var Response|\PHPUnit_Framework_MockObject_MockObject $response */
  131. $response = $this->createMock(Response::class);
  132. $this->reflector
  133. ->expects($this->once())
  134. ->method('hasAnnotation')
  135. ->with('BruteForceProtection')
  136. ->willReturn(true);
  137. $response
  138. ->expects($this->once())
  139. ->method('isThrottled')
  140. ->willReturn(false);
  141. $this->reflector
  142. ->expects($this->never())
  143. ->method('getAnnotationParameter');
  144. $this->request
  145. ->expects($this->never())
  146. ->method('getRemoteAddress');
  147. $this->throttler
  148. ->expects($this->never())
  149. ->method('sleepDelay');
  150. $this->throttler
  151. ->expects($this->never())
  152. ->method('registerAttempt');
  153. /** @var Controller|\PHPUnit_Framework_MockObject_MockObject $controller */
  154. $controller = $this->createMock(Controller::class);
  155. $this->bruteForceMiddleware->afterController($controller, 'testMethod' ,$response);
  156. }
  157. public function testAfterControllerWithoutAnnotation() {
  158. $this->reflector
  159. ->expects($this->once())
  160. ->method('hasAnnotation')
  161. ->with('BruteForceProtection')
  162. ->willReturn(false);
  163. $this->reflector
  164. ->expects($this->never())
  165. ->method('getAnnotationParameter');
  166. $this->request
  167. ->expects($this->never())
  168. ->method('getRemoteAddress');
  169. $this->throttler
  170. ->expects($this->never())
  171. ->method('sleepDelay');
  172. /** @var Controller|\PHPUnit_Framework_MockObject_MockObject $controller */
  173. $controller = $this->createMock(Controller::class);
  174. /** @var Response|\PHPUnit_Framework_MockObject_MockObject $response */
  175. $response = $this->createMock(Response::class);
  176. $this->bruteForceMiddleware->afterController($controller, 'testMethod' ,$response);
  177. }
  178. }