smtp_sender_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package server
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "testing"
  5. )
  6. func TestFormatMail_Basic(t *testing.T) {
  7. actual, _ := formatMail("https://ntfy.sh", "1.2.3.4", "ntfy@ntfy.sh", "phil@example.com", &message{
  8. ID: "abc",
  9. Time: 1640382204,
  10. Event: "message",
  11. Topic: "alerts",
  12. Message: "A simple message",
  13. })
  14. expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  15. To: phil@example.com
  16. Date: Fri, 24 Dec 2021 21:43:24 +0000
  17. Subject: A simple message
  18. Content-Type: text/plain; charset="utf-8"
  19. A simple message
  20. --
  21. This message was sent by 1.2.3.4 at Fri, 24 Dec 2021 21:43:24 UTC via https://ntfy.sh/alerts`
  22. require.Equal(t, expected, actual)
  23. }
  24. func TestFormatMail_JustEmojis(t *testing.T) {
  25. actual, _ := formatMail("https://ntfy.sh", "1.2.3.4", "ntfy@ntfy.sh", "phil@example.com", &message{
  26. ID: "abc",
  27. Time: 1640382204,
  28. Event: "message",
  29. Topic: "alerts",
  30. Message: "A simple message",
  31. Tags: []string{"grinning"},
  32. })
  33. expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  34. To: phil@example.com
  35. Date: Fri, 24 Dec 2021 21:43:24 +0000
  36. Subject: =?utf-8?b?8J+YgCBBIHNpbXBsZSBtZXNzYWdl?=
  37. Content-Type: text/plain; charset="utf-8"
  38. A simple message
  39. --
  40. This message was sent by 1.2.3.4 at Fri, 24 Dec 2021 21:43:24 UTC via https://ntfy.sh/alerts`
  41. require.Equal(t, expected, actual)
  42. }
  43. func TestFormatMail_JustOtherTags(t *testing.T) {
  44. actual, _ := formatMail("https://ntfy.sh", "1.2.3.4", "ntfy@ntfy.sh", "phil@example.com", &message{
  45. ID: "abc",
  46. Time: 1640382204,
  47. Event: "message",
  48. Topic: "alerts",
  49. Message: "A simple message",
  50. Tags: []string{"not-an-emoji"},
  51. })
  52. expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  53. To: phil@example.com
  54. Date: Fri, 24 Dec 2021 21:43:24 +0000
  55. Subject: A simple message
  56. Content-Type: text/plain; charset="utf-8"
  57. A simple message
  58. Tags: not-an-emoji
  59. --
  60. This message was sent by 1.2.3.4 at Fri, 24 Dec 2021 21:43:24 UTC via https://ntfy.sh/alerts`
  61. require.Equal(t, expected, actual)
  62. }
  63. func TestFormatMail_JustPriority(t *testing.T) {
  64. actual, _ := formatMail("https://ntfy.sh", "1.2.3.4", "ntfy@ntfy.sh", "phil@example.com", &message{
  65. ID: "abc",
  66. Time: 1640382204,
  67. Event: "message",
  68. Topic: "alerts",
  69. Message: "A simple message",
  70. Priority: 2,
  71. })
  72. expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  73. To: phil@example.com
  74. Date: Fri, 24 Dec 2021 21:43:24 +0000
  75. Subject: A simple message
  76. Content-Type: text/plain; charset="utf-8"
  77. A simple message
  78. Priority: low
  79. --
  80. This message was sent by 1.2.3.4 at Fri, 24 Dec 2021 21:43:24 UTC via https://ntfy.sh/alerts`
  81. require.Equal(t, expected, actual)
  82. }
  83. func TestFormatMail_UTF8Subject(t *testing.T) {
  84. actual, _ := formatMail("https://ntfy.sh", "1.2.3.4", "ntfy@ntfy.sh", "phil@example.com", &message{
  85. ID: "abc",
  86. Time: 1640382204,
  87. Event: "message",
  88. Topic: "alerts",
  89. Message: "A simple message",
  90. Title: " :: A not so simple title öäüß ¡Hola, señor!",
  91. })
  92. expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  93. To: phil@example.com
  94. Date: Fri, 24 Dec 2021 21:43:24 +0000
  95. Subject: =?utf-8?b?IDo6IEEgbm90IHNvIHNpbXBsZSB0aXRsZSDDtsOkw7zDnyDCoUhvbGEsIHNl?= =?utf-8?b?w7FvciE=?=
  96. Content-Type: text/plain; charset="utf-8"
  97. A simple message
  98. --
  99. This message was sent by 1.2.3.4 at Fri, 24 Dec 2021 21:43:24 UTC via https://ntfy.sh/alerts`
  100. require.Equal(t, expected, actual)
  101. }
  102. func TestFormatMail_WithAllTheThings(t *testing.T) {
  103. actual, _ := formatMail("https://ntfy.sh", "1.2.3.4", "ntfy@ntfy.sh", "phil@example.com", &message{
  104. ID: "abc",
  105. Time: 1640382204,
  106. Event: "message",
  107. Topic: "alerts",
  108. Priority: 5,
  109. Tags: []string{"warning", "skull", "tag123", "other"},
  110. Title: "Oh no 🙈\nThis is a message across\nmultiple lines",
  111. Message: "A message that contains monkeys 🙉\nNo really, though. Monkeys!",
  112. })
  113. expected := `From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  114. To: phil@example.com
  115. Date: Fri, 24 Dec 2021 21:43:24 +0000
  116. Subject: =?utf-8?b?4pqg77iPIPCfkoAgT2ggbm8g8J+ZiCBUaGlzIGlzIGEgbWVzc2FnZSBhY3Jv?= =?utf-8?b?c3MgbXVsdGlwbGUgbGluZXM=?=
  117. Content-Type: text/plain; charset="utf-8"
  118. A message that contains monkeys 🙉
  119. No really, though. Monkeys!
  120. Tags: tag123, other
  121. Priority: max
  122. --
  123. This message was sent by 1.2.3.4 at Fri, 24 Dec 2021 21:43:24 UTC via https://ntfy.sh/alerts`
  124. require.Equal(t, expected, actual)
  125. }