DirectHomeTest.php 4.9 KB

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