Skip to content

Commit

Permalink
Merge pull request #9493 from nextcloud/fix/horde-no-password-provided
Browse files Browse the repository at this point in the history
fix(jobs): Skip background jobs if no authentication is possible
  • Loading branch information
miaulalala authored Mar 25, 2024
2 parents 538fd37 + f1d3fda commit 133da74
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 43 deletions.
11 changes: 5 additions & 6 deletions lib/BackgroundJob/PreviewEnhancementProcessingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public function run($argument) {
return;
}

if (!$account->getMailAccount()->canAuthenticateImap()) {
$this->logger->info('Ignoring preprocessing job for provisioned account as athentication on IMAP not possible');
return;
}

$user = $this->userManager->get($account->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
Expand All @@ -84,12 +89,6 @@ public function run($argument) {
return;
}

$dbAccount = $account->getMailAccount();
if (!is_null($dbAccount->getProvisioningId()) && $dbAccount->getInboundPassword() === null) {
$this->logger->info("Ignoring preprocessing job for provisioned account that has no password set yet");
return;
}

$limitTimestamp = $this->time->getTime() - (60 * 60 * 24 * 14); // Two weeks into the past
$this->preprocessingService->process($limitTimestamp, $account);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/BackgroundJob/QuotaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ protected function run($argument): void {
return;
}

if(!$account->getMailAccount()->canAuthenticateImap()) {
$this->logger->debug('No authentication on IMAP possible, skipping quota job');
return;
}

$user = $this->userManager->get($account->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
Expand Down
11 changes: 5 additions & 6 deletions lib/BackgroundJob/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ protected function run($argument) {
return;
}

if(!$account->getMailAccount()->canAuthenticateImap()) {
$this->logger->debug('No authentication on IMAP possible, skipping background sync job');
return;
}

$user = $this->userManager->get($account->getUserId());
if ($user === null || !$user->isEnabled()) {
$this->logger->debug(sprintf(
Expand All @@ -100,12 +105,6 @@ protected function run($argument) {
return;
}

$dbAccount = $account->getMailAccount();
if (!is_null($dbAccount->getProvisioningId()) && $dbAccount->getInboundPassword() === null) {
$this->logger->info("Ignoring cron sync for provisioned account that has no password set yet");
return;
}

try {
$this->mailboxSync->sync($account, $this->logger, true);
$this->syncService->syncAccount($account, $this->logger);
Expand Down
6 changes: 3 additions & 3 deletions lib/BackgroundJob/TrainImportanceClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ protected function run($argument) {
return;
}

$dbAccount = $account->getMailAccount();
if (!is_null($dbAccount->getProvisioningId()) && $dbAccount->getInboundPassword() === null) {
$this->logger->info("Ignoring cron training for provisioned account that has no password set yet");
if(!$account->getMailAccount()->canAuthenticateImap()) {
$this->logger->debug('Cron importance classifier training not possible: no authentication on IMAP possible');
return;
}

if ($this->preferences->getPreference($account->getUserId(), 'tag-classified-messages') === 'false') {
$this->logger->debug("classification is turned off for account $accountId");
return;
Expand Down
4 changes: 4 additions & 0 deletions lib/Db/MailAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ public function setOutOfOfficeFollowsSystem(bool $outOfOfficeFollowsSystem): voi
$this->setOooFollowsSystem($outOfOfficeFollowsSystem);
}

public function canAuthenticateImap(): bool {
return isset($this->inboundPassword) || isset($this->oauthAccessToken);
}

/**
* @return array
*/
Expand Down
78 changes: 72 additions & 6 deletions tests/Unit/BackgroundJob/QuotaJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ public function testAccountDoesntExist(): void {
}

public function testUserDoesntExist(): void {
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(123);
$account->method('getUserId')->willReturn('user123');
$account->method('getMailAccount')->willReturn($mailAccount);
$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('findById')
Expand Down Expand Up @@ -121,11 +125,67 @@ public function testUserDoesntExist(): void {
);
}

public function testQuotaNoAuthentication(): void {
$oldQuota = 10;
$newQuota = 20;
$quotaDTO = new Quota(20, 100);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => false,
]);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
'getMailAccount' => $mailAccount,
]);
$user = $this->createConfiguredMock(IUser::class, [
'isEnabled' => true,
]);

$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('findById')
->with(123)
->willReturn($account);
$account->expects(self::once())
->method('getMailAccount')
->willReturn($mailAccount);
$mailAccount->expects(self::once())
->method('canAuthenticateImap');
$this->serviceMock->getParameter('userManager')
->expects(self::never())
->method('get');
$this->serviceMock->getParameter('logger')
->expects(self::once())
->method('debug');
$this->serviceMock->getParameter('mailManager')
->expects(self::never())
->method('getQuota');
$account->expects(self::never())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::never())
->method('getQuotaPercentage')
->willReturn($newQuota);
$this->serviceMock->getParameter('accountService')
->expects(self::never())
->method('update')
->with($mailAccount);

$this->job->setArgument([
'accountId' => 123,
]);
$this->job->start(
$this->createMock(JobList::class),
);
}

public function testQuotaTooLow(): void {
$oldQuota = 10;
$newQuota = 20;
$quotaDTO = new Quota(20, 100);
$mailAccount = $this->createMock(MailAccount::class);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
Expand All @@ -140,6 +200,8 @@ public function testQuotaTooLow(): void {
->method('findById')
->with(123)
->willReturn($account);
$mailAccount->expects(self::once())
->method('canAuthenticateImap');
$this->serviceMock->getParameter('userManager')
->expects(self::once())
->method('get')
Expand All @@ -155,7 +217,7 @@ public function testQuotaTooLow(): void {
$account->expects(self::once())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::exactly(2))
$account->expects(self::exactly(3))
->method('getMailAccount')
->willReturn($mailAccount);
$account->expects(self::once())
Expand All @@ -178,7 +240,9 @@ public function testQuotaWithNotification(): void {
$oldQuota = 85;
$newQuota = 95;
$quotaDTO = new Quota(95, 100);
$mailAccount = $this->createMock(MailAccount::class);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
Expand Down Expand Up @@ -207,7 +271,7 @@ public function testQuotaWithNotification(): void {
$account->expects(self::once())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::exactly(2))
$account->expects(self::exactly(3))
->method('getMailAccount')
->willReturn($mailAccount);
$account->expects(self::once())
Expand Down Expand Up @@ -282,7 +346,9 @@ public function testQuotaZero(): void {
$oldQuota = 0;
$newQuota = 0;
$quotaDTO = new Quota(0, 0);
$mailAccount = $this->createMock(MailAccount::class);
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$account = $this->createConfiguredMock(Account::class, [
'getId' => 123,
'getUserId' => 'user123',
Expand Down Expand Up @@ -312,7 +378,7 @@ public function testQuotaZero(): void {
$account->expects(self::once())
->method('calculateAndSetQuotaPercentage')
->with($quotaDTO);
$account->expects(self::exactly(2))
$account->expects(self::exactly(3))
->method('getMailAccount')
->willReturn($mailAccount);
$account->expects(self::once())
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/BackgroundJob/SyncJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OC\BackgroundJob\JobList;
use OCA\Mail\Account;
use OCA\Mail\BackgroundJob\SyncJob;
use OCA\Mail\Db\MailAccount;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\ILogger;
use OCP\IUser;
Expand Down Expand Up @@ -90,10 +91,51 @@ public function testAccountDoesntExist(): void {
);
}

public function testNoAuthentication(): void {
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => false,
]);
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(123);
$account->method('getUserId')->willReturn('user123');
$account->method('getMailAccount')->willReturn($mailAccount);

$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('findById')
->with(123)
->willReturn($account);
$this->serviceMock->getParameter('logger')
->expects(self::once())
->method('debug')
->with('No authentication on IMAP possible, skipping background sync job');
$this->serviceMock->getParameter('userManager')
->expects(self::never())
->method('get');
$this->serviceMock->getParameter('mailboxSync')
->expects(self::never())
->method('sync');
$this->serviceMock->getParameter('syncService')
->expects(self::never())
->method('syncAccount');

$this->job->setArgument([
'accountId' => 123,
]);
$this->job->execute(
$this->createMock(JobList::class),
$this->createMock(ILogger::class)
);
}

public function testUserDoesntExist(): void {
$mailAccount = $this->createConfiguredMock(MailAccount::class, [
'canAuthenticateImap' => true,
]);
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(123);
$account->method('getUserId')->willReturn('user123');
$account->method('getMailAccount')->willReturn($mailAccount);
$this->serviceMock->getParameter('accountService')
->expects(self::once())
->method('findById')
Expand Down
37 changes: 15 additions & 22 deletions tests/Unit/Job/PreviewEnhancementProcessingJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,54 +95,47 @@ public function testNoAccount(): void {
}

public function testNoUser(): void {
$mailAccount = new MailAccount();
$mailAccount->setUserId(1);
$account = new Account($mailAccount);
$mailAccount = $this->createMock(MailAccount::class);
$mailAccount->method('canAuthenticateImap')->willReturn(true);
$account = $this->createMock(Account::class);
$account->method('getUserId')->willReturn('user123');
$account->method('getMailAccount')->willReturn($mailAccount);

$this->accountService->expects(self::once())
->method('findById')
->with(self::$argument['accountId'])
->willReturn($account);
$this->manager->expects(self::once())
->method('get')
->with($account->getUserId())
->willReturn(null);
->method('get');
$this->logger->expects(self::once())
->method('debug');

$this->job->run(self::$argument);
}

public function testProvisionedNoPassword(): void {
$mailAccount = new MailAccount();
$mailAccount->setUserId(1);
$mailAccount->setProvisioningId(1);
$mailAccount->setInboundPassword(null);
$mailAccount = $this->createMock(MailAccount::class);
$mailAccount->method('canAuthenticateImap')->willReturn(false);
$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->getUserId())
->willReturn($user);
$user->expects(self::once())
->method('isEnabled')
->willReturn(true);
$this->manager->expects(self::never())
->method('get');
$this->logger->expects(self::once())
->method('info');

$this->job->run(self::$argument);
}

public function testProcessing(): void {
$mailAccount = new MailAccount();
$mailAccount->setUserId(1);
$account = new Account($mailAccount);
$mailAccount = $this->createMock(MailAccount::class);
$mailAccount->method('canAuthenticateImap')->willReturn(true);
$account = $this->createMock(Account::class);
$account->method('getUserId')->willReturn('user123');
$account->method('getMailAccount')->willReturn($mailAccount);
$time = time();
$user = $this->createMock(IUser::class);
$user->setEnabled();
Expand Down

0 comments on commit 133da74

Please sign in to comment.