Skip to content

Commit

Permalink
test: fix redis not being used properly in imapClientFactoryTest
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Steinmetz <[email protected]>
  • Loading branch information
st3iny committed Sep 23, 2024
1 parent d42f23a commit d9c84f9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
50 changes: 50 additions & 0 deletions tests/Integration/Framework/Caching.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

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

namespace OCA\Mail\Tests\Integration\Framework;

use OC\Memcache\Factory;
use OCA\Mail\Cache\HordeCacheFactory;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\Profiler\IProfiler;
use OCP\Security\ICrypto;
use OCP\Server;
use Psr\Log\LoggerInterface;

class Caching {
/**
* Force usage of a real cache as configured in system config. The original ICacheFactory
* service closure is hard-coded to always return an instance of ArrayCache when the global
* PHPUNIT_RUN is defined.
*/
public static function getImapClientFactoryAndConfiguredCacheFactory(?ICrypto $crypto = null): array {
$config = Server::get(IConfig::class);
$cacheFactory = new Factory(
static fn () => 'mail-integration-tests',
Server::get(LoggerInterface::class),
Server::get(IProfiler::class),
$config->getSystemValue('memcache.local', null),
$config->getSystemValue('memcache.distributed', null),
$config->getSystemValue('memcache.locking', null),
$config->getSystemValueString('redis_log_file')
);
$imapClient = new IMAPClientFactory(
$crypto ?? Server::get(ICrypto::class),
$config,
$cacheFactory,
Server::get(IEventDispatcher::class),
Server::get(ITimeFactory::class),
Server::get(HordeCacheFactory::class),
);
return [$imapClient, $cacheFactory];
}
}
11 changes: 10 additions & 1 deletion tests/Integration/IMAP/IMAPClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCA\Mail\Db\MailAccount;
use OCA\Mail\IMAP\HordeImapClient;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Tests\Integration\Framework\Caching;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -110,13 +111,21 @@ public function testRateLimiting(): void {
if (ltrim($cacheClass, '\\') !== Redis::class) {
$this->markTestSkipped('Redis not available. Found ' . $cacheClass);
}

[$imapClientFactory, $cacheFactory] = Caching::getImapClientFactoryAndConfiguredCacheFactory($this->crypto);
$this->assertInstanceOf(
Redis::class,
$cacheFactory->createDistributed(),
'Distributed cache is not Redis',
);

$account = $this->getTestAccount();
$this->crypto->expects($this->once())
->method('decrypt')
->with('encrypted')
->willReturn('notmypassword');

$client = $this->factory->getClient($account);
$client = $imapClientFactory->getClient($account);
self::assertInstanceOf(HordeImapClient::class, $client);
foreach ([1, 2, 3] as $attempts) {
try {
Expand Down

0 comments on commit d9c84f9

Please sign in to comment.