DirectHomeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\Unit\Direct;
  25. use OC\Security\Bruteforce\Throttler;
  26. use OCA\DAV\Db\Direct;
  27. use OCA\DAV\Db\DirectMapper;
  28. use OCA\DAV\Direct\DirectFile;
  29. use OCA\DAV\Direct\DirectHome;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\IRootFolder;
  33. use OCP\IRequest;
  34. use Sabre\DAV\Exception\Forbidden;
  35. use Sabre\DAV\Exception\MethodNotAllowed;
  36. use Sabre\DAV\Exception\NotFound;
  37. use Test\TestCase;
  38. class DirectHomeTest extends TestCase {
  39. /** @var DirectMapper|\PHPUnit_Framework_MockObject_MockObject */
  40. private $directMapper;
  41. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  42. private $rootFolder;
  43. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  44. private $timeFactory;
  45. /** @var Throttler|\PHPUnit_Framework_MockObject_MockObject */
  46. private $throttler;
  47. /** @var IRequest */
  48. private $request;
  49. /** @var DirectHome */
  50. private $directHome;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->directMapper = $this->createMock(DirectMapper::class);
  54. $this->rootFolder = $this->createMock(IRootFolder::class);
  55. $this->timeFactory = $this->createMock(ITimeFactory::class);
  56. $this->throttler = $this->createMock(Throttler::class);
  57. $this->request = $this->createMock(IRequest::class);
  58. $this->timeFactory->method('getTime')
  59. ->willReturn(42);
  60. $this->request->method('getRemoteAddress')
  61. ->willReturn('1.2.3.4');
  62. $this->directHome = new DirectHome(
  63. $this->rootFolder,
  64. $this->directMapper,
  65. $this->timeFactory,
  66. $this->throttler,
  67. $this->request
  68. );
  69. }
  70. public function testCreateFile() {
  71. $this->expectException(Forbidden::class);
  72. $this->directHome->createFile('foo', 'bar');
  73. }
  74. public function testCreateDirectory() {
  75. $this->expectException(Forbidden::class);
  76. $this->directHome->createDirectory('foo');
  77. }
  78. public function testGetChildren() {
  79. $this->expectException(MethodNotAllowed::class);
  80. $this->directHome->getChildren();
  81. }
  82. public function testChildExists() {
  83. $this->assertFalse($this->directHome->childExists('foo'));
  84. }
  85. public function testDelete() {
  86. $this->expectException(Forbidden::class);
  87. $this->directHome->delete();
  88. }
  89. public function testGetName() {
  90. $this->assertSame('direct', $this->directHome->getName());
  91. }
  92. public function testSetName() {
  93. $this->expectException(Forbidden::class);
  94. $this->directHome->setName('foo');
  95. }
  96. public function testGetLastModified() {
  97. $this->assertSame(0, $this->directHome->getLastModified());
  98. }
  99. public function testGetChildValid() {
  100. $direct = Direct::fromParams([
  101. 'expiration' => 100,
  102. ]);
  103. $this->directMapper->method('getByToken')
  104. ->with('longtoken')
  105. ->willReturn($direct);
  106. $this->throttler->expects($this->never())
  107. ->method($this->anything());
  108. $result = $this->directHome->getChild('longtoken');
  109. $this->assertInstanceOf(DirectFile::class, $result);
  110. }
  111. public function testGetChildExpired() {
  112. $direct = Direct::fromParams([
  113. 'expiration' => 41,
  114. ]);
  115. $this->directMapper->method('getByToken')
  116. ->with('longtoken')
  117. ->willReturn($direct);
  118. $this->throttler->expects($this->never())
  119. ->method($this->anything());
  120. $this->expectException(NotFound::class);
  121. $this->directHome->getChild('longtoken');
  122. }
  123. public function testGetChildInvalid() {
  124. $this->directMapper->method('getByToken')
  125. ->with('longtoken')
  126. ->willThrowException(new DoesNotExistException('not found'));
  127. $this->throttler->expects($this->once())
  128. ->method('registerAttempt')
  129. ->with(
  130. 'directlink',
  131. '1.2.3.4'
  132. );
  133. $this->throttler->expects($this->once())
  134. ->method('sleepDelay')
  135. ->with(
  136. '1.2.3.4',
  137. 'directlink'
  138. );
  139. $this->expectException(NotFound::class);
  140. $this->directHome->getChild('longtoken');
  141. }
  142. }