ApplicationTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @copyright 2022, Vincent Petry <vincent@nextcloud.com>
  4. *
  5. * @author Vincent Petry <vincent@nextcloud.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files_Sharing\Tests;
  24. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  25. use OCP\Files\Events\BeforeZipCreatedEvent;
  26. use Psr\Log\LoggerInterface;
  27. use OC\Share20\Manager;
  28. use OC\EventDispatcher\EventDispatcher;
  29. use OCA\Files_Sharing\AppInfo\Application;
  30. use OCA\Files_Sharing\SharedStorage;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Storage\IStorage;
  36. use OCP\IServerContainer;
  37. use OCP\IUser;
  38. use OCP\IUserSession;
  39. use OCP\Share\IAttributes;
  40. use OCP\Share\IShare;
  41. use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
  42. use Test\TestCase;
  43. class ApplicationTest extends TestCase {
  44. private Application $application;
  45. private IEventDispatcher $eventDispatcher;
  46. /** @var IUserSession */
  47. private $userSession;
  48. /** @var IRootFolder */
  49. private $rootFolder;
  50. /** @var Manager */
  51. private $manager;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->application = new Application([]);
  55. $symfonyDispatcher = new SymfonyDispatcher();
  56. $this->eventDispatcher = new EventDispatcher(
  57. $symfonyDispatcher,
  58. $this->createMock(IServerContainer::class),
  59. $this->createMock(LoggerInterface::class)
  60. );
  61. $this->userSession = $this->createMock(IUserSession::class);
  62. $this->rootFolder = $this->createMock(IRootFolder::class);
  63. $this->application->registerDownloadEvents(
  64. $this->eventDispatcher,
  65. $this->userSession,
  66. $this->rootFolder
  67. );
  68. }
  69. public function providesDataForCanGet(): array {
  70. // normal file (sender) - can download directly
  71. $senderFileStorage = $this->createMock(IStorage::class);
  72. $senderFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
  73. $senderFile = $this->createMock(File::class);
  74. $senderFile->method('getStorage')->willReturn($senderFileStorage);
  75. $senderUserFolder = $this->createMock(Folder::class);
  76. $senderUserFolder->method('get')->willReturn($senderFile);
  77. $result[] = [ '/bar.txt', $senderUserFolder, true ];
  78. // shared file (receiver) with attribute secure-view-enabled set false -
  79. // can download directly
  80. $receiverFileShareAttributes = $this->createMock(IAttributes::class);
  81. $receiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true);
  82. $receiverFileShare = $this->createMock(IShare::class);
  83. $receiverFileShare->method('getAttributes')->willReturn($receiverFileShareAttributes);
  84. $receiverFileStorage = $this->createMock(SharedStorage::class);
  85. $receiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  86. $receiverFileStorage->method('getShare')->willReturn($receiverFileShare);
  87. $receiverFile = $this->createMock(File::class);
  88. $receiverFile->method('getStorage')->willReturn($receiverFileStorage);
  89. $receiverUserFolder = $this->createMock(Folder::class);
  90. $receiverUserFolder->method('get')->willReturn($receiverFile);
  91. $result[] = [ '/share-bar.txt', $receiverUserFolder, true ];
  92. // shared file (receiver) with attribute secure-view-enabled set true -
  93. // cannot download directly
  94. $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
  95. $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
  96. $secureReceiverFileShare = $this->createMock(IShare::class);
  97. $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
  98. $secureReceiverFileStorage = $this->createMock(SharedStorage::class);
  99. $secureReceiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  100. $secureReceiverFileStorage->method('getShare')->willReturn($secureReceiverFileShare);
  101. $secureReceiverFile = $this->createMock(File::class);
  102. $secureReceiverFile->method('getStorage')->willReturn($secureReceiverFileStorage);
  103. $secureReceiverUserFolder = $this->createMock(Folder::class);
  104. $secureReceiverUserFolder->method('get')->willReturn($secureReceiverFile);
  105. $result[] = [ '/secure-share-bar.txt', $secureReceiverUserFolder, false ];
  106. return $result;
  107. }
  108. /**
  109. * @dataProvider providesDataForCanGet
  110. */
  111. public function testCheckDirectCanBeDownloaded(string $path, Folder $userFolder, bool $run): void {
  112. $user = $this->createMock(IUser::class);
  113. $user->method('getUID')->willReturn('test');
  114. $this->userSession->method('getUser')->willReturn($user);
  115. $this->userSession->method('isLoggedIn')->willReturn(true);
  116. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  117. // Simulate direct download of file
  118. $event = new BeforeDirectFileDownloadEvent($path);
  119. $this->eventDispatcher->dispatchTyped($event);
  120. $this->assertEquals($run, $event->isSuccessful());
  121. }
  122. public function providesDataForCanZip(): array {
  123. // Mock: Normal file/folder storage
  124. $nonSharedStorage = $this->createMock(IStorage::class);
  125. $nonSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
  126. // Mock: Secure-view file/folder shared storage
  127. $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
  128. $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
  129. $secureReceiverFileShare = $this->createMock(IShare::class);
  130. $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
  131. $secureSharedStorage = $this->createMock(SharedStorage::class);
  132. $secureSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  133. $secureSharedStorage->method('getShare')->willReturn($secureReceiverFileShare);
  134. // 1. can download zipped 2 non-shared files inside non-shared folder
  135. // 2. can download zipped non-shared folder
  136. $sender1File = $this->createMock(File::class);
  137. $sender1File->method('getStorage')->willReturn($nonSharedStorage);
  138. $sender1Folder = $this->createMock(Folder::class);
  139. $sender1Folder->method('getStorage')->willReturn($nonSharedStorage);
  140. $sender1Folder->method('getDirectoryListing')->willReturn([$sender1File, $sender1File]);
  141. $sender1RootFolder = $this->createMock(Folder::class);
  142. $sender1RootFolder->method('getStorage')->willReturn($nonSharedStorage);
  143. $sender1RootFolder->method('getDirectoryListing')->willReturn([$sender1Folder]);
  144. $sender1UserFolder = $this->createMock(Folder::class);
  145. $sender1UserFolder->method('get')->willReturn($sender1RootFolder);
  146. $return[] = [ '/folder', ['bar1.txt', 'bar2.txt'], $sender1UserFolder, true ];
  147. $return[] = [ '/', ['folder'], $sender1UserFolder, true ];
  148. // 3. cannot download zipped 1 non-shared file and 1 secure-shared inside non-shared folder
  149. $receiver1File = $this->createMock(File::class);
  150. $receiver1File->method('getStorage')->willReturn($nonSharedStorage);
  151. $receiver1SecureFile = $this->createMock(File::class);
  152. $receiver1SecureFile->method('getStorage')->willReturn($secureSharedStorage);
  153. $receiver1Folder = $this->createMock(Folder::class);
  154. $receiver1Folder->method('getStorage')->willReturn($nonSharedStorage);
  155. $receiver1Folder->method('getDirectoryListing')->willReturn([$receiver1File, $receiver1SecureFile]);
  156. $receiver1RootFolder = $this->createMock(Folder::class);
  157. $receiver1RootFolder->method('getStorage')->willReturn($nonSharedStorage);
  158. $receiver1RootFolder->method('getDirectoryListing')->willReturn([$receiver1Folder]);
  159. $receiver1UserFolder = $this->createMock(Folder::class);
  160. $receiver1UserFolder->method('get')->willReturn($receiver1RootFolder);
  161. $return[] = [ '/folder', ['secured-bar1.txt', 'bar2.txt'], $receiver1UserFolder, false ];
  162. // 4. cannot download zipped secure-shared folder
  163. $receiver2Folder = $this->createMock(Folder::class);
  164. $receiver2Folder->method('getStorage')->willReturn($secureSharedStorage);
  165. $receiver2RootFolder = $this->createMock(Folder::class);
  166. $receiver2RootFolder->method('getStorage')->willReturn($nonSharedStorage);
  167. $receiver2RootFolder->method('getDirectoryListing')->willReturn([$receiver2Folder]);
  168. $receiver2UserFolder = $this->createMock(Folder::class);
  169. $receiver2UserFolder->method('get')->willReturn($receiver2RootFolder);
  170. $return[] = [ '/', ['secured-folder'], $receiver2UserFolder, false ];
  171. return $return;
  172. }
  173. /**
  174. * @dataProvider providesDataForCanZip
  175. */
  176. public function testCheckZipCanBeDownloaded(string $dir, array $files, Folder $userFolder, bool $run): void {
  177. $user = $this->createMock(IUser::class);
  178. $user->method('getUID')->willReturn('test');
  179. $this->userSession->method('getUser')->willReturn($user);
  180. $this->userSession->method('isLoggedIn')->willReturn(true);
  181. $this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder);
  182. // Simulate zip download of folder folder
  183. $event = new BeforeZipCreatedEvent($dir, $files);
  184. $this->eventDispatcher->dispatchTyped($event);
  185. $this->assertEquals($run, $event->isSuccessful());
  186. $this->assertEquals($run, $event->getErrorMessage() === null);
  187. }
  188. public function testCheckFileUserNotFound(): void {
  189. $this->userSession->method('isLoggedIn')->willReturn(false);
  190. // Simulate zip download of folder folder
  191. $event = new BeforeZipCreatedEvent('/test', ['test.txt']);
  192. $this->eventDispatcher->dispatchTyped($event);
  193. // It should run as this would restrict e.g. share links otherwise
  194. $this->assertTrue($event->isSuccessful());
  195. $this->assertEquals(null, $event->getErrorMessage());
  196. }
  197. }