|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Unit\IMAP; |
| 11 | + |
| 12 | +use ChristophWurst\Nextcloud\Testing\TestCase; |
| 13 | +use Horde_Imap_Client_Exception; |
| 14 | +use OCA\Mail\IMAP\HordeImapClient; |
| 15 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 16 | +use OCP\IMemcache; |
| 17 | +use OCP\IMemcacheTTL; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | + |
| 20 | +interface IMemcacheWithTTL extends IMemcache, IMemcacheTTL { |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Testable subclass that stubs out the real IMAP connection. |
| 25 | + */ |
| 26 | +class TestableHordeImapClient extends HordeImapClient { |
| 27 | + public function __construct() { |
| 28 | + // Skip Horde constructor — we only test rate limiter logic. |
| 29 | + } |
| 30 | + |
| 31 | + protected function imapLogin() { |
| 32 | + throw new Horde_Imap_Client_Exception( |
| 33 | + 'Authentication failed.', |
| 34 | + Horde_Imap_Client_Exception::LOGIN_AUTHENTICATIONFAILED, |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class HordeImapClientTest extends TestCase { |
| 40 | + private IMemcacheWithTTL|MockObject $cache; |
| 41 | + private ITimeFactory|MockObject $timeFactory; |
| 42 | + private TestableHordeImapClient $client; |
| 43 | + |
| 44 | + protected function setUp(): void { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->cache = $this->createMock(IMemcacheWithTTL::class); |
| 48 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
| 49 | + |
| 50 | + $this->client = new TestableHordeImapClient(); |
| 51 | + $this->client->enableRateLimiter($this->cache, 'testhash', $this->timeFactory); |
| 52 | + } |
| 53 | + |
| 54 | + public function testSetsTtlOnAuthFailure(): void { |
| 55 | + $this->timeFactory->method('getTime')->willReturn(100000); |
| 56 | + $expectedKey = 'testhash9'; |
| 57 | + $this->cache->method('get')->with($expectedKey)->willReturn(null); |
| 58 | + $this->cache->expects($this->once())->method('inc')->with($expectedKey); |
| 59 | + $this->cache->expects($this->once())->method('setTTL')->with($expectedKey, 3 * 60 * 60); |
| 60 | + |
| 61 | + $this->expectException(Horde_Imap_Client_Exception::class); |
| 62 | + $this->client->login(); |
| 63 | + } |
| 64 | +} |
0 commit comments