from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from hints import * import json import shlex import uuid DATA_JSON = """ @data@ """ DATA = json.loads(DATA_JSON) relay_ip = DATA["relayIP"] liam.succeed(DATA["checkSieve"]) start_all() ns.wait_for_unit("bind.service") ns.wait_for_open_port(53) liam.wait_for_unit("nginx.service") liam.wait_for_open_port(80) liam.copy_from_host(DATA["acmeTest"], DATA["acmeTestDest"]) checker.wait_for_unit("network-online.target") checker.succeed("wget http://liam.dis8.net/.well-known/acme-challenge/test") liam.succeed("doveadm mailbox create -u shelvacu testFolder") liam.wait_for_unit("postfix.service") liam.wait_for_unit("dovecot2.service") relay.wait_for_unit("mailpit.service") def make_command(args: list) -> str: return " ".join(map(shlex.quote, (map(str, args)))) class TesterThing(): uuid: str = "" default_smtp: dict[str, str] = {} default_imap: dict[str, str] = {} default_mailpit: dict[str, str] = {} def __init__(self, username: str, smtp: dict[str, str] = {}, imap: dict[str, str] = {}, mailpit: dict[str, str] = {}): self.uuid = str(uuid.uuid4()) self.default_smtp = { "rcptto": "someone@example.com", "username": username, **smtp } self.default_imap = { "username": username, **imap } self.default_mailpit = { "mailpit-url": f"http://{relay_ip}:8025", **mailpit } def run_expecting_json(self, name: str, **kwargs: dict[str, str]) -> dict[str, Any]: args:list[str] = [name] for k, v in kwargs: dashed = k.replace("_","-") args.append(f"--{dashed}") args.append(v) res = checker.succeed(make_command(args)) res = res.strip() assert res != "" return json.loads(res) def run_smtp(self, **kwargs: dict[str, str]) -> bool: args = {"message_magic": self.uuid, **self.default_smtp, **kwargs} res = self.run_expecting_json("mailpit-smtp", **args) return res["result"] def smtp_accepted(self, **kwargs): res = self.run_smtp(**kwargs) assert res, "Message was not accepted when it should have been" def smtp_rejected(self, **kwargs): res = self.run_smtp(**kwargs) assert not res, "Message was accepted when it was supposed to be rejected" # The order of these shouldn't matter, other than what fails first. Whatever is at the top is probably whatever I was working on most recently. checks = f""" --submission --mailfrom robot@vacu.store --rcptto someone@example.com --username vacustore --expect-mailpit-received --mailpit-url http://{relay_ip}:8025 --submission --mailfrom foobar@vacu.store --rcptto someone@example.com --username vacustore --expect-refused --submission --mailfrom abc@shelvacu.com --rcptto someone@example.com --username vacustore --expect-refused # test refilter --mailfrom whoeve2@example.com --rcptto sieve2est@shelvacu.com --username shelvacu --imap-move-to MagicRefilter --imap-dir com.shelvacu # refilter doesnt activate on other folders --mailfrom whoeve2@example.com --rcptto sieve2est@shelvacu.com --username shelvacu --imap-move-to testFolder --imap-dir testFolder --mailfrom whoeve2@example.com --rcptto sieve2est@shelvacu.com --username shelvacu --imap-move-to INBOX --imap-dir INBOX # test the sieve script is working --mailfrom whoever@example.com --rcptto sievetest@shelvacu.com --username shelvacu --imap-dir com.shelvacu # refilter doesnt activate on julie's --mailfrom blarg@example.com --rcptto julie@shelvacu.com --username julie --imap-move-to MagicRefilter --imap-dir MagicRefilter --mailfrom asshole-spammer@example.com --rcptto whatever@shelvacu.com --header "List-unsubscribe: whatver" --username shelvacu --expect-flag spamish --imap-dir "com.shelvacu.#spamish" --mailfrom shipment-tracking@amazon.com --rcptto amznbsns@shelvacu.com --subject "Your Amazon.com order has shipped (#123-1234)" --username shelvacu --imap-dir com.shelvacu --expect-flag \\\\Seen --expect-flag auto-marked-read --expect-flag amazon-ignore --rcptto shelvacu@shelvacu.com --username shelvacu --smtp-starttls --submission --mailfrom me@shelvacu.com --rcptto foo@example.com --username shelvacu --expect-mailpit-received --mailpit-url http://{relay_ip}:8025 --submission --mailfrom me@dis8.net --rcptto foo@example.com --username shelvacu --expect-mailpit-not-received --mailpit-url http://{relay_ip}:8025 # julie's emails should NOT get sieve'd like mine --rcptto julie@shelvacu.com --username julie --imap-dir INBOX --rcptto julie+stuff@shelvacu.com --username julie --imap-dir INBOX --rcptto shelvacu@shelvacu.com --username shelvacu --rcptto julie@shelvacu.com --username julie --rcptto foobar@shelvacu.com --username shelvacu --rcptto sales@theviolincase.com --username julie --rcptto superwow@shop.theviolincase.com --username julie --rcptto roboman@vacu.store --username shelvacu --mailfrom bob@vacu.store --expect-refused --mailfrom shelvacu@shelvacu.com --expect-refused --mailfrom julie@shelvacu.com --expect-refused --mailfrom @vacu.store --expect-refused --mailfrom reject-spam-test@example.com --expect-refused --submission --expect-refused --mailfrom julie@shelvacu.com --username shelvacu --submission --expect-refused --mailfrom fubar@theviolincase.com --username shelvacu --submission --expect-refused --mailfrom fubar@vacu.store --username julie --submission --mailfrom shelvacu@shelvacu.com --rcptto foo@example.com --username shelvacu --password shelvacu --expect-sent --submission --mailfrom shelvacu@shelvacu.com --rcptto foo@example.com --username shelvacu@shelvacu.com --password shelvacu --expect-sent --submission --mailfrom foo@vacu.store --rcptto foo@example.com --username shelvacu --password shelvacu --expect-sent --submission --mailfrom foo@vacu.store --rcptto foo@example.com --username shelvacu@shelvacu.com --password shelvacu --expect-sent --submission --mailfrom foo@violingifts.com --rcptto foo@example.com --username julie --password julie --expect-sent --submission --mailfrom foo@violingifts.com --rcptto foo@example.com --username julie@shelvacu.com --password julie --expect-sent """ for check in checks.split("\n"): check = check.strip() if check == "" or check[0] == "#": continue res = checker.succeed("mailtest " + check.strip()) print(res)