update tests

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2018-06-11 15:30:04 +02:00
parent 5d0d33a0d4
commit 9d145bb5dd
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
1 changed files with 126 additions and 266 deletions

View File

@ -29,18 +29,28 @@
namespace OCA\FederatedFileSharing\Tests;
use OC\AppFramework\Http;
use OC\Federation\CloudIdManager;
use OC\Files\Filesystem;
use OCA\FederatedFileSharing\FederatedShareProvider;
use OCA\FederatedFileSharing\Controller\RequestHandlerController;
use OCP\AppFramework\Http\DataResponse;
use OCP\Federation\ICloudFederationFactory;
use OCP\Federation\ICloudFederationProvider;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudFederationShare;
use OCP\Federation\ICloudIdManager;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Share;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
/**
* Class RequestHandlerTest
@ -48,19 +58,17 @@ use OCP\Share\IShare;
* @package OCA\FederatedFileSharing\Tests
* @group DB
*/
class RequestHandlerControllerTest extends TestCase {
class RequestHandlerControllerTest extends \Test\TestCase {
const TEST_FOLDER_NAME = '/folder_share_api_test';
private $owner = 'owner';
private $user1 = 'user1';
private $user2 = 'user2';
private $ownerCloudId = 'owner@server0.org';
private $user1CloudId = 'user1@server1.org';
private $user2CloudId = 'user2@server2.org';
/**
* @var \OCP\IDBConnection
*/
private $connection;
/**
* @var RequestHandlerController
*/
private $s2s;
/** @var RequestHandlerController */
private $requestHandler;
/** @var \OCA\FederatedFileSharing\FederatedShareProvider|\PHPUnit_Framework_MockObject_MockObject */
private $federatedShareProvider;
@ -77,17 +85,34 @@ class RequestHandlerControllerTest extends TestCase {
/** @var IShare|\PHPUnit_Framework_MockObject_MockObject */
private $share;
/** @var ICloudIdManager */
/** @var ICloudIdManager|\PHPUnit_Framework_MockObject_MockObject */
private $cloudIdManager;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
protected function setUp() {
parent::setUp();
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
private $request;
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
\OC\Share\Share::registerBackend('test', 'Test\Share\Backend');
/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
private $connection;
/** @var Share\IManager|\PHPUnit_Framework_MockObject_MockObject */
private $shareManager;
/** @var ICloudFederationFactory|\PHPUnit_Framework_MockObject_MockObject */
private $cloudFederationFactory;
/** @var ICloudFederationProviderManager|\PHPUnit_Framework_MockObject_MockObject */
private $cloudFederationProviderManager;
/** @var ICloudFederationProvider|\PHPUnit_Framework_MockObject_MockObject */
private $cloudFederationProvider;
/** @var ICloudFederationShare|\PHPUnit_Framework_MockObject_MockObject */
private $cloudFederationShare;
protected function setUp() {
$this->share = $this->getMockBuilder(IShare::class)->getMock();
$this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
@ -104,292 +129,127 @@ class RequestHandlerControllerTest extends TestCase {
$this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
->disableOriginalConstructor()->getMock();
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
$this->request = $this->createMock(IRequest::class);
$this->connection = $this->createMock(IDBConnection::class);
$this->shareManager = $this->createMock(Share\IManager::class);
$this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
$this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
$this->cloudFederationProvider = $this->createMock(ICloudFederationProvider::class);
$this->cloudFederationShare = $this->createMock(ICloudFederationShare::class);
$this->cloudIdManager = new CloudIdManager();
$this->logger = $this->createMock(ILogger::class);
$this->s2s = new RequestHandlerController(
$this->requestHandler = new RequestHandlerController(
'federatedfilesharing',
\OC::$server->getRequest(),
$this->request,
$this->federatedShareProvider,
\OC::$server->getDatabaseConnection(),
\OC::$server->getShareManager(),
$this->connection,
$this->shareManager,
$this->notifications,
$this->addressHandler,
$this->userManager,
$this->cloudIdManager,
$this->logger
$this->logger,
$this->cloudFederationFactory,
$this->cloudFederationProviderManager
);
$this->connection = \OC::$server->getDatabaseConnection();
}
protected function tearDown() {
$qb = $this->connection->getQueryBuilder();
$qb->delete('share_external');
$qb->execute();
$qb = $this->connection->getQueryBuilder();
$qb->delete('share');
$qb->execute();
parent::tearDown();
}
/**
* @medium
*/
function testCreateShare() {
// simulate a post request
$_POST['remote'] = 'localhost';
$_POST['token'] = 'token';
$_POST['name'] = 'name';
$_POST['owner'] = 'owner';
$_POST['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
$_POST['owner'] = $this->owner;
$_POST['sharedBy'] = $this->user1;
$_POST['shareWith'] = $this->user2;
$_POST['remoteId'] = 1;
$_POST['sharedByFederatedId'] = $this->user1CloudId;
$_POST['ownerFederatedId'] = $this->ownerCloudId;
$this->s2s->createShare(null);
$this->cloudFederationFactory->expects($this->once())->method('getCloudFederationShare')
->with(
$this->user2,
'name',
'',
1,
$this->ownerCloudId,
$this->owner,
$this->user1CloudId,
$this->user1,
'token',
'user',
'file'
)->willReturn($this->cloudFederationShare);
$qb = $this->connection->getQueryBuilder();
$qb->select('*')
->from('share_external')
->where(
$qb->expr()->eq('remote_id', $qb->createNamedParameter(1))
);
$result = $qb->execute();
$data = $result->fetch();
$result->closeCursor();
/** @var ICloudFederationProvider|\PHPUnit_Framework_MockObject_MockObject $provider */
$this->cloudFederationProviderManager->expects($this->once())
->method('getCloudFederationProvider')
->with('file')
->willReturn($this->cloudFederationProvider);
$this->cloudFederationProvider->expects($this->once())->method('shareReceived')
->with($this->cloudFederationShare);
$result = $this->requestHandler->createShare();
$this->assertInstanceOf(DataResponse::class, $result);
$this->assertSame('localhost', $data['remote']);
$this->assertSame('token', $data['share_token']);
$this->assertSame('/name', $data['name']);
$this->assertSame('owner', $data['owner']);
$this->assertSame(self::TEST_FILES_SHARING_API_USER2, $data['user']);
$this->assertSame(1, (int)$data['remote_id']);
$this->assertSame(0, (int)$data['accepted']);
}
function testDeclineShare() {
$this->s2s = $this->getMockBuilder('\OCA\FederatedFileSharing\Controller\RequestHandlerController')
->setConstructorArgs(
[
'federatedfilessharing',
\OC::$server->getRequest(),
$this->federatedShareProvider,
\OC::$server->getDatabaseConnection(),
\OC::$server->getShareManager(),
$this->notifications,
$this->addressHandler,
$this->userManager,
$this->cloudIdManager,
$this->logger,
]
)->setMethods(['executeDeclineShare', 'verifyShare'])->getMock();
$this->s2s->expects($this->once())->method('executeDeclineShare');
$this->s2s->expects($this->any())->method('verifyShare')->willReturn(true);
$id = 42;
$_POST['token'] = 'token';
$this->s2s->declineShare(42);
}
function XtestDeclineShareMultiple() {
$this->share->expects($this->any())->method('verifyShare')->willReturn(true);
$dummy = \OC_DB::prepare('
INSERT INTO `*PREFIX*share`
(`share_type`, `uid_owner`, `item_type`, `item_source`, `item_target`, `file_source`, `file_target`, `permissions`, `stime`, `token`, `share_with`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');
$dummy->execute(array(\OCP\Share::SHARE_TYPE_REMOTE, self::TEST_FILES_SHARING_API_USER1, 'test', '1', '/1', '1', '/test.txt', '1', time(), 'token1', 'foo@bar'));
$dummy->execute(array(\OCP\Share::SHARE_TYPE_REMOTE, self::TEST_FILES_SHARING_API_USER1, 'test', '1', '/1', '1', '/test.txt', '1', time(), 'token2', 'bar@bar'));
$verify = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
$result = $verify->execute();
$data = $result->fetchAll();
$this->assertCount(2, $data);
$_POST['token'] = 'token1';
$this->s2s->declineShare(array('id' => $data[0]['id']));
$verify = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
$result = $verify->execute();
$data = $result->fetchAll();
$this->assertCount(1, $data);
$this->assertEquals('bar@bar', $data[0]['share_with']);
$_POST['token'] = 'token2';
$this->s2s->declineShare(array('id' => $data[0]['id']));
$verify = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`');
$result = $verify->execute();
$data = $result->fetchAll();
$this->assertEmpty($data);
}
/**
* @dataProvider dataTestDeleteUser
*/
function testDeleteUser($toDelete, $expected, $remainingUsers) {
$this->createDummyS2SShares();
$httpClientService = $this->createMock(IClientService::class);
$client = $this->createMock(IClient::class);
$response = $this->createMock(IResponse::class);
$client
->expects($this->any())
->method('get')
->willReturn($response);
$client
->expects($this->any())
->method('post')
->willReturn($response);
$httpClientService
->expects($this->any())
->method('newClient')
->willReturn($client);
$manager = new \OCA\Files_Sharing\External\Manager(
\OC::$server->getDatabaseConnection(),
Filesystem::getMountManager(),
Filesystem::getLoader(),
$httpClientService,
\OC::$server->getNotificationManager(),
\OC::$server->query(\OCP\OCS\IDiscoveryService::class),
$toDelete
);
$manager->removeUserShares($toDelete);
$query = $this->connection->prepare('SELECT `user` FROM `*PREFIX*share_external`');
$query->execute();
$result = $query->fetchAll();
foreach ($result as $r) {
$remainingShares[$r['user']] = isset($remainingShares[$r['user']]) ? $remainingShares[$r['user']] + 1 : 1;
}
$this->assertSame($remainingUsers, count($remainingShares));
foreach ($expected as $key => $value) {
if ($key === $toDelete) {
$this->assertArrayNotHasKey($key, $remainingShares);
} else {
$this->assertSame($value, $remainingShares[$key]);
}
}
}
function dataTestDeleteUser() {
return array(
array('user1', array('user1' => 0, 'user2' => 3, 'user3' => 3), 2),
array('user2', array('user1' => 4, 'user2' => 0, 'user3' => 3), 2),
array('user3', array('user1' => 4, 'user2' => 3, 'user3' => 0), 2),
array('user4', array('user1' => 4, 'user2' => 3, 'user3' => 3), 3),
);
}
private function createDummyS2SShares() {
$query = $this->connection->prepare('
INSERT INTO `*PREFIX*share_external`
(`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `remote_id`, `accepted`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');
$users = array('user1', 'user2', 'user3');
for ($i = 0; $i < 10; $i++) {
$user = $users[$i%3];
$query->execute(array('remote', 'token', 'password', 'name', 'owner', $user, 'mount point', $i, $i, 0));
}
$query = $this->connection->prepare('SELECT `id` FROM `*PREFIX*share_external`');
$query->execute();
$dummyEntries = $query->fetchAll();
$this->assertSame(10, count($dummyEntries));
}
/**
* @dataProvider dataTestGetShare
*
* @param bool $found
* @param bool $correctId
* @param bool $correctToken
*/
public function testGetShare($found, $correctId, $correctToken) {
$connection = \OC::$server->getDatabaseConnection();
$query = $connection->getQueryBuilder();
$stime = time();
$query->insert('share')
->values(
[
'share_type' => $query->createNamedParameter(FederatedShareProvider::SHARE_TYPE_REMOTE),
'uid_owner' => $query->createNamedParameter(self::TEST_FILES_SHARING_API_USER1),
'uid_initiator' => $query->createNamedParameter(self::TEST_FILES_SHARING_API_USER2),
'item_type' => $query->createNamedParameter('test'),
'item_source' => $query->createNamedParameter('1'),
'item_target' => $query->createNamedParameter('/1'),
'file_source' => $query->createNamedParameter('1'),
'file_target' => $query->createNamedParameter('/test.txt'),
'permissions' => $query->createNamedParameter('1'),
'stime' => $query->createNamedParameter($stime),
'token' => $query->createNamedParameter('token'),
'share_with' => $query->createNamedParameter('foo@bar'),
]
)->execute();
$id = $query->getLastInsertId();
$expected = [
'share_type' => (string)FederatedShareProvider::SHARE_TYPE_REMOTE,
'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
'item_type' => 'test',
'item_source' => '1',
'item_target' => '/1',
'file_source' => '1',
'file_target' => '/test.txt',
'permissions' => '1',
'stime' => (string)$stime,
'token' => 'token',
'share_with' => 'foo@bar',
'id' => (string)$id,
'uid_initiator' => self::TEST_FILES_SHARING_API_USER2,
'parent' => null,
'accepted' => '0',
'expiration' => null,
'password' => null,
'mail_send' => '0',
'share_name' => null,
$notification = [
'sharedSecret' => 'token',
'message' => 'Recipient declined the share'
];
$searchToken = $correctToken ? 'token' : 'wrongToken';
$searchId = $correctId ? $id : -1;
$this->cloudFederationProviderManager->expects($this->once())
->method('getCloudFederationProvider')
->with('file')
->willReturn($this->cloudFederationProvider);
$result = $this->invokePrivate($this->s2s, 'getShare', [$searchId, $searchToken]);
$this->cloudFederationProvider->expects($this->once())
->method('notificationReceived')
->with('SHARE_DECLINED', $id, $notification);
$result = $this->requestHandler->declineShare($id);
$this->assertInstanceOf(DataResponse::class, $result);
if ($found) {
$this->assertEquals($expected, $result);
} else {
$this->assertSame(false, $result);
}
}
public function dataTestGetShare() {
return [
[true, true, true],
[false, false, true],
[false, true, false],
[false, false, false],
function testAcceptShare() {
$id = 42;
$_POST['token'] = 'token';
$notification = [
'sharedSecret' => 'token',
'message' => 'Recipient accept the share'
];
$this->cloudFederationProviderManager->expects($this->once())
->method('getCloudFederationProvider')
->with('file')
->willReturn($this->cloudFederationProvider);
$this->cloudFederationProvider->expects($this->once())
->method('notificationReceived')
->with('SHARE_ACCEPTED', $id, $notification);
$result = $this->requestHandler->acceptShare($id);
$this->assertInstanceOf(DataResponse::class, $result);
}
}