1
0

DirectHomeTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\Unit\Direct;
  8. use OCA\DAV\Db\Direct;
  9. use OCA\DAV\Db\DirectMapper;
  10. use OCA\DAV\Direct\DirectFile;
  11. use OCA\DAV\Direct\DirectHome;
  12. use OCP\AppFramework\Db\DoesNotExistException;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\Files\IRootFolder;
  16. use OCP\IRequest;
  17. use OCP\Security\Bruteforce\IThrottler;
  18. use Sabre\DAV\Exception\Forbidden;
  19. use Sabre\DAV\Exception\MethodNotAllowed;
  20. use Sabre\DAV\Exception\NotFound;
  21. use Test\TestCase;
  22. class DirectHomeTest extends TestCase {
  23. /** @var DirectMapper|\PHPUnit\Framework\MockObject\MockObject */
  24. private $directMapper;
  25. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  26. private $rootFolder;
  27. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  28. private $timeFactory;
  29. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  30. private $throttler;
  31. /** @var IRequest */
  32. private $request;
  33. /** @var DirectHome */
  34. private $directHome;
  35. /** @var IEventDispatcher */
  36. private $eventDispatcher;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->directMapper = $this->createMock(DirectMapper::class);
  40. $this->rootFolder = $this->createMock(IRootFolder::class);
  41. $this->timeFactory = $this->createMock(ITimeFactory::class);
  42. $this->throttler = $this->createMock(IThrottler::class);
  43. $this->request = $this->createMock(IRequest::class);
  44. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  45. $this->timeFactory->method('getTime')
  46. ->willReturn(42);
  47. $this->request->method('getRemoteAddress')
  48. ->willReturn('1.2.3.4');
  49. $this->directHome = new DirectHome(
  50. $this->rootFolder,
  51. $this->directMapper,
  52. $this->timeFactory,
  53. $this->throttler,
  54. $this->request,
  55. $this->eventDispatcher
  56. );
  57. }
  58. public function testCreateFile(): void {
  59. $this->expectException(Forbidden::class);
  60. $this->directHome->createFile('foo', 'bar');
  61. }
  62. public function testCreateDirectory(): void {
  63. $this->expectException(Forbidden::class);
  64. $this->directHome->createDirectory('foo');
  65. }
  66. public function testGetChildren(): void {
  67. $this->expectException(MethodNotAllowed::class);
  68. $this->directHome->getChildren();
  69. }
  70. public function testChildExists(): void {
  71. $this->assertFalse($this->directHome->childExists('foo'));
  72. }
  73. public function testDelete(): void {
  74. $this->expectException(Forbidden::class);
  75. $this->directHome->delete();
  76. }
  77. public function testGetName(): void {
  78. $this->assertSame('direct', $this->directHome->getName());
  79. }
  80. public function testSetName(): void {
  81. $this->expectException(Forbidden::class);
  82. $this->directHome->setName('foo');
  83. }
  84. public function testGetLastModified(): void {
  85. $this->assertSame(0, $this->directHome->getLastModified());
  86. }
  87. public function testGetChildValid(): void {
  88. $direct = Direct::fromParams([
  89. 'expiration' => 100,
  90. ]);
  91. $this->directMapper->method('getByToken')
  92. ->with('longtoken')
  93. ->willReturn($direct);
  94. $this->throttler->expects($this->never())
  95. ->method($this->anything());
  96. $result = $this->directHome->getChild('longtoken');
  97. $this->assertInstanceOf(DirectFile::class, $result);
  98. }
  99. public function testGetChildExpired(): void {
  100. $direct = Direct::fromParams([
  101. 'expiration' => 41,
  102. ]);
  103. $this->directMapper->method('getByToken')
  104. ->with('longtoken')
  105. ->willReturn($direct);
  106. $this->throttler->expects($this->never())
  107. ->method($this->anything());
  108. $this->expectException(NotFound::class);
  109. $this->directHome->getChild('longtoken');
  110. }
  111. public function testGetChildInvalid(): void {
  112. $this->directMapper->method('getByToken')
  113. ->with('longtoken')
  114. ->willThrowException(new DoesNotExistException('not found'));
  115. $this->throttler->expects($this->once())
  116. ->method('registerAttempt')
  117. ->with(
  118. 'directlink',
  119. '1.2.3.4'
  120. );
  121. $this->throttler->expects($this->once())
  122. ->method('sleepDelay')
  123. ->with(
  124. '1.2.3.4',
  125. 'directlink'
  126. );
  127. $this->expectException(NotFound::class);
  128. $this->directHome->getChild('longtoken');
  129. }
  130. }