mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-11-09 10:21:45 +00:00
[Web][Rspamd] Keep the global archive switch in Redis only
This commit is contained in:
15 files changed
+78
-55
No files matched your search
@@ -5,13 +5,13 @@ declare(strict_types=1);
|
||||
namespace Mailcow\Tests\Archive\Model;
|
||||
|
||||
use Mailcow\Archive\Model\Mailbox;
|
||||
use Mailcow\Archive\Redis\ArchiveState;
|
||||
use Mailcow\Archive\Service\MailboxListService;
|
||||
use Mailcow\Archive\Repository\SettingsRepository;
|
||||
use Mailcow\Archive\Model\Settings;
|
||||
use Mailcow\Core\Context;
|
||||
use Mailcow\Core\UserError;
|
||||
use Mailcow\Tests\Support\FakeArchiveLookup;
|
||||
use Mailcow\Tests\Support\FakeContext;
|
||||
use Mailcow\Tests\Support\FakeRedis;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -43,20 +43,18 @@ class MailboxTest extends TestCase
|
||||
{
|
||||
$lookup = new FakeArchiveLookup();
|
||||
$lookup->mailboxes = ['b@firma.de' => ['inherit', 1], 'a@other.de' => ['1', 0], 'c@firma.de' => ['0', 1]];
|
||||
$settings = new Settings();
|
||||
$repo = $this->createMock(SettingsRepository::class);
|
||||
$repo->method('load')->willReturnCallback(fn(): Settings => $settings);
|
||||
$redis = new FakeRedis();
|
||||
$readOnly = new Context('admin', 'API', [], false, 'ro');
|
||||
|
||||
$this->assertSame(['enabled' => 0, 'mailboxes' => []], (new MailboxListService($lookup, $repo, $readOnly))->archived());
|
||||
$this->assertSame(['enabled' => 0, 'mailboxes' => []], (new MailboxListService($lookup, new ArchiveState($redis), $readOnly))->archived());
|
||||
|
||||
$settings->enabled = true;
|
||||
$redis->strings['ARCHIVE_ENABLED'] = '1';
|
||||
$this->assertSame(['enabled' => 1, 'mailboxes' => [
|
||||
['username' => 'b@firma.de', 'domain' => 'firma.de', 'name' => '', 'active' => 1, 'source' => 'domain'],
|
||||
['username' => 'a@other.de', 'domain' => 'other.de', 'name' => '', 'active' => 1, 'source' => 'mailbox'],
|
||||
]], (new MailboxListService($lookup, $repo, $readOnly))->archived());
|
||||
]], (new MailboxListService($lookup, new ArchiveState($redis), $readOnly))->archived());
|
||||
|
||||
$this->expectException(UserError::class);
|
||||
(new MailboxListService($lookup, $repo, FakeContext::domainAdmin()))->archived();
|
||||
(new MailboxListService($lookup, new ArchiveState($redis), FakeContext::domainAdmin()))->archived();
|
||||
}
|
||||
}
|
||||
@@ -25,12 +25,25 @@ class ArchiveStateTest extends TestCase
|
||||
$this->redis->lPush('POSTFIX_MAILLOG', json_encode(['time' => $time, 'program' => $program, 'message' => $message]));
|
||||
}
|
||||
|
||||
public function testSwitchIsMirroredForRspamd(): void
|
||||
public function testGlobalSwitchIsKeptHere(): void
|
||||
{
|
||||
// no key yet: off
|
||||
$this->assertFalse($this->state->enabled());
|
||||
$this->state->setEnabled(true);
|
||||
$this->assertSame('1', $this->redis->strings['ARCHIVE_ENABLED']);
|
||||
$this->assertTrue($this->state->enabled());
|
||||
$this->state->setEnabled(false);
|
||||
$this->assertSame('0', $this->redis->strings['ARCHIVE_ENABLED']);
|
||||
$this->assertFalse($this->state->enabled());
|
||||
// no connection or a failing one: off, rspamd cannot read the switch either
|
||||
$this->assertFalse((new ArchiveState(null))->enabled());
|
||||
$failing = new class {
|
||||
public function get(string $key): string
|
||||
{
|
||||
throw new \RuntimeException('connection lost');
|
||||
}
|
||||
};
|
||||
$this->assertFalse((new ArchiveState($failing))->enabled());
|
||||
}
|
||||
|
||||
public function testDroppedCopiesAreCountedAndTheLast100Listed(): void
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user