[Web][Rspamd] Keep the global archive switch in Redis only

This commit is contained in:
FreddleSpl0it committed 2026-11-09 01:09:43 +01:00
1 parent 2fc6868b72
commit c48691fca7
15 files changed
+78 -55

No files matched your search

@@ -29,7 +29,6 @@ class JournalServiceTest extends TestCase
protected function setUp(): void
{
$this->stored = new Settings();
$this->stored->enabled = true;
$this->stored->address = 'journal@archive.example';
$this->stored->signingKey = 'key';
$this->lookup = new FakeArchiveLookup();
@@ -38,6 +37,7 @@ class JournalServiceTest extends TestCase
$this->lookup->aliases[$username] = $username;
}
$this->redis = new FakeRedis();
$this->redis->strings['ARCHIVE_ENABLED'] = '1';
$this->smtp = new FakeSmtpClient();
}
@@ -87,9 +87,8 @@ class JournalServiceTest extends TestCase
$this->stored->signingKey = '';
$this->assertSame(['archive' => false], $this->service()->prepare(['from' => 'x@extern.example', 'rcpt' => ['anna@firma.de']]));
$this->stored->enabled = false;
$this->redis->strings['ARCHIVE_ENABLED'] = '0';
$this->assertSame(['archive' => false], $this->service()->prepare(['from' => 'x@extern.example', 'rcpt' => ['anna@firma.de']]));
$this->assertSame('0', $this->redis->strings['ARCHIVE_ENABLED']);
$this->assertSame([], $this->smtp->submitted);
}
@@ -140,7 +139,7 @@ class JournalServiceTest extends TestCase
public function testTestMail(): void
{
$this->stored->enabled = false;
$this->redis->strings['ARCHIVE_ENABLED'] = '0';
$this->assertSame('Q1', $this->service(FakeContext::admin())->testMail());
@@ -4,21 +4,23 @@ declare(strict_types=1);
namespace Mailcow\Tests\Archive\Service;
use Mailcow\Archive\Model\Settings;
use Mailcow\Archive\Repository\SettingsRepository;
use Mailcow\Archive\Redis\ArchiveState;
use Mailcow\Archive\Service\MailboxListService;
use Mailcow\Core\AccessDenied;
use Mailcow\Core\Context;
use Mailcow\Tests\Support\FakeArchiveLookup;
use Mailcow\Tests\Support\FakeContext;
use Mailcow\Tests\Support\FakeRedis;
use PHPUnit\Framework\TestCase;
class MailboxListServiceTest extends TestCase
{
private FakeArchiveLookup $lookup;
private FakeRedis $redis;
protected function setUp(): void
{
$this->redis = new FakeRedis();
$this->lookup = new FakeArchiveLookup();
// username => [mailbox setting, domain default]
$this->lookup->mailboxes = [
@@ -31,11 +33,8 @@ class MailboxListServiceTest extends TestCase
private function service(bool $enabled, ?Context $context = null): MailboxListService
{
$settings = new Settings();
$settings->enabled = $enabled;
$repo = $this->createStub(SettingsRepository::class);
$repo->method('load')->willReturn($settings);
return new MailboxListService($this->lookup, $repo, $context ?? FakeContext::admin());
$this->redis->strings['ARCHIVE_ENABLED'] = $enabled ? '1' : '0';
return new MailboxListService($this->lookup, new ArchiveState($this->redis), $context ?? FakeContext::admin());
}
public function testArchivedMailboxesWithTheirSource(): void
@@ -15,7 +15,6 @@ use Mailcow\Tests\Support\FakeArchiveLookup;
use Mailcow\Tests\Support\FakeContext;
use Mailcow\Tests\Support\FakeRedis;
use Mailcow\Tests\Support\FakeSmtpClient;
use PDOException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
@@ -74,12 +73,19 @@ class SettingsServiceTest extends TestCase
$this->assertSame('', $this->service()->save(['password' => '', 'password_remove' => '1'])->password);
}
public function testEnabledIsOffWithoutTheTable(): void
public function testSwitchIsKeptInRedisOnly(): void
{
$repo = $this->createMock(SettingsRepository::class);
$repo->method('load')->willThrowException(new PDOException('no such table'));
$this->assertFalse((new SettingsService($repo, $this->lookup, new ArchiveState($this->redis), $this->smtp, FakeContext::admin()))->enabled());
$this->assertFalse($this->service()->enabled());
$this->assertSame(0, $this->service()->get()->toArray()['enabled']);
$this->service()->save(['enabled' => '1']);
$this->assertSame('1', $this->redis->strings['ARCHIVE_ENABLED']);
$this->assertSame(1, $this->service()->get()->toArray()['enabled']);
// saving other settings keeps the switch
$this->service()->save(['port' => '2525']);
$this->assertTrue($this->service()->enabled());
// a lost Redis key shows as off, as rspamd sees it
unset($this->redis->strings['ARCHIVE_ENABLED']);
$this->assertFalse($this->service()->enabled());
}
public function testAddressOutsideTheMailcowDomains(): void
@@ -24,12 +24,12 @@ class StatusServiceTest extends TestCase
protected function setUp(): void
{
$this->redis = new FakeRedis();
$this->redis->strings['ARCHIVE_ENABLED'] = '1';
}
private function service(?Context $context = null): StatusService
{
$settings = new Settings();
$settings->enabled = true;
$settings->address = 'journal@archive.example';
$settings->profile = 'none';
$repo = $this->createStub(SettingsRepository::class);
@@ -70,6 +70,12 @@ class StatusServiceTest extends TestCase
], $this->service()->status());
}
public function testSwitchFromRedis(): void
{
$this->redis->strings['ARCHIVE_ENABLED'] = '0';
$this->assertSame(0, $this->service()->status()['enabled']);
}
public function testLogWithMessageIds(): void
{
(new ArchiveState($this->redis))->dropped('4XyZ1', 'owner lookup failed', 'm1@firma.de');