152 lines
5.4 KiB
PHP
152 lines
5.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once PLUGIN_ROOT . '/scripts/global_autoresponder_worker.php';
|
|
|
|
test('exim worker sends active rule for resolved mailbox only', function (): void {
|
|
$settingsPath = TEST_TMP . '/worker-settings.conf';
|
|
test_write($settingsPath, "GLOBAL_AUTORESPONDER_BACKEND=exim\n");
|
|
$settings = Settings::load($settingsPath);
|
|
|
|
$virtualRoot = TEST_TMP . '/worker-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");
|
|
|
|
$repo = new RuleRepository(TEST_TMP . '/worker-data');
|
|
$created = $repo->create('alice', [
|
|
'subject' => 'Urlop',
|
|
'subject_prefix' => 'Urlop',
|
|
'reply_every_message' => false,
|
|
'repeat_minutes' => 60,
|
|
'body' => 'Odpowiem później',
|
|
'start_ts' => 900,
|
|
'end_ts' => 1100,
|
|
'enabled' => true,
|
|
]);
|
|
|
|
$sent = [];
|
|
$worker = new GlobalAutoresponderWorker(
|
|
$settings,
|
|
$repo,
|
|
new MailboxDirectory($virtualRoot),
|
|
new RepeatState(TEST_TMP . '/worker-state', 1000),
|
|
static function (string $from, string $to, string $subject, string $body) use (&$sent): bool {
|
|
$sent[] = compact('from', 'to', 'subject', 'body');
|
|
return true;
|
|
},
|
|
1000
|
|
);
|
|
|
|
$count = $worker->handle(
|
|
['SENDER_ADDRESS' => 'person@example.net', 'LOCAL_PART' => 'info', 'DOMAIN' => 'example.com'],
|
|
"Auto-Submitted: no\nSubject: hello\n\nBody"
|
|
);
|
|
|
|
assert_same(1, $count);
|
|
assert_same('info@example.com', $sent[0]['from']);
|
|
assert_same('person@example.net', $sent[0]['to']);
|
|
assert_same('Urlop: hello', $sent[0]['subject']);
|
|
assert_same('Odpowiem później', $sent[0]['body']);
|
|
|
|
$count = $worker->handle(
|
|
['SENDER_ADDRESS' => 'person@example.net', 'RECIPIENT' => 'info@example.com'],
|
|
"Auto-Submitted: no\nSubject: hello\n\nBody"
|
|
);
|
|
assert_same(0, $count, 'repeat policy should block immediate second response');
|
|
|
|
assert_true(isset($created['id']));
|
|
});
|
|
|
|
test('exim worker suppresses automated messages and ignores unresolved recipients', function (): void {
|
|
$settingsPath = TEST_TMP . '/worker-suppress-settings.conf';
|
|
test_write($settingsPath, "GLOBAL_AUTORESPONDER_BACKEND=exim\n");
|
|
$settings = Settings::load($settingsPath);
|
|
|
|
$virtualRoot = TEST_TMP . '/worker-suppress-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");
|
|
test_write($virtualRoot . '/example.com/aliases', "alias: info\n");
|
|
|
|
$repo = new RuleRepository(TEST_TMP . '/worker-suppress-data');
|
|
$repo->create('alice', [
|
|
'subject_prefix' => 'Urlop',
|
|
'reply_every_message' => true,
|
|
'repeat_minutes' => 0,
|
|
'body' => 'Body',
|
|
'start_ts' => 1,
|
|
'end_ts' => 2000,
|
|
'enabled' => true,
|
|
]);
|
|
|
|
$sent = 0;
|
|
$worker = new GlobalAutoresponderWorker(
|
|
$settings,
|
|
$repo,
|
|
new MailboxDirectory($virtualRoot),
|
|
new RepeatState(TEST_TMP . '/worker-suppress-state', 1000),
|
|
static function () use (&$sent): bool {
|
|
$sent++;
|
|
return true;
|
|
},
|
|
1000
|
|
);
|
|
|
|
assert_same(0, $worker->handle(
|
|
['SENDER_ADDRESS' => 'person@example.net', 'RECIPIENT' => 'info@example.com'],
|
|
"Precedence: bulk\n\nBody"
|
|
));
|
|
assert_same(0, $worker->handle(
|
|
['SENDER_ADDRESS' => 'person@example.net', 'RECIPIENT' => 'alias@example.com'],
|
|
"Auto-Submitted: no\n\nBody"
|
|
));
|
|
assert_same(0, $sent);
|
|
});
|
|
|
|
test('exim worker respects static mailbox snapshot scope', function (): void {
|
|
$settingsPath = TEST_TMP . '/worker-snapshot-settings.conf';
|
|
test_write($settingsPath, "GLOBAL_AUTORESPONDER_BACKEND=exim\n");
|
|
$settings = Settings::load($settingsPath);
|
|
|
|
$virtualRoot = TEST_TMP . '/worker-snapshot-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\nsales:x:1000:12::/home/alice/imap/example.com/sales:/bin/false\n");
|
|
|
|
$repo = new RuleRepository(TEST_TMP . '/worker-snapshot-data');
|
|
$repo->create('alice', [
|
|
'subject_prefix' => 'Urlop',
|
|
'reply_every_message' => true,
|
|
'repeat_minutes' => 0,
|
|
'body' => 'Body',
|
|
'start_ts' => 1,
|
|
'end_ts' => 2000,
|
|
'enabled' => true,
|
|
'dynamic_scope' => false,
|
|
'mailbox_snapshot' => [
|
|
['domain' => 'example.com', 'mailbox' => 'info', 'email' => 'info@example.com'],
|
|
],
|
|
]);
|
|
|
|
$sent = 0;
|
|
$worker = new GlobalAutoresponderWorker(
|
|
$settings,
|
|
$repo,
|
|
new MailboxDirectory($virtualRoot),
|
|
new RepeatState(TEST_TMP . '/worker-snapshot-state', 1000),
|
|
static function () use (&$sent): bool {
|
|
$sent++;
|
|
return true;
|
|
},
|
|
1000
|
|
);
|
|
|
|
assert_same(0, $worker->handle(
|
|
['SENDER_ADDRESS' => 'person@example.net', 'RECIPIENT' => 'sales@example.com'],
|
|
"Auto-Submitted: no\n\nBody"
|
|
));
|
|
assert_same(1, $worker->handle(
|
|
['SENDER_ADDRESS' => 'person@example.net', 'RECIPIENT' => 'info@example.com'],
|
|
"Auto-Submitted: no\n\nBody"
|
|
));
|
|
assert_same(1, $sent);
|
|
});
|