DirectControllerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Tests\Unit\DAV\Controller;
  26. use OCA\DAV\Controller\DirectController;
  27. use OCA\DAV\Db\Direct;
  28. use OCA\DAV\Db\DirectMapper;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCS\OCSBadRequestException;
  31. use OCP\AppFramework\OCS\OCSNotFoundException;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\Files\File;
  34. use OCP\Files\Folder;
  35. use OCP\Files\IRootFolder;
  36. use OCP\IRequest;
  37. use OCP\IURLGenerator;
  38. use OCP\Security\ISecureRandom;
  39. use Test\TestCase;
  40. class DirectControllerTest extends TestCase {
  41. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  42. private $rootFolder;
  43. /** @var DirectMapper|\PHPUnit_Framework_MockObject_MockObject */
  44. private $directMapper;
  45. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  46. private $random;
  47. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  48. private $timeFactory;
  49. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  50. private $urlGenerator;
  51. /** @var DirectController */
  52. private $controller;
  53. protected function setUp(): void {
  54. parent::setUp();
  55. $this->rootFolder = $this->createMock(IRootFolder::class);
  56. $this->directMapper = $this->createMock(DirectMapper::class);
  57. $this->random = $this->createMock(ISecureRandom::class);
  58. $this->timeFactory = $this->createMock(ITimeFactory::class);
  59. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  60. $this->controller = new DirectController(
  61. 'dav',
  62. $this->createMock(IRequest::class),
  63. $this->rootFolder,
  64. 'awesomeUser',
  65. $this->directMapper,
  66. $this->random,
  67. $this->timeFactory,
  68. $this->urlGenerator
  69. );
  70. }
  71. public function testGetUrlNonExistingFileId() {
  72. $userFolder = $this->createMock(Folder::class);
  73. $this->rootFolder->method('getUserFolder')
  74. ->with('awesomeUser')
  75. ->willReturn($userFolder);
  76. $userFolder->method('getById')
  77. ->with(101)
  78. ->willReturn([]);
  79. $this->expectException(OCSNotFoundException::class);
  80. $this->controller->getUrl(101);
  81. }
  82. public function testGetUrlForFolder() {
  83. $userFolder = $this->createMock(Folder::class);
  84. $this->rootFolder->method('getUserFolder')
  85. ->with('awesomeUser')
  86. ->willReturn($userFolder);
  87. $folder = $this->createMock(Folder::class);
  88. $userFolder->method('getById')
  89. ->with(101)
  90. ->willReturn([$folder]);
  91. $this->expectException(OCSBadRequestException::class);
  92. $this->controller->getUrl(101);
  93. }
  94. public function testGetUrlValid() {
  95. $userFolder = $this->createMock(Folder::class);
  96. $this->rootFolder->method('getUserFolder')
  97. ->with('awesomeUser')
  98. ->willReturn($userFolder);
  99. $file = $this->createMock(File::class);
  100. $this->timeFactory->method('getTime')
  101. ->willReturn(42);
  102. $userFolder->method('getById')
  103. ->with(101)
  104. ->willReturn([$file]);
  105. $this->random->method('generate')
  106. ->with(
  107. 60,
  108. ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS
  109. )->willReturn('superduperlongtoken');
  110. $this->directMapper->expects($this->once())
  111. ->method('insert')
  112. ->willReturnCallback(function (Direct $direct) {
  113. $this->assertSame('awesomeUser', $direct->getUserId());
  114. $this->assertSame(101, $direct->getFileId());
  115. $this->assertSame('superduperlongtoken', $direct->getToken());
  116. $this->assertSame(42 + 60*60*8, $direct->getExpiration());
  117. });
  118. $this->urlGenerator->method('getAbsoluteURL')
  119. ->willReturnCallback(function (string $url) {
  120. return 'https://my.nextcloud/'.$url;
  121. });
  122. $result = $this->controller->getUrl(101);
  123. $this->assertInstanceOf(DataResponse::class, $result);
  124. $this->assertSame([
  125. 'url' => 'https://my.nextcloud/remote.php/direct/superduperlongtoken',
  126. ], $result->getData());
  127. }
  128. }