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.

151 lines
4.0 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
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. "testing"
  6. )
  7. func Test_parseConfig(t *testing.T) {
  8. t.Run("Default config", func(t *testing.T) {
  9. os.Clearenv()
  10. cfg, err := parseConfig()
  11. if err != nil {
  12. t.Error()
  13. return
  14. }
  15. if cfg.Port != 8080 {
  16. t.Error("Default Port not 8080")
  17. }
  18. if len(cfg.HoneyPots) != 1 || cfg.HoneyPots[0] != "_t_email" {
  19. t.Error("Default HoneyPots are wrong")
  20. }
  21. if cfg.SmtpPort != 587 {
  22. t.Error("SMTP Port not 587")
  23. }
  24. if len(cfg.Blacklist) != 2 || cfg.Blacklist[0] != "gambling" || cfg.Blacklist[1] != "casino" {
  25. t.Error("Default Blacklist is wrong")
  26. }
  27. })
  28. t.Run("Correct config parsing", func(t *testing.T) {
  29. os.Clearenv()
  30. _ = os.Setenv("PORT", "1111")
  31. _ = os.Setenv("HONEYPOTS", "pot,abc")
  32. _ = os.Setenv("EMAIL_TO", "mail@example.com")
  33. _ = os.Setenv("ALLOWED_TO", "mail@example.com,test@example.com")
  34. _ = os.Setenv("EMAIL_FROM", "forms@example.com")
  35. _ = os.Setenv("SMTP_USER", "test@example.com")
  36. _ = os.Setenv("SMTP_PASS", "secret")
  37. _ = os.Setenv("SMTP_HOST", "smtp.example.com")
  38. _ = os.Setenv("SMTP_PORT", "100")
  39. _ = os.Setenv("GOOGLE_API_KEY", "abc")
  40. _ = os.Setenv("BLACKLIST", "test,abc")
  41. cfg, err := parseConfig()
  42. if err != nil {
  43. t.Error()
  44. return
  45. }
  46. if !reflect.DeepEqual(cfg.Port, 1111) {
  47. t.Error("Port is wrong")
  48. }
  49. if !reflect.DeepEqual(cfg.HoneyPots, []string{"pot", "abc"}) {
  50. t.Error("HoneyPots are wrong")
  51. }
  52. if !reflect.DeepEqual(cfg.DefaultRecipient, "mail@example.com") {
  53. t.Error("DefaultRecipient is wrong")
  54. }
  55. if !reflect.DeepEqual(cfg.AllowedRecipients, []string{"mail@example.com", "test@example.com"}) {
  56. t.Error("AllowedRecipients are wrong")
  57. }
  58. if !reflect.DeepEqual(cfg.Sender, "forms@example.com") {
  59. t.Error("Sender is wrong")
  60. }
  61. if !reflect.DeepEqual(cfg.SmtpUser, "test@example.com") {
  62. t.Error("SMTP user is wrong")
  63. }
  64. if !reflect.DeepEqual(cfg.SmtpPassword, "secret") {
  65. t.Error("SMTP password is wrong")
  66. }
  67. if !reflect.DeepEqual(cfg.SmtpHost, "smtp.example.com") {
  68. t.Error("SMTP host is wrong")
  69. }
  70. if !reflect.DeepEqual(cfg.SmtpPort, 100) {
  71. t.Error("SMTP port is wrong")
  72. }
  73. if !reflect.DeepEqual(cfg.GoogleApiKey, "abc") {
  74. t.Error("Google API Key is wrong")
  75. }
  76. if !reflect.DeepEqual(cfg.Blacklist, []string{"test", "abc"}) {
  77. t.Error("Blacklist is wrong")
  78. }
  79. })
  80. t.Run("Error when wrong config", func(t *testing.T) {
  81. os.Clearenv()
  82. _ = os.Setenv("PORT", "ABC")
  83. _, err := parseConfig()
  84. if err == nil {
  85. t.Error()
  86. }
  87. })
  88. }
  89. func Test_checkRequiredConfig(t *testing.T) {
  90. validConfig := &config{
  91. Port: 8080,
  92. HoneyPots: []string{"_t_email"},
  93. DefaultRecipient: "mail@example.com",
  94. AllowedRecipients: []string{"mail@example.com"},
  95. Sender: "forms@example.com",
  96. SmtpUser: "test@example.com",
  97. SmtpPassword: "secret",
  98. SmtpHost: "smtp.example.com",
  99. SmtpPort: 587,
  100. }
  101. t.Run("Valid config", func(t *testing.T) {
  102. if true != checkRequiredConfig(validConfig) {
  103. t.Error()
  104. }
  105. })
  106. t.Run("Default recipient missing", func(t *testing.T) {
  107. newConfig := *validConfig
  108. newConfig.DefaultRecipient = ""
  109. if false != checkRequiredConfig(&newConfig) {
  110. t.Error()
  111. }
  112. })
  113. t.Run("Allowed recipients missing", func(t *testing.T) {
  114. newConfig := *validConfig
  115. newConfig.AllowedRecipients = nil
  116. if false != checkRequiredConfig(&newConfig) {
  117. t.Error()
  118. }
  119. })
  120. t.Run("Sender missing", func(t *testing.T) {
  121. newConfig := *validConfig
  122. newConfig.Sender = ""
  123. if false != checkRequiredConfig(&newConfig) {
  124. t.Error()
  125. }
  126. })
  127. t.Run("SMTP user missing", func(t *testing.T) {
  128. newConfig := *validConfig
  129. newConfig.SmtpUser = ""
  130. if false != checkRequiredConfig(&newConfig) {
  131. t.Error()
  132. }
  133. })
  134. t.Run("SMTP password missing", func(t *testing.T) {
  135. newConfig := *validConfig
  136. newConfig.SmtpPassword = ""
  137. if false != checkRequiredConfig(&newConfig) {
  138. t.Error()
  139. }
  140. })
  141. t.Run("SMTP host missing", func(t *testing.T) {
  142. newConfig := *validConfig
  143. newConfig.SmtpHost = ""
  144. if false != checkRequiredConfig(&newConfig) {
  145. t.Error()
  146. }
  147. })
  148. }