mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-11-07 01:09:21 +00:00
[Web][Tests] Add archive module tests
This commit is contained in:
14 files changed
+1403
No files matched your search
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Tests\Archive\Model;
|
||||
|
||||
use Mailcow\Archive\Model\Mailbox;
|
||||
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 PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MailboxTest extends TestCase
|
||||
{
|
||||
public static function settings(): array
|
||||
{
|
||||
return [
|
||||
'inherit, domain on' => ['inherit', 1, true],
|
||||
'inherit, domain off' => ['inherit', 0, false],
|
||||
'missing attribute' => [null, 1, true],
|
||||
'unknown value' => ['yes', 0, false],
|
||||
'on' => ['1', 0, true],
|
||||
'on as number' => [1, 0, true],
|
||||
'off' => ['0', 1, false],
|
||||
'off as number' => [0, 1, false],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('settings')]
|
||||
public function testEffective(mixed $archive, int $domainArchive, bool $archived): void
|
||||
{
|
||||
$this->assertSame($archived, Mailbox::effective($archive, $domainArchive));
|
||||
}
|
||||
|
||||
public function testListOfArchivedMailboxes(): void
|
||||
{
|
||||
$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);
|
||||
$readOnly = new Context('admin', 'API', [], false, 'ro');
|
||||
|
||||
$this->assertSame(['enabled' => 0, 'mailboxes' => []], (new MailboxListService($lookup, $repo, $readOnly))->archived());
|
||||
|
||||
$settings->enabled = true;
|
||||
$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());
|
||||
|
||||
$this->expectException(UserError::class);
|
||||
(new MailboxListService($lookup, $repo, FakeContext::domainAdmin()))->archived();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Tests\Archive\Model;
|
||||
|
||||
use Mailcow\Archive\Model\Settings;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SettingsTest extends TestCase
|
||||
{
|
||||
public static function nexthops(): array
|
||||
{
|
||||
return [
|
||||
'mx of the address domain' => ['', 25, 'archive.example'],
|
||||
'mx, other port' => ['', 2525, 'archive.example:2525'],
|
||||
'host' => ['smtp.archive.example', 25, '[smtp.archive.example]:25'],
|
||||
'ipv4' => ['192.0.2.5', 587, '[192.0.2.5]:587'],
|
||||
'ipv6' => ['2001:db8::25', 25, '[ipv6:2001:db8::25]:25'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('nexthops')]
|
||||
public function testNexthop(string $host, int $port, string $nexthop): void
|
||||
{
|
||||
$settings = new Settings();
|
||||
$settings->address = 'journal@archive.example';
|
||||
$settings->host = $host;
|
||||
$settings->port = $port;
|
||||
|
||||
$this->assertSame($nexthop, $settings->nexthop());
|
||||
}
|
||||
|
||||
public function testCanDeliverAndNoSecretsInTheApi(): void
|
||||
{
|
||||
$settings = new Settings();
|
||||
$this->assertFalse($settings->canDeliver());
|
||||
$settings->address = 'journal@archive.example';
|
||||
$this->assertFalse($settings->canDeliver());
|
||||
$settings->profile = 'envelope';
|
||||
$this->assertTrue($settings->canDeliver());
|
||||
|
||||
$settings->password = 'password-value';
|
||||
$settings->signingKey = 'signing-key-value';
|
||||
$api = json_encode($settings->toArray());
|
||||
$this->assertStringNotContainsString('password-value', $api);
|
||||
$this->assertStringNotContainsString('signing-key-value', $api);
|
||||
$this->assertSame([1, 1], [$settings->toArray()['has_password'], $settings->toArray()['has_signing_secret']]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user