LimiterTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Security\RateLimiting;
  23. use OC\Security\RateLimiting\Backend\IBackend;
  24. use OC\Security\RateLimiting\Limiter;
  25. use OCP\IUser;
  26. use Test\TestCase;
  27. class LimiterTest extends TestCase {
  28. /** @var IBackend|\PHPUnit\Framework\MockObject\MockObject */
  29. private $backend;
  30. /** @var Limiter */
  31. private $limiter;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->backend = $this->createMock(IBackend::class);
  35. $this->limiter = new Limiter(
  36. $this->backend
  37. );
  38. }
  39. public function testRegisterAnonRequestExceeded() {
  40. $this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
  41. $this->expectExceptionMessage('Rate limit exceeded');
  42. $this->backend
  43. ->expects($this->once())
  44. ->method('getAttempts')
  45. ->with(
  46. 'MyIdentifier',
  47. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
  48. )
  49. ->willReturn(101);
  50. $this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
  51. }
  52. public function testRegisterAnonRequestSuccess() {
  53. $this->backend
  54. ->expects($this->once())
  55. ->method('getAttempts')
  56. ->with(
  57. 'MyIdentifier',
  58. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
  59. )
  60. ->willReturn(99);
  61. $this->backend
  62. ->expects($this->once())
  63. ->method('registerAttempt')
  64. ->with(
  65. 'MyIdentifier',
  66. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
  67. 100
  68. );
  69. $this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
  70. }
  71. public function testRegisterUserRequestExceeded() {
  72. $this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
  73. $this->expectExceptionMessage('Rate limit exceeded');
  74. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  75. $user = $this->createMock(IUser::class);
  76. $user
  77. ->expects($this->once())
  78. ->method('getUID')
  79. ->willReturn('MyUid');
  80. $this->backend
  81. ->expects($this->once())
  82. ->method('getAttempts')
  83. ->with(
  84. 'MyIdentifier',
  85. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
  86. )
  87. ->willReturn(101);
  88. $this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
  89. }
  90. public function testRegisterUserRequestSuccess() {
  91. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  92. $user = $this->createMock(IUser::class);
  93. $user
  94. ->expects($this->once())
  95. ->method('getUID')
  96. ->willReturn('MyUid');
  97. $this->backend
  98. ->expects($this->once())
  99. ->method('getAttempts')
  100. ->with(
  101. 'MyIdentifier',
  102. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
  103. )
  104. ->willReturn(99);
  105. $this->backend
  106. ->expects($this->once())
  107. ->method('registerAttempt')
  108. ->with(
  109. 'MyIdentifier',
  110. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
  111. 100
  112. );
  113. $this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
  114. }
  115. }