@ -6,6 +6,7 @@
*/
namespace OCA\Files_Sharing\Tests\Controller;
use OCA\Federation\TrustedServers;
use OCA\Files_Sharing\Controller\ShareAPIController;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\DataResponse;
@ -28,6 +29,7 @@ use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IPreview;
use OCP\IRequest;
use OCP\ITagManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
@ -75,6 +77,8 @@ class ShareAPIControllerTest extends TestCase {
private LoggerInterface& MockObject $logger;
private IProviderFactory& MockObject $factory;
private IMailer& MockObject $mailer;
private ITagManager& MockObject $tagManager;
private TrustedServers& MockObject $trustedServers;
protected function setUp(): void {
$this->shareManager = $this->createMock(IManager::class);
@ -110,6 +114,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger = $this->createMock(LoggerInterface::class);
$this->factory = $this->createMock(IProviderFactory::class);
$this->mailer = $this->createMock(IMailer::class);
$this->tagManager = $this->createMock(ITagManager::class);
$this->trustedServers = $this->createMock(TrustedServers::class);
$this->ocs = new ShareAPIController(
$this->appName,
@ -129,8 +135,11 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->currentUser,
$this->tagManager,
$this->trustedServers,
$this->currentUser
);
}
/**
@ -156,6 +165,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])->onlyMethods(['formatShare'])
->getMock();
@ -840,6 +851,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])
@ -1473,6 +1486,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])
->onlyMethods(['formatShare'])
@ -1815,6 +1830,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])->setMethods(['formatShare'])
->getMock();
@ -1912,6 +1929,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])->setMethods(['formatShare'])
->getMock();
@ -2337,6 +2356,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])->setMethods(['formatShare'])
->getMock();
@ -2407,6 +2428,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])->setMethods(['formatShare'])
->getMock();
@ -2638,6 +2661,8 @@ class ShareAPIControllerTest extends TestCase {
$this->logger,
$this->factory,
$this->mailer,
$this->tagManager,
$this->trustedServers,
$this->currentUser,
])->setMethods(['formatShare'])
->getMock();
@ -4430,6 +4455,7 @@ class ShareAPIControllerTest extends TestCase {
'mount-type' => '',
'attributes' => null,
'item_permissions' => 1,
'is_trusted_server' => false,
], $share, [], false
];
@ -4483,6 +4509,7 @@ class ShareAPIControllerTest extends TestCase {
'mount-type' => '',
'attributes' => null,
'item_permissions' => 1,
'is_trusted_server' => false,
], $share, [], false
];
@ -5137,4 +5164,139 @@ class ShareAPIControllerTest extends TestCase {
$node->method('getId')->willReturn(42);
return [$userFolder, $node];
}
public function trustedServerProvider(): array {
return [
'Trusted server' => [true, true],
'Untrusted server' => [false, false],
];
}
/**
* @dataProvider trustedServerProvider
*/
public function testFormatShareWithFederatedShare(bool $isKnownServer, bool $isTrusted): void {
$nodeId = 12;
$nodePath = '/test.txt';
$share = $this->createShare(
1,
IShare::TYPE_REMOTE,
'recipient@remoteserver.com', // shared with
'sender@testserver.com', // shared by
'shareOwner', // share owner
$nodePath, // path
Constants::PERMISSION_READ,
time(),
null,
null,
$nodePath,
$nodeId
);
$node = $this->createMock(\OCP\Files\File::class);
$node->method('getId')->willReturn($nodeId);
$node->method('getPath')->willReturn($nodePath);
$node->method('getInternalPath')->willReturn(ltrim($nodePath, '/'));
$mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class);
$mountPoint->method('getMountType')->willReturn('local');
$node->method('getMountPoint')->willReturn($mountPoint);
$node->method('getMimetype')->willReturn('text/plain');
$storage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$storageCache = $this->createMock(\OCP\Files\Cache\ICache::class);
$storageCache->method('getNumericStorageId')->willReturn(1);
$storage->method('getCache')->willReturn($storageCache);
$storage->method('getId')->willReturn('home::shareOwner');
$node->method('getStorage')->willReturn($storage);
$parent = $this->createMock(\OCP\Files\Folder::class);
$parent->method('getId')->willReturn(2);
$node->method('getParent')->willReturn($parent);
$node->method('getSize')->willReturn(1234);
$node->method('getMTime')->willReturn(1234567890);
$this->previewManager->method('isAvailable')->with($node)->willReturn(false);
$this->rootFolder->method('getUserFolder')
->with($this->currentUser)
->willReturnSelf();
$this->rootFolder->method('getFirstNodeById')
->with($share->getNodeId())
->willReturn($node);
$this->rootFolder->method('getRelativePath')
->with($node->getPath())
->willReturnArgument(0);
$serverName = 'remoteserver.com';
$this->trustedServers->method('isTrustedServer')
->with($serverName)
->willReturn($isKnownServer);
$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
$this->assertSame($isTrusted, $result['is_trusted_server']);
}
public function testFormatShareWithFederatedShareWithAtInUsername(): void {
$nodeId = 12;
$nodePath = '/test.txt';
$share = $this->createShare(
1,
IShare::TYPE_REMOTE,
'recipient@domain.com@remoteserver.com',
'sender@testserver.com',
'shareOwner',
$nodePath,
Constants::PERMISSION_READ,
time(),
null,
null,
$nodePath,
$nodeId
);
$node = $this->createMock(\OCP\Files\File::class);
$node->method('getId')->willReturn($nodeId);
$node->method('getPath')->willReturn($nodePath);
$node->method('getInternalPath')->willReturn(ltrim($nodePath, '/'));
$mountPoint = $this->createMock(\OCP\Files\Mount\IMountPoint::class);
$mountPoint->method('getMountType')->willReturn('local');
$node->method('getMountPoint')->willReturn($mountPoint);
$node->method('getMimetype')->willReturn('text/plain');
$storage = $this->createMock(\OCP\Files\Storage\IStorage::class);
$storageCache = $this->createMock(\OCP\Files\Cache\ICache::class);
$storageCache->method('getNumericStorageId')->willReturn(1);
$storage->method('getCache')->willReturn($storageCache);
$storage->method('getId')->willReturn('home::shareOwner');
$node->method('getStorage')->willReturn($storage);
$parent = $this->createMock(\OCP\Files\Folder::class);
$parent->method('getId')->willReturn(2);
$node->method('getParent')->willReturn($parent);
$node->method('getSize')->willReturn(1234);
$node->method('getMTime')->willReturn(1234567890);
$this->previewManager->method('isAvailable')->with($node)->willReturn(false);
$this->rootFolder->method('getUserFolder')
->with($this->currentUser)
->willReturnSelf();
$this->rootFolder->method('getFirstNodeById')
->with($share->getNodeId())
->willReturn($node);
$this->rootFolder->method('getRelativePath')
->with($node->getPath())
->willReturnArgument(0);
$serverName = 'remoteserver.com';
$this->trustedServers->method('isTrustedServer')
->with($serverName)
->willReturn(true);
$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
$this->assertTrue($result['is_trusted_server']);
}
}