Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/dav/lib/Command/RemoveInvalidShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace OCA\DAV\Command;

use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\DAV\RemoteUserPrincipalBackend;
use OCP\IDBConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -24,6 +25,7 @@ class RemoveInvalidShares extends Command {
public function __construct(
private IDBConnection $connection,
private Principal $principalBackend,
private RemoteUserPrincipalBackend $remoteUserPrincipalBackend,
) {
parent::__construct();
}
Expand All @@ -42,7 +44,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

while ($row = $result->fetch()) {
$principaluri = $row['principaluri'];
$p = $this->principalBackend->getPrincipalByPath($principaluri);
$p = $this->principalBackend->getPrincipalByPath($principaluri)
?? $this->remoteUserPrincipalBackend->getPrincipalByPath($principaluri);
if ($p === null) {
$this->deleteSharesForPrincipal($principaluri);
}
Expand Down
113 changes: 101 additions & 12 deletions apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

use OCA\DAV\Command\RemoveInvalidShares;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\DAV\RemoteUserPrincipalBackend;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;
Expand All @@ -23,32 +26,118 @@
* @group DB
*/
class RemoveInvalidSharesTest extends TestCase {
private RemoveInvalidShares $command;

private IDBConnection $db;
private Principal&MockObject $principalBackend;
private RemoteUserPrincipalBackend&MockObject $remoteUserPrincipalBackend;

protected function setUp(): void {
parent::setUp();
$db = Server::get(IDBConnection::class);

$db->insertIfNotExist('*PREFIX*dav_shares', [
$this->db = Server::get(IDBConnection::class);
$this->principalBackend = $this->createMock(Principal::class);
$this->remoteUserPrincipalBackend = $this->createMock(RemoteUserPrincipalBackend::class);

$this->db->insertIfNotExist('*PREFIX*dav_shares', [
'principaluri' => 'principal:unknown',
'type' => 'calendar',
'access' => 2,
'resourceid' => 666,
]);
}

public function test(): void {
$db = Server::get(IDBConnection::class);
$principal = $this->createMock(Principal::class);
$this->db->insertIfNotExist('*PREFIX*dav_shares', [
'principaluri' => 'principals/remote-users/foobar',
'type' => 'calendar',
'access' => 2,
'resourceid' => 666,
]);

$repair = new RemoveInvalidShares($db, $principal);
$this->invokePrivate($repair, 'run', [$this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)]);
$this->command = new RemoveInvalidShares(
$this->db,
$this->principalBackend,
$this->remoteUserPrincipalBackend,
);
}

$query = $db->getQueryBuilder();
private function selectShares(): array {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('dav_shares')
->where($query->expr()->eq('principaluri', $query->createNamedParameter('principal:unknown')));
->where($query->expr()->in(
'principaluri',
$query->createNamedParameter(
['principal:unknown', 'principals/remote-users/foobar'],
IQueryBuilder::PARAM_STR_ARRAY,
),
));
$result = $query->executeQuery();
$data = $result->fetchAll();
$result->closeCursor();
$this->assertEquals(0, count($data));

return $data;
}

public function testWithoutPrincipals(): void {
$this->principalBackend->method('getPrincipalByPath')
->willReturnMap([
['principal:unknown', null],
['principals/remote-users/foobar', null],
]);
$this->remoteUserPrincipalBackend->method('getPrincipalByPath')
->willReturnMap([
['principal:unknown', null],
['principals/remote-users/foobar', null],
]);

$this->command->run(
$this->createMock(InputInterface::class),
$this->createMock(OutputInterface::class),
);

$data = $this->selectShares();
$this->assertCount(0, $data);
}

public function testWithLocalPrincipal(): void {
$this->principalBackend->method('getPrincipalByPath')
->willReturnMap([
['principal:unknown', ['uri' => 'principal:unknown']],
['principals/remote-users/foobar', null],
]);
$this->remoteUserPrincipalBackend->method('getPrincipalByPath')
->willReturnMap([
['principals/remote-users/foobar', null],
]);

$this->command->run(
$this->createMock(InputInterface::class),
$this->createMock(OutputInterface::class),
);

$data = $this->selectShares();
$this->assertCount(1, $data);
$this->assertEquals('principal:unknown', $data[0]['principaluri']);
}

public function testWithRemotePrincipal() {
$this->principalBackend->method('getPrincipalByPath')
->willReturnMap([
['principal:unknown', null],
['principals/remote-users/foobar', null],
]);
$this->remoteUserPrincipalBackend->method('getPrincipalByPath')
->willReturnMap([
['principal:unknown', null],
['principals/remote-users/foobar', ['uri' => 'principals/remote-users/foobar']],
]);

$this->command->run(
$this->createMock(InputInterface::class),
$this->createMock(OutputInterface::class),
);

$data = $this->selectShares();
$this->assertCount(1, $data);
$this->assertEquals('principals/remote-users/foobar', $data[0]['principaluri']);
}
}
Loading