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
7 changes: 7 additions & 0 deletions lib/Db/DelegationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public function find(int $accountId, string $uid): Delegation {
return $this->findEntity($qb);
}

public function deleteByUserId(string $userId): void {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not covered by any test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I write integration tests for the mapper ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
$qb->executeStatement();
}

/**
* @throws DoesNotExistException
*/
Expand Down
5 changes: 5 additions & 0 deletions lib/Listener/UserDeletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\DelegationService;
use OCA\Mail\Service\TextBlockService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
Expand All @@ -30,6 +31,7 @@ class UserDeletedListener implements IEventListener {
public function __construct(
AccountService $accountService,
private TextBlockService $textBlockService,
private DelegationService $delegationService,
LoggerInterface $logger,
) {
$this->accountService = $accountService;
Expand Down Expand Up @@ -59,5 +61,8 @@ public function handle(Event $event): void {
$this->textBlockService->deleteByUserId(
$user->getUID()
);
$this->delegationService->deleteByUserId(
$user->getUID()
);
}
}
4 changes: 4 additions & 0 deletions lib/Service/DelegationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function delegate(Account $account, string $userId, string $currentUserId
return $result;
}

public function deleteByUserId(string $userId): void {
$this->delegationMapper->deleteByUserId($userId);
}

public function findDelegatedToUsersForAccount(int $accountId): array {
return array_map(function (Delegation $delegation) {
$displayName = $this->userManager->get($delegation->getUserId())?->getDisplayName();
Expand Down
137 changes: 137 additions & 0 deletions tests/Integration/Db/DelegationMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Tests\Integration\Db;

use ChristophWurst\Nextcloud\Testing\DatabaseTransaction;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Db\Delegation;
use OCA\Mail\Db\DelegationMapper;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IDBConnection;
use OCP\Server;

/**
* @group DB
* @covers \OCA\Mail\Db\DelegationMapper
*/
class DelegationMapperTest extends TestCase {
use DatabaseTransaction;

private IDBConnection $db;
private DelegationMapper $mapper;
private MailAccountMapper $accountMapper;

protected function setUp(): void {
parent::setUp();

$this->db = Server::get(IDBConnection::class);
$this->mapper = new DelegationMapper($this->db);
$this->accountMapper = new MailAccountMapper($this->db);
}

private function createDelegation(int $accountId, string $userId): Delegation {
$delegation = new Delegation();
$delegation->setAccountId($accountId);
$delegation->setUserId($userId);
return $this->mapper->insert($delegation);
}

private function createAccount(string $ownerUid): MailAccount {
$account = new MailAccount();
$account->setName('Owner');
$account->setEmail('owner@example.com');
$account->setUserId($ownerUid);
return $this->accountMapper->insert($account);
}

public function testFindDelegatedToUsers(): void {
$accountId = $this->createAccount('owner-a')->getId();
$otherAccountId = $this->createAccount('owner-b')->getId();
$this->createDelegation($accountId, 'alice');
$this->createDelegation($accountId, 'bob');
$this->createDelegation($otherAccountId, 'carol');

$result = $this->mapper->findDelegatedToUsers($accountId);

$this->assertCount(2, $result);
$uids = array_map(static fn (Delegation $d) => $d->getUserId(), $result);
$this->assertEqualsCanonicalizing(['alice', 'bob'], $uids);
}

public function testFindDelegatedToUsersReturnsEmpty(): void {
$accountId = $this->createAccount('owner-empty')->getId();

$this->assertSame([], $this->mapper->findDelegatedToUsers($accountId));
}

public function testFind(): void {
$accountId = $this->createAccount('owner-c')->getId();
$inserted = $this->createDelegation($accountId, 'dave');

$result = $this->mapper->find($accountId, 'dave');

$this->assertSame($inserted->getId(), $result->getId());
$this->assertSame('dave', $result->getUserId());
$this->assertSame($accountId, $result->getAccountId());
}

public function testFindThrowsWhenMissing(): void {
$accountId = $this->createAccount('owner-d')->getId();

$this->expectException(DoesNotExistException::class);

$this->mapper->find($accountId, 'nobody');
}

public function testFindAccountOwnerForDelegatedUser(): void {
$account = $this->createAccount('owner-uid');
$this->createDelegation($account->getId(), 'delegate');

$owner = $this->mapper->findAccountOwnerForDelegatedUser($account->getId(), 'delegate');

$this->assertSame('owner-uid', $owner);
}

public function testFindAccountOwnerThrowsWhenNoDelegation(): void {
$account = $this->createAccount('owner-uid');

$this->expectException(DoesNotExistException::class);

$this->mapper->findAccountOwnerForDelegatedUser($account->getId(), 'delegate');
}

public function testDeleteByUserId(): void {
$accountA = $this->createAccount('owner-a')->getId();
$accountB = $this->createAccount('owner-b')->getId();
$this->createDelegation($accountA, 'delegate');
$this->createDelegation($accountB, 'delegate');
$this->createDelegation($accountA, 'other');

$this->mapper->deleteByUserId('delegate');

$remaining = array_map(
static fn (Delegation $d) => $d->getUserId(),
$this->mapper->findDelegatedToUsers($accountA),
);
$this->assertSame(['other'], $remaining);
$this->assertSame([], $this->mapper->findDelegatedToUsers($accountB));
}

public function testDeleteByUserIdNoMatch(): void {
$accountId = $this->createAccount('owner-uid')->getId();
$this->createDelegation($accountId, 'delegate');

$this->mapper->deleteByUserId('nobody');

$this->assertCount(1, $this->mapper->findDelegatedToUsers($accountId));
}
}
29 changes: 28 additions & 1 deletion tests/Unit/Listener/UserDeletedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Listener\UserDeletedListener;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\DelegationService;
use OCA\Mail\Service\TextBlockService;
use OCP\EventDispatcher\Event;
use OCP\IUser;
Expand All @@ -26,6 +27,7 @@ class UserDeletedListenerTest extends TestCase {
private AccountService&MockObject $accountService;

private TextBlockService&MockObject $textBlockService;
private DelegationService&MockObject $delegationService;
private LoggerInterface&MockObject $logger;
private UserDeletedListener $listener;

Expand All @@ -35,10 +37,12 @@ protected function setUp(): void {
$this->accountService = $this->createMock(AccountService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->textBlockService = $this->createMock(TextBlockService::class);
$this->delegationService = $this->createMock(DelegationService::class);

$this->listener = new UserDeletedListener(
$this->accountService,
$this->textBlockService,
$this->delegationService,
$this->logger
);
}
Expand Down Expand Up @@ -68,6 +72,9 @@ public function testHandleUnrelated(): void {
$this->textBlockService->expects($this->never())
->method('deleteByUserId');

$this->delegationService->expects($this->never())
->method('deleteByUserId');

$this->listener->handle($event);

$this->addToAssertionCount(1);
Expand All @@ -92,6 +99,10 @@ public function testHandleUserDeletedWithNoAccounts(): void {
->method('deleteByUserId')
->with('test-user');

$this->delegationService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand All @@ -109,14 +120,17 @@ public function testHandleUserDeletedWithSingleAccount(): void {
->method('delete')
->with('test-user', 42);


$this->logger->expects($this->never())
->method('error');

$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->delegationService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand Down Expand Up @@ -146,6 +160,10 @@ public function testHandleUserDeletedWithMultipleAccounts(): void {
->method('deleteByUserId')
->with('test-user');

$this->delegationService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand Down Expand Up @@ -177,6 +195,10 @@ public function testHandleUserDeletedWithClientException(): void {
->method('deleteByUserId')
->with('test-user');

$this->delegationService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}

Expand Down Expand Up @@ -209,10 +231,15 @@ public function testHandleUserDeletedWithPartialFailure(): void {
'Could not delete user\'s Mail account: Failed to delete account 2',
['exception' => $exception]
);

$this->textBlockService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->delegationService->expects($this->once())
->method('deleteByUserId')
->with('test-user');

$this->listener->handle($event);
}
}
Loading