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.

144 lines
3.7 KiB

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