RateLimitingMiddlewareTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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\RateLimitingMiddleware;
  23. use OC\AppFramework\Utility\ControllerMethodReflector;
  24. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  25. use OC\Security\RateLimiting\Limiter;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\IRequest;
  30. use OCP\IUser;
  31. use OCP\IUserSession;
  32. use Test\TestCase;
  33. /**
  34. * @group DB
  35. */
  36. class RateLimitingMiddlewareTest extends TestCase {
  37. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  38. private $request;
  39. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  40. private $userSession;
  41. /** @var ControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  42. private $reflector;
  43. /** @var Limiter|\PHPUnit\Framework\MockObject\MockObject */
  44. private $limiter;
  45. /** @var RateLimitingMiddleware */
  46. private $rateLimitingMiddleware;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->request = $this->createMock(IRequest::class);
  50. $this->userSession = $this->createMock(IUserSession::class);
  51. $this->reflector = $this->createMock(ControllerMethodReflector::class);
  52. $this->limiter = $this->createMock(Limiter::class);
  53. $this->rateLimitingMiddleware = new RateLimitingMiddleware(
  54. $this->request,
  55. $this->userSession,
  56. $this->reflector,
  57. $this->limiter
  58. );
  59. }
  60. public function testBeforeControllerWithoutAnnotation() {
  61. $this->reflector
  62. ->expects($this->at(0))
  63. ->method('getAnnotationParameter')
  64. ->with('AnonRateThrottle', 'limit')
  65. ->willReturn('');
  66. $this->reflector
  67. ->expects($this->at(1))
  68. ->method('getAnnotationParameter')
  69. ->with('AnonRateThrottle', 'period')
  70. ->willReturn('');
  71. $this->reflector
  72. ->expects($this->at(2))
  73. ->method('getAnnotationParameter')
  74. ->with('UserRateThrottle', 'limit')
  75. ->willReturn('');
  76. $this->reflector
  77. ->expects($this->at(3))
  78. ->method('getAnnotationParameter')
  79. ->with('UserRateThrottle', 'period')
  80. ->willReturn('');
  81. $this->limiter
  82. ->expects($this->never())
  83. ->method('registerUserRequest');
  84. $this->limiter
  85. ->expects($this->never())
  86. ->method('registerAnonRequest');
  87. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  88. $controller = $this->createMock(Controller::class);
  89. $this->rateLimitingMiddleware->beforeController($controller, 'testMethod');
  90. }
  91. public function testBeforeControllerForAnon() {
  92. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  93. $controller = $this->createMock(Controller::class);
  94. $this->request
  95. ->expects($this->once())
  96. ->method('getRemoteAddress')
  97. ->willReturn('127.0.0.1');
  98. $this->reflector
  99. ->expects($this->at(0))
  100. ->method('getAnnotationParameter')
  101. ->with('AnonRateThrottle', 'limit')
  102. ->willReturn('100');
  103. $this->reflector
  104. ->expects($this->at(1))
  105. ->method('getAnnotationParameter')
  106. ->with('AnonRateThrottle', 'period')
  107. ->willReturn('10');
  108. $this->reflector
  109. ->expects($this->at(2))
  110. ->method('getAnnotationParameter')
  111. ->with('UserRateThrottle', 'limit')
  112. ->willReturn('');
  113. $this->reflector
  114. ->expects($this->at(3))
  115. ->method('getAnnotationParameter')
  116. ->with('UserRateThrottle', 'period')
  117. ->willReturn('');
  118. $this->limiter
  119. ->expects($this->never())
  120. ->method('registerUserRequest');
  121. $this->limiter
  122. ->expects($this->once())
  123. ->method('registerAnonRequest')
  124. ->with(get_class($controller) . '::testMethod', '100', '10', '127.0.0.1');
  125. $this->rateLimitingMiddleware->beforeController($controller, 'testMethod');
  126. }
  127. public function testBeforeControllerForLoggedIn() {
  128. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  129. $controller = $this->createMock(Controller::class);
  130. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  131. $user = $this->createMock(IUser::class);
  132. $this->userSession
  133. ->expects($this->once())
  134. ->method('isLoggedIn')
  135. ->willReturn(true);
  136. $this->userSession
  137. ->expects($this->once())
  138. ->method('getUser')
  139. ->willReturn($user);
  140. $this->reflector
  141. ->expects($this->at(0))
  142. ->method('getAnnotationParameter')
  143. ->with('AnonRateThrottle', 'limit')
  144. ->willReturn('');
  145. $this->reflector
  146. ->expects($this->at(1))
  147. ->method('getAnnotationParameter')
  148. ->with('AnonRateThrottle', 'period')
  149. ->willReturn('');
  150. $this->reflector
  151. ->expects($this->at(2))
  152. ->method('getAnnotationParameter')
  153. ->with('UserRateThrottle', 'limit')
  154. ->willReturn('100');
  155. $this->reflector
  156. ->expects($this->at(3))
  157. ->method('getAnnotationParameter')
  158. ->with('UserRateThrottle', 'period')
  159. ->willReturn('10');
  160. $this->limiter
  161. ->expects($this->never())
  162. ->method('registerAnonRequest');
  163. $this->limiter
  164. ->expects($this->once())
  165. ->method('registerUserRequest')
  166. ->with(get_class($controller) . '::testMethod', '100', '10', $user);
  167. $this->rateLimitingMiddleware->beforeController($controller, 'testMethod');
  168. }
  169. public function testBeforeControllerAnonWithFallback() {
  170. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  171. $controller = $this->createMock(Controller::class);
  172. $this->request
  173. ->expects($this->once())
  174. ->method('getRemoteAddress')
  175. ->willReturn('127.0.0.1');
  176. $this->userSession
  177. ->expects($this->once())
  178. ->method('isLoggedIn')
  179. ->willReturn(false);
  180. $this->reflector
  181. ->expects($this->at(0))
  182. ->method('getAnnotationParameter')
  183. ->with('AnonRateThrottle', 'limit')
  184. ->willReturn('200');
  185. $this->reflector
  186. ->expects($this->at(1))
  187. ->method('getAnnotationParameter')
  188. ->with('AnonRateThrottle', 'period')
  189. ->willReturn('20');
  190. $this->reflector
  191. ->expects($this->at(2))
  192. ->method('getAnnotationParameter')
  193. ->with('UserRateThrottle', 'limit')
  194. ->willReturn('100');
  195. $this->reflector
  196. ->expects($this->at(3))
  197. ->method('getAnnotationParameter')
  198. ->with('UserRateThrottle', 'period')
  199. ->willReturn('10');
  200. $this->limiter
  201. ->expects($this->never())
  202. ->method('registerUserRequest');
  203. $this->limiter
  204. ->expects($this->once())
  205. ->method('registerAnonRequest')
  206. ->with(get_class($controller) . '::testMethod', '200', '20', '127.0.0.1');
  207. $this->rateLimitingMiddleware->beforeController($controller, 'testMethod');
  208. }
  209. public function testAfterExceptionWithOtherException() {
  210. $this->expectException(\Exception::class);
  211. $this->expectExceptionMessage('My test exception');
  212. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  213. $controller = $this->createMock(Controller::class);
  214. $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new \Exception('My test exception'));
  215. }
  216. public function testAfterExceptionWithJsonBody() {
  217. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  218. $controller = $this->createMock(Controller::class);
  219. $this->request
  220. ->expects($this->once())
  221. ->method('getHeader')
  222. ->with('Accept')
  223. ->willReturn('JSON');
  224. $result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
  225. $expected = new DataResponse([], 429
  226. );
  227. $this->assertEquals($expected, $result);
  228. }
  229. public function testAfterExceptionWithHtmlBody() {
  230. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject $controller */
  231. $controller = $this->createMock(Controller::class);
  232. $this->request
  233. ->expects($this->once())
  234. ->method('getHeader')
  235. ->with('Accept')
  236. ->willReturn('html');
  237. $result = $this->rateLimitingMiddleware->afterException($controller, 'testMethod', new RateLimitExceededException());
  238. $expected = new TemplateResponse(
  239. 'core',
  240. '429',
  241. [],
  242. TemplateResponse::RENDER_AS_GUEST
  243. );
  244. $expected->setStatus(429);
  245. $this->assertEquals($expected, $result);
  246. $this->assertIsString($result->render());
  247. }
  248. }