smtp_sender_test.go 4.0 KB

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