test_construct.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import pytest
  2. from redmail import EmailHandler, MultiEmailHandler
  3. from redmail.email.sender import EmailSender
  4. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  5. def test_construct_kwargs_minimal(cls):
  6. hdlr = cls(host="localhost", port=0, receivers=["me@example.com"], subject="Some logging")
  7. assert hdlr.email.host == 'localhost'
  8. assert hdlr.email.port == 0
  9. assert hdlr.email.receivers == ["me@example.com"]
  10. assert hdlr.email.subject == "Some logging"
  11. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  12. def test_construct_kwargs(cls):
  13. hdlr = cls(host="localhost", port=0, receivers=["me@example.com"], subject="Some logging", text="Error: {{ msg }}", html="<h1>Error: {{ msg }}</h1>")
  14. assert hdlr.email.host == 'localhost'
  15. assert hdlr.email.port == 0
  16. assert hdlr.email.receivers == ["me@example.com"]
  17. assert hdlr.email.subject == "Some logging"
  18. assert hdlr.email.text == "Error: {{ msg }}"
  19. assert hdlr.email.html == "<h1>Error: {{ msg }}</h1>"
  20. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  21. def test_kwargs_error_missing(cls):
  22. # Missing subject
  23. with pytest.raises(TypeError):
  24. hdlr = cls(host="localhost", port=0, receivers=["me@example.com"])
  25. # Missing receivers
  26. with pytest.raises(TypeError):
  27. hdlr = cls(host="localhost", port=0, subject="Some logging")
  28. # Missing host
  29. with pytest.raises(TypeError):
  30. hdlr = cls(port=0, receivers=["me@example.com"], subject="Some logging")
  31. # Missing port
  32. with pytest.raises(TypeError):
  33. hdlr = cls(host="localhost", receivers=["me@example.com"], subject="Some logging")
  34. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  35. def test_kwargs_error_invalid_attr(cls):
  36. with pytest.raises(AttributeError):
  37. hdlr = cls(host="localhost", port=0, receivers=["me@example.com"], subject="Some logging", not_existing="something")
  38. # Testing with passing EmailSender
  39. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  40. def test_sender_with_kwargs(cls):
  41. sender = EmailSender(host="localhost", port=0)
  42. hdlr = cls(email=sender, subject="A log", receivers=["me@example.com"])
  43. assert hdlr.email is not sender
  44. assert hdlr.email.subject == "A log"
  45. assert hdlr.email.receivers == ["me@example.com"]
  46. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  47. def test_sender(cls):
  48. sender = EmailSender(host="localhost", port=0)
  49. sender.subject = "A log"
  50. sender.receivers = ["me@example.com"]
  51. hdlr = cls(email=sender)
  52. assert hdlr.email is not sender
  53. assert hdlr.email.subject == "A log"
  54. assert hdlr.email.receivers == ["me@example.com"]
  55. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  56. def test_sender_with_kwargs_error_invalid_attr(cls):
  57. sender = EmailSender(host="localhost", port=0)
  58. with pytest.raises(AttributeError):
  59. hdlr = cls(email=sender, not_existing="something")
  60. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  61. def test_sender_error_missing(cls):
  62. # Missing subject
  63. sender = EmailSender(host="localhost", port=0)
  64. with pytest.raises(TypeError):
  65. hdlr = cls(email=sender, receivers=["me@example.com"])
  66. # Missing receivers
  67. with pytest.raises(TypeError):
  68. hdlr = cls(email=sender, subject="Some logging")
  69. @pytest.mark.parametrize("cls", [EmailHandler, MultiEmailHandler])
  70. def test_sender_invalid_attr(cls):
  71. sender = EmailSender(host="localhost", port=0)
  72. with pytest.raises(AttributeError):
  73. hdlr = cls(email=sender, not_existing="something")