Skip to content

Commit

Permalink
fixup! Add imip processing
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Aug 16, 2022
1 parent 173131c commit b136fc7
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/BackgroundJob/PreviewEnhancementProcessingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(ITimeFactory $time,
/**
* @return void
*/
protected function run($argument) {
public function run($argument) {
$accountId = (int)$argument['accountId'];

try {
Expand Down Expand Up @@ -92,7 +92,7 @@ protected function run($argument) {
}

try {
$limitTimestamp = $this->time->getTime() - 60 * 60 * 24 * 14; // Two weeks into the past
$limitTimestamp = $this->time->getTime() - (60 * 60 * 24 * 14); // Two weeks into the past
$this->preprocessingService->process($limitTimestamp, $account);
} catch (Exception $e) {
$this->logger->error('Error processing messages', ['exception' => $e]);
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/PreprocessingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ public function __construct(
*/
public function process(int $limitTimestamp, Account $account): void {
$mailboxes = $this->mailboxMapper->findAll($account);
if (empty($mailboxes)) {
$this->logger->info('No mailboxes found.');
return;
}
$mailboxIds = array_unique(array_map(function (Mailbox $mailbox) {
return $mailbox->getId();
}, $mailboxes));


$messages = $this->messageMapper->getUnanalysed($limitTimestamp, $mailboxIds);
if (empty($messages)) {
$this->logger->info('No structure data to analyse.');
Expand Down
187 changes: 187 additions & 0 deletions tests/Unit/Job/PreviewEnhancementProcessingJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
/*
* *
* * Mail App
* *
* * @copyright 2022 Anna Larch <anna.larch@gmx.net>
* *
* * @author Anna Larch <anna.larch@gmx.net>
* *
* * This library is free software; you can redistribute it and/or
* * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* * License as published by the Free Software Foundation; either
* * version 3 of the License, or any later version.
* *
* * This library is distributed in the hope that it will be useful,
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
* *
* * You should have received a copy of the GNU Affero General Public
* * License along with this library. If not, see <http://www.gnu.org/licenses/>.
* *
*
*/

namespace OCA\Mail\Tests\Unit\Job;

use OCA\Mail\Account;
use OCA\Mail\BackgroundJob\PreviewEnhancementProcessingJob;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\PreprocessingService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\DB\Exception;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use ChristophWurst\Nextcloud\Testing\TestCase;
use Psr\Log\LoggerInterface;

class PreviewEnhancementProcessingJobTest extends TestCase {

/** @var ITimeFactory|ITimeFactory&MockObject|MockObject */
private $time;

/** @var IUserManager|IUserManager&MockObject|MockObject */
private $manager;

/** @var AccountService|AccountService&MockObject|MockObject */
private $accountService;

/** @var PreprocessingService|PreprocessingService&MockObject|MockObject */
private $preprocessingService;

/** @var MockObject|LoggerInterface|LoggerInterface&MockObject */
private $logger;

/** @var IJobList|IJobList&MockObject|MockObject */
private $jobList;

/** @var int[] */
private static $argument;

public function setUp(): void {
parent::setUp();
$this->time = $this->createMock(ITimeFactory::class);
$this->manager = $this->createMock(IUserManager::class);
$this->accountService = $this->createMock(AccountService::class);
$this->preprocessingService = $this->createMock(PreprocessingService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->jobList = $this->createMock(IJobList::class);
$this->job = new PreviewEnhancementProcessingJob(
$this->time,
$this->manager,
$this->accountService,
$this->preprocessingService,
$this->logger,
$this->jobList
);

self::$argument = ['accountId' => 1];
}

public function testNoAccount(): void {
$this->accountService->expects(self::once())
->method('findById')
->with(self::$argument['accountId'])
->willThrowException(new DoesNotExistException('Account does not exist'));

$this->logger->expects(self::once())
->method('debug');
$this->jobList->expects(self::once())
->method('remove');
$this->job->run(self::$argument);
}

public function testNoUser(): void {
$account = new Account(new MailAccount());
$this->accountService->expects(self::once())
->method('findById')
->with(self::$argument['accountId'])
->willReturn($account);

$this->manager->expects(self::once())
->method('get')
->with($account)
->willReturn(null);
$this->logger->expects(self::once())
->method('debug');
$this->job->run(self::$argument);
}

public function testProvisionedNoPassword(): void {
$mailAccount = new MailAccount();
$mailAccount->setProvisioningId(1);
$account = new Account($mailAccount);
$user = $this->createMock(IUser::class);
$user->setEnabled();
$this->accountService->expects(self::once())
->method('findById')
->with(self::$argument['accountId'])
->willReturn($account);

$this->manager->expects(self::once())
->method('get')
->with($account)
->willReturn($user);
$this->logger->expects(self::once())
->method('info');
$this->job->run(self::$argument);
}

public function testProcessingError(): void {
$mailAccount = new MailAccount();
$account = new Account($mailAccount);
$time = time();
$user = $this->createMock(IUser::class);
$user->setEnabled();
$this->accountService->expects(self::once())
->method('findById')
->with(self::$argument['accountId'])
->willReturn($account);

$this->manager->expects(self::once())
->method('get')
->with($account)
->willReturn($user);
$this->time->expects(self::once())
->method('getTime')
->willReturn($time);
$this->preprocessingService->expects(self::once())
->method('process')
->with(($time - (60 * 60 * 24 * 14)), $account)
->willThrowException(new Exception());
$this->logger->expects(self::once())
->method('error');
$this->job->run(self::$argument);
}

public function testProcessing(): void {
$mailAccount = new MailAccount();
$account = new Account($mailAccount);
$time = time();
$user = $this->createMock(IUser::class);
$user->setEnabled();
$this->accountService->expects(self::once())
->method('findById')
->with(self::$argument['accountId'])
->willReturn($account);

$this->manager->expects(self::once())
->method('get')
->with($account)
->willReturn($user);
$this->time->expects(self::once())
->method('getTime')
->willReturn($time);
$this->preprocessingService->expects(self::once())
->method('process')
->with(($time - (60 * 60 * 24 * 14)), $account);
$this->logger->expects(self::never())
->method('error');
$this->job->run(self::$argument);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Service/IMipServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,39 @@ public function testIsSpecialUse(): void {
$this->service->process();
}

public function testIsArchive(): void {
$message = new Message();
$message->setImipMessage(true);
$message->setUid(1);
$message->setMailboxId(100);
$mailbox = new Mailbox();
$mailbox->setId(100);
$mailbox->setAccountId(200);
$mailbox->setSpecialUse('["archive"]');
$mailAccount = new MailAccount();
$account = new Account($mailAccount);

$this->messageMapper->expects(self::once())
->method('findIMipMessagesAscending')
->willReturn([$message]);
$this->mailboxMapper->expects(self::once())
->method('findById')
->with($message->getMailboxId())
->willReturn($mailbox);
$this->accountService->expects(self::once())
->method('findById')
->with($mailbox->getAccountId())
->willReturn($account);
$this->messageMapper->expects(self::once())
->method('updateImipData');
$this->calendarManager->expects(self::never())
->method('handleIMipReply');
$this->calendarManager->expects(self::never())
->method('handleIMipCancel');

$this->service->process();
}

public function testNoSchedulingInfo(): void {
$message = new Message();
$message->setImipMessage(true);
Expand Down
8 changes: 0 additions & 8 deletions tests/Unit/Service/PreprocessingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,13 @@

use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Account;
use OCA\Mail\Address;
use OCA\Mail\AddressList;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\Message;
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\PreviewEnhancer;
use OCA\Mail\Model\IMAPMessage;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\IMipService;
use OCA\Mail\Service\MailManager;
use OCA\Mail\Service\PreprocessingService;
use OCP\Calendar\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

Expand Down

0 comments on commit b136fc7

Please sign in to comment.