Add some extra tests and improve errors

This commit is contained in:
Melon 2023-08-23 17:36:23 +01:00
parent 9b87a8a857
commit 306f2c1e8e
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 63 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package api
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
postfixLookup "github.com/1f349/lotus/postfix-lookup" postfixLookup "github.com/1f349/lotus/postfix-lookup"
"github.com/1f349/lotus/smtp" "github.com/1f349/lotus/smtp"
"github.com/julienschmidt/httprouter" "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 // prepare the mail for sending
mail, err := j.PrepareMail(timeNow()) mail, err := j.PrepareMail(timeNow())
if err != nil { if err != nil {
apiError(rw, http.StatusBadRequest, "Invalid mail message") apiError(rw, http.StatusBadRequest, fmt.Sprintf("Invalid mail message: %s", err))
return return
} }
// this looks up the underlying account for the sender alias // this looks up the underlying account for the sender alias
println(mail.From)
lookup, err := defaultPostfixLookup(mail.From) lookup, err := defaultPostfixLookup(mail.From)
// the alias does not exist // the alias does not exist

View File

@ -144,7 +144,67 @@ var messageSenderTestData = []struct {
smtp: &fakeSmtp{}, smtp: &fakeSmtp{},
claims: makeFakeAuthClaims("admin@example.com"), claims: makeFakeAuthClaims("admin@example.com"),
status: http.StatusBadRequest, 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 <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: mail: unclosed angle-addr",
},
{
req: func() (*http.Request, error) {
j, err := json.Marshal(smtp.Json{
From: "noreply@example.com",
ReplyTo: "admin@example.com",
To: "a <user>",
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) { req: func() (*http.Request, error) {