askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_email.py · 9.8 KB · 202 lines
| 1 | """01 email adapter -- outbox enqueue, the pure delivery decision, the |
| 2 | transports, and the daemon drain pass (log mode end-to-end, retry/backoff, |
| 3 | dead-lettering, rate limiting). See plan/vocabulary/01-email-adapter-spec.md. |
| 4 | """ |
| 5 | |
| 6 | import smtplib |
| 7 | from datetime import datetime, timezone |
| 8 | from pathlib import Path |
| 9 | |
| 10 | import object_daemon |
| 11 | import object_email |
| 12 | import object_packages |
| 13 | import object_records |
| 14 | |
| 15 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 16 | |
| 17 | |
| 18 | # ---- config from env ------------------------------------------------------ |
| 19 | |
| 20 | def test_smtp_config_defaults_and_configured_gate(): |
| 21 | disabled = object_email.smtp_config_from_env({}) |
| 22 | assert disabled.mode == "disabled" and not disabled.configured |
| 23 | # live needs a host |
| 24 | assert not object_email.smtp_config_from_env({"DBBASIC_SMTP_MODE": "live"}).configured |
| 25 | live = object_email.smtp_config_from_env({"DBBASIC_SMTP_MODE": "live", "DBBASIC_SMTP_HOST": "mail.x"}) |
| 26 | assert live.configured and live.host == "mail.x" and live.port == 587 |
| 27 | # log mode is always "configured" (writes a file, no network) |
| 28 | assert object_email.smtp_config_from_env({"DBBASIC_SMTP_MODE": "log"}).configured |
| 29 | # bad mode falls back to disabled |
| 30 | assert object_email.smtp_config_from_env({"DBBASIC_SMTP_MODE": "garbage"}).mode == "disabled" |
| 31 | |
| 32 | |
| 33 | def test_smtp_config_reads_tunables(): |
| 34 | cfg = object_email.smtp_config_from_env({ |
| 35 | "DBBASIC_SMTP_MODE": "live", "DBBASIC_SMTP_HOST": "h", |
| 36 | "DBBASIC_SMTP_MAX_ATTEMPTS": "3", "DBBASIC_SMTP_RETRY_BASE_SECONDS": "10", |
| 37 | "DBBASIC_SMTP_RETRY_MAX_SECONDS": "100", "DBBASIC_SMTP_RATE_LIMIT_PER_MINUTE": "2", |
| 38 | "DBBASIC_SMTP_BATCH_SIZE": "5", "DBBASIC_SMTP_USE_TLS": "false", |
| 39 | }) |
| 40 | assert (cfg.max_attempts, cfg.retry_base, cfg.retry_max) == (3, 10, 100) |
| 41 | assert cfg.rate_limit == 2 and cfg.batch_size == 5 and cfg.use_tls is False |
| 42 | |
| 43 | |
| 44 | # ---- pure delivery decision ---------------------------------------------- |
| 45 | |
| 46 | def test_backoff_is_exponential_and_capped(): |
| 47 | cfg = object_email.smtp_config_from_env({"DBBASIC_SMTP_RETRY_BASE_SECONDS": "30", "DBBASIC_SMTP_RETRY_MAX_SECONDS": "3600"}) |
| 48 | assert object_email.backoff_seconds(1, cfg) == 30 |
| 49 | assert object_email.backoff_seconds(2, cfg) == 60 |
| 50 | assert object_email.backoff_seconds(3, cfg) == 120 |
| 51 | assert object_email.backoff_seconds(50, cfg) == 3600 # capped |
| 52 | |
| 53 | |
| 54 | def test_is_due_respects_status_and_schedule(): |
| 55 | now = datetime(2026, 7, 21, 12, 0, tzinfo=timezone.utc) |
| 56 | assert object_email.is_due({"status": "queued", "next_attempt_at": "2026-07-21T11:59:00Z"}, now) |
| 57 | assert not object_email.is_due({"status": "queued", "next_attempt_at": "2026-07-21T12:01:00Z"}, now) |
| 58 | assert not object_email.is_due({"status": "sent", "next_attempt_at": "2026-01-01T00:00:00Z"}, now) |
| 59 | assert object_email.is_due({"status": "queued", "next_attempt_at": ""}, now) # blank = immediately |
| 60 | |
| 61 | |
| 62 | def test_plan_delivery_success_marks_sent(): |
| 63 | cfg = object_email.smtp_config_from_env({}) |
| 64 | now = datetime(2026, 7, 21, 12, 0, tzinfo=timezone.utc) |
| 65 | out = object_email.plan_delivery({"attempts": "0"}, cfg, ok=True, now=now) |
| 66 | assert out["status"] == "sent" and out["sent_at"].startswith("2026-07-21T12:00") |
| 67 | |
| 68 | |
| 69 | def test_plan_delivery_transient_retries_then_dead_at_ceiling(): |
| 70 | cfg = object_email.smtp_config_from_env({"DBBASIC_SMTP_MAX_ATTEMPTS": "3"}) |
| 71 | now = datetime(2026, 7, 21, 12, 0, tzinfo=timezone.utc) |
| 72 | # first failure: attempts 0 -> 1, stays queued with a future next_attempt_at |
| 73 | a = object_email.plan_delivery({"attempts": "0", "max_attempts": "3"}, cfg, ok=False, error="boom", now=now) |
| 74 | assert a["status"] == "queued" and a["attempts"] == "1" and a["last_error"] == "boom" |
| 75 | assert a["next_attempt_at"] > "2026-07-21T12:00" |
| 76 | # final failure: attempts 2 -> 3 == max -> dead |
| 77 | b = object_email.plan_delivery({"attempts": "2", "max_attempts": "3"}, cfg, ok=False, error="boom", now=now) |
| 78 | assert b["status"] == "dead" and b["attempts"] == "3" |
| 79 | |
| 80 | |
| 81 | def test_plan_delivery_permanent_dies_immediately(): |
| 82 | cfg = object_email.smtp_config_from_env({"DBBASIC_SMTP_MAX_ATTEMPTS": "9"}) |
| 83 | out = object_email.plan_delivery({"attempts": "0", "max_attempts": "9"}, cfg, ok=False, permanent=True, error="bad addr") |
| 84 | assert out["status"] == "dead" # permanent error skips the retry budget |
| 85 | |
| 86 | |
| 87 | def test_attempt_delivery_classifies_sender_outcomes(): |
| 88 | cfg = object_email.smtp_config_from_env({"DBBASIC_SMTP_MODE": "live", "DBBASIC_SMTP_HOST": "h", "DBBASIC_SMTP_MAX_ATTEMPTS": "5"}) |
| 89 | rec = {"attempts": "0", "max_attempts": "5", "to": "a@b.c"} |
| 90 | |
| 91 | assert object_email.attempt_delivery(rec, cfg, sender=lambda c, r: None)["status"] == "sent" |
| 92 | |
| 93 | def refuse(c, r): |
| 94 | raise smtplib.SMTPRecipientsRefused({"a@b.c": (550, b"no such user")}) |
| 95 | assert object_email.attempt_delivery(rec, cfg, sender=refuse)["status"] == "dead" |
| 96 | |
| 97 | def flap(c, r): |
| 98 | raise ConnectionRefusedError("connection refused") |
| 99 | transient = object_email.attempt_delivery(rec, cfg, sender=flap) |
| 100 | assert transient["status"] == "queued" and transient["attempts"] == "1" |
| 101 | |
| 102 | |
| 103 | def test_rate_window_reset_rolls_over_after_a_minute(): |
| 104 | fresh = object_email.rate_window_reset(None, 1000.0) |
| 105 | assert fresh == {"window_start": 1000.0, "sent_count": 0} |
| 106 | # same window (<60s later): preserved |
| 107 | same = object_email.rate_window_reset({"window_start": 1000.0, "sent_count": 4}, 1030.0) |
| 108 | assert same["sent_count"] == 4 and same["window_start"] == 1000.0 |
| 109 | # >=60s later: rolls to a new window |
| 110 | rolled = object_email.rate_window_reset({"window_start": 1000.0, "sent_count": 4}, 1061.0) |
| 111 | assert rolled == {"window_start": 1061.0, "sent_count": 0} |
| 112 | |
| 113 | |
| 114 | def test_build_message_falls_back_to_env_from(): |
| 115 | cfg = object_email.smtp_config_from_env({"DBBASIC_SMTP_FROM": "noreply@x.com"}) |
| 116 | msg = object_email.build_message({"to": "u@x.com", "subject": "hi", "text_body": "body", "from_addr": ""}, cfg) |
| 117 | assert msg["From"] == "noreply@x.com" and msg["To"] == "u@x.com" |
| 118 | # an explicit from on the row wins over the env default |
| 119 | msg2 = object_email.build_message({"to": "u@x.com", "subject": "s", "text_body": "b", "from_addr": "me@y.com"}, cfg) |
| 120 | assert msg2["From"] == "me@y.com" |
| 121 | |
| 122 | |
| 123 | # ---- enqueue + daemon drain (end to end) ---------------------------------- |
| 124 | |
| 125 | def _install(tmp_path): |
| 126 | data_dir = tmp_path / "data" |
| 127 | object_root = tmp_path / "objects" |
| 128 | object_root.mkdir() |
| 129 | object_packages.install_package("app-email", root=PACKAGES_ROOT, base_dir=data_dir, |
| 130 | object_roots=[object_root], allow_replace=True) |
| 131 | return data_dir |
| 132 | |
| 133 | |
| 134 | def _outbox(data_dir): |
| 135 | return object_records.read_collection_records("email_outbox", base_dir=data_dir) |
| 136 | |
| 137 | |
| 138 | def test_enqueue_writes_one_queued_row(tmp_path): |
| 139 | data_dir = _install(tmp_path) |
| 140 | cfg = object_email.smtp_config_from_env({"DBBASIC_SMTP_MAX_ATTEMPTS": "7"}) |
| 141 | rid = object_email.enqueue("u@x.com", "Welcome", "Thanks for joining\n", |
| 142 | source_object_id="submit_signup", extra={"submission_id": "c1"}, |
| 143 | base_dir=data_dir, config=cfg) |
| 144 | rows = _outbox(data_dir) |
| 145 | assert len(rows) == 1 |
| 146 | row = rows[0] |
| 147 | assert row["id"] == rid and row["status"] == "queued" and row["attempts"] == "0" |
| 148 | assert row["max_attempts"] == "7" and row["to"] == "u@x.com" and row["subject"] == "Welcome" |
| 149 | assert row["source_object_id"] == "submit_signup" and "c1" in row["extra"] |
| 150 | assert row["next_attempt_at"] # scheduled immediately |
| 151 | |
| 152 | |
| 153 | def test_daemon_pass_delivers_in_log_mode(tmp_path, monkeypatch): |
| 154 | data_dir = _install(tmp_path) |
| 155 | log_path = tmp_path / "mail.log" |
| 156 | monkeypatch.setenv("DBBASIC_SMTP_MODE", "log") |
| 157 | monkeypatch.setenv("DBBASIC_SMTP_LOG_PATH", str(log_path)) |
| 158 | object_email.enqueue("a@x.com", "One", "first", base_dir=data_dir) |
| 159 | object_email.enqueue("b@x.com", "Two", "second", base_dir=data_dir) |
| 160 | |
| 161 | result = object_daemon.process_email_outbox(base_dir=data_dir) |
| 162 | assert result == {"attempted": 2, "sent": 2, "dead": 0, "rate_limited": 0} |
| 163 | assert all(r["status"] == "sent" and r["sent_at"] for r in _outbox(data_dir)) |
| 164 | assert len(log_path.read_text().strip().splitlines()) == 2 |
| 165 | # re-run: nothing is due anymore |
| 166 | assert object_daemon.process_email_outbox(base_dir=data_dir) == {"attempted": 0, "sent": 0, "dead": 0} |
| 167 | |
| 168 | |
| 169 | def test_daemon_pass_queues_only_when_unconfigured(tmp_path, monkeypatch): |
| 170 | data_dir = _install(tmp_path) |
| 171 | monkeypatch.delenv("DBBASIC_SMTP_MODE", raising=False) # disabled |
| 172 | object_email.enqueue("a@x.com", "Held", "queues forever", base_dir=data_dir) |
| 173 | assert object_daemon.process_email_outbox(base_dir=data_dir) is None |
| 174 | assert _outbox(data_dir)[0]["status"] == "queued" # untouched, fully inspectable |
| 175 | |
| 176 | |
| 177 | def test_daemon_pass_retries_transient_failure(tmp_path, monkeypatch): |
| 178 | data_dir = _install(tmp_path) |
| 179 | monkeypatch.setenv("DBBASIC_SMTP_MODE", "log") |
| 180 | monkeypatch.setattr(object_email, "sender_for", lambda cfg: _boom) |
| 181 | object_email.enqueue("a@x.com", "Flaky", "body", base_dir=data_dir) |
| 182 | result = object_daemon.process_email_outbox(base_dir=data_dir) |
| 183 | assert result["attempted"] == 1 and result["sent"] == 0 and result["dead"] == 0 |
| 184 | row = _outbox(data_dir)[0] |
| 185 | assert row["status"] == "queued" and row["attempts"] == "1" and "boom" in row["last_error"] |
| 186 | |
| 187 | |
| 188 | def test_daemon_pass_rate_limits_the_batch(tmp_path, monkeypatch): |
| 189 | data_dir = _install(tmp_path) |
| 190 | monkeypatch.setenv("DBBASIC_SMTP_MODE", "log") |
| 191 | monkeypatch.setenv("DBBASIC_SMTP_LOG_PATH", str(tmp_path / "m.log")) |
| 192 | monkeypatch.setenv("DBBASIC_SMTP_RATE_LIMIT_PER_MINUTE", "1") |
| 193 | for i in range(3): |
| 194 | object_email.enqueue(f"u{i}@x.com", "S", "b", base_dir=data_dir) |
| 195 | result = object_daemon.process_email_outbox(base_dir=data_dir) |
| 196 | assert result["sent"] == 1 and result["rate_limited"] == 2 |
| 197 | statuses = sorted(r["status"] for r in _outbox(data_dir)) |
| 198 | assert statuses == ["queued", "queued", "sent"] # two held for the next window |
| 199 | |
| 200 | |
| 201 | def _boom(config, record): |
| 202 | raise ConnectionRefusedError("boom: connection refused") |