123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace OCA\Files_Sharing\Tests;
- use OCA\Files_Sharing\AppInfo\Application;
- use OCA\Files_Sharing\Listener\BeforeDirectFileDownloadListener;
- use OCA\Files_Sharing\Listener\BeforeZipCreatedListener;
- use OCA\Files_Sharing\SharedStorage;
- use OCP\Files\Events\BeforeDirectFileDownloadEvent;
- use OCP\Files\Events\BeforeZipCreatedEvent;
- use OCP\Files\File;
- use OCP\Files\Folder;
- use OCP\Files\IRootFolder;
- use OCP\Files\Storage\IStorage;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\Share\IAttributes;
- use OCP\Share\IShare;
- use Test\TestCase;
- class ApplicationTest extends TestCase {
- private Application $application;
-
- private $userSession;
-
- private $rootFolder;
- protected function setUp(): void {
- parent::setUp();
- $this->application = new Application([]);
- $this->userSession = $this->createMock(IUserSession::class);
- $this->rootFolder = $this->createMock(IRootFolder::class);
- }
- public function providesDataForCanGet(): array {
-
- $senderFileStorage = $this->createMock(IStorage::class);
- $senderFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
- $senderFile = $this->createMock(File::class);
- $senderFile->method('getStorage')->willReturn($senderFileStorage);
- $senderUserFolder = $this->createMock(Folder::class);
- $senderUserFolder->method('get')->willReturn($senderFile);
- $result[] = [ '/bar.txt', $senderUserFolder, true ];
-
-
- $receiverFileShareAttributes = $this->createMock(IAttributes::class);
- $receiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(true);
- $receiverFileShare = $this->createMock(IShare::class);
- $receiverFileShare->method('getAttributes')->willReturn($receiverFileShareAttributes);
- $receiverFileStorage = $this->createMock(SharedStorage::class);
- $receiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
- $receiverFileStorage->method('getShare')->willReturn($receiverFileShare);
- $receiverFile = $this->createMock(File::class);
- $receiverFile->method('getStorage')->willReturn($receiverFileStorage);
- $receiverUserFolder = $this->createMock(Folder::class);
- $receiverUserFolder->method('get')->willReturn($receiverFile);
- $result[] = [ '/share-bar.txt', $receiverUserFolder, true ];
-
-
- $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
- $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
- $secureReceiverFileShare = $this->createMock(IShare::class);
- $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
- $secureReceiverFileStorage = $this->createMock(SharedStorage::class);
- $secureReceiverFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
- $secureReceiverFileStorage->method('getShare')->willReturn($secureReceiverFileShare);
- $secureReceiverFile = $this->createMock(File::class);
- $secureReceiverFile->method('getStorage')->willReturn($secureReceiverFileStorage);
- $secureReceiverUserFolder = $this->createMock(Folder::class);
- $secureReceiverUserFolder->method('get')->willReturn($secureReceiverFile);
- $result[] = [ '/secure-share-bar.txt', $secureReceiverUserFolder, false ];
- return $result;
- }
-
- public function testCheckDirectCanBeDownloaded(string $path, Folder $userFolder, bool $run): void {
- $user = $this->createMock(IUser::class);
- $user->method('getUID')->willReturn('test');
- $this->userSession->method('getUser')->willReturn($user);
- $this->userSession->method('isLoggedIn')->willReturn(true);
- $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
-
- $event = new BeforeDirectFileDownloadEvent($path);
- $listener = new BeforeDirectFileDownloadListener(
- $this->userSession,
- $this->rootFolder
- );
- $listener->handle($event);
- $this->assertEquals($run, $event->isSuccessful());
- }
- public function providesDataForCanZip(): array {
-
- $nonSharedStorage = $this->createMock(IStorage::class);
- $nonSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
-
- $secureReceiverFileShareAttributes = $this->createMock(IAttributes::class);
- $secureReceiverFileShareAttributes->method('getAttribute')->with('permissions', 'download')->willReturn(false);
- $secureReceiverFileShare = $this->createMock(IShare::class);
- $secureReceiverFileShare->method('getAttributes')->willReturn($secureReceiverFileShareAttributes);
- $secureSharedStorage = $this->createMock(SharedStorage::class);
- $secureSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
- $secureSharedStorage->method('getShare')->willReturn($secureReceiverFileShare);
-
-
- $sender1File = $this->createMock(File::class);
- $sender1File->method('getStorage')->willReturn($nonSharedStorage);
- $sender1Folder = $this->createMock(Folder::class);
- $sender1Folder->method('getStorage')->willReturn($nonSharedStorage);
- $sender1Folder->method('getDirectoryListing')->willReturn([$sender1File, $sender1File]);
- $sender1RootFolder = $this->createMock(Folder::class);
- $sender1RootFolder->method('getStorage')->willReturn($nonSharedStorage);
- $sender1RootFolder->method('getDirectoryListing')->willReturn([$sender1Folder]);
- $sender1UserFolder = $this->createMock(Folder::class);
- $sender1UserFolder->method('get')->willReturn($sender1RootFolder);
- $return[] = [ '/folder', ['bar1.txt', 'bar2.txt'], $sender1UserFolder, true ];
- $return[] = [ '/', ['folder'], $sender1UserFolder, true ];
-
- $receiver1File = $this->createMock(File::class);
- $receiver1File->method('getStorage')->willReturn($nonSharedStorage);
- $receiver1SecureFile = $this->createMock(File::class);
- $receiver1SecureFile->method('getStorage')->willReturn($secureSharedStorage);
- $receiver1Folder = $this->createMock(Folder::class);
- $receiver1Folder->method('getStorage')->willReturn($nonSharedStorage);
- $receiver1Folder->method('getDirectoryListing')->willReturn([$receiver1File, $receiver1SecureFile]);
- $receiver1RootFolder = $this->createMock(Folder::class);
- $receiver1RootFolder->method('getStorage')->willReturn($nonSharedStorage);
- $receiver1RootFolder->method('getDirectoryListing')->willReturn([$receiver1Folder]);
- $receiver1UserFolder = $this->createMock(Folder::class);
- $receiver1UserFolder->method('get')->willReturn($receiver1RootFolder);
- $return[] = [ '/folder', ['secured-bar1.txt', 'bar2.txt'], $receiver1UserFolder, false ];
-
- $receiver2Folder = $this->createMock(Folder::class);
- $receiver2Folder->method('getStorage')->willReturn($secureSharedStorage);
- $receiver2RootFolder = $this->createMock(Folder::class);
- $receiver2RootFolder->method('getStorage')->willReturn($nonSharedStorage);
- $receiver2RootFolder->method('getDirectoryListing')->willReturn([$receiver2Folder]);
- $receiver2UserFolder = $this->createMock(Folder::class);
- $receiver2UserFolder->method('get')->willReturn($receiver2RootFolder);
- $return[] = [ '/', ['secured-folder'], $receiver2UserFolder, false ];
- return $return;
- }
-
- public function testCheckZipCanBeDownloaded(string $dir, array $files, Folder $userFolder, bool $run): void {
- $user = $this->createMock(IUser::class);
- $user->method('getUID')->willReturn('test');
- $this->userSession->method('getUser')->willReturn($user);
- $this->userSession->method('isLoggedIn')->willReturn(true);
- $this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder);
-
- $event = new BeforeZipCreatedEvent($dir, $files);
- $listener = new BeforeZipCreatedListener(
- $this->userSession,
- $this->rootFolder
- );
- $listener->handle($event);
- $this->assertEquals($run, $event->isSuccessful());
- $this->assertEquals($run, $event->getErrorMessage() === null);
- }
- public function testCheckFileUserNotFound(): void {
- $this->userSession->method('isLoggedIn')->willReturn(false);
-
- $event = new BeforeZipCreatedEvent('/test', ['test.txt']);
- $listener = new BeforeZipCreatedListener(
- $this->userSession,
- $this->rootFolder
- );
- $listener->handle($event);
-
- $this->assertTrue($event->isSuccessful());
- $this->assertEquals(null, $event->getErrorMessage());
- }
- }
|