mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Refactor datastore into it's own package for #67
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
|
||||
"github.com/jhillyerd/inbucket/datastore"
|
||||
"github.com/jhillyerd/inbucket/httpd"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"github.com/jhillyerd/inbucket/rest/model"
|
||||
@@ -64,7 +65,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
|
||||
return fmt.Errorf("Failed to get mailbox for %q: %v", name, err)
|
||||
}
|
||||
msg, err := mb.GetMessage(id)
|
||||
if err == smtpd.ErrNotExist {
|
||||
if err == datastore.ErrNotExist {
|
||||
http.NotFound(w, req)
|
||||
return nil
|
||||
}
|
||||
@@ -149,7 +150,7 @@ func MailboxSourceV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Contex
|
||||
return fmt.Errorf("Failed to get mailbox for %q: %v", name, err)
|
||||
}
|
||||
message, err := mb.GetMessage(id)
|
||||
if err == smtpd.ErrNotExist {
|
||||
if err == datastore.ErrNotExist {
|
||||
http.NotFound(w, req)
|
||||
return nil
|
||||
}
|
||||
@@ -183,7 +184,7 @@ func MailboxDeleteV1(w http.ResponseWriter, req *http.Request, ctx *httpd.Contex
|
||||
return fmt.Errorf("Failed to get mailbox for %q: %v", name, err)
|
||||
}
|
||||
message, err := mb.GetMessage(id)
|
||||
if err == smtpd.ErrNotExist {
|
||||
if err == datastore.ErrNotExist {
|
||||
http.NotFound(w, req)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
"github.com/jhillyerd/inbucket/datastore"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -47,7 +47,7 @@ func TestRestMailboxList(t *testing.T) {
|
||||
// Test empty mailbox
|
||||
emptybox := &MockMailbox{}
|
||||
ds.On("MailboxFor", "empty").Return(emptybox, nil)
|
||||
emptybox.On("GetMessages").Return([]smtpd.Message{}, nil)
|
||||
emptybox.On("GetMessages").Return([]datastore.Message{}, nil)
|
||||
|
||||
w, err = testRestGet(baseURL + "/mailbox/empty")
|
||||
expectCode = 200
|
||||
@@ -79,7 +79,7 @@ func TestRestMailboxList(t *testing.T) {
|
||||
// Test MailboxFor error
|
||||
error2box := &MockMailbox{}
|
||||
ds.On("MailboxFor", "error2").Return(error2box, nil)
|
||||
error2box.On("GetMessages").Return([]smtpd.Message{}, fmt.Errorf("Internal error 2"))
|
||||
error2box.On("GetMessages").Return([]datastore.Message{}, fmt.Errorf("Internal error 2"))
|
||||
|
||||
w, err = testRestGet(baseURL + "/mailbox/error2")
|
||||
expectCode = 500
|
||||
@@ -111,7 +111,7 @@ func TestRestMailboxList(t *testing.T) {
|
||||
ds.On("MailboxFor", "good").Return(goodbox, nil)
|
||||
msg1 := data1.MockMessage()
|
||||
msg2 := data2.MockMessage()
|
||||
goodbox.On("GetMessages").Return([]smtpd.Message{msg1, msg2}, nil)
|
||||
goodbox.On("GetMessages").Return([]datastore.Message{msg1, msg2}, nil)
|
||||
|
||||
// Check return code
|
||||
w, err = testRestGet(baseURL + "/mailbox/good")
|
||||
@@ -171,7 +171,7 @@ func TestRestMessage(t *testing.T) {
|
||||
// Test requesting a message that does not exist
|
||||
emptybox := &MockMailbox{}
|
||||
ds.On("MailboxFor", "empty").Return(emptybox, nil)
|
||||
emptybox.On("GetMessage", "0001").Return(&MockMessage{}, smtpd.ErrNotExist)
|
||||
emptybox.On("GetMessage", "0001").Return(&MockMessage{}, datastore.ErrNotExist)
|
||||
|
||||
w, err = testRestGet(baseURL + "/mailbox/empty/0001")
|
||||
expectCode = 404
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jhillyerd/enmime"
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
"github.com/jhillyerd/inbucket/datastore"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
@@ -15,14 +15,14 @@ type MockDataStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockDataStore) MailboxFor(name string) (smtpd.Mailbox, error) {
|
||||
func (m *MockDataStore) MailboxFor(name string) (datastore.Mailbox, error) {
|
||||
args := m.Called(name)
|
||||
return args.Get(0).(smtpd.Mailbox), args.Error(1)
|
||||
return args.Get(0).(datastore.Mailbox), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockDataStore) AllMailboxes() ([]smtpd.Mailbox, error) {
|
||||
func (m *MockDataStore) AllMailboxes() ([]datastore.Mailbox, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).([]smtpd.Mailbox), args.Error(1)
|
||||
return args.Get(0).([]datastore.Mailbox), args.Error(1)
|
||||
}
|
||||
|
||||
// Mock Mailbox object
|
||||
@@ -30,14 +30,14 @@ type MockMailbox struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockMailbox) GetMessages() ([]smtpd.Message, error) {
|
||||
func (m *MockMailbox) GetMessages() ([]datastore.Message, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).([]smtpd.Message), args.Error(1)
|
||||
return args.Get(0).([]datastore.Message), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockMailbox) GetMessage(id string) (smtpd.Message, error) {
|
||||
func (m *MockMailbox) GetMessage(id string) (datastore.Message, error) {
|
||||
args := m.Called(id)
|
||||
return args.Get(0).(smtpd.Message), args.Error(1)
|
||||
return args.Get(0).(datastore.Message), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockMailbox) Purge() error {
|
||||
@@ -45,9 +45,9 @@ func (m *MockMailbox) Purge() error {
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockMailbox) NewMessage() (smtpd.Message, error) {
|
||||
func (m *MockMailbox) NewMessage() (datastore.Message, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).(smtpd.Message), args.Error(1)
|
||||
return args.Get(0).(datastore.Message), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockMailbox) Name() string {
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
|
||||
"github.com/jhillyerd/enmime"
|
||||
"github.com/jhillyerd/inbucket/config"
|
||||
"github.com/jhillyerd/inbucket/datastore"
|
||||
"github.com/jhillyerd/inbucket/httpd"
|
||||
"github.com/jhillyerd/inbucket/msghub"
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
)
|
||||
|
||||
type InputMessageData struct {
|
||||
@@ -188,7 +188,7 @@ func testRestGet(url string) (*httptest.ResponseRecorder, error) {
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func setupWebServer(ds smtpd.DataStore) *bytes.Buffer {
|
||||
func setupWebServer(ds datastore.DataStore) *bytes.Buffer {
|
||||
// Capture log output
|
||||
buf := new(bytes.Buffer)
|
||||
log.SetOutput(buf)
|
||||
|
||||
Reference in New Issue
Block a user