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,14 +1380,36 @@ func TestClient_SetSMTPAuth(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) {
tests := []struct {
name string
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"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(DefaultHost)
if err != nil {
t.Fatalf("failed to create new client: %s", err)
}
client.SetSMTPAuthCustom(
smtp.PlainAuth("", "", "", "", false),
)
client.SetSMTPAuthCustom(tt.authFunc)
if client.smtpAuth == nil {
t.Errorf("failed to set custom SMTP auth, expected auth method but got nil")
}
@ -1396,9 +1418,12 @@ func TestClient_SetSMTPAuthCustom(t *testing.T) {
client.smtpAuthType)
}
authType := reflect.TypeOf(client.smtpAuth).String()
if authType != "*smtp.plainAuth" {
if authType != tt.want {
t.Errorf("failed to set custom SMTP auth, expected auth method type: %s, got: %s",
"*smtp.plainAuth", authType)
tt.want, authType)
}
})
}
})
}