diff --git a/client_test.go b/client_test.go index 481db6d..c240a1f 100644 --- a/client_test.go +++ b/client_test.go @@ -417,6 +417,67 @@ func TestClient_DialWithContext(t *testing.T) { } } +// TestClient_DialWithContextInvalidHost tests the DialWithContext method with intentional breaking +// for the Client object +func TestClient_DialWithContextInvalidHost(t *testing.T) { + c, err := getTestConnection(true) + if err != nil { + t.Skipf("failed to create test client: %s. Skipping tests", err) + } + c.co = nil + c.host = "invalid.addr" + ctx := context.Background() + if err := c.DialWithContext(ctx); err == nil { + t.Errorf("dial succeeded but was supposed to fail") + return + } +} + +// TestClient_DialWithContextInvalidHELO tests the DialWithContext method with intentional breaking +// for the Client object +func TestClient_DialWithContextInvalidHELO(t *testing.T) { + c, err := getTestConnection(true) + if err != nil { + t.Skipf("failed to create test client: %s. Skipping tests", err) + } + c.co = nil + c.helo = "" + ctx := context.Background() + if err := c.DialWithContext(ctx); err == nil { + t.Errorf("dial succeeded but was supposed to fail") + return + } +} + +// TestClient_DialWithContextInvalidAuth tests the DialWithContext method with intentional breaking +// for the Client object +func TestClient_DialWithContextInvalidAuth(t *testing.T) { + c, err := getTestConnection(true) + if err != nil { + t.Skipf("failed to create test client: %s. Skipping tests", err) + } + c.user = "invalid" + c.pass = "invalid" + c.SetSMTPAuthCustom(auth.LoginAuth("invalid", "invalid", "invalid")) + ctx := context.Background() + if err := c.DialWithContext(ctx); err == nil { + t.Errorf("dial succeeded but was supposed to fail") + return + } +} + +// TestClient_checkConn tests the checkConn method with intentional breaking for the Client object +func TestClient_checkConn(t *testing.T) { + c, err := getTestConnection(true) + if err != nil { + t.Skipf("failed to create test client: %s. Skipping tests", err) + } + c.co = nil + if err := c.checkConn(); err == nil { + t.Errorf("connCheck() should fail but succeeded") + } +} + // TestClient_DiealWithContextOptions tests the DialWithContext method plus different options // for the Client object func TestClient_DialWithContextOptions(t *testing.T) { @@ -468,6 +529,9 @@ func TestClient_DialWithContextOptions(t *testing.T) { // getTestConnection takes environment variables to establish a connection to a real // SMTP server to test all functionality that requires a connection func getTestConnection(auth bool) (*Client, error) { + if os.Getenv("TEST_SKIP_ONLINE") != "" { + return nil, fmt.Errorf("env variable TEST_SKIP_ONLINE is set. Skipping online tests") + } th := os.Getenv("TEST_HOST") if th == "" { return nil, fmt.Errorf("no TEST_HOST set") diff --git a/header_test.go b/header_test.go new file mode 100644 index 0000000..59a627d --- /dev/null +++ b/header_test.go @@ -0,0 +1,39 @@ +package mail + +import ( + "testing" +) + +// TestImportance_StringFuncs tests the different string method of the Importance object +func TestImportance_StringFuncs(t *testing.T) { + tests := []struct { + name string + imp Importance + wantns string + xprio string + want string + }{ + {"Importance: Non-Urgent", ImportanceNonUrgent, "0", "5", "non-urgent"}, + {"Importance: Low", ImportanceLow, "0", "5", "low"}, + {"Importance: Normal", ImportanceNormal, "", "", ""}, + {"Importance: High", ImportanceHigh, "1", "1", "high"}, + {"Importance: Urgent", ImportanceUrgent, "1", "1", "urgent"}, + {"Importance: Unknown", 9, "", "", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.imp.NumString() != tt.wantns { + t.Errorf("wrong number string for Importance returned. Expected: %s, got: %s", + tt.wantns, tt.imp.NumString()) + } + if tt.imp.XPrioString() != tt.xprio { + t.Errorf("wrong x-prio string for Importance returned. Expected: %s, got: %s", + tt.xprio, tt.imp.XPrioString()) + } + if tt.imp.String() != tt.want { + t.Errorf("wrong string for Importance returned. Expected: %s, got: %s", + tt.want, tt.imp.String()) + } + }) + } +}