diff options
author | Björn Schießle <schiessle@owncloud.com> | 2016-05-04 12:16:02 +0200 |
---|---|---|
committer | Björn Schießle <schiessle@owncloud.com> | 2016-05-20 21:15:11 +0200 |
commit | 8f87e1104d37063ff561a69348f725a2b907b9f5 (patch) | |
tree | 299c83e7ee9f46a8c86a285b8a0c75e26ad73a49 /apps | |
parent | 5a1641bed23182a35cfbdcdb526b3c41260a7c0f (diff) | |
download | nextcloud-server-8f87e1104d37063ff561a69348f725a2b907b9f5.tar.gz nextcloud-server-8f87e1104d37063ff561a69348f725a2b907b9f5.zip |
use query builder for getShare and add tests
Diffstat (limited to 'apps')
-rw-r--r-- | apps/federatedfilesharing/lib/RequestHandler.php | 34 | ||||
-rw-r--r-- | apps/federatedfilesharing/tests/RequestHandlerTest.php | 75 |
2 files changed, 99 insertions, 10 deletions
diff --git a/apps/federatedfilesharing/lib/RequestHandler.php b/apps/federatedfilesharing/lib/RequestHandler.php index 65925c3823c..90621666b6a 100644 --- a/apps/federatedfilesharing/lib/RequestHandler.php +++ b/apps/federatedfilesharing/lib/RequestHandler.php @@ -29,6 +29,7 @@ use OCA\FederatedFileSharing\DiscoveryManager; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\Files_Sharing\Activity; use OCP\Files\NotFoundException; +use OCP\IDBConnection; /** * Class RequestHandler @@ -42,14 +43,21 @@ class RequestHandler { /** @var FederatedShareProvider */ private $federatedShareProvider; + /** @var IDBConnection */ + private $connection; + + /** @var string */ + private $shareTable = 'share'; /** * Server2Server constructor. * * @param FederatedShareProvider $federatedShareProvider + * @param IDBConnection $connection */ - public function __construct(FederatedShareProvider $federatedShareProvider) { + public function __construct(FederatedShareProvider $federatedShareProvider, IDBConnection $connection) { $this->federatedShareProvider = $federatedShareProvider; + $this->connection = $connection; } /** @@ -162,10 +170,10 @@ class RequestHandler { $id = $params['id']; $token = isset($_POST['token']) ? $_POST['token'] : null; - $share = self::getShare($id, $token); + $share = $this->getShare($id, $token); if ($share) { - list($file, $link) = self::getFile($share['uid_owner'], $share['file_source']); + list($file, $link) = $this->getFile($share['uid_owner'], $share['file_source']); $event = \OC::$server->getActivityManager()->generateEvent(); $event->setApp(Activity::FILES_SHARING_APP) @@ -278,14 +286,22 @@ class RequestHandler { * * @param int $id * @param string $token - * @return array + * @return array|bool */ - private function getShare($id, $token) { - $query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ? AND `token` = ? AND `share_type` = ?'); - $query->execute(array($id, $token, \OCP\Share::SHARE_TYPE_REMOTE)); - $share = $query->fetchRow(); + protected function getShare($id, $token) { + $query = $this->connection->getQueryBuilder(); + $query->select('*')->from($this->shareTable) + ->where($query->expr()->eq('token', $query->createNamedParameter($token))) + ->andWhere($query->expr()->eq('share_type', $query->createNamedParameter(FederatedShareProvider::SHARE_TYPE_REMOTE))) + ->andWhere($query->expr()->eq('id', $query->createNamedParameter($id))); + + $result = $query->execute()->fetchAll(); + + if (!empty($result) && isset($result[0])) { + return $result[0]; + } - return $share; + return false; } /** diff --git a/apps/federatedfilesharing/tests/RequestHandlerTest.php b/apps/federatedfilesharing/tests/RequestHandlerTest.php index ef0074dc039..84b25701c6d 100644 --- a/apps/federatedfilesharing/tests/RequestHandlerTest.php +++ b/apps/federatedfilesharing/tests/RequestHandlerTest.php @@ -27,6 +27,7 @@ namespace OCA\FederatedFileSharing\Tests; use OC\Files\Filesystem; use OCA\FederatedFileSharing\DiscoveryManager; +use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\RequestHandler; /** @@ -74,7 +75,7 @@ class RequestHandlerTest extends TestCase { $this->registerHttpHelper($httpHelperMock); - $this->s2s = new RequestHandler($this->federatedShareProvider); + $this->s2s = new RequestHandler($this->federatedShareProvider, \OC::$server->getDatabaseConnection()); $this->connection = \OC::$server->getDatabaseConnection(); } @@ -265,4 +266,76 @@ class RequestHandlerTest extends TestCase { $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, + 'mail_send' => '0' + ]; + + $searchToken = $correctToken ? 'token' : 'wrongToken'; + $searchId = $correctId ? $id : -1; + + $result = $this->invokePrivate($this->s2s, 'getShare', [$searchId, $searchToken]); + + 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], + ]; + } + } |