CapabilitiesTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Security\Bruteforce;
  22. use OC\Security\Bruteforce\Capabilities;
  23. use OC\Security\Bruteforce\Throttler;
  24. use OCP\IRequest;
  25. use Test\TestCase;
  26. class CapabilitiesTest extends TestCase {
  27. /** @var Capabilities */
  28. private $capabilities;
  29. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  30. private $request;
  31. /** @var Throttler|\PHPUnit\Framework\MockObject\MockObject */
  32. private $throttler;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->request = $this->createMock(IRequest::class);
  36. $this->throttler = $this->createMock(Throttler::class);
  37. $this->capabilities = new Capabilities(
  38. $this->request,
  39. $this->throttler
  40. );
  41. }
  42. public function testGetCapabilities() {
  43. $this->throttler->expects($this->atLeastOnce())
  44. ->method('getDelay')
  45. ->with('10.10.10.10')
  46. ->willReturn(42);
  47. $this->request->method('getRemoteAddress')
  48. ->willReturn('10.10.10.10');
  49. $expected = [
  50. 'bruteforce' => [
  51. 'delay' => 42
  52. ]
  53. ];
  54. $result = $this->capabilities->getCapabilities();
  55. $this->assertEquals($expected, $result);
  56. }
  57. public function testGetCapabilitiesOnCli() {
  58. $this->throttler->expects($this->atLeastOnce())
  59. ->method('getDelay')
  60. ->with('')
  61. ->willReturn(0);
  62. $this->request->method('getRemoteAddress')
  63. ->willReturn('');
  64. $expected = [
  65. 'bruteforce' => [
  66. 'delay' => 0
  67. ]
  68. ];
  69. $result = $this->capabilities->getCapabilities();
  70. $this->assertEquals($expected, $result);
  71. }
  72. }