1
0

DirectControllerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\DAV\Controller;
  8. use OCA\DAV\Controller\DirectController;
  9. use OCA\DAV\Db\Direct;
  10. use OCA\DAV\Db\DirectMapper;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\OCS\OCSBadRequestException;
  13. use OCP\AppFramework\OCS\OCSNotFoundException;
  14. use OCP\AppFramework\Utility\ITimeFactory;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\Files\File;
  17. use OCP\Files\Folder;
  18. use OCP\Files\IRootFolder;
  19. use OCP\IRequest;
  20. use OCP\IUrlGenerator;
  21. use OCP\Security\ISecureRandom;
  22. use Test\TestCase;
  23. class DirectControllerTest extends TestCase {
  24. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  25. private $rootFolder;
  26. /** @var DirectMapper|\PHPUnit\Framework\MockObject\MockObject */
  27. private $directMapper;
  28. /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */
  29. private $random;
  30. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  31. private $timeFactory;
  32. /** @var IUrlGenerator|\PHPUnit\Framework\MockObject\MockObject */
  33. private $urlGenerator;
  34. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  35. private $eventDispatcher;
  36. private DirectController $controller;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->rootFolder = $this->createMock(IRootFolder::class);
  40. $this->directMapper = $this->createMock(DirectMapper::class);
  41. $this->random = $this->createMock(ISecureRandom::class);
  42. $this->timeFactory = $this->createMock(ITimeFactory::class);
  43. $this->urlGenerator = $this->createMock(IUrlGenerator::class);
  44. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  45. $this->controller = new DirectController(
  46. 'dav',
  47. $this->createMock(IRequest::class),
  48. $this->rootFolder,
  49. 'awesomeUser',
  50. $this->directMapper,
  51. $this->random,
  52. $this->timeFactory,
  53. $this->urlGenerator,
  54. $this->eventDispatcher
  55. );
  56. }
  57. public function testGetUrlNonExistingFileId(): void {
  58. $userFolder = $this->createMock(Folder::class);
  59. $this->rootFolder->method('getUserFolder')
  60. ->with('awesomeUser')
  61. ->willReturn($userFolder);
  62. $userFolder->method('getById')
  63. ->with(101)
  64. ->willReturn([]);
  65. $this->expectException(OCSNotFoundException::class);
  66. $this->controller->getUrl(101);
  67. }
  68. public function testGetUrlForFolder(): void {
  69. $userFolder = $this->createMock(Folder::class);
  70. $this->rootFolder->method('getUserFolder')
  71. ->with('awesomeUser')
  72. ->willReturn($userFolder);
  73. $folder = $this->createMock(Folder::class);
  74. $userFolder->method('getFirstNodeById')
  75. ->with(101)
  76. ->willReturn($folder);
  77. $this->expectException(OCSBadRequestException::class);
  78. $this->controller->getUrl(101);
  79. }
  80. public function testGetUrlValid(): void {
  81. $userFolder = $this->createMock(Folder::class);
  82. $this->rootFolder->method('getUserFolder')
  83. ->with('awesomeUser')
  84. ->willReturn($userFolder);
  85. $file = $this->createMock(File::class);
  86. $this->timeFactory->method('getTime')
  87. ->willReturn(42);
  88. $userFolder->method('getFirstNodeById')
  89. ->with(101)
  90. ->willReturn($file);
  91. $userFolder->method('getRelativePath')
  92. ->willReturn('/path');
  93. $this->random->method('generate')
  94. ->with(
  95. 60,
  96. ISecureRandom::CHAR_ALPHANUMERIC
  97. )->willReturn('superduperlongtoken');
  98. $this->directMapper->expects($this->once())
  99. ->method('insert')
  100. ->willReturnCallback(function (Direct $direct) {
  101. $this->assertSame('awesomeUser', $direct->getUserId());
  102. $this->assertSame(101, $direct->getFileId());
  103. $this->assertSame('superduperlongtoken', $direct->getToken());
  104. $this->assertSame(42 + 60 * 60 * 8, $direct->getExpiration());
  105. return $direct;
  106. });
  107. $this->urlGenerator->method('getAbsoluteURL')
  108. ->willReturnCallback(function (string $url) {
  109. return 'https://my.nextcloud/' . $url;
  110. });
  111. $result = $this->controller->getUrl(101);
  112. $this->assertInstanceOf(DataResponse::class, $result);
  113. $this->assertSame([
  114. 'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken',
  115. ], $result->getData());
  116. }
  117. }