[Web][Tests] Add archive module tests

This commit is contained in:
FreddleSpl0it committed 2026-11-07 00:18:58 +01:00
1 parent 2ac2edf0ad
commit fc56d53029
14 files changed
+1403

No files matched your search

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Mailcow\Tests\Archive\Service;
use Mailcow\Archive\Model\Settings;
use Mailcow\Archive\Repository\SettingsRepository;
use Mailcow\Archive\Service\MailboxListService;
use Mailcow\Core\AccessDenied;
use Mailcow\Core\Context;
use Mailcow\Tests\Support\FakeArchiveLookup;
use Mailcow\Tests\Support\FakeContext;
use PHPUnit\Framework\TestCase;
class MailboxListServiceTest extends TestCase
{
private FakeArchiveLookup $lookup;
protected function setUp(): void
{
$this->lookup = new FakeArchiveLookup();
// username => [mailbox setting, domain default]
$this->lookup->mailboxes = [
'own@firma.de' => ['1', 0],
'inherit@firma.de' => ['inherit', 1],
'excluded@firma.de' => ['0', 1],
'plain@other.example' => ['inherit', 0],
];
}
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());
}
public function testArchivedMailboxesWithTheirSource(): void
{
$expected = ['enabled' => 1, 'mailboxes' => [
['username' => 'own@firma.de', 'domain' => 'firma.de', 'name' => '', 'active' => 1, 'source' => 'mailbox'],
['username' => 'inherit@firma.de', 'domain' => 'firma.de', 'name' => '', 'active' => 1, 'source' => 'domain'],
]];
$this->assertSame($expected, $this->service(true)->archived());
// read-only API keys too: archivecow keeps its mailboxes in line with this list
$this->assertSame($expected, $this->service(true, new Context('admin', 'API', [], false, 'ro'))->archived());
}
public function testEmptyWhileArchivingIsOff(): void
{
$this->assertSame(['enabled' => 0, 'mailboxes' => []], $this->service(false)->archived());
}
public function testOnlyAdmins(): void
{
foreach ([FakeContext::domainAdmin(['own@firma.de'], ['firma.de'], ['archive' => '1']), FakeContext::user('own@firma.de')] as $context) {
try {
$this->service(true, $context)->archived();
$this->fail('AccessDenied expected');
} catch (AccessDenied) {
$this->addToAssertionCount(1);
}
}
}
}