Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Check For Existing Files And Attachments #97

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open

Added Check For Existing Files And Attachments #97

wants to merge 38 commits into from

Conversation

Gamma169
Copy link

This is a fix for #65
I created a method for the message struct that gets called before the send() function. It checks to see if the files listed in the attachments and embedded fields of the message struct actually exist. If they do, it returns nil for an error, if they don't, it returns the appropriate file does not exist error as specified in the golang os library docs

This gets caught before the send() function gets called, and therefore does not send any part of the email at all as to before, the error would be caught by the io.WriterTo in the smtp.go file only after it writes the body of the email. That's why the email would be sent, but then not send attachments and say that the email isn't sent.

I did not include a test for this because the testing suite seems to only tests for positive cases and not negative cases that are supposed to throw errors.

@Gamma169
Copy link
Author

Note that I changed tests slightly with my second commit. Since we're now testing that the files ACTUALLY exist, the first commit failed tests with attachments because the tests just mocked the files. I created some empty files in the tests to make sure my added checks pass.

If anything, this can act showing that my amendments actually work.

@ivy
Copy link

ivy commented Dec 6, 2017

The added functionality looks good to me but we need to break this out into its own test (e.g. TestNonExistentEmbedsAndAttachments) and fix TestRename so that it's still representative of testing that the Rename method is behaving as expected.

@@ -705,6 +709,7 @@ func getBoundaries(t *testing.T, count int, m string) []string {
var boundaryRegExp = regexp.MustCompile("boundary=(\\w+)")

func mockCopyFile(name string) (string, FileSetting) {
os.Create(filepath.Base(name))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but won't this also create a bunch of files in the working directory? We need a teardown function to remove them when the test suite finishes. Also, this ends up being a step away from an actual copyFile and not a mock.

I need to think about this more. I'll come back after I've reviewed some of the other pull requests.

Copy link

@ivy ivy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gamma169 If you could come up with a solution to test setup/teardown and write a separate test TestNonExistentEmbedsAndAttachments, that would be wonderful. If not, I'll see what I can do later.

@pedromorgan
Copy link

@Gamma169 Any chance u can submit this PR against the fork https://github.com/go-mail/gomail

@Gamma169
Copy link
Author

Gamma169 commented Dec 7, 2017

I'll see if I can update these this weekend to address the comments. @pedromorgan, I will also make the PR on the fork specified.

@ivy I'll look into doing a test for it, but since the command is supposed to fail when missing an attachment and there are no analogous tests testing failure, I'm not sure how you want it set up. I can look into it, but if you give me a starting point to help figure it out and do it in the way you want, that would be helpful.

@ivy
Copy link

ivy commented Dec 7, 2017

Thanks @Gamma169, looking forward to your pull request. 😄 Just as a heads up, there's a minor merge conflict to look out for between your changes and the current HEAD. If you're not familiar with rebasing, I can help you out.

As for the tests, I was thinking something along the lines of:

diff --git a/send_test.go b/send_test.go
index 9bf05b9..ffa1f0f 100644
--- a/send_test.go
+++ b/send_test.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"io"
 	"reflect"
+	"strings"
 	"testing"
 )
 
@@ -50,6 +51,25 @@ func TestSend(t *testing.T) {
 	}
 }
 
+func TestSendValidatesEmbeds(t *testing.T) {
+	s := &mockSendCloser{
+		mockSender: stubSend(t, testFrom, []string{testTo1, testTo2}, testMsg),
+		close: func() error {
+			t.Error("Close() should not be called in Send()")
+			return nil
+		},
+	}
+
+	m := getTestMessage()
+	m.Embed("this-file-does-not-exist")
+
+	err := Send(s, m)
+	if err == nil || !strings.HasSuffix(err.Error(),
+		"no such file or directory") {
+		t.Errorf("Send(): expected stat error but got %v", err)
+	}
+}
+
 func getTestMessage() *Message {
 	m := NewMessage()
 	m.SetHeader("From", testFrom)

There would also need to be an additional TestSendValidatesAttachments doing the same for Message.Attach().

Changes introduced in 92faf95 were causing all builds to execute using
Go 1.7.4. This fixes the config to once again build against each
supported Go runtime.
The import statement for quotedprintable was accidentally removed during the
package rename in 1e5036a.
@Gamma169
Copy link
Author

Gamma169 commented Dec 8, 2017

I should be fine fixing the merge conflict hopefully. And thanks for the test code. I think I can take it from here.

@Gamma169
Copy link
Author

Just finished updating the code and adding in the tests. I included a teardown function at the end of every test where we call mockcopy and therefore create the file.

I also found that my code broke the Rename functionality. I fixed this by adding a private field originalName to the file struct. When the checks for attachments and enbeds happens, it looks at this original name of the file.

The reason it needed this is because the Rename function changes the Name field but doesn't change the field originally passed in as the name in the CopyFunc field. Therefore it has the original name in the copyfunc but a different Name parameter. Therefore the only way I could see a fix to the problem was to add an originalName field. I hope that this is acceptable

@Gamma169
Copy link
Author

PS I don't see any conflicts, by the way. Was it somehow only on your side?

@Gamma169
Copy link
Author

@pedromorgan Submitted the PR to other repo. Let me know if that works for you.

@Gamma169
Copy link
Author

Crap, it looks like I screwed everything up when I merged this branch into the other fork... I'm looking into fixing this.

@Gamma169
Copy link
Author

Okay... I finally reverted it back to where I did the first comment. Will make a new branch off of this one and make the PR on the other fork on that other branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants