Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: drop use of \OCA\Mail\Account::getImapConnection #8962

Merged
merged 1 commit into from
Oct 17, 2023
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
13 changes: 10 additions & 3 deletions lib/Service/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\BackgroundJob\IJobList;
use function array_map;
Expand All @@ -54,12 +55,17 @@ class AccountService {
/** @var IJobList */
private $jobList;

/** @var IMAPClientFactory*/
private $imapClientFactory;

public function __construct(MailAccountMapper $mapper,
AliasesService $aliasesService,
IJobList $jobList) {
IJobList $jobList,
IMAPClientFactory $imapClientFactory) {
$this->mapper = $mapper;
$this->aliasesService = $aliasesService;
$this->jobList = $jobList;
$this->imapClientFactory = $imapClientFactory;
}

/**
Expand Down Expand Up @@ -192,9 +198,10 @@ public function getAllAcounts(): array {
public function testAccountConnection(string $currentUserId, int $accountId) :bool {
$account = $this->find($currentUserId, $accountId);
try {
$account->getImapConnection();
$client = $this->imapClientFactory->getClient($account);
$client->close();
return true;
} catch (ServiceException $e) {
} catch (\Throwable $e) {
return false;
}
}
Expand Down
41 changes: 40 additions & 1 deletion tests/Unit/Service/AccountServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
namespace OCA\Mail\Tests\Unit\Service;

use ChristophWurst\Nextcloud\Testing\TestCase;
use Horde_Imap_Client_Socket;
use OCA\Mail\Account;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AliasesService;
use OCP\BackgroundJob\IJobList;
Expand Down Expand Up @@ -56,21 +59,30 @@ class AccountServiceTest extends TestCase {
/** @var IJobList|MockObject */
private $jobList;

/** @var IMAPClientFactory|MockObject */
private $imapClientFactory;

/** @var Horde_Imap_Client_Socket|MockObject */
private $client;

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

$this->mapper = $this->createMock(MailAccountMapper::class);
$this->l10n = $this->createMock(IL10N::class);
$this->aliasesService = $this->createMock(AliasesService::class);
$this->jobList = $this->createMock(IJobList::class);
$this->imapClientFactory = $this->createMock(IMAPClientFactory::class);
$this->accountService = new AccountService(
$this->mapper,
$this->aliasesService,
$this->jobList
$this->jobList,
$this->imapClientFactory
);

$this->account1 = $this->createMock(MailAccount::class);
$this->account2 = $this->createMock(MailAccount::class);
$this->client = $this->createMock(Horde_Imap_Client_Socket::class);
}

public function testFindByUserId() {
Expand Down Expand Up @@ -179,4 +191,31 @@ public function testUpdateSignature() {

$this->accountService->updateSignature($id, $uid, $signature);
}
public function testAccountsFailedConnection() {
$accountId = 1;
$this->imapClientFactory->expects($this->once())
->method('getClient')
->willThrowException(new ClientException());
$this->mapper->expects($this->once())
->method('find')
->with($this->user, $accountId)
->willReturn($this->account1);
$connected = $this->accountService->testAccountConnection($this->user, $accountId);
$this->assertFalse($connected);
}
public function testAccountsSuccesfulConnection() {
$accountId = 1;
$this->imapClientFactory->expects($this->once())
->method('getClient')
->willReturn($this->client);
$this->client->expects($this->once())
->method('close')
->willReturn(null);
$this->mapper->expects($this->once())
->method('find')
->with($this->user, $accountId)
->willReturn($this->account1);
$connected = $this->accountService->testAccountConnection($this->user, $accountId);
$this->assertTrue($connected);
}
}