Refactor SMTP Auth unit test with table-driven approach

Replaced the single test case for setting custom SMTP authentication with a table-driven approach. This refactor improves test coverage by including multiple authentication methods such as CRAM-MD5, LOGIN, PLAIN, SCRAM, and XOAUTH2.
This commit is contained in:
Winni Neessen 2024-10-23 16:46:18 +02:00
parent d4dc212dd3
commit ae7160ddba
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -1380,25 +1380,50 @@ func TestClient_SetSMTPAuth(t *testing.T) {
} }
func TestClient_SetSMTPAuthCustom(t *testing.T) { func TestClient_SetSMTPAuthCustom(t *testing.T) {
t.Run("SetSMTPAuthCustom with PLAIN auth", func(t *testing.T) { t.Run("SetSMTPAuthCustom", func(t *testing.T) {
client, err := NewClient(DefaultHost) tests := []struct {
if err != nil { name string
t.Fatalf("failed to create new client: %s", err) authFunc smtp.Auth
want string
}{
{"CRAM-MD5", smtp.CRAMMD5Auth("", ""), "*smtp.cramMD5Auth"},
{"LOGIN", smtp.LoginAuth("", "", "", false),
"*smtp.loginAuth"},
{"LOGIN-NOENC", smtp.LoginAuth("", "", "", true),
"*smtp.loginAuth"},
{"PLAIN", smtp.PlainAuth("", "", "", "", false),
"*smtp.plainAuth"},
{"PLAIN-NOENC", smtp.PlainAuth("", "", "", "", true),
"*smtp.plainAuth"},
{"SCRAM-SHA-1", smtp.ScramSHA1Auth("", ""), "*smtp.scramAuth"},
{"SCRAM-SHA-1-PLUS", smtp.ScramSHA1PlusAuth("", "", nil),
"*smtp.scramAuth"},
{"SCRAM-SHA-256", smtp.ScramSHA256Auth("", ""), "*smtp.scramAuth"},
{"SCRAM-SHA-256-PLUS", smtp.ScramSHA256PlusAuth("", "", nil),
"*smtp.scramAuth"},
{"XOAUTH2", smtp.XOAuth2Auth("", ""), "*smtp.xoauth2Auth"},
} }
client.SetSMTPAuthCustom( for _, tt := range tests {
smtp.PlainAuth("", "", "", "", false), t.Run(tt.name, func(t *testing.T) {
) client, err := NewClient(DefaultHost)
if client.smtpAuth == nil { if err != nil {
t.Errorf("failed to set custom SMTP auth, expected auth method but got nil") t.Fatalf("failed to create new client: %s", err)
} }
if client.smtpAuthType != SMTPAuthCustom { client.SetSMTPAuthCustom(tt.authFunc)
t.Errorf("failed to set custom SMTP auth, want auth type: %s, got: %s", SMTPAuthCustom, if client.smtpAuth == nil {
client.smtpAuthType) t.Errorf("failed to set custom SMTP auth, expected auth method but got nil")
} }
authType := reflect.TypeOf(client.smtpAuth).String() if client.smtpAuthType != SMTPAuthCustom {
if authType != "*smtp.plainAuth" { t.Errorf("failed to set custom SMTP auth, want auth type: %s, got: %s", SMTPAuthCustom,
t.Errorf("failed to set custom SMTP auth, expected auth method type: %s, got: %s", client.smtpAuthType)
"*smtp.plainAuth", authType) }
authType := reflect.TypeOf(client.smtpAuth).String()
if authType != tt.want {
t.Errorf("failed to set custom SMTP auth, expected auth method type: %s, got: %s",
tt.want, authType)
}
})
} }
}) })
} }