mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-11-07 17:29:21 +00:00
70 lines
2.5 KiB
PHP
70 lines
2.5 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|
|
}
|