diff --git a/msg_test.go b/msg_test.go index b353558..b50edf9 100644 --- a/msg_test.go +++ b/msg_test.go @@ -4832,6 +4832,46 @@ func TestMsg_EmbedFile(t *testing.T) { }) } +func TestMsg_EmbedReader(t *testing.T) { + t.Run("EmbedReader with file", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + file, err := os.Open("testdata/embed.txt") + if err != nil { + t.Fatalf("failed to open file: %s", err) + } + t.Cleanup(func() { + if err := file.Close(); err != nil { + t.Errorf("failed to close file: %s", err) + } + }) + if err = message.EmbedReader("embed.txt", file); err != nil { + t.Fatalf("failed to embed reader: %s", err) + } + embeds := message.GetEmbeds() + if len(embeds) != 1 { + t.Fatalf("failed to retrieve embeds list") + } + if embeds[0] == nil { + t.Fatal("expected embed to be not nil") + } + if embeds[0].Name != "embed.txt" { + t.Errorf("expected embed name to be %s, got: %s", "embed.txt", embeds[0].Name) + } + messageBuf := bytes.NewBuffer(nil) + _, err = embeds[0].Writer(messageBuf) + if err != nil { + t.Errorf("writer func failed: %s", err) + } + got := strings.TrimSpace(messageBuf.String()) + if !strings.EqualFold(got, "This is a test embed") { + t.Errorf("expected message body to be %s, got: %s", "This is a test embed", got) + } + }) +} + /* // TestNewMsgWithMiddleware tests WithMiddleware diff --git a/msg_unix_test.go b/msg_unix_test.go index ae4d018..0200470 100644 --- a/msg_unix_test.go +++ b/msg_unix_test.go @@ -177,6 +177,144 @@ func TestMsg_AttachReadSeeker_unixOnly(t *testing.T) { }) } +func TestMsg_EmbedFile_unixOnly(t *testing.T) { + t.Run("EmbedFile with fileFromFS fails on open", func(t *testing.T) { + if os.Getenv("PERFORM_UNIX_OPEN_WRITE_TESTS") != "true" { + t.Skipf("PERFORM_UNIX_OPEN_WRITE_TESTS variable is not set. Skipping unix open/write tests") + } + + tempFile, err := os.CreateTemp("testdata/tmp", "embedfile-open-write-test.*.txt") + if err != nil { + t.Fatalf("failed to create temp file: %s", err) + } + t.Cleanup(func() { + if err := os.Remove(tempFile.Name()); err != nil { + t.Errorf("failed to remove temp file: %s", err) + } + }) + if err = os.Chmod(tempFile.Name(), 0o000); err != nil { + t.Fatalf("failed to chmod temp file: %s", err) + } + + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.EmbedFile(tempFile.Name()) + embeds := message.GetEmbeds() + if len(embeds) != 1 { + t.Fatalf("failed to get embeds, expected 1, got: %d", len(embeds)) + } + messageBuf := bytes.NewBuffer(nil) + _, err = embeds[0].Writer(messageBuf) + if err == nil { + t.Error("writer func expected to fail, but didn't") + } + if !errors.Is(err, os.ErrPermission) { + t.Errorf("expected error to be %s, got: %s", os.ErrPermission, err) + } + }) + t.Run("EmbedFile with fileFromFS fails on copy", func(t *testing.T) { + tempfile, err := os.CreateTemp("testdata/tmp", "embedfile-close-early.*.txt") + if err != nil { + t.Fatalf("failed to create temp file: %s", err) + } + t.Cleanup(func() { + if err := os.Remove(tempfile.Name()); err != nil { + t.Errorf("failed to remove temp file: %s", err) + } + }) + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + message.EmbedFile("testdata/embed.txt") + emebeds := message.GetEmbeds() + if len(emebeds) != 1 { + t.Fatalf("failed to get emebeds, expected 1, got: %d", len(emebeds)) + } + messageBuf, err := os.Open(tempfile.Name()) + if err != nil { + t.Fatalf("failed to open temp file: %s", err) + } + // We close early to cause an error during io.Copy + if err = messageBuf.Close(); err != nil { + t.Fatalf("failed to close temp file: %s", err) + } + _, err = emebeds[0].Writer(messageBuf) + if err == nil { + t.Error("writer func expected to fail, but didn't") + } + }) +} + +func TestMsg_EmbedReader_unixOnly(t *testing.T) { + t.Run("EmbedReader with fileFromReader fails on copy", func(t *testing.T) { + tempfile, err := os.CreateTemp("", "embedfile-close-early.*.txt") + if err != nil { + t.Fatalf("failed to create temp file: %s", err) + } + t.Cleanup(func() { + if err := os.Remove(tempfile.Name()); err != nil { + t.Errorf("failed to remove temp file: %s", err) + } + }) + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + file, err := os.Open("testdata/embed.txt") + if err != nil { + t.Fatalf("failed to open file: %s", err) + } + t.Cleanup(func() { + if err := file.Close(); err != nil { + t.Errorf("failed to close file: %s", err) + } + }) + if err = message.EmbedReader("embed.txt", file); err != nil { + t.Fatalf("failed to embed reader: %s", err) + } + embeds := message.GetEmbeds() + if len(embeds) != 1 { + t.Fatalf("failed to get embeds, expected 1, got: %d", len(embeds)) + } + messageBuf, err := os.Open(tempfile.Name()) + if err != nil { + t.Fatalf("failed to open temp file: %s", err) + } + // We close early to cause an error during io.Copy + if err = messageBuf.Close(); err != nil { + t.Fatalf("failed to close temp file: %s", err) + } + _, err = embeds[0].Writer(messageBuf) + if err == nil { + t.Error("writer func expected to fail, but didn't") + } + }) + t.Run("EmbedReader with fileFromReader on closed reader", func(t *testing.T) { + tempfile, err := os.CreateTemp("", "embedfile-close-early.*.txt") + if err != nil { + t.Fatalf("failed to create temp file: %s", err) + } + if err = tempfile.Close(); err != nil { + t.Fatalf("failed to close temp file: %s", err) + } + t.Cleanup(func() { + if err := os.Remove(tempfile.Name()); err != nil { + t.Errorf("failed to remove temp file: %s", err) + } + }) + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + if err = message.EmbedReader("embed.txt", tempfile); err == nil { + t.Fatalf("expected error, got nil") + } + }) +} + // TestMsg_WriteToSendmailWithContext tests the WriteToSendmailWithContext() method of the Msg func TestMsg_WriteToSendmailWithContext(t *testing.T) { if os.Getenv("TEST_SENDMAIL") != "true" {