mailer_test.go 3.9 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 := `Content-Type: text/plain; charset="utf-8"
  15. From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  16. To: phil@example.com
  17. Subject: A simple message
  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 := `Content-Type: text/plain; charset="utf-8"
  33. From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  34. To: phil@example.com
  35. Subject: 😀 A simple message
  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 := `Content-Type: text/plain; charset="utf-8"
  51. From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  52. To: phil@example.com
  53. Subject: A simple message
  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 := `Content-Type: text/plain; charset="utf-8"
  70. From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  71. To: phil@example.com
  72. Subject: A simple message
  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 := `Content-Type: text/plain; charset="utf-8"
  89. From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  90. To: phil@example.com
  91. Subject: :: A not so simple title öäüß ¡Hola, señor!
  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 := `Content-Type: text/plain; charset="utf-8"
  109. From: "ntfy.sh/alerts" <ntfy@ntfy.sh>
  110. To: phil@example.com
  111. Subject: ⚠️ 💀 Oh no 🙈 This is a message across multiple lines
  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. }