mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-10-22 18:48:54 +00:00
[Web][Tests] Add PHPUnit setup and imapsync module tests
This commit is contained in:
18 files changed
+3948
-1
No files matched your search
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Tests\Support;
|
||||
|
||||
use Mailcow\Core\Context;
|
||||
|
||||
/**
|
||||
* Context with a fixed ACL world instead of the legacy hasMailboxObjectAccess()/hasDomainAccess() bridge:
|
||||
* like the legacy functions, the own mailbox is always accessible for a logged-in role.
|
||||
*/
|
||||
class FakeContext extends Context
|
||||
{
|
||||
private array $mailboxes;
|
||||
private array $domains;
|
||||
|
||||
public function __construct(string $role, ?string $username, array $acl = ['syncjobs' => '1'], bool $accessAll = false,
|
||||
array $mailboxes = [], array $domains = [])
|
||||
{
|
||||
parent::__construct($role, $username, $acl, $accessAll);
|
||||
$this->mailboxes = $mailboxes;
|
||||
$this->domains = $domains;
|
||||
}
|
||||
|
||||
public static function admin(array $acl = ['syncjobs' => '1']): self
|
||||
{
|
||||
return new self('admin', 'admin', $acl, false, ['*'], ['*']);
|
||||
}
|
||||
|
||||
public static function domainAdmin(array $mailboxes = [], array $domains = [], array $acl = ['syncjobs' => '1']): self
|
||||
{
|
||||
return new self('domainadmin', 'da', $acl, false, $mailboxes, $domains);
|
||||
}
|
||||
|
||||
public static function user(string $username = 'user@example.org', array $acl = ['syncjobs' => '1']): self
|
||||
{
|
||||
return new self('user', $username, $acl);
|
||||
}
|
||||
|
||||
public function canAccessMailbox(string $mailbox): bool
|
||||
{
|
||||
if ($this->accessAll) {
|
||||
return true;
|
||||
}
|
||||
if (!in_array($this->role, ['admin', 'domainadmin', 'user'], true) || $this->username === null) {
|
||||
return false;
|
||||
}
|
||||
return $mailbox === $this->username || in_array($mailbox, $this->mailboxes, true) || in_array('*', $this->mailboxes, true);
|
||||
}
|
||||
|
||||
public function canAccessDomain(string $domain): bool
|
||||
{
|
||||
return $this->accessAll || in_array($domain, $this->domains, true) || in_array('*', $this->domains, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Tests\Support;
|
||||
|
||||
use Mailcow\Imapsync\Http\OAuthClient;
|
||||
|
||||
/**
|
||||
* OAuthClient without network: resolve() answers from $addresses, send() records the curl options
|
||||
* and returns $response.
|
||||
*/
|
||||
class FakeOAuthClient extends OAuthClient
|
||||
{
|
||||
/** @var array<string, string[]> host => addresses */
|
||||
public array $addresses = [];
|
||||
/** @var array{0: string|false, 1: int} */
|
||||
public array $response = ['{}', 200];
|
||||
/** @var array[] curl options of every send() */
|
||||
public array $sent = [];
|
||||
/** @var string[] hosts passed to resolve() */
|
||||
public array $resolved = [];
|
||||
|
||||
protected function resolve(string $host): array
|
||||
{
|
||||
$this->resolved[] = $host;
|
||||
return $this->addresses[$host] ?? [];
|
||||
}
|
||||
|
||||
protected function send(array $options): array
|
||||
{
|
||||
$this->sent[] = $options;
|
||||
return $this->response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Tests\Support;
|
||||
|
||||
/**
|
||||
* Calls recorded by the legacy ACL stand-ins in legacy_acl_functions.php.
|
||||
*/
|
||||
class LegacyAclCalls
|
||||
{
|
||||
public static array $calls = [];
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
// Stand-ins for the legacy ACL functions of data/web/inc/functions.inc.php, used by ContextTest only:
|
||||
// they record the call and answer from a fixed table.
|
||||
|
||||
use Mailcow\Tests\Support\LegacyAclCalls;
|
||||
|
||||
if (!function_exists('hasMailboxObjectAccess')) {
|
||||
function hasMailboxObjectAccess($username, $role, $object)
|
||||
{
|
||||
LegacyAclCalls::$calls[] = ['mailbox', $username, $role, $object];
|
||||
return $object === 'allowed@example.org';
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('hasDomainAccess')) {
|
||||
function hasDomainAccess($username, $role, $domain)
|
||||
{
|
||||
LegacyAclCalls::$calls[] = ['domain', $username, $role, $domain];
|
||||
return $domain === 'example.org';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user