1
0

LimiterTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Security\RateLimiting;
  8. use OC\Security\RateLimiting\Backend\IBackend;
  9. use OC\Security\RateLimiting\Limiter;
  10. use OCP\IUser;
  11. use Test\TestCase;
  12. class LimiterTest extends TestCase {
  13. /** @var IBackend|\PHPUnit\Framework\MockObject\MockObject */
  14. private $backend;
  15. /** @var Limiter */
  16. private $limiter;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->backend = $this->createMock(IBackend::class);
  20. $this->limiter = new Limiter(
  21. $this->backend
  22. );
  23. }
  24. public function testRegisterAnonRequestExceeded(): void {
  25. $this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
  26. $this->expectExceptionMessage('Rate limit exceeded');
  27. $this->backend
  28. ->expects($this->once())
  29. ->method('getAttempts')
  30. ->with(
  31. 'MyIdentifier',
  32. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
  33. )
  34. ->willReturn(101);
  35. $this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
  36. }
  37. public function testRegisterAnonRequestSuccess(): void {
  38. $this->backend
  39. ->expects($this->once())
  40. ->method('getAttempts')
  41. ->with(
  42. 'MyIdentifier',
  43. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
  44. )
  45. ->willReturn(99);
  46. $this->backend
  47. ->expects($this->once())
  48. ->method('registerAttempt')
  49. ->with(
  50. 'MyIdentifier',
  51. '4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
  52. 100
  53. );
  54. $this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
  55. }
  56. public function testRegisterUserRequestExceeded(): void {
  57. $this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
  58. $this->expectExceptionMessage('Rate limit exceeded');
  59. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  60. $user = $this->createMock(IUser::class);
  61. $user
  62. ->expects($this->once())
  63. ->method('getUID')
  64. ->willReturn('MyUid');
  65. $this->backend
  66. ->expects($this->once())
  67. ->method('getAttempts')
  68. ->with(
  69. 'MyIdentifier',
  70. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
  71. )
  72. ->willReturn(101);
  73. $this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
  74. }
  75. public function testRegisterUserRequestSuccess(): void {
  76. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
  77. $user = $this->createMock(IUser::class);
  78. $user
  79. ->expects($this->once())
  80. ->method('getUID')
  81. ->willReturn('MyUid');
  82. $this->backend
  83. ->expects($this->once())
  84. ->method('getAttempts')
  85. ->with(
  86. 'MyIdentifier',
  87. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
  88. )
  89. ->willReturn(99);
  90. $this->backend
  91. ->expects($this->once())
  92. ->method('registerAttempt')
  93. ->with(
  94. 'MyIdentifier',
  95. 'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
  96. 100
  97. );
  98. $this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
  99. }
  100. }