mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-20 02:57:05 +00:00
message: Implement service layer, stubs for #81
I've made some effort to wire the manager into the controllers, but tests are currently failing.
This commit is contained in:
@@ -24,7 +24,7 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
messages, err := ctx.DataStore.GetMessages(name)
|
||||
messages, err := ctx.MsgSvc.GetMetadata(name)
|
||||
if err != nil {
|
||||
// This doesn't indicate empty, likely an IO error
|
||||
return fmt.Errorf("Failed to get messages for %v: %v", name, err)
|
||||
@@ -35,12 +35,12 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
|
||||
for i, msg := range messages {
|
||||
jmessages[i] = &model.JSONMessageHeaderV1{
|
||||
Mailbox: name,
|
||||
ID: msg.ID(),
|
||||
From: msg.From().String(),
|
||||
To: stringutil.StringAddressList(msg.To()),
|
||||
Subject: msg.Subject(),
|
||||
Date: msg.Date(),
|
||||
Size: msg.Size(),
|
||||
ID: msg.ID,
|
||||
From: msg.From.String(),
|
||||
To: stringutil.StringAddressList(msg.To),
|
||||
Subject: msg.Subject,
|
||||
Date: msg.Date,
|
||||
Size: msg.Size,
|
||||
}
|
||||
}
|
||||
return web.RenderJSON(w, jmessages)
|
||||
@@ -54,7 +54,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msg, err := ctx.DataStore.GetMessage(name, id)
|
||||
msg, err := ctx.MsgSvc.GetMessage(name, id)
|
||||
if err == storage.ErrNotExist {
|
||||
http.NotFound(w, req)
|
||||
return nil
|
||||
@@ -63,14 +63,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
|
||||
// This doesn't indicate empty, likely an IO error
|
||||
return fmt.Errorf("GetMessage(%q) failed: %v", id, err)
|
||||
}
|
||||
header, err := msg.ReadHeader()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ReadHeader(%q) failed: %v", id, err)
|
||||
}
|
||||
mime, err := msg.ReadBody()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ReadBody(%q) failed: %v", id, err)
|
||||
}
|
||||
mime := msg.Envelope
|
||||
|
||||
attachments := make([]*model.JSONMessageAttachmentV1, len(mime.Attachments))
|
||||
for i, att := range mime.Attachments {
|
||||
@@ -89,13 +82,13 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
|
||||
return web.RenderJSON(w,
|
||||
&model.JSONMessageV1{
|
||||
Mailbox: name,
|
||||
ID: msg.ID(),
|
||||
From: msg.From().String(),
|
||||
To: stringutil.StringAddressList(msg.To()),
|
||||
Subject: msg.Subject(),
|
||||
Date: msg.Date(),
|
||||
Size: msg.Size(),
|
||||
Header: header.Header,
|
||||
ID: msg.ID,
|
||||
From: msg.From.String(),
|
||||
To: stringutil.StringAddressList(msg.To),
|
||||
Subject: msg.Subject,
|
||||
Date: msg.Date,
|
||||
Size: msg.Size,
|
||||
Header: mime.Root.Header,
|
||||
Body: &model.JSONMessageBodyV1{
|
||||
Text: mime.Text,
|
||||
HTML: mime.HTML,
|
||||
@@ -112,7 +105,7 @@ func MailboxPurgeV1(w http.ResponseWriter, req *http.Request, ctx *web.Context)
|
||||
return err
|
||||
}
|
||||
// Delete all messages
|
||||
err = ctx.DataStore.PurgeMessages(name)
|
||||
err = ctx.MsgSvc.PurgeMessages(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Mailbox(%q) purge failed: %v", name, err)
|
||||
}
|
||||
@@ -129,25 +122,20 @@ func MailboxSourceV1(w http.ResponseWriter, req *http.Request, ctx *web.Context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
message, err := ctx.DataStore.GetMessage(name, id)
|
||||
|
||||
r, err := ctx.MsgSvc.SourceReader(name, id)
|
||||
if err == storage.ErrNotExist {
|
||||
http.NotFound(w, req)
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
// This doesn't indicate missing, likely an IO error
|
||||
return fmt.Errorf("GetMessage(%q) failed: %v", id, err)
|
||||
return fmt.Errorf("SourceReader(%q) failed: %v", id, err)
|
||||
}
|
||||
raw, err := message.ReadRaw()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ReadRaw(%q) failed: %v", id, err)
|
||||
}
|
||||
|
||||
// Output message source
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
if _, err := io.WriteString(w, *raw); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
_, err = io.Copy(w, r)
|
||||
return err
|
||||
}
|
||||
|
||||
// MailboxDeleteV1 removes a particular message from a mailbox
|
||||
@@ -158,7 +146,7 @@ func MailboxDeleteV1(w http.ResponseWriter, req *http.Request, ctx *web.Context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ctx.DataStore.RemoveMessage(name, id)
|
||||
err = ctx.MsgSvc.RemoveMessage(name, id)
|
||||
if err == storage.ErrNotExist {
|
||||
http.NotFound(w, req)
|
||||
return nil
|
||||
|
||||
@@ -4,10 +4,14 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/mail"
|
||||
"net/textproto"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jhillyerd/enmime"
|
||||
"github.com/jhillyerd/inbucket/pkg/message"
|
||||
"github.com/jhillyerd/inbucket/pkg/test"
|
||||
)
|
||||
|
||||
@@ -31,7 +35,8 @@ const (
|
||||
func TestRestMailboxList(t *testing.T) {
|
||||
// Setup
|
||||
ds := test.NewStore()
|
||||
logbuf := setupWebServer(ds)
|
||||
mm := test.NewManager()
|
||||
logbuf := setupWebServer(mm, ds)
|
||||
|
||||
// Test invalid mailbox name
|
||||
w, err := testRestGet(baseURL + "/mailbox/foo@bar")
|
||||
@@ -80,10 +85,24 @@ func TestRestMailboxList(t *testing.T) {
|
||||
Subject: "subject 2",
|
||||
Date: time.Date(2012, 7, 1, 10, 11, 12, 253, time.FixedZone("PDT", -700)),
|
||||
}
|
||||
msg1 := data1.MockMessage()
|
||||
msg2 := data2.MockMessage()
|
||||
ds.AddMessage("good", msg1)
|
||||
ds.AddMessage("good", msg2)
|
||||
meta1 := message.Metadata{
|
||||
Mailbox: "good",
|
||||
ID: "0001",
|
||||
From: &mail.Address{Name: "", Address: "from1@host"},
|
||||
To: []*mail.Address{{Name: "", Address: "to1@host"}},
|
||||
Subject: "subject 1",
|
||||
Date: time.Date(2012, 2, 1, 10, 11, 12, 253, time.FixedZone("PST", -800)),
|
||||
}
|
||||
meta2 := message.Metadata{
|
||||
Mailbox: "good",
|
||||
ID: "0002",
|
||||
From: &mail.Address{Name: "", Address: "from2@host"},
|
||||
To: []*mail.Address{{Name: "", Address: "to1@host"}},
|
||||
Subject: "subject 2",
|
||||
Date: time.Date(2012, 7, 1, 10, 11, 12, 253, time.FixedZone("PDT", -700)),
|
||||
}
|
||||
mm.AddMessage("good", &message.Message{Metadata: meta1})
|
||||
mm.AddMessage("good", &message.Message{Metadata: meta2})
|
||||
|
||||
// Check return code
|
||||
w, err = testRestGet(baseURL + "/mailbox/good")
|
||||
@@ -96,6 +115,25 @@ func TestRestMailboxList(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check JSON
|
||||
got := w.Body.String()
|
||||
testStrings := []string{
|
||||
`{"mailbox":"good","id":"0001","from":"\u003cfrom1@host\u003e",` +
|
||||
`"to":["\u003cto1@host\u003e"],"subject":"subject 1",` +
|
||||
`"date":"2012-02-01T10:11:12.000000253-00:13","size":0}`,
|
||||
`{"mailbox":"good","id":"0002","from":"\u003cfrom2@host\u003e",` +
|
||||
`"to":["\u003cto1@host\u003e"],"subject":"subject 2",` +
|
||||
`"date":"2012-07-01T10:11:12.000000253-00:11","size":0}`,
|
||||
}
|
||||
for _, ts := range testStrings {
|
||||
t.Run(ts, func(t *testing.T) {
|
||||
if !strings.Contains(got, ts) {
|
||||
t.Errorf("got:\n%s\nwant to contain:\n%s", got, ts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Check JSON
|
||||
// TODO transitional while refactoring
|
||||
dec := json.NewDecoder(w.Body)
|
||||
var result []interface{}
|
||||
if err := dec.Decode(&result); err != nil {
|
||||
@@ -128,7 +166,8 @@ func TestRestMailboxList(t *testing.T) {
|
||||
func TestRestMessage(t *testing.T) {
|
||||
// Setup
|
||||
ds := test.NewStore()
|
||||
logbuf := setupWebServer(ds)
|
||||
mm := test.NewManager()
|
||||
logbuf := setupWebServer(mm, ds)
|
||||
|
||||
// Test invalid mailbox name
|
||||
w, err := testRestGet(baseURL + "/mailbox/foo@bar/0001")
|
||||
@@ -168,6 +207,26 @@ func TestRestMessage(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test JSON message headers
|
||||
msg1 := &message.Message{
|
||||
Metadata: message.Metadata{
|
||||
Mailbox: "good",
|
||||
ID: "0001",
|
||||
From: &mail.Address{Name: "", Address: "from1@host"},
|
||||
To: []*mail.Address{{Name: "", Address: "to1@host"}},
|
||||
Subject: "subject 1",
|
||||
Date: time.Date(2012, 2, 1, 10, 11, 12, 253, time.FixedZone("PST", -800)),
|
||||
},
|
||||
Envelope: &enmime.Envelope{
|
||||
Text: "This is some text",
|
||||
HTML: "This is some HTML",
|
||||
Root: &enmime.Part{
|
||||
Header: textproto.MIMEHeader{
|
||||
"To": []string{"fred@fish.com", "keyword@nsa.gov"},
|
||||
"From": []string{"noreply@inbucket.org"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
data1 := &InputMessageData{
|
||||
Mailbox: "good",
|
||||
ID: "0001",
|
||||
@@ -181,8 +240,7 @@ func TestRestMessage(t *testing.T) {
|
||||
Text: "This is some text",
|
||||
HTML: "This is some HTML",
|
||||
}
|
||||
msg1 := data1.MockMessage()
|
||||
ds.AddMessage("good", msg1)
|
||||
mm.AddMessage("good", msg1)
|
||||
|
||||
// Check return code
|
||||
w, err = testRestGet(baseURL + "/mailbox/good/0001")
|
||||
@@ -195,6 +253,7 @@ func TestRestMessage(t *testing.T) {
|
||||
}
|
||||
|
||||
// Check JSON
|
||||
// TODO transitional while refactoring
|
||||
dec := json.NewDecoder(w.Body)
|
||||
var result map[string]interface{}
|
||||
if err := dec.Decode(&result); err != nil {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/mail"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -26,7 +25,7 @@ type JSONMessageV1 struct {
|
||||
Date time.Time `json:"date"`
|
||||
Size int64 `json:"size"`
|
||||
Body *JSONMessageBodyV1 `json:"body"`
|
||||
Header mail.Header `json:"header"`
|
||||
Header map[string][]string `json:"header"`
|
||||
Attachments []*JSONMessageAttachmentV1 `json:"attachments"`
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/jhillyerd/enmime"
|
||||
"github.com/jhillyerd/inbucket/pkg/config"
|
||||
"github.com/jhillyerd/inbucket/pkg/message"
|
||||
"github.com/jhillyerd/inbucket/pkg/msghub"
|
||||
"github.com/jhillyerd/inbucket/pkg/server/web"
|
||||
"github.com/jhillyerd/inbucket/pkg/storage"
|
||||
@@ -193,7 +194,7 @@ func testRestGet(url string) (*httptest.ResponseRecorder, error) {
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func setupWebServer(ds storage.Store) *bytes.Buffer {
|
||||
func setupWebServer(mm message.Manager, ds storage.Store) *bytes.Buffer {
|
||||
// Capture log output
|
||||
buf := new(bytes.Buffer)
|
||||
log.SetOutput(buf)
|
||||
@@ -205,7 +206,7 @@ func setupWebServer(ds storage.Store) *bytes.Buffer {
|
||||
PublicDir: "../themes/bootstrap/public",
|
||||
}
|
||||
shutdownChan := make(chan bool)
|
||||
web.Initialize(cfg, shutdownChan, ds, &msghub.Hub{})
|
||||
web.Initialize(cfg, shutdownChan, mm, ds, &msghub.Hub{})
|
||||
SetupRoutes(web.Router)
|
||||
|
||||
return buf
|
||||
|
||||
Reference in New Issue
Block a user