fix: harden backend synchronization

This commit is contained in:
Marek Miklewicz
2026-06-02 21:06:25 +02:00
parent 4b531a4c24
commit 64b62a5793
20 changed files with 254 additions and 16 deletions
+12
View File
@@ -18,3 +18,15 @@ test('directadmin vacation api builds scoped payload for one mailbox', function
assert_same('morning', $payload['starttime']);
assert_same('evening', $payload['endtime']);
});
test('directadmin vacation api detects existing native vacation by mailbox', function (): void {
$api = new DirectAdminVacationApi('/usr/local/directadmin/directadmin', static function (string $owner, string $endpoint, array $payload): string {
assert_same('alice', $owner);
assert_same('CMD_API_EMAIL_VACATION', $endpoint);
assert_same(['domain' => 'example.com'], $payload);
return 'info=starttime%3Dmorning%26text%3DBody&sales=starttime%3Devening';
});
assert_true($api->vacationExists('alice', 'info@example.com'));
assert_false($api->vacationExists('alice', 'new@example.com'));
});
@@ -28,6 +28,10 @@ test('directadmin vacation sync creates tracked entries for enabled rule mailbox
$calls = [];
$api = new class($calls) extends DirectAdminVacationApi {
public function __construct(private array &$calls) {}
public function vacationExists(string $owner, string $email): bool
{
return false;
}
public function saveVacation(string $owner, string $email, array $rule, bool $exists): void
{
$this->calls[] = ['save', $owner, $email, $exists, $rule['subject']];
@@ -94,3 +98,52 @@ test('directadmin vacation sync deletes stale tracked entries after rule removal
assert_same([['delete', 'alice', 'info@example.com']], $calls);
assert_same([], $syncRepo->entriesForRule('alice', (string)$rule['id']));
});
test('directadmin vacation sync refuses to overwrite untracked native vacation', function (): void {
$virtualRoot = TEST_TMP . '/da-sync-conflict-virtual';
test_write($virtualRoot . '/domainowners', "example.com: alice\n");
test_write($virtualRoot . '/example.com/passwd', "info:x:1000:12::/home/alice/imap/example.com/info:/bin/false\n");
$dataDir = TEST_TMP . '/da-sync-conflict-data';
$repo = new RuleRepository($dataDir);
$repo->create('alice', [
'subject' => 'Urlop',
'body' => 'Body',
'start_value' => '2026-06-10|morning',
'end_value' => '2026-06-24|evening',
'start_ts' => 1,
'end_ts' => 2,
'enabled' => true,
'dynamic_scope' => true,
]);
$calls = [];
$api = new class($calls) extends DirectAdminVacationApi {
public function __construct(private array &$calls) {}
public function vacationExists(string $owner, string $email): bool
{
return $email === 'info@example.com';
}
public function saveVacation(string $owner, string $email, array $rule, bool $exists): void
{
$this->calls[] = ['save', $owner, $email];
}
};
$sync = new DirectAdminVacationSyncService(
$repo,
new DirectAdminSyncRepository($dataDir),
new MailboxDirectory($virtualRoot),
$api
);
$thrown = false;
try {
$sync->syncUser('alice');
} catch (RuntimeException $e) {
$thrown = true;
assert_contains('Untracked DirectAdmin vacation already exists', $e->getMessage());
}
assert_true($thrown);
assert_same([], $calls);
});
+55
View File
@@ -112,3 +112,58 @@ test('calendar picker is based on selected rule month and supports month navigat
assert_contains('Luty 2027', $html);
assert_false(strpos($html, 'Czerwiec 2026') !== false);
});
test('rule form localizes calendar and preview labels without translating user content globally', function (): void {
$settingsPath = TEST_TMP . '/form-i18n.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=en\nGLOBAL_AUTORESPONDER_BACKEND=exim\n");
$settings = Settings::load($settingsPath);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'en', TEST_TMP . '/config'),
$settings,
new RuleRepository(TEST_TMP . '/data'),
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('en', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$html = render_rule_form($ctx, 'create', null);
assert_contains('<th>Mon</th>', $html);
assert_contains('Selected:', $html);
assert_false(strpos($html, 'Wybrano') !== false);
assert_false(strpos($html, '<th>Pn</th>') !== false);
$preview = render_preview_box($ctx, ['subject' => 'Delete', 'body' => 'Cancel']);
assert_contains('<strong>Subject:</strong> Delete', $preview);
assert_contains('Cancel', $preview);
});
test('directadmin backend allows only one enabled global rule per account', function (): void {
$settingsPath = TEST_TMP . '/form-da-single.conf';
test_write($settingsPath, "DEFAULT_PLUGIN_LANGUAGE=pl\nGLOBAL_AUTORESPONDER_BACKEND=da\n");
$settings = Settings::load($settingsPath);
$repo = new RuleRepository(TEST_TMP . '/single-data');
$repo->create('alice', [
'subject' => 'Pierwsza',
'body' => 'Body',
'enabled' => true,
'start_value' => '2026-06-10|morning',
'end_value' => '2026-06-24|evening',
]);
$ctx = new AppContext(
new DirectAdminUser('alice', 'enhanced', 'pl', TEST_TMP . '/config'),
$settings,
$repo,
new CsrfGuard('secret', 'alice', 'sid'),
Lang::load('pl', $settings),
new AuditLog(TEST_TMP . '/audit.log')
);
$thrown = false;
try {
enforce_rule_backend_constraints($ctx, null, ['enabled' => true]);
} catch (RuntimeException $e) {
$thrown = true;
assert_contains('Backend DirectAdmin obsługuje tylko jedną aktywną regułę globalną.', $e->getMessage());
}
assert_true($thrown);
});
+13
View File
@@ -6,6 +6,7 @@ test('packing guidelines exclude docs and allow later manual icon', function ():
assert_contains('docs/dokumentacja.html', $packing);
assert_contains('must not contain `docs/`', $packing);
assert_contains('may be added later at `src/images/user_icon.svg`', $packing);
assert_contains("scripts/package.sh", $packing);
});
test('plugin metadata runs user level as root on directadmin 1.689 plus', function (): void {
@@ -44,3 +45,15 @@ test('backend script protects custom exim config with rollback and exact section
assert_contains('if ! awk -v section="$section" -v snippet="$snippet"', $script);
assert_contains('if (inserted != 1)', $script);
});
test('lifecycle scripts install cron sync and remove exim integration on uninstall', function (): void {
$install = file_get_contents(PLUGIN_ROOT . '/scripts/install.sh') ?: '';
$update = file_get_contents(PLUGIN_ROOT . '/scripts/update.sh') ?: '';
$uninstall = file_get_contents(PLUGIN_ROOT . '/scripts/uninstall.sh') ?: '';
assert_contains('/etc/cron.d/global-autoresponder', $install);
assert_contains('sync_directadmin_vacations.php', $install);
assert_contains('/etc/cron.d/global-autoresponder', $update);
assert_contains('sync_directadmin_vacations.php', $update);
assert_contains('backend.sh" da', $uninstall);
});