diff --git a/api/send-message.go b/api/send-message.go index bd35c22..abc2700 100644 --- a/api/send-message.go +++ b/api/send-message.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "errors" + "fmt" postfixLookup "github.com/1f349/lotus/postfix-lookup" "github.com/1f349/lotus/smtp" "github.com/julienschmidt/httprouter" @@ -34,12 +35,11 @@ func MessageSender(send Smtp) func(rw http.ResponseWriter, req *http.Request, pa // prepare the mail for sending mail, err := j.PrepareMail(timeNow()) if err != nil { - apiError(rw, http.StatusBadRequest, "Invalid mail message") + apiError(rw, http.StatusBadRequest, fmt.Sprintf("Invalid mail message: %s", err)) return } // this looks up the underlying account for the sender alias - println(mail.From) lookup, err := defaultPostfixLookup(mail.From) // the alias does not exist diff --git a/api/send-message_test.go b/api/send-message_test.go index 6829251..670c058 100644 --- a/api/send-message_test.go +++ b/api/send-message_test.go @@ -144,7 +144,67 @@ var messageSenderTestData = []struct { smtp: &fakeSmtp{}, claims: makeFakeAuthClaims("admin@example.com"), status: http.StatusBadRequest, - output: "Invalid mail message", + output: "Invalid mail message: multiple from addresses", + }, + { + req: func() (*http.Request, error) { + j, err := json.Marshal(smtp.Json{ + From: "noreply@example.com", + ReplyTo: "admin@example.com", + To: "user@example.com", + Subject: "Test Subject", + BodyType: "no", + Body: "Plain text", + }) + if err != nil { + return nil, err + } + return http.NewRequest(http.MethodPost, "https://api.example.com/v1/mail/message", bytes.NewReader(j)) + }, + smtp: &fakeSmtp{}, + claims: makeFakeAuthClaims("admin@example.com"), + status: http.StatusBadRequest, + output: "Invalid mail message: invalid body type", + }, + { + req: func() (*http.Request, error) { + j, err := json.Marshal(smtp.Json{ + From: "noreply@example.com", + ReplyTo: "admin@example.com", + To: "a ", + Subject: "Test Subject", + BodyType: "no", + Body: "Plain text", + }) + if err != nil { + return nil, err + } + return http.NewRequest(http.MethodPost, "https://api.example.com/v1/mail/message", bytes.NewReader(j)) + }, + smtp: &fakeSmtp{}, + claims: makeFakeAuthClaims("admin@example.com"), + status: http.StatusBadRequest, + output: "Invalid mail message: mail: missing @ in addr-spec", }, { req: func() (*http.Request, error) {