CapabilitiesTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Bruteforce;
  8. use OC\Security\Bruteforce\Capabilities;
  9. use OCP\IRequest;
  10. use OCP\Security\Bruteforce\IThrottler;
  11. use Test\TestCase;
  12. class CapabilitiesTest extends TestCase {
  13. /** @var Capabilities */
  14. private $capabilities;
  15. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  16. private $request;
  17. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  18. private $throttler;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->request = $this->createMock(IRequest::class);
  22. $this->throttler = $this->createMock(IThrottler::class);
  23. $this->capabilities = new Capabilities(
  24. $this->request,
  25. $this->throttler
  26. );
  27. }
  28. public function testGetCapabilities(): void {
  29. $this->throttler->expects($this->atLeastOnce())
  30. ->method('getDelay')
  31. ->with('10.10.10.10')
  32. ->willReturn(42);
  33. $this->throttler->expects($this->atLeastOnce())
  34. ->method('isBypassListed')
  35. ->with('10.10.10.10')
  36. ->willReturn(true);
  37. $this->request->method('getRemoteAddress')
  38. ->willReturn('10.10.10.10');
  39. $expected = [
  40. 'bruteforce' => [
  41. 'delay' => 42,
  42. 'allow-listed' => true,
  43. ]
  44. ];
  45. $result = $this->capabilities->getCapabilities();
  46. $this->assertEquals($expected, $result);
  47. }
  48. public function testGetCapabilitiesOnCli(): void {
  49. $this->throttler->expects($this->atLeastOnce())
  50. ->method('getDelay')
  51. ->with('')
  52. ->willReturn(0);
  53. $this->request->method('getRemoteAddress')
  54. ->willReturn('');
  55. $expected = [
  56. 'bruteforce' => [
  57. 'delay' => 0,
  58. 'allow-listed' => false,
  59. ]
  60. ];
  61. $result = $this->capabilities->getCapabilities();
  62. $this->assertEquals($expected, $result);
  63. }
  64. }