test-archive-process-email 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/python3
  2. import sys
  3. # Avoid creating archive-process-emailc
  4. if hasattr(sys, "dont_write_bytecode"):
  5. sys.dont_write_bytecode = True
  6. import imp
  7. import unittest
  8. import os
  9. import django
  10. import io
  11. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nm2.settings_tapi")
  12. django.setup()
  13. import backend.models as bmodels
  14. Dispatcher = imp.load_source("ape", "./archive-process-email").Dispatcher
  15. class TestLookup(unittest.TestCase):
  16. def setUp(self):
  17. # Get an active process with an AM and a uid
  18. self.proc = bmodels.Process.objects.filter(manager__isnull=False, person__uid__isnull=False, is_active=True)[0]
  19. def make_email(self, dk=None, src="am", dst="nm"):
  20. """
  21. src and dst can be "am", "nm", "fd", or a list of Person objects
  22. """
  23. from email.mime.text import MIMEText
  24. from email.utils import formataddr
  25. def get_addr(t):
  26. if t == "am":
  27. return formataddr((self.proc.manager.person.fullname, self.proc.manager.person.uid + "@debian.org"))
  28. elif t == "nm":
  29. return formataddr((self.proc.person.fullname, self.proc.person.email))
  30. elif t == "fd":
  31. return "Enrico Zini <enrico@enricozini.org>"
  32. elif isinstance(t, list):
  33. return ", ".join(formataddr((p.fullname, p.email)) for p in t)
  34. else:
  35. raise KeyError("Invalid actor: '%s'" % t)
  36. res = MIMEText("body", _charset="utf-8")
  37. res["From"] = get_addr(src)
  38. res["To"] = get_addr(dst)
  39. res["Subject"] = "Test mail"
  40. if dk is not None:
  41. res["Delivered-To"] = "archive-%s@nm.debian.org" % dk
  42. else:
  43. res["Delivered-To"] = "archive@nm.debian.org"
  44. return res.as_string().encode("utf-8")
  45. def make_dispatcher(self, **kw):
  46. return Dispatcher(io.BytesIO(self.make_email(**kw)))
  47. def testDestkeyEmail(self):
  48. dest = self.proc.person.email.replace("@", "=")
  49. d = self.make_dispatcher(dk=dest)
  50. key = d.get_dest_key()
  51. self.assertEqual(key, dest)
  52. arc_key = d.archive_key_from_dest_key(key)
  53. self.assertEqual(arc_key, self.proc.archive_key)
  54. def testDestkeyUid(self):
  55. dest = self.proc.person.uid
  56. d = self.make_dispatcher(dk=dest)
  57. key = d.get_dest_key()
  58. self.assertEqual(key, dest)
  59. arc_key = d.archive_key_from_dest_key(key)
  60. self.assertEqual(arc_key, self.proc.archive_key)
  61. if __name__ == '__main__':
  62. unittest.main()