A simple way to handle form submissions from static websites.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "os"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func Test_findRecipient(t *testing.T) {
  10. prepare := func() {
  11. os.Clearenv()
  12. _ = os.Setenv("ALLOWED_TO", "mail@example.com,test@example.com")
  13. _ = os.Setenv("EMAIL_TO", "mail@example.com")
  14. appConfig, _ = parseConfig()
  15. }
  16. t.Run("No recipient specified", func(t *testing.T) {
  17. prepare()
  18. values := &FormValues{}
  19. result := findRecipient(values)
  20. if result != "mail@example.com" {
  21. t.Error()
  22. }
  23. })
  24. t.Run("Multiple recipients specified", func(t *testing.T) {
  25. prepare()
  26. values := &FormValues{
  27. "_to": {"abc@example.com", "def@example.com"},
  28. }
  29. result := findRecipient(values)
  30. if result != "mail@example.com" {
  31. t.Error()
  32. }
  33. })
  34. t.Run("Allowed recipient specified", func(t *testing.T) {
  35. prepare()
  36. values := &FormValues{
  37. "_to": {"test@example.com"},
  38. }
  39. result := findRecipient(values)
  40. if result != "test@example.com" {
  41. t.Error()
  42. }
  43. })
  44. t.Run("Forbidden recipient specified", func(t *testing.T) {
  45. prepare()
  46. values := &FormValues{
  47. "_to": {"forbidden@example.com"},
  48. }
  49. result := findRecipient(values)
  50. if result != "mail@example.com" {
  51. t.Error()
  52. }
  53. })
  54. }
  55. func Test_findFormName(t *testing.T) {
  56. t.Run("No form name", func(t *testing.T) {
  57. if "a form" != findFormName(&FormValues{}) {
  58. t.Error()
  59. }
  60. })
  61. t.Run("Multiple form names", func(t *testing.T) {
  62. if "a form" != findFormName(&FormValues{"_formName": {"Test", "ABC"}}) {
  63. t.Error()
  64. }
  65. })
  66. t.Run("Form name", func(t *testing.T) {
  67. if "Test" != findFormName(&FormValues{"_formName": {"Test"}}) {
  68. t.Error()
  69. }
  70. })
  71. }
  72. func Test_findReplyTo(t *testing.T) {
  73. t.Run("No replyTo", func(t *testing.T) {
  74. if "" != findReplyTo(&FormValues{}) {
  75. t.Error()
  76. }
  77. })
  78. t.Run("Multiple replyTo", func(t *testing.T) {
  79. if "" != findReplyTo(&FormValues{"_replyTo": {"test@example.com", "test2@example.com"}}) {
  80. t.Error()
  81. }
  82. })
  83. t.Run("replyTo", func(t *testing.T) {
  84. if "test@example.com" != findReplyTo(&FormValues{"_replyTo": {"test@example.com"}}) {
  85. t.Error()
  86. }
  87. })
  88. }
  89. func Test_removeMetaValues(t *testing.T) {
  90. t.Run("Remove meta values", func(t *testing.T) {
  91. result := removeMetaValues(&FormValues{
  92. "_test": {"abc"},
  93. "test": {"def"},
  94. })
  95. want := FormValues{
  96. "test": {"def"},
  97. }
  98. if !reflect.DeepEqual(*result, want) {
  99. t.Error()
  100. }
  101. })
  102. }
  103. func Test_buildMessage(t *testing.T) {
  104. t.Run("Test message", func(t *testing.T) {
  105. os.Clearenv()
  106. _ = os.Setenv("EMAIL_TO", "mail@example.com")
  107. _ = os.Setenv("ALLOWED_TO", "mail@example.com,test@example.com")
  108. _ = os.Setenv("EMAIL_FROM", "forms@example.com")
  109. appConfig, _ = parseConfig()
  110. values := &FormValues{
  111. "_formName": {"Testform"},
  112. "_replyTo": {"reply@example.com"},
  113. "Testkey": {"Testvalue"},
  114. "Another Key": {"Test", "ABC"},
  115. }
  116. date := time.Now()
  117. result := buildMessage("test@example.com", date, values)
  118. if !strings.Contains(result, "Reply-To: reply@example.com") {
  119. t.Error()
  120. }
  121. if !strings.Contains(result, "Subject: New submission on Testform") {
  122. t.Error()
  123. }
  124. if !strings.Contains(result, "Testkey: Testvalue") {
  125. t.Error()
  126. }
  127. if !strings.Contains(result, "Another Key: Test, ABC") {
  128. t.Error()
  129. }
  130. })
  131. }