DirectControllerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  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\DAV\Controller;
  27. use OCA\DAV\Controller\DirectController;
  28. use OCA\DAV\Db\Direct;
  29. use OCA\DAV\Db\DirectMapper;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSBadRequestException;
  32. use OCP\AppFramework\OCS\OCSNotFoundException;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\Files\File;
  36. use OCP\Files\Folder;
  37. use OCP\Files\IRootFolder;
  38. use OCP\IRequest;
  39. use OCP\IUrlGenerator;
  40. use OCP\Security\ISecureRandom;
  41. use Test\TestCase;
  42. class DirectControllerTest extends TestCase {
  43. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  44. private $rootFolder;
  45. /** @var DirectMapper|\PHPUnit\Framework\MockObject\MockObject */
  46. private $directMapper;
  47. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  48. private $random;
  49. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  50. private $timeFactory;
  51. /** @var IUrlGenerator|\PHPUnit\Framework\MockObject\MockObject */
  52. private $urlGenerator;
  53. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  54. private $eventDispatcher;
  55. private DirectController $controller;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->rootFolder = $this->createMock(IRootFolder::class);
  59. $this->directMapper = $this->createMock(DirectMapper::class);
  60. $this->random = $this->createMock(ISecureRandom::class);
  61. $this->timeFactory = $this->createMock(ITimeFactory::class);
  62. $this->urlGenerator = $this->createMock(IUrlGenerator::class);
  63. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  64. $this->controller = new DirectController(
  65. 'dav',
  66. $this->createMock(IRequest::class),
  67. $this->rootFolder,
  68. 'awesomeUser',
  69. $this->directMapper,
  70. $this->random,
  71. $this->timeFactory,
  72. $this->urlGenerator,
  73. $this->eventDispatcher
  74. );
  75. }
  76. public function testGetUrlNonExistingFileId(): void {
  77. $userFolder = $this->createMock(Folder::class);
  78. $this->rootFolder->method('getUserFolder')
  79. ->with('awesomeUser')
  80. ->willReturn($userFolder);
  81. $userFolder->method('getById')
  82. ->with(101)
  83. ->willReturn([]);
  84. $this->expectException(OCSNotFoundException::class);
  85. $this->controller->getUrl(101);
  86. }
  87. public function testGetUrlForFolder(): void {
  88. $userFolder = $this->createMock(Folder::class);
  89. $this->rootFolder->method('getUserFolder')
  90. ->with('awesomeUser')
  91. ->willReturn($userFolder);
  92. $folder = $this->createMock(Folder::class);
  93. $userFolder->method('getFirstNodeById')
  94. ->with(101)
  95. ->willReturn($folder);
  96. $this->expectException(OCSBadRequestException::class);
  97. $this->controller->getUrl(101);
  98. }
  99. public function testGetUrlValid(): void {
  100. $userFolder = $this->createMock(Folder::class);
  101. $this->rootFolder->method('getUserFolder')
  102. ->with('awesomeUser')
  103. ->willReturn($userFolder);
  104. $file = $this->createMock(File::class);
  105. $this->timeFactory->method('getTime')
  106. ->willReturn(42);
  107. $userFolder->method('getFirstNodeById')
  108. ->with(101)
  109. ->willReturn($file);
  110. $userFolder->method('getRelativePath')
  111. ->willReturn('/path');
  112. $this->random->method('generate')
  113. ->with(
  114. 60,
  115. ISecureRandom::CHAR_ALPHANUMERIC
  116. )->willReturn('superduperlongtoken');
  117. $this->directMapper->expects($this->once())
  118. ->method('insert')
  119. ->willReturnCallback(function (Direct $direct) {
  120. $this->assertSame('awesomeUser', $direct->getUserId());
  121. $this->assertSame(101, $direct->getFileId());
  122. $this->assertSame('superduperlongtoken', $direct->getToken());
  123. $this->assertSame(42 + 60 * 60 * 8, $direct->getExpiration());
  124. return $direct;
  125. });
  126. $this->urlGenerator->method('getAbsoluteURL')
  127. ->willReturnCallback(function (string $url) {
  128. return 'https://my.nextcloud/'.$url;
  129. });
  130. $result = $this->controller->getUrl(101);
  131. $this->assertInstanceOf(DataResponse::class, $result);
  132. $this->assertSame([
  133. 'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken',
  134. ], $result->getData());
  135. }
  136. }