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

test: fix redis not being used properly in imapClientFactoryTest #10076

Merged
merged 1 commit into from
Sep 24, 2024
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
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
Loading