request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock(); $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider') ->disableOriginalConstructor()->getMock(); $this->shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock(); $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler') ->disableOriginalConstructor()->getMock(); $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock(); $this->userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock(); $this->share = new Share($this->rootFolder, $this->userManager); $this->session = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock(); $this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock(); $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock(); $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock(); $this->contactsManager = $this->createMock(IContactsManager::class); $this->cloudIdManager = new CloudIdManager( $this->contactsManager, $this->createMock(IURLGenerator::class), $this->userManager, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class) ); $this->controller = new MountPublicLinkController( 'federatedfilesharing', $this->request, $this->federatedShareProvider, $this->shareManager, $this->addressHandler, $this->session, $this->l10n, $this->userSession, $this->clientService, $this->cloudIdManager, $this->createMock(LoggerInterface::class), ); } /** * @dataProvider dataTestCreateFederatedShare * * @param string $shareWith * @param bool $outgoingSharesAllowed * @param bool $validShareWith * @param string $token * @param bool $validToken * @param bool $createSuccessful * @param string $expectedReturnData */ public function testCreateFederatedShare($shareWith, $outgoingSharesAllowed, $validShareWith, $token, $validToken, $createSuccessful, $expectedReturnData, $permissions, ): void { $this->federatedShareProvider->expects($this->any()) ->method('isOutgoingServer2serverShareEnabled') ->willReturn($outgoingSharesAllowed); $this->addressHandler->expects($this->any())->method('splitUserRemote') ->with($shareWith) ->willReturnCallback( function ($shareWith) use ($validShareWith, $expectedReturnData) { if ($validShareWith) { return ['user', 'server']; } throw new HintException($expectedReturnData, $expectedReturnData); } ); $share = $this->share; $share->setPermissions($permissions); $this->shareManager->expects($this->any())->method('getShareByToken') ->with($token) ->willReturnCallback( function ($token) use ($validToken, $share, $expectedReturnData) { if ($validToken) { return $share; } throw new HintException($expectedReturnData, $expectedReturnData); } ); $this->federatedShareProvider->expects($this->any())->method('create') ->with($share) ->willReturnCallback( function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) { $this->assertEquals($shareWith, $share->getSharedWith()); if ($createSuccessful) { return $share; } throw new HintException($expectedReturnData, $expectedReturnData); } ); $result = $this->controller->createFederatedShare($shareWith, $token); $errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed; if ($errorCase) { $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus()); $this->assertTrue(isset($result->getData()['message'])); $this->assertSame($expectedReturnData, $result->getData()['message']); } else { $this->assertSame(Http::STATUS_OK, $result->getStatus()); $this->assertTrue(isset($result->getData()['remoteUrl'])); $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']); } } public function dataTestCreateFederatedShare() { return [ //shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData ['user@server', true, true, 'token', true, true, 'server', 31], ['user@server', true, true, 'token', false, false, 'server', 4], ['user@server', true, false, 'token', true, true, 'invalid federated cloud id', 31], ['user@server', true, false, 'token', false, true, 'invalid federated cloud id', 31], ['user@server', true, false, 'token', false, false, 'invalid federated cloud id', 31], ['user@server', true, false, 'token', true, false, 'invalid federated cloud id', 31], ['user@server', true, true, 'token', false, true, 'invalid token', 31], ['user@server', true, true, 'token', false, false, 'invalid token', 31], ['user@server', true, true, 'token', true, false, 'can not create share', 31], ['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares', 31], ]; } }