ApplicationTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\LegacyHooks;
  28. use OC\Share20\Manager;
  29. use OC\EventDispatcher\EventDispatcher;
  30. use OCA\Files_Sharing\AppInfo\Application;
  31. use OCA\Files_Sharing\SharedStorage;
  32. use OCP\Constants;
  33. use OCP\EventDispatcher\GenericEvent;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\Files\Cache\ICacheEntry;
  36. use OCP\Files\Event\BeforeDirectGetEvent;
  37. use OCP\Files\File;
  38. use OCP\Files\Folder;
  39. use OCP\Files\IRootFolder;
  40. use OCP\Files\Storage\IStorage;
  41. use OCP\IServerContainer;
  42. use OCP\IUser;
  43. use OCP\IUserSession;
  44. use OCP\Share\IAttributes;
  45. use OCP\Share\IShare;
  46. use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher;
  47. use Test\TestCase;
  48. class ApplicationTest extends TestCase {
  49. private Application $application;
  50. private IEventDispatcher $eventDispatcher;
  51. /** @var IUserSession */
  52. private $userSession;
  53. /** @var IRootFolder */
  54. private $rootFolder;
  55. /** @var Manager */ private $manager;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->application = new Application([]);
  59. $symfonyDispatcher = new SymfonyDispatcher();
  60. $this->eventDispatcher = new EventDispatcher(
  61. $symfonyDispatcher,
  62. $this->createMock(IServerContainer::class),
  63. $this->createMock(LoggerInterface::class)
  64. );
  65. $this->userSession = $this->createMock(IUserSession::class);
  66. $this->rootFolder = $this->createMock(IRootFolder::class);
  67. $this->application->registerDownloadEvents(
  68. $this->eventDispatcher,
  69. $this->userSession,
  70. $this->rootFolder
  71. );
  72. }
  73. public function providesDataForCanGet(): array {
  74. // normal file (sender) - can download directly
  75. $senderFileStorage = $this->createMock(IStorage::class);
  76. $senderFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
  77. $senderFile = $this->createMock(File::class);
  78. $senderFile->method('getStorage')->willReturn($senderFileStorage);
  79. $senderUserFolder = $this->createMock(Folder::class);
  80. $senderUserFolder->method('get')->willReturn($senderFile);
  81. $result[] = [ '/bar.txt', $senderUserFolder, true ];
  82. // shared file (receiver) with attribute secure-view-enabled set false -
  83. // can download directly
  84. $receiverFileShareAttributes = $this->createMock(IAttributes::class);
  85. $receiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true);
  86. $receiverFileShare = $this->createMock(IShare::class);
  87. $receiverFileShare->method('getAttributes')->willReturn($receiverFileShareAttributes);
  88. $receiverFileStorage = $this->createMock(SharedStorage::class);
  89. $receiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  90. $receiverFileStorage->method('getShare')->willReturn($receiverFileShare);
  91. $receiverFile = $this->createMock(File::class);
  92. $receiverFile->method('getStorage')->willReturn($receiverFileStorage);
  93. $receiverUserFolder = $this->createMock(Folder::class);
  94. $receiverUserFolder->method('get')->willReturn($receiverFile);
  95. $result[] = [ '/share-bar.txt', $receiverUserFolder, true ];
  96. // shared file (receiver) with attribute secure-view-enabled set true -
  97. // cannot download directly
  98. $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
  99. $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
  100. $secureReceiverFileShare = $this->createMock(IShare::class);
  101. $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
  102. $secureReceiverFileStorage = $this->createMock(SharedStorage::class);
  103. $secureReceiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  104. $secureReceiverFileStorage->method('getShare')->willReturn($secureReceiverFileShare);
  105. $secureReceiverFile = $this->createMock(File::class);
  106. $secureReceiverFile->method('getStorage')->willReturn($secureReceiverFileStorage);
  107. $secureReceiverUserFolder = $this->createMock(Folder::class);
  108. $secureReceiverUserFolder->method('get')->willReturn($secureReceiverFile);
  109. $result[] = [ '/secure-share-bar.txt', $secureReceiverUserFolder, false ];
  110. return $result;
  111. }
  112. /**
  113. * @dataProvider providesDataForCanGet
  114. */
  115. public function testCheckDirectCanBeDownloaded(string $path, Folder $userFolder, bool $run): void {
  116. $user = $this->createMock(IUser::class);
  117. $user->method('getUID')->willReturn('test');
  118. $this->userSession->method('getUser')->willReturn($user);
  119. $this->userSession->method('isLoggedIn')->willReturn(true);
  120. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  121. // Simulate direct download of file
  122. $event = new BeforeDirectFileDownloadEvent($path);
  123. $this->eventDispatcher->dispatchTyped($event);
  124. $this->assertEquals($run, $event->isSuccessful());
  125. }
  126. public function providesDataForCanZip(): array {
  127. // Mock: Normal file/folder storage
  128. $nonSharedStorage = $this->createMock(IStorage::class);
  129. $nonSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
  130. // Mock: Secure-view file/folder shared storage
  131. $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
  132. $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
  133. $secureReceiverFileShare = $this->createMock(IShare::class);
  134. $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
  135. $secureSharedStorage = $this->createMock(SharedStorage::class);
  136. $secureSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
  137. $secureSharedStorage->method('getShare')->willReturn($secureReceiverFileShare);
  138. // 1. can download zipped 2 non-shared files inside non-shared folder
  139. // 2. can download zipped non-shared folder
  140. $sender1File = $this->createMock(File::class);
  141. $sender1File->method('getStorage')->willReturn($nonSharedStorage);
  142. $sender1Folder = $this->createMock(Folder::class);
  143. $sender1Folder->method('getStorage')->willReturn($nonSharedStorage);
  144. $sender1Folder->method('getDirectoryListing')->willReturn([$sender1File, $sender1File]);
  145. $sender1RootFolder = $this->createMock(Folder::class);
  146. $sender1RootFolder->method('getStorage')->willReturn($nonSharedStorage);
  147. $sender1RootFolder->method('getDirectoryListing')->willReturn([$sender1Folder]);
  148. $sender1UserFolder = $this->createMock(Folder::class);
  149. $sender1UserFolder->method('get')->willReturn($sender1RootFolder);
  150. $return[] = [ '/folder', ['bar1.txt', 'bar2.txt'], $sender1UserFolder, true ];
  151. $return[] = [ '/', ['folder'], $sender1UserFolder, true ];
  152. // 3. cannot download zipped 1 non-shared file and 1 secure-shared inside non-shared folder
  153. $receiver1File = $this->createMock(File::class);
  154. $receiver1File->method('getStorage')->willReturn($nonSharedStorage);
  155. $receiver1SecureFile = $this->createMock(File::class);
  156. $receiver1SecureFile->method('getStorage')->willReturn($secureSharedStorage);
  157. $receiver1Folder = $this->createMock(Folder::class);
  158. $receiver1Folder->method('getStorage')->willReturn($nonSharedStorage);
  159. $receiver1Folder->method('getDirectoryListing')->willReturn([$receiver1File, $receiver1SecureFile]);
  160. $receiver1RootFolder = $this->createMock(Folder::class);
  161. $receiver1RootFolder->method('getStorage')->willReturn($nonSharedStorage);
  162. $receiver1RootFolder->method('getDirectoryListing')->willReturn([$receiver1Folder]);
  163. $receiver1UserFolder = $this->createMock(Folder::class);
  164. $receiver1UserFolder->method('get')->willReturn($receiver1RootFolder);
  165. $return[] = [ '/folder', ['secured-bar1.txt', 'bar2.txt'], $receiver1UserFolder, false ];
  166. // 4. cannot download zipped secure-shared folder
  167. $receiver2Folder = $this->createMock(Folder::class);
  168. $receiver2Folder->method('getStorage')->willReturn($secureSharedStorage);
  169. $receiver2RootFolder = $this->createMock(Folder::class);
  170. $receiver2RootFolder->method('getStorage')->willReturn($nonSharedStorage);
  171. $receiver2RootFolder->method('getDirectoryListing')->willReturn([$receiver2Folder]);
  172. $receiver2UserFolder = $this->createMock(Folder::class);
  173. $receiver2UserFolder->method('get')->willReturn($receiver2RootFolder);
  174. $return[] = [ '/', ['secured-folder'], $receiver2UserFolder, false ];
  175. return $return;
  176. }
  177. /**
  178. * @dataProvider providesDataForCanZip
  179. */
  180. public function testCheckZipCanBeDownloaded(string $dir, array $files, Folder $userFolder, bool $run): void {
  181. $user = $this->createMock(IUser::class);
  182. $user->method('getUID')->willReturn('test');
  183. $this->userSession->method('getUser')->willReturn($user);
  184. $this->userSession->method('isLoggedIn')->willReturn(true);
  185. $this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder);
  186. // Simulate zip download of folder folder
  187. $event = new BeforeZipCreatedEvent($dir, $files);
  188. $this->eventDispatcher->dispatchTyped($event);
  189. $this->assertEquals($run, $event->isSuccessful());
  190. $this->assertEquals($run, $event->getErrorMessage() === null);
  191. }
  192. public function testCheckFileUserNotFound(): void {
  193. $this->userSession->method('isLoggedIn')->willReturn(false);
  194. // Simulate zip download of folder folder
  195. $event = new BeforeZipCreatedEvent('/test', ['test.txt']);
  196. $this->eventDispatcher->dispatchTyped($event);
  197. // It should run as this would restrict e.g. share links otherwise
  198. $this->assertTrue($event->isSuccessful());
  199. $this->assertEquals(null, $event->getErrorMessage());
  200. }
  201. }