CapabilitiesTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 OC\Security\Bruteforce\Throttler;
  25. use OCP\IRequest;
  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 Throttler|\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(Throttler::class);
  38. $this->capabilities = new Capabilities(
  39. $this->request,
  40. $this->throttler
  41. );
  42. }
  43. public function testGetCapabilities() {
  44. $this->throttler->expects($this->atLeastOnce())
  45. ->method('getDelay')
  46. ->with('10.10.10.10')
  47. ->willReturn(42);
  48. $this->request->method('getRemoteAddress')
  49. ->willReturn('10.10.10.10');
  50. $expected = [
  51. 'bruteforce' => [
  52. 'delay' => 42
  53. ]
  54. ];
  55. $result = $this->capabilities->getCapabilities();
  56. $this->assertEquals($expected, $result);
  57. }
  58. public function testGetCapabilitiesOnCli() {
  59. $this->throttler->expects($this->atLeastOnce())
  60. ->method('getDelay')
  61. ->with('')
  62. ->willReturn(0);
  63. $this->request->method('getRemoteAddress')
  64. ->willReturn('');
  65. $expected = [
  66. 'bruteforce' => [
  67. 'delay' => 0
  68. ]
  69. ];
  70. $result = $this->capabilities->getCapabilities();
  71. $this->assertEquals($expected, $result);
  72. }
  73. }