|
| 1 | +import types |
| 2 | +import builtins |
| 3 | +from src.emailservice import send_demo |
| 4 | + |
| 5 | +class FakeSMTP: |
| 6 | + def __init__(self, host, port): |
| 7 | + self.host, self.port = host, port |
| 8 | + self.sent = [] |
| 9 | + def __enter__(self): return self |
| 10 | + def __exit__(self, *exc): return False |
| 11 | + def send_message(self, msg): |
| 12 | + self.sent.append(msg) |
| 13 | + |
| 14 | +def test_send_demo_invokes_smtp(monkeypatch): |
| 15 | + captured = {"instance": None} |
| 16 | + |
| 17 | + def fake_smtp_ctor(host, port): |
| 18 | + inst = FakeSMTP(host, port) |
| 19 | + captured["instance"] = inst |
| 20 | + return inst |
| 21 | + |
| 22 | + monkeypatch.setenv("SMTP_HOST", "localhost") |
| 23 | + monkeypatch.setenv("SMTP_PORT", "1025") |
| 24 | + monkeypatch.setattr(send_demo.smtplib, "SMTP", fake_smtp_ctor) |
| 25 | + |
| 26 | + send_demo. main([ "Oren", "[email protected]", "12345"]) |
| 27 | + |
| 28 | + inst = captured["instance"] |
| 29 | + assert inst is not None |
| 30 | + assert inst.host == "localhost" and inst.port == 1025 |
| 31 | + assert len(inst.sent) == 1 |
| 32 | + msg = inst.sent[0] |
| 33 | + assert "Oren" in msg["To"] |
| 34 | + assert "12345" in msg["Subject"] |
0 commit comments