Coverage for postrfp/mail/stub.py: 97%

34 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-22 21:34 +0000

1import logging 

2import uuid 

3from pathlib import Path 

4from typing import List, Dict, Union 

5 

6import pymustache # type: ignore[import] 

7from postrfp import conf 

8from postrfp.mail.schemas import TemplateEmailModel, SimpleEmailModel 

9 

10 

11log = logging.getLogger("postrfp.mail.dummy_mailer") 

12 

13 

14# Dummy mailbox for testing 

15MAILBOX: List[Dict[str, str]] = [] 

16stars: str = "\n" + ("*" * 90) + "\n" 

17 

18 

19def clear_mailbox() -> None: 

20 MAILBOX.clear() 

21 

22 

23def get_tmpl(evt_type: str) -> str: 

24 path = Path(conf.CONF.template_dir) / f"{evt_type}.txt" 

25 tmpl = path.read_text() 

26 log.debug("Template \n: %s \n", tmpl) 

27 return tmpl 

28 

29 

30def render_msg(tmpl_string: str, model: dict) -> str: 

31 return pymustache.render(tmpl_string, model) 

32 

33 

34def send_email(model: Union[TemplateEmailModel, SimpleEmailModel]) -> str: 

35 # Convert Pydantic model to dict 

36 model_dict = model.model_dump() 

37 

38 # Stub only handles template emails currently 

39 if not isinstance(model, TemplateEmailModel): 

40 raise TypeError("Stub mailer currently only supports template emails") 

41 

42 tmpl = get_tmpl(model_dict["TemplateAlias"]) 

43 # import ipdb; ipdb.set_trace() 

44 msg = render_msg(tmpl, model_dict["TemplateModel"]) 

45 to_email = model_dict["To"] 

46 with open("/tmp/emailout.txt", "a+") as f: # nosec 

47 f.write(f"To: {to_email}") 

48 f.write(stars) 

49 f.write(msg) 

50 f.write(stars) 

51 message_id = str(uuid.uuid4()) 

52 MAILBOX.append(dict(to_address=to_email, text=msg, MessageID=message_id)) 

53 return message_id