jlelse
/
MailyGo
Archived
1
Fork 0
This repository has been archived on 2024-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
MailyGo/config_test.go

152 lines
4.0 KiB
Go
Raw Permalink Normal View History

2020-03-14 21:26:34 +00:00
package main
import (
"os"
"reflect"
"testing"
)
func Test_parseConfig(t *testing.T) {
t.Run("Default config", func(t *testing.T) {
os.Clearenv()
cfg, err := parseConfig()
if err != nil {
t.Error()
return
}
if cfg.Port != 8080 {
t.Error("Default Port not 8080")
}
if len(cfg.HoneyPots) != 1 || cfg.HoneyPots[0] != "_t_email" {
t.Error("Default HoneyPots are wrong")
}
2020-10-22 18:18:11 +00:00
if cfg.SMTPPort != 587 {
2020-03-14 21:26:34 +00:00
t.Error("SMTP Port not 587")
}
2020-10-22 18:18:11 +00:00
if len(cfg.BlacklistArray) != 2 || cfg.BlacklistArray[0] != "gambling" || cfg.BlacklistArray[1] != "casino" {
2020-03-22 09:43:45 +00:00
t.Error("Default Blacklist is wrong")
}
2020-03-14 21:26:34 +00:00
})
t.Run("Correct config parsing", func(t *testing.T) {
os.Clearenv()
_ = os.Setenv("PORT", "1111")
_ = os.Setenv("HONEYPOTS", "pot,abc")
_ = os.Setenv("EMAIL_TO", "mail@example.com")
2020-03-14 21:26:34 +00:00
_ = os.Setenv("ALLOWED_TO", "mail@example.com,test@example.com")
_ = os.Setenv("EMAIL_FROM", "forms@example.com")
_ = os.Setenv("SMTP_USER", "test@example.com")
_ = os.Setenv("SMTP_PASS", "secret")
_ = os.Setenv("SMTP_HOST", "smtp.example.com")
_ = os.Setenv("SMTP_PORT", "100")
2020-03-21 12:03:56 +00:00
_ = os.Setenv("GOOGLE_API_KEY", "abc")
2020-03-22 09:43:45 +00:00
_ = os.Setenv("BLACKLIST", "test,abc")
2020-03-14 21:26:34 +00:00
cfg, err := parseConfig()
if err != nil {
t.Error()
return
}
if !reflect.DeepEqual(cfg.Port, 1111) {
t.Error("Port is wrong")
}
if !reflect.DeepEqual(cfg.HoneyPots, []string{"pot", "abc"}) {
t.Error("HoneyPots are wrong")
}
if !reflect.DeepEqual(cfg.DefaultRecipient, "mail@example.com") {
t.Error("DefaultRecipient is wrong")
}
if !reflect.DeepEqual(cfg.AllowedRecipients, []string{"mail@example.com", "test@example.com"}) {
t.Error("AllowedRecipients are wrong")
}
if !reflect.DeepEqual(cfg.Sender, "forms@example.com") {
t.Error("Sender is wrong")
}
2020-10-22 18:18:11 +00:00
if !reflect.DeepEqual(cfg.SMTPUser, "test@example.com") {
2020-03-14 21:26:34 +00:00
t.Error("SMTP user is wrong")
}
2020-10-22 18:18:11 +00:00
if !reflect.DeepEqual(cfg.SMTPPassword, "secret") {
2020-03-14 21:26:34 +00:00
t.Error("SMTP password is wrong")
}
2020-10-22 18:18:11 +00:00
if !reflect.DeepEqual(cfg.SMTPHost, "smtp.example.com") {
2020-03-14 21:26:34 +00:00
t.Error("SMTP host is wrong")
}
2020-10-22 18:18:11 +00:00
if !reflect.DeepEqual(cfg.SMTPPort, 100) {
2020-03-14 21:26:34 +00:00
t.Error("SMTP port is wrong")
}
2020-10-22 18:18:11 +00:00
if !reflect.DeepEqual(cfg.GoogleAPIKey, "abc") {
2020-03-21 12:03:56 +00:00
t.Error("Google API Key is wrong")
}
2020-10-22 18:18:11 +00:00
if !reflect.DeepEqual(cfg.BlacklistArray, []string{"test", "abc"}) {
2020-03-22 09:43:45 +00:00
t.Error("Blacklist is wrong")
}
2020-03-14 21:26:34 +00:00
})
t.Run("Error when wrong config", func(t *testing.T) {
os.Clearenv()
_ = os.Setenv("PORT", "ABC")
_, err := parseConfig()
if err == nil {
t.Error()
}
})
}
func Test_checkRequiredConfig(t *testing.T) {
2020-04-12 10:34:51 +00:00
validConfig := &config{
2020-03-14 21:26:34 +00:00
Port: 8080,
HoneyPots: []string{"_t_email"},
DefaultRecipient: "mail@example.com",
AllowedRecipients: []string{"mail@example.com"},
Sender: "forms@example.com",
2020-10-22 18:18:11 +00:00
SMTPUser: "test@example.com",
SMTPPassword: "secret",
SMTPHost: "smtp.example.com",
SMTPPort: 587,
2020-03-14 21:26:34 +00:00
}
t.Run("Valid config", func(t *testing.T) {
if true != checkRequiredConfig(validConfig) {
t.Error()
}
})
t.Run("Default recipient missing", func(t *testing.T) {
2020-04-12 10:34:51 +00:00
newConfig := *validConfig
2020-03-14 21:26:34 +00:00
newConfig.DefaultRecipient = ""
2020-04-12 10:34:51 +00:00
if false != checkRequiredConfig(&newConfig) {
2020-03-14 21:26:34 +00:00
t.Error()
}
})
t.Run("Allowed recipients missing", func(t *testing.T) {
2020-04-12 10:34:51 +00:00
newConfig := *validConfig
2020-03-14 21:26:34 +00:00
newConfig.AllowedRecipients = nil
2020-04-12 10:34:51 +00:00
if false != checkRequiredConfig(&newConfig) {
2020-03-14 21:26:34 +00:00
t.Error()
}
})
t.Run("Sender missing", func(t *testing.T) {
2020-04-12 10:34:51 +00:00
newConfig := *validConfig
2020-03-14 21:26:34 +00:00
newConfig.Sender = ""
2020-04-12 10:34:51 +00:00
if false != checkRequiredConfig(&newConfig) {
2020-03-14 21:26:34 +00:00
t.Error()
}
})
t.Run("SMTP user missing", func(t *testing.T) {
2020-04-12 10:34:51 +00:00
newConfig := *validConfig
2020-10-22 18:18:11 +00:00
newConfig.SMTPUser = ""
2020-04-12 10:34:51 +00:00
if false != checkRequiredConfig(&newConfig) {
2020-03-14 21:26:34 +00:00
t.Error()
}
})
t.Run("SMTP password missing", func(t *testing.T) {
2020-04-12 10:34:51 +00:00
newConfig := *validConfig
2020-10-22 18:18:11 +00:00
newConfig.SMTPPassword = ""
2020-04-12 10:34:51 +00:00
if false != checkRequiredConfig(&newConfig) {
2020-03-14 21:26:34 +00:00
t.Error()
}
})
t.Run("SMTP host missing", func(t *testing.T) {
2020-04-12 10:34:51 +00:00
newConfig := *validConfig
2020-10-22 18:18:11 +00:00
newConfig.SMTPHost = ""
2020-04-12 10:34:51 +00:00
if false != checkRequiredConfig(&newConfig) {
2020-03-14 21:26:34 +00:00
t.Error()
}
})
}