[Web][Tests] Add PHPUnit setup and imapsync module tests

This commit is contained in:
FreddleSpl0it committed 2026-10-22 10:07:52 +02:00
1 parent b2a152fe45
commit e756b7857e
18 files changed
+3948 -1

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Mailcow\Tests\Core;
use Mailcow\Core\AccessDenied;
use Mailcow\Core\ApiResponse;
use Mailcow\Core\UserError;
use PHPUnit\Framework\TestCase;
class ApiResponseTest extends TestCase
{
protected function setUp(): void
{
$_SESSION = [];
}
public function testMessagesUseTheMailcowFormat(): void
{
$response = new ApiResponse();
$this->assertTrue($response->success(['mailbox_modified', 'a@example.org'], ['syncjob', 'add', 'job', []]));
$this->assertFalse($response->danger('access_denied', ['syncjob', 'edit', 'job', []]));
$this->assertSame([
['type' => 'success', 'log' => ['syncjob', 'add', 'job', []], 'msg' => ['mailbox_modified', 'a@example.org']],
['type' => 'danger', 'log' => ['syncjob', 'edit', 'job', []], 'msg' => 'access_denied'],
], $response->messages());
}
public function testSecretsAreMaskedInArrayLogParts(): void
{
$response = new ApiResponse(['password1', 'oauth_client_secret']);
$response->success('ok', ['syncjob', 'add', 'job', [
'password1' => 'secret',
'oauth_client_secret' => '',
'user1' => 'remote',
'nested' => ['password1' => 'kept'],
]]);
$this->assertSame(['syncjob', 'add', 'job', [
'password1' => '*',
'oauth_client_secret' => '',
'user1' => 'remote',
'nested' => ['password1' => 'kept'],
]], $response->messages()[0]['log']);
}
public function testNullSecretStaysNull(): void
{
$response = new ApiResponse(['password1']);
$response->danger('x', ['syncjob', 'add', 'job', ['password1' => null]]);
$this->assertSame(['password1' => null], $response->messages()[0]['log'][3]);
}
public function testUserErrorBecomesDangerMessage(): void
{
$response = new ApiResponse();
$this->assertFalse($response->error(new UserError('object_exists', ['name']), ['l']));
$response->error(new AccessDenied(), ['l']);
$this->assertSame(['object_exists', 'name'], $response->messages()[0]['msg']);
$this->assertSame('danger', $response->messages()[0]['type']);
$this->assertSame('access_denied', $response->messages()[1]['msg']);
}
public function testFlushAppendsToSessionAndStartsOver(): void
{
$_SESSION['return'] = [['type' => 'success', 'log' => [], 'msg' => 'earlier']];
$response = new ApiResponse();
$response->success('first', ['l']);
$response->danger('second', ['l']);
$response->flush();
$response->flush();
$this->assertSame(['earlier', 'first', 'second'], array_column($_SESSION['return'], 'msg'));
$this->assertSame([], $response->messages());
}
public function testFlushWithoutMessagesLeavesSessionUntouched(): void
{
(new ApiResponse())->flush();
$this->assertArrayNotHasKey('return', $_SESSION);
}
}
+84
View File
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Mailcow\Tests\Core;
use Mailcow\Core\Context;
use Mailcow\Tests\Support\LegacyAclCalls;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../Support/legacy_acl_functions.php';
class ContextTest extends TestCase
{
protected function setUp(): void
{
LegacyAclCalls::$calls = [];
}
public function testFromSessionMapsRoleUsernameAclAndAccessAll(): void
{
$context = Context::fromSession([
'mailcow_cc_role' => 'domainadmin',
'mailcow_cc_username' => 'da',
'acl' => ['syncjobs' => '1', 'spam_score' => '0'],
'access_all_exception' => '1',
]);
$this->assertSame('domainadmin', $context->role);
$this->assertSame('da', $context->username);
$this->assertTrue($context->isDomainAdmin());
$this->assertFalse($context->isAdmin());
$this->assertFalse($context->isUser());
$this->assertTrue($context->hasAcl('syncjobs'));
$this->assertFalse($context->hasAcl('spam_score'));
$this->assertFalse($context->hasAcl('missing'));
$this->assertTrue($context->accessAll);
}
public function testEmptySessionHasNoRights(): void
{
$context = Context::fromSession([]);
$this->assertSame('', $context->role);
$this->assertNull($context->username);
$this->assertFalse($context->isAdmin() || $context->isDomainAdmin() || $context->isUser());
$this->assertFalse($context->hasAcl('syncjobs'));
$this->assertFalse($context->accessAll);
}
public function testAclAndAccessAllAcceptIntegerOneLikeTheLegacyCode(): void
{
$context = Context::fromSession(['mailcow_cc_role' => 'user', 'acl' => ['syncjobs' => 1], 'access_all_exception' => 1]);
$this->assertTrue($context->hasAcl('syncjobs'));
$this->assertTrue($context->accessAll);
$this->assertFalse(Context::fromSession(['access_all_exception' => '0'])->accessAll);
$this->assertFalse(Context::fromSession(['acl' => 'not an array'])->hasAcl('syncjobs'));
}
public function testSystemContextIsNoAccount(): void
{
$context = Context::system();
$this->assertSame('system', $context->role);
$this->assertNull($context->username);
$this->assertFalse($context->isAdmin());
$this->assertFalse($context->hasAcl('syncjobs'));
}
public function testBridgePassesUsernameAndRoleToTheLegacyFunctions(): void
{
$context = new Context('user', 'user@example.org');
$this->assertTrue($context->canAccessMailbox('allowed@example.org'));
$this->assertFalse($context->canAccessMailbox('other@example.org'));
$this->assertTrue($context->canAccessDomain('example.org'));
$this->assertSame([
['mailbox', 'user@example.org', 'user', 'allowed@example.org'],
['mailbox', 'user@example.org', 'user', 'other@example.org'],
['domain', 'user@example.org', 'user', 'example.org'],
], LegacyAclCalls::$calls);
}
}