mirror of
https://github.com/nextcloud/passman.git
synced 2025-11-18 14:55:37 +08:00
Added unit tests agains sharerequest mapper
Added some helper functions on the database helper test Renamed method so it's less confusing
This commit is contained in:
parent
286476a546
commit
52c072631d
4 changed files with 220 additions and 3 deletions
|
|
@ -11,6 +11,7 @@ namespace OCA\Passman\Db;
|
|||
|
||||
use Icewind\SMB\Share;
|
||||
use OCA\Passman\Utility\Utils;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
|
|
@ -41,7 +42,7 @@ class ShareRequestMapper extends Mapper {
|
|||
* @param $item_guid
|
||||
* @return ShareRequest[]
|
||||
*/
|
||||
public function getRequestsByItemGuid($item_guid){
|
||||
public function getRequestsByItemGuidGroupedByUser($item_guid){
|
||||
$this->db->executeQuery("SET sql_mode = '';");
|
||||
$q = "SELECT *, target_user_id FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? GROUP BY target_user_id;";
|
||||
return $this->findEntities($q, [$item_guid]);
|
||||
|
|
@ -82,6 +83,7 @@ class ShareRequestMapper extends Mapper {
|
|||
* Gets a share request by it's unique incremental id
|
||||
* @param $id
|
||||
* @return ShareRequest
|
||||
* @throws DoesNotExistException
|
||||
*/
|
||||
public function getShareRequestById($id){
|
||||
$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE id = ?";
|
||||
|
|
|
|||
|
|
@ -216,7 +216,7 @@ class ShareService {
|
|||
}
|
||||
|
||||
public function getCredentialPendingAclList($item_guid) {
|
||||
return $this->shareRequest->getRequestsByItemGuid($item_guid);
|
||||
return $this->shareRequest->getRequestsByItemGuidGroupedByUser($item_guid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -117,7 +117,7 @@ abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase {
|
|||
$result = [];
|
||||
for ($i = 0; $i < $rows; $i++) {
|
||||
$row = $table->getRow($i);
|
||||
if ($row[$field_name] == $value_match){
|
||||
if ($row[$field_name] === $value_match){
|
||||
$result[] = $row;
|
||||
}
|
||||
}
|
||||
|
|
@ -125,6 +125,24 @@ abstract class DatabaseHelperTest extends PHPUnit_Extensions_Database_TestCase {
|
|||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the given array
|
||||
* @param $dataset The data to filter
|
||||
* @param $field_name The array key to match against
|
||||
* @param $value_match The value to compare to
|
||||
*/
|
||||
public function filterDataset($dataset, $field_name, $value_match) {
|
||||
$ret = [];
|
||||
|
||||
foreach ($dataset as $value){
|
||||
if ($value[$field_name] === $value_match){
|
||||
$ret[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @coversNothing
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -30,6 +30,9 @@ class ShareRequestMapperTest extends DatabaseHelperTest {
|
|||
*/
|
||||
protected $dataset;
|
||||
|
||||
/**
|
||||
* @after
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->dataset = $this->getTableDataset(self::TABLES[0]);
|
||||
|
|
@ -94,4 +97,198 @@ class ShareRequestMapperTest extends DatabaseHelperTest {
|
|||
|
||||
$this->assertSame($tmp->jsonSerialize(), $insert_data->jsonSerialize());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getRequestsByItemGuidGroupedByUser
|
||||
*/
|
||||
public function testGetRequestsByItemGuidGroupedByUser() {
|
||||
$dataset = $this->findInDataset(
|
||||
self::TABLES[0],
|
||||
'item_guid',
|
||||
$this->dataset->getRow(0)['item_guid']
|
||||
);
|
||||
|
||||
$result = $this->mapper->getRequestsByItemGuidGroupedByUser($dataset[0]['item_guid']);
|
||||
|
||||
$this->assertCount(count($dataset), $result);
|
||||
|
||||
foreach ($dataset as &$row) $row = ShareRequest::fromRow($row);
|
||||
|
||||
$this->assertEquals($dataset, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getUserPendingRequests
|
||||
*/
|
||||
public function testGetUserPendingRequests() {
|
||||
$dataset = $this->findInDataset(
|
||||
self::TABLES[0],
|
||||
'target_user_id',
|
||||
$this->dataset->getRow(0)['target_user_id']
|
||||
);
|
||||
|
||||
$result = $this->mapper->getUserPendingRequests($dataset[0]['target_user_id']);
|
||||
$this->assertCount(count($dataset), $result);
|
||||
|
||||
foreach ($dataset as &$row) $row = ShareRequest::fromRow($row);
|
||||
|
||||
$this->assertEquals($dataset, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::cleanItemRequestsForUser
|
||||
*/
|
||||
public function testCleanItemRequestsForUser() {
|
||||
$tmp = ShareRequest::fromRow($this->dataset->getRow(0));
|
||||
|
||||
// Useless confusing return type that does not match nextcloud phpdocs, not checking
|
||||
$result = $this->mapper->cleanItemRequestsForUser($tmp->getItemId(), $tmp->getTargetUserId());
|
||||
|
||||
$result = $this->mapper->getUserPendingRequests($tmp->getTargetUserId());
|
||||
|
||||
foreach ($result as $row) {
|
||||
if ($row->getItemId() === $tmp->getItemId()) {
|
||||
$this->fail("The user("+$row->getTargetUserId()+") still has request pointing towards the item id: " + $row->getItemId() + " all the request for this user to that item should have been deleted on this test");
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getShareRequestById
|
||||
*/
|
||||
public function testGetShareRequestById() {
|
||||
$expected = ShareRequest::fromRow($this->dataset->getRow(0));
|
||||
|
||||
$data = $this->mapper->getShareRequestById($expected->getId());
|
||||
$this->assertInstanceOf(ShareRequest::class, $data);
|
||||
$this->assertEquals($expected, $data);
|
||||
|
||||
$this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
|
||||
$this->mapper->getShareRequestById(PHP_INT_MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::deleteShareRequest
|
||||
*/
|
||||
public function testDeleteShareRequest() {
|
||||
$tmp = ShareRequest::fromRow($this->dataset->getRow(0));
|
||||
|
||||
$result = $this->mapper->deleteShareRequest($tmp);
|
||||
$this->assertInstanceOf(ShareRequest::class, $result);
|
||||
|
||||
$this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
|
||||
$this->mapper->getShareRequestById($tmp->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getShareRequestsByItemGuid
|
||||
*/
|
||||
public function testGetShareRequestsByItemGuid() {
|
||||
$dataset = $this->findInDataset(
|
||||
self::TABLES[0],
|
||||
'item_guid',
|
||||
$this->dataset->getRow(0)['item_guid']
|
||||
);
|
||||
|
||||
$data = $this->mapper->getShareRequestsByItemGuid($dataset[0]['item_guid']);
|
||||
$this->assertCount(count($dataset), $data);
|
||||
|
||||
foreach ($dataset as &$row) $row = ShareRequest::fromRow($row);
|
||||
|
||||
$this->assertEquals($dataset, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::updateShareRequest
|
||||
*/
|
||||
public function testUpdateShareRequest() {
|
||||
$req = ShareRequest::fromRow($this->dataset->getRow(0));
|
||||
|
||||
$data = $this->mapper->getShareRequestById($req->getId());
|
||||
$this->assertEquals($req, $data);
|
||||
$data->setTargetVaultId($data->getTargetVaultId() + 50);
|
||||
|
||||
$tmp = $this->mapper->updateShareRequest($data);
|
||||
$this->assertEquals($data, $tmp);
|
||||
|
||||
$tmp = $this->mapper->getShareRequestById($req->getId());
|
||||
$this->assertNotSame($req->getTargetVaultId(), $tmp->getTargetVaultId());
|
||||
$this->assertSame($data->getTargetVaultId(), $tmp->getTargetVaultId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getPendingShareRequests
|
||||
*/
|
||||
public function testGetPendingShareRequests() {
|
||||
$dataset = $this->findInDataset(
|
||||
self::TABLES[0],
|
||||
'item_guid',
|
||||
$this->dataset->getRow(0)['item_guid']
|
||||
);
|
||||
$dataset = $this->filterDataset(
|
||||
$dataset,
|
||||
'target_user_id',
|
||||
$dataset[0]['target_user_id']
|
||||
);
|
||||
|
||||
$data = $this->mapper->getPendingShareRequests(
|
||||
$dataset[0]['item_guid'],
|
||||
$dataset[0]['target_user_id']
|
||||
);
|
||||
|
||||
foreach ($dataset as &$row) $row = ShareRequest::fromRow($row);
|
||||
|
||||
$this->assertCount(count($dataset), $data);
|
||||
$this->assertEquals($dataset, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: Check why test fails, seems the database update is not executing properly somehow
|
||||
* @covers ::updatePendinRequestPermissions
|
||||
*/
|
||||
public function testUpdatePendinRequestPermissions() {
|
||||
$dataset = $this->findInDataset(
|
||||
self::TABLES[0],
|
||||
'item_guid',
|
||||
$this->dataset->getRow(0)['item_guid']
|
||||
);
|
||||
$dataset = $this->filterDataset(
|
||||
$dataset,
|
||||
'target_user_id',
|
||||
$dataset[0]['target_user_id']
|
||||
);
|
||||
|
||||
$this->mapper->updatePendinRequestPermissions(
|
||||
$dataset[0]['item_guid'],
|
||||
$dataset[0]['target_user_id'],
|
||||
$dataset[0]['permissions'] + 4
|
||||
);
|
||||
|
||||
$after_update = $this->mapper->getPendingShareRequests(
|
||||
$dataset[0]['item_guid'],
|
||||
$dataset[0]['target_user_id']
|
||||
);
|
||||
|
||||
$this->assertCount(count($dataset), $after_update);
|
||||
|
||||
foreach ($dataset as &$row) $row = ShareRequest::fromRow($row);
|
||||
|
||||
foreach ($after_update as $row) {
|
||||
foreach ($dataset as $compare) {
|
||||
if ($compare->getId() === $row->getId()){
|
||||
$this->assertNotEquals($compare->getPermissions(), $row->getPermissions());
|
||||
$this->assertSame($compare->getPermissions() + 4, $row->getPermissions());
|
||||
|
||||
$tmp_data = $row->jsonSerialize();
|
||||
$tmp_compare = $compare->jsonSerialize();
|
||||
|
||||
unset($tmp_compare['permissions']);
|
||||
unset($tmp_data['permissions']);
|
||||
$this->assertSame($tmp_compare, $tmp_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue