From 17cb590a455f27cb5409df03ef9f112d9096a7f2 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 23 Oct 2024 15:44:21 +0200 Subject: [PATCH] Add test for Client.SetSMTPAuth function Introduce a new test to validate the functionality of the Client.SetSMTPAuth method. This test covers various SMTP authentication types and ensures that the method correctly sets the expected authentication type while also verifying the override behavior for different custom and default authentications. --- client_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/client_test.go b/client_test.go index ad1b483..adeeec5 100644 --- a/client_test.go +++ b/client_test.go @@ -1289,6 +1289,96 @@ func TestClient_SetPassword(t *testing.T) { }) } +func TestClient_SetSMTPAuth(t *testing.T) { + t.Run("SetSMTPAuth", func(t *testing.T) { + tests := []struct { + name string + auth SMTPAuthType + expected SMTPAuthType + }{ + {"CRAM-MD5", SMTPAuthCramMD5, SMTPAuthCramMD5}, + {"LOGIN", SMTPAuthLogin, SMTPAuthLogin}, + {"LOGIN-NOENC", SMTPAuthLoginNoEnc, SMTPAuthLoginNoEnc}, + {"NOAUTH", SMTPAuthNoAuth, SMTPAuthNoAuth}, + {"PLAIN", SMTPAuthPlain, SMTPAuthPlain}, + {"PLAIN-NOENC", SMTPAuthPlainNoEnc, SMTPAuthPlainNoEnc}, + {"SCRAM-SHA-1", SMTPAuthSCRAMSHA1, SMTPAuthSCRAMSHA1}, + {"SCRAM-SHA-1-PLUS", SMTPAuthSCRAMSHA1PLUS, SMTPAuthSCRAMSHA1PLUS}, + {"SCRAM-SHA-256", SMTPAuthSCRAMSHA256, SMTPAuthSCRAMSHA256}, + {"SCRAM-SHA-256-PLUS", SMTPAuthSCRAMSHA256PLUS, SMTPAuthSCRAMSHA256PLUS}, + {"XOAUTH2", SMTPAuthXOAUTH2, SMTPAuthXOAUTH2}, + } + + client, err := NewClient(DefaultHost) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client.SetSMTPAuth(tt.auth) + if client.smtpAuthType != tt.expected { + t.Errorf("failed to set expected SMTPAuthType, want: %s, got: %s", tt.expected, + client.smtpAuthType) + } + }) + } + }) + t.Run("SetSMTPAuth to override WithSMTPAuth", func(t *testing.T) { + tests := []struct { + name string + auth SMTPAuthType + expected SMTPAuthType + }{ + {"CRAM-MD5", SMTPAuthCramMD5, SMTPAuthCramMD5}, + {"LOGIN", SMTPAuthLogin, SMTPAuthLogin}, + {"LOGIN-NOENC", SMTPAuthLoginNoEnc, SMTPAuthLoginNoEnc}, + {"NOAUTH", SMTPAuthNoAuth, SMTPAuthNoAuth}, + {"PLAIN", SMTPAuthPlain, SMTPAuthPlain}, + {"PLAIN-NOENC", SMTPAuthPlainNoEnc, SMTPAuthPlainNoEnc}, + {"SCRAM-SHA-1", SMTPAuthSCRAMSHA1, SMTPAuthSCRAMSHA1}, + {"SCRAM-SHA-1-PLUS", SMTPAuthSCRAMSHA1PLUS, SMTPAuthSCRAMSHA1PLUS}, + {"SCRAM-SHA-256", SMTPAuthSCRAMSHA256, SMTPAuthSCRAMSHA256}, + {"SCRAM-SHA-256-PLUS", SMTPAuthSCRAMSHA256PLUS, SMTPAuthSCRAMSHA256PLUS}, + {"XOAUTH2", SMTPAuthXOAUTH2, SMTPAuthXOAUTH2}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client, err := NewClient(DefaultHost, WithSMTPAuth(SMTPAuthLogin)) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + if client.smtpAuthType != SMTPAuthLogin { + t.Fatalf("failed to create client with LOGIN auth, got: %s", client.smtpAuthType) + } + client.SetSMTPAuth(tt.auth) + if client.smtpAuthType != tt.expected { + t.Errorf("failed to set expected SMTPAuthType, want: %s, got: %s", tt.expected, + client.smtpAuthType) + } + }) + } + }) + t.Run("SetSMTPAuth override custom auth", func(t *testing.T) { + client, err := NewClient(DefaultHost, + WithSMTPAuthCustom(smtp.LoginAuth("", "", "", false))) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + if client.smtpAuthType != SMTPAuthCustom { + t.Fatalf("failed to create client with Custom auth, got: %s", client.smtpAuthType) + } + client.SetSMTPAuth(SMTPAuthSCRAMSHA256) + if client.smtpAuthType != SMTPAuthSCRAMSHA256 { + t.Errorf("failed to set expected SMTPAuthType, want: %s, got: %s", SMTPAuthSCRAMSHA256, + client.smtpAuthType) + } + if client.smtpAuth != nil { + t.Errorf("failed to set expected SMTPAuth, want: nil, got: %s", client.smtpAuth) + } + }) +} + /*