RateLimitingMiddlewareTest.php 8.5 KB

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