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.

140 lines
3.6 KiB

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