1
0

CapabilitiesTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Roeland Jago Douma <roeland@famdouma.nl>
  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\Bruteforce;
  23. use OC\Security\Bruteforce\Capabilities;
  24. use OCP\IRequest;
  25. use OCP\Security\Bruteforce\IThrottler;
  26. use Test\TestCase;
  27. class CapabilitiesTest extends TestCase {
  28. /** @var Capabilities */
  29. private $capabilities;
  30. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  31. private $request;
  32. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  33. private $throttler;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->request = $this->createMock(IRequest::class);
  37. $this->throttler = $this->createMock(IThrottler::class);
  38. $this->capabilities = new Capabilities(
  39. $this->request,
  40. $this->throttler
  41. );
  42. }
  43. public function testGetCapabilities(): void {
  44. $this->throttler->expects($this->atLeastOnce())
  45. ->method('getDelay')
  46. ->with('10.10.10.10')
  47. ->willReturn(42);
  48. $this->throttler->expects($this->atLeastOnce())
  49. ->method('isBypassListed')
  50. ->with('10.10.10.10')
  51. ->willReturn(true);
  52. $this->request->method('getRemoteAddress')
  53. ->willReturn('10.10.10.10');
  54. $expected = [
  55. 'bruteforce' => [
  56. 'delay' => 42,
  57. 'allow-listed' => true,
  58. ]
  59. ];
  60. $result = $this->capabilities->getCapabilities();
  61. $this->assertEquals($expected, $result);
  62. }
  63. public function testGetCapabilitiesOnCli(): void {
  64. $this->throttler->expects($this->atLeastOnce())
  65. ->method('getDelay')
  66. ->with('')
  67. ->willReturn(0);
  68. $this->request->method('getRemoteAddress')
  69. ->willReturn('');
  70. $expected = [
  71. 'bruteforce' => [
  72. 'delay' => 0,
  73. 'allow-listed' => false,
  74. ]
  75. ];
  76. $result = $this->capabilities->getCapabilities();
  77. $this->assertEquals($expected, $result);
  78. }
  79. }