From 3234c13277a74427a3d0bc6bc1686a528aede7f0 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Tue, 15 Oct 2024 20:02:24 +0200 Subject: [PATCH] Add tests for SetLogAuthData method Introduced TestClient_SetLogAuthData to verify the proper behavior of the SetLogAuthData method in both client and SMTP tests. This ensures that logAuthData is enabled or disabled as expected, increasing code reliability. --- client_test.go | 18 ++++++++++++++++++ smtp/smtp_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/client_test.go b/client_test.go index c767e75..0af9f9c 100644 --- a/client_test.go +++ b/client_test.go @@ -123,6 +123,7 @@ func TestNewClientWithOptions(t *testing.T) { {"WithoutNoop()", WithoutNoop(), false}, {"WithDebugLog()", WithDebugLog(), false}, {"WithLogger()", WithLogger(log.New(os.Stderr, log.LevelDebug)), false}, + {"WithLogger()", WithLogAuthData(), false}, {"WithDialContextFunc()", WithDialContextFunc(func(ctx context.Context, network, address string) (net.Conn, error) { return nil, nil }), false}, @@ -578,6 +579,23 @@ func TestWithoutNoop(t *testing.T) { } } +func TestClient_SetLogAuthData(t *testing.T) { + c, err := NewClient(DefaultHost, WithLogAuthData()) + if err != nil { + t.Errorf("failed to create new client: %s", err) + return + } + if !c.logAuthData { + t.Errorf("WithLogAuthData failed. c.logAuthData expected to be: %t, got: %t", true, + c.logAuthData) + } + c.SetLogAuthData(false) + if c.logAuthData { + t.Errorf("SetLogAuthData failed. c.logAuthData expected to be: %t, got: %t", false, + c.logAuthData) + } +} + // TestSetSMTPAuthCustom tests the SetSMTPAuthCustom method for the Client object func TestSetSMTPAuthCustom(t *testing.T) { tests := []struct { diff --git a/smtp/smtp_test.go b/smtp/smtp_test.go index 4fd32eb..52b6b5a 100644 --- a/smtp/smtp_test.go +++ b/smtp/smtp_test.go @@ -1111,6 +1111,32 @@ func TestClient_SetLogger(t *testing.T) { c.logger.Debugf(log.Log{Direction: log.DirServerToClient, Format: "%s", Messages: []interface{}{"test"}}) } +func TestClient_SetLogAuthData(t *testing.T) { + server := strings.Join(strings.Split(newClientServer, "\n"), "\r\n") + + var cmdbuf strings.Builder + bcmdbuf := bufio.NewWriter(&cmdbuf) + out := func() string { + if err := bcmdbuf.Flush(); err != nil { + t.Errorf("failed to flush: %s", err) + } + return cmdbuf.String() + } + var fake faker + fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf) + c, err := NewClient(fake, "fake.host") + if err != nil { + t.Fatalf("NewClient: %v\n(after %v)", err, out()) + } + defer func() { + _ = c.Close() + }() + c.SetLogAuthData() + if !c.logAuthData { + t.Error("Expected logAuthData to be true but received false") + } +} + var newClientServer = `220 hello world 250-mx.google.com at your service 250-SIZE 35651584